2018-02-05 13:29:46 -08:00
|
|
|
|
//
|
|
|
|
|
|
// AppDelegate.swift
|
2019-04-15 15:03:05 -05:00
|
|
|
|
// NetNewsWire
|
2018-02-05 13:29:46 -08:00
|
|
|
|
//
|
2019-04-15 15:03:05 -05:00
|
|
|
|
// Created by Maurice Parker on 4/8/19.
|
|
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
2018-02-05 13:29:46 -08:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2026-01-11 20:42:34 -08:00
|
|
|
|
@preconcurrency import BackgroundTasks
|
2025-10-02 21:32:09 -07:00
|
|
|
|
import os
|
|
|
|
|
|
import WidgetKit
|
2019-04-15 15:03:05 -05:00
|
|
|
|
import RSCore
|
2019-04-24 07:30:35 -05:00
|
|
|
|
import RSWeb
|
2019-04-15 15:03:05 -05:00
|
|
|
|
import Account
|
2025-10-02 21:32:09 -07:00
|
|
|
|
import Articles
|
2020-07-30 17:40:45 -05:00
|
|
|
|
import Secrets
|
2026-03-13 09:43:32 -07:00
|
|
|
|
import ErrorLog
|
2026-05-29 10:46:00 -07:00
|
|
|
|
import Images
|
2019-04-15 15:03:05 -05:00
|
|
|
|
|
2025-11-18 16:39:51 -08:00
|
|
|
|
@MainActor var appDelegate: AppDelegate!
|
2018-02-05 13:29:46 -08:00
|
|
|
|
|
2025-11-18 16:39:51 -08:00
|
|
|
|
@main
|
2025-11-18 20:31:25 -08:00
|
|
|
|
@MainActor final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, UnreadCountProvider {
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-11-03 21:32:24 -08:00
|
|
|
|
private let backgroundTaskDispatchQueue = DispatchQueue.init(label: "BGTaskScheduler")
|
|
|
|
|
|
|
2019-05-20 13:51:08 -05:00
|
|
|
|
var shuttingDown = false {
|
|
|
|
|
|
didSet {
|
|
|
|
|
|
if shuttingDown {
|
2025-11-03 21:32:24 -08:00
|
|
|
|
ArticleStatusSyncTimer.shared.stop()
|
2019-05-20 13:51:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-08-02 10:33:23 -07:00
|
|
|
|
nonisolated private static let logger = Logger(subsystem: Logger.nnwSubsystem, category: "Application")
|
2024-12-30 15:17:20 -08:00
|
|
|
|
|
2019-04-15 15:03:05 -05:00
|
|
|
|
var unreadCount = 0 {
|
|
|
|
|
|
didSet {
|
|
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
2025-10-01 11:49:49 -07:00
|
|
|
|
updateBadge()
|
2019-04-15 15:03:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-12-01 16:51:25 -06:00
|
|
|
|
var isSyncArticleStatusRunning = false
|
|
|
|
|
|
var isWaitingForSyncTasks = false
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-04-15 15:03:05 -05:00
|
|
|
|
override init() {
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
appDelegate = self
|
2019-09-12 10:59:26 -05:00
|
|
|
|
|
2025-11-05 11:37:16 -08:00
|
|
|
|
AccountManager.shared.start()
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-04-15 15:03:05 -05:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2019-04-26 15:24:39 -05:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountRefreshDidFinish(_:)), name: .AccountRefreshDidFinish, object: nil)
|
2026-10-06 11:00:55 -07:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleUnreadCountDisplaySettingDidChange(_:)), name: .unreadCountDisplaySettingDidChange, object: nil)
|
2019-04-15 15:03:05 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-04-15 13:30:10 -05:00
|
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
2026-10-22 13:32:07 -07:00
|
|
|
|
AppDefaults.registerDefaults()
|
|
|
|
|
|
|
2026-05-29 10:46:00 -07:00
|
|
|
|
FaviconGenerator.templateImage = Assets.Images.faviconTemplate
|
|
|
|
|
|
|
2026-07-29 10:17:31 -07:00
|
|
|
|
WebViewConfiguration.resolveBrowserUserAgent()
|
2026-04-01 13:56:55 -07:00
|
|
|
|
Task {
|
|
|
|
|
|
await WebViewConfiguration.compileContentBlockingRules()
|
|
|
|
|
|
}
|
2019-11-06 16:53:13 -06:00
|
|
|
|
|
2026-10-22 13:46:32 -07:00
|
|
|
|
let isFirstRun = AppDefaults.isFirstRun
|
2019-04-15 15:03:05 -05:00
|
|
|
|
if isFirstRun {
|
2025-10-02 21:32:09 -07:00
|
|
|
|
Self.logger.info("Is first run.")
|
2019-04-15 15:03:05 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-10-18 10:24:32 -05:00
|
|
|
|
if isFirstRun && !AccountManager.shared.anyAccountHasAtLeastOneFeed() {
|
|
|
|
|
|
let localAccount = AccountManager.shared.defaultAccount
|
|
|
|
|
|
DefaultFeedsImporter.importDefaultFeeds(account: localAccount)
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-11-06 16:53:13 -06:00
|
|
|
|
registerBackgroundTasks()
|
2019-11-08 12:20:21 -06:00
|
|
|
|
CacheCleaner.purgeIfNecessary()
|
2019-09-01 16:54:07 -05:00
|
|
|
|
initializeHomeScreenQuickActions()
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-04-15 15:03:05 -05:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
self.unreadCount = AccountManager.shared.unreadCount
|
2025-09-18 10:03:22 -07:00
|
|
|
|
// Force the badge to update on launch.
|
2025-10-01 11:49:49 -07:00
|
|
|
|
self.updateBadge()
|
2019-04-15 15:03:05 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-09-16 09:50:15 -07:00
|
|
|
|
// Silent CloudKit pushes don’t need notification permission. setBadgeCount does.
|
|
|
|
|
|
UIApplication.shared.registerForRemoteNotifications()
|
|
|
|
|
|
|
|
|
|
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { _, _ in }
|
2020-03-30 02:48:25 -05:00
|
|
|
|
|
2019-10-02 19:42:16 -05:00
|
|
|
|
UNUserNotificationCenter.current().delegate = self
|
2025-11-03 20:50:02 -08:00
|
|
|
|
UserNotificationManager.shared.start()
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-11-18 16:39:51 -08:00
|
|
|
|
ArticleThemesManager.shared.start()
|
2025-11-04 21:30:13 -08:00
|
|
|
|
NetworkMonitor.shared.start()
|
2026-01-05 12:05:35 -08:00
|
|
|
|
|
|
|
|
|
|
#if !SKIP_APP_GROUP_ACCESS
|
2025-11-03 21:02:12 -08:00
|
|
|
|
ExtensionContainersFile.shared.start()
|
2025-11-03 21:15:50 -08:00
|
|
|
|
ExtensionFeedAddRequestFile.shared.start()
|
2026-01-05 12:05:35 -08:00
|
|
|
|
#endif
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-05-20 13:51:08 -05:00
|
|
|
|
#if DEBUG
|
2025-11-03 21:32:24 -08:00
|
|
|
|
ArticleStatusSyncTimer.shared.update()
|
2019-05-20 13:51:08 -05:00
|
|
|
|
#endif
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2018-02-05 13:29:46 -08:00
|
|
|
|
return true
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2018-02-05 13:29:46 -08:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 18:07:54 -08:00
|
|
|
|
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
|
2025-11-05 13:15:36 -08:00
|
|
|
|
Task { @MainActor in
|
2026-06-06 16:04:56 -07:00
|
|
|
|
self.resumeIfNecessary()
|
2025-11-05 13:15:36 -08:00
|
|
|
|
await AccountManager.shared.receiveRemoteNotification(userInfo: userInfo)
|
|
|
|
|
|
self.suspendApplication()
|
|
|
|
|
|
completionHandler(.newData)
|
2020-05-02 10:02:58 -05:00
|
|
|
|
}
|
2020-03-30 02:48:25 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-06-28 10:28:02 -05:00
|
|
|
|
func applicationWillTerminate(_ application: UIApplication) {
|
|
|
|
|
|
shuttingDown = true
|
2018-02-05 13:29:46 -08:00
|
|
|
|
}
|
2021-05-08 12:42:44 -07:00
|
|
|
|
|
|
|
|
|
|
func applicationDidEnterBackground(_ application: UIApplication) {
|
2025-10-01 11:49:49 -07:00
|
|
|
|
updateBadge()
|
2026-03-07 17:18:03 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
|
2026-03-14 21:50:55 -07:00
|
|
|
|
AppNotification.postLowMemory()
|
2021-05-08 12:42:44 -07:00
|
|
|
|
}
|
2025-10-01 11:49:49 -07:00
|
|
|
|
|
|
|
|
|
|
private func updateBadge() {
|
|
|
|
|
|
assert(unreadCount == AccountManager.shared.unreadCount)
|
2026-10-22 13:46:32 -07:00
|
|
|
|
let badgeCount = AppDefaults.unreadCountDisplay == .count ? unreadCount : 0
|
2026-10-06 11:00:55 -07:00
|
|
|
|
UNUserNotificationCenter.current().setBadgeCount(badgeCount)
|
2025-10-01 11:49:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-28 10:28:02 -05:00
|
|
|
|
// MARK: Notifications
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-06-28 10:28:02 -05:00
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
|
|
|
|
|
if note.object is AccountManager {
|
|
|
|
|
|
unreadCount = AccountManager.shared.unreadCount
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-06-28 10:28:02 -05:00
|
|
|
|
@objc func accountRefreshDidFinish(_ note: Notification) {
|
2026-10-22 13:46:32 -07:00
|
|
|
|
AppDefaults.lastRefresh = Date()
|
2019-06-28 10:28:02 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-10-06 11:00:55 -07:00
|
|
|
|
@objc func handleUnreadCountDisplaySettingDidChange(_ notification: Notification) {
|
|
|
|
|
|
updateBadge()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-28 10:28:02 -05:00
|
|
|
|
// MARK: - API
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-02 17:50:35 -08:00
|
|
|
|
func manualRefresh(errorHandler: @escaping @Sendable (Error) -> Void) {
|
2026-01-04 12:36:41 -08:00
|
|
|
|
let sceneDelegates = UIApplication.shared.connectedScenes.compactMap { $0.delegate as? SceneDelegate }
|
|
|
|
|
|
for sceneDelegate in sceneDelegates {
|
|
|
|
|
|
sceneDelegate.cleanUp(conditional: true)
|
2020-03-11 14:47:00 -06:00
|
|
|
|
}
|
2025-11-16 19:59:48 -08:00
|
|
|
|
AccountManager.shared.refreshAllWithoutWaiting(errorHandler: errorHandler)
|
2020-03-11 14:47:00 -06:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-06-06 16:04:56 -07:00
|
|
|
|
/// Un-suspend network activity if it was suspended on background entry.
|
|
|
|
|
|
func resumeIfNecessary() {
|
2026-07-28 21:41:11 -07:00
|
|
|
|
AppNotification.postAppDidBecomeActive()
|
2020-01-10 16:32:06 -07:00
|
|
|
|
if AccountManager.shared.isSuspended {
|
|
|
|
|
|
AccountManager.shared.resumeAll()
|
2025-10-02 21:32:09 -07:00
|
|
|
|
Self.logger.info("Application processing resumed.")
|
2020-01-10 16:32:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-06-28 10:28:02 -05:00
|
|
|
|
func prepareAccountsForBackground() {
|
2025-10-01 11:49:49 -07:00
|
|
|
|
updateBadge()
|
2026-01-05 12:05:35 -08:00
|
|
|
|
|
|
|
|
|
|
#if !SKIP_APP_GROUP_ACCESS
|
2025-11-03 21:15:50 -08:00
|
|
|
|
ExtensionFeedAddRequestFile.shared.suspend()
|
2026-01-05 12:05:35 -08:00
|
|
|
|
#endif
|
2026-01-07 15:44:49 -08:00
|
|
|
|
|
2025-11-03 21:32:24 -08:00
|
|
|
|
ArticleStatusSyncTimer.shared.invalidate()
|
2019-06-19 23:26:03 +08:00
|
|
|
|
scheduleBackgroundFeedRefresh()
|
2019-10-31 19:20:52 -05:00
|
|
|
|
syncArticleStatus()
|
2025-11-16 19:59:48 -08:00
|
|
|
|
WidgetDataEncoder.shared?.encode()
|
2019-12-01 16:51:25 -06:00
|
|
|
|
waitForSyncTasksToFinish()
|
2018-02-05 13:29:46 -08:00
|
|
|
|
}
|
2024-12-04 17:36:08 -08:00
|
|
|
|
|
2019-06-28 10:28:02 -05:00
|
|
|
|
func prepareAccountsForForeground() {
|
2025-10-01 11:49:49 -07:00
|
|
|
|
updateBadge()
|
2026-01-05 19:35:03 -08:00
|
|
|
|
#if !SKIP_APP_GROUP_ACCESS
|
2025-11-03 21:15:50 -08:00
|
|
|
|
ExtensionFeedAddRequestFile.shared.resume()
|
2026-01-05 19:35:03 -08:00
|
|
|
|
#endif
|
2025-11-03 21:32:24 -08:00
|
|
|
|
ArticleStatusSyncTimer.shared.update()
|
2020-04-21 20:23:46 -05:00
|
|
|
|
|
2026-10-22 13:46:32 -07:00
|
|
|
|
if let lastRefresh = AppDefaults.lastRefresh {
|
2019-04-26 15:24:39 -05:00
|
|
|
|
if Date() > lastRefresh.addingTimeInterval(15 * 60) {
|
2025-11-16 19:59:48 -08:00
|
|
|
|
AccountManager.shared.refreshAllWithoutWaiting(errorHandler: ErrorHandler.log)
|
2019-06-28 10:28:02 -05:00
|
|
|
|
} else {
|
2025-11-16 19:59:48 -08:00
|
|
|
|
AccountManager.shared.syncArticleStatusAllWithoutWaiting()
|
2019-04-26 15:24:39 -05:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2025-11-16 19:59:48 -08:00
|
|
|
|
AccountManager.shared.refreshAllWithoutWaiting(errorHandler: ErrorHandler.log)
|
2019-04-26 15:24:39 -05:00
|
|
|
|
}
|
2019-04-24 07:30:35 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-11-18 19:58:48 -08:00
|
|
|
|
nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
2024-12-07 14:52:38 -08:00
|
|
|
|
completionHandler([.list, .banner, .badge, .sound])
|
2019-10-02 19:42:16 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-08-04 10:43:49 -07:00
|
|
|
|
// Wrapper to safely transfer non-Sendable values to MainActor
|
|
|
|
|
|
private struct UnsafeSendable<T>: @unchecked Sendable {
|
|
|
|
|
|
let value: T
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-08-04 10:43:49 -07:00
|
|
|
|
nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-11-18 19:58:48 -08:00
|
|
|
|
let wrappedResponse = UnsafeSendable(value: response)
|
|
|
|
|
|
let wrappedCompletionHandler = UnsafeSendable(value: completionHandler)
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-11-18 19:58:48 -08:00
|
|
|
|
Task { @MainActor in
|
2026-08-04 10:43:49 -07:00
|
|
|
|
handle(notificationResponse: wrappedResponse.value)
|
2025-11-18 19:58:48 -08:00
|
|
|
|
wrappedCompletionHandler.value()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-04 10:43:49 -07:00
|
|
|
|
|
|
|
|
|
|
private func handle(notificationResponse response: UNNotificationResponse) {
|
|
|
|
|
|
|
|
|
|
|
|
let userInfo = response.notification.request.content.userInfo
|
|
|
|
|
|
|
|
|
|
|
|
switch response.actionIdentifier {
|
|
|
|
|
|
case UserNotificationManager.ActionIdentifier.markAsRead:
|
|
|
|
|
|
handleMarkAsRead(userInfo: userInfo)
|
|
|
|
|
|
case UserNotificationManager.ActionIdentifier.markAsStarred:
|
|
|
|
|
|
handleMarkAsStarred(userInfo: userInfo)
|
|
|
|
|
|
default:
|
|
|
|
|
|
if let sceneDelegate = response.targetScene?.delegate as? SceneDelegate {
|
|
|
|
|
|
sceneDelegate.handle(response)
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
|
|
|
|
|
|
sceneDelegate.coordinator.dismissIfLaunchingFromExternalAction()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-02-05 13:29:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-01 16:54:07 -05:00
|
|
|
|
// MARK: App Initialization
|
|
|
|
|
|
|
|
|
|
|
|
private extension AppDelegate {
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-09-01 16:54:07 -05:00
|
|
|
|
private func initializeHomeScreenQuickActions() {
|
|
|
|
|
|
let unreadTitle = NSLocalizedString("First Unread", comment: "First Unread")
|
2019-09-26 15:51:16 -05:00
|
|
|
|
let unreadIcon = UIApplicationShortcutIcon(systemImageName: "chevron.down.circle")
|
2019-09-01 16:54:07 -05:00
|
|
|
|
let unreadItem = UIApplicationShortcutItem(type: "com.ranchero.NetNewsWire.FirstUnread", localizedTitle: unreadTitle, localizedSubtitle: nil, icon: unreadIcon, userInfo: nil)
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-09-01 16:54:07 -05:00
|
|
|
|
let searchTitle = NSLocalizedString("Search", comment: "Search")
|
|
|
|
|
|
let searchIcon = UIApplicationShortcutIcon(systemImageName: "magnifyingglass")
|
|
|
|
|
|
let searchItem = UIApplicationShortcutItem(type: "com.ranchero.NetNewsWire.ShowSearch", localizedTitle: searchTitle, localizedSubtitle: nil, icon: searchIcon, userInfo: nil)
|
|
|
|
|
|
|
2019-09-02 15:45:09 -05:00
|
|
|
|
let addTitle = NSLocalizedString("Add Feed", comment: "Add Feed")
|
2019-09-02 15:14:26 -05:00
|
|
|
|
let addIcon = UIApplicationShortcutIcon(systemImageName: "plus")
|
|
|
|
|
|
let addItem = UIApplicationShortcutItem(type: "com.ranchero.NetNewsWire.ShowAdd", localizedTitle: addTitle, localizedSubtitle: nil, icon: addIcon, userInfo: nil)
|
|
|
|
|
|
|
2019-09-02 16:05:55 -05:00
|
|
|
|
UIApplication.shared.shortcutItems = [addItem, searchItem, unreadItem]
|
2019-09-01 16:54:07 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-09-01 16:54:07 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-31 19:20:52 -05:00
|
|
|
|
// MARK: Go To Background
|
2019-12-02 14:14:35 -06:00
|
|
|
|
|
2019-10-31 19:20:52 -05:00
|
|
|
|
private extension AppDelegate {
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-12-01 16:51:25 -06:00
|
|
|
|
func waitForSyncTasksToFinish() {
|
2026-09-04 08:31:18 -07:00
|
|
|
|
guard !isWaitingForSyncTasks && UIApplication.shared.applicationState == .background else {
|
2019-10-31 19:20:52 -05:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-09-04 08:31:18 -07:00
|
|
|
|
isWaitingForSyncTasks = true
|
|
|
|
|
|
|
|
|
|
|
|
var backgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid
|
|
|
|
|
|
|
|
|
|
|
|
/// Make sure this run’s background task is ended exactly once.
|
|
|
|
|
|
func endWaitBackgroundTask() {
|
|
|
|
|
|
guard backgroundTaskIdentifier != .invalid else {
|
|
|
|
|
|
return
|
2019-10-31 19:20:52 -05:00
|
|
|
|
}
|
2026-09-04 08:31:18 -07:00
|
|
|
|
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
|
|
|
|
|
|
backgroundTaskIdentifier = .invalid
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let waitTask = Task { @MainActor in
|
|
|
|
|
|
let shouldSuspend = await waitToComplete()
|
|
|
|
|
|
isWaitingForSyncTasks = false
|
|
|
|
|
|
if shouldSuspend {
|
|
|
|
|
|
suspendApplication()
|
|
|
|
|
|
}
|
|
|
|
|
|
endWaitBackgroundTask()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Wait for Sync Tasks") {
|
|
|
|
|
|
waitTask.cancel()
|
|
|
|
|
|
self.suspendApplication()
|
|
|
|
|
|
endWaitBackgroundTask()
|
|
|
|
|
|
Self.logger.info("Accounts wait for progress terminated for running too long.")
|
2019-10-31 19:20:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-09-04 08:31:18 -07:00
|
|
|
|
/// Wait for the refresh, status sync, and widget encode to finish. Returns whether the app should suspend.
|
|
|
|
|
|
func waitToComplete() async -> Bool {
|
|
|
|
|
|
while !Task.isCancelled {
|
|
|
|
|
|
guard UIApplication.shared.applicationState == .background else {
|
|
|
|
|
|
Self.logger.info("App came back to foreground, no longer waiting.")
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if AccountManager.shared.refreshInProgress || isSyncArticleStatusRunning || WidgetDataEncoder.shared?.isRunning ?? false {
|
|
|
|
|
|
Self.logger.info("Waiting for sync to finish…")
|
|
|
|
|
|
try? await Task.sleep(for: .seconds(1))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Self.logger.info("Refresh progress complete.")
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2019-12-01 16:51:25 -06:00
|
|
|
|
}
|
2026-09-04 08:31:18 -07:00
|
|
|
|
|
|
|
|
|
|
return false
|
2019-12-01 16:51:25 -06:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-10-31 19:20:52 -05:00
|
|
|
|
func syncArticleStatus() {
|
2026-09-03 11:49:15 -07:00
|
|
|
|
guard !isSyncArticleStatusRunning else {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-12-01 16:51:25 -06:00
|
|
|
|
isSyncArticleStatusRunning = true
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-09-03 11:49:15 -07:00
|
|
|
|
var backgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid
|
|
|
|
|
|
|
|
|
|
|
|
/// Make sure this run’s background task is ended exactly once.
|
|
|
|
|
|
func endSyncBackgroundTask() {
|
|
|
|
|
|
guard backgroundTaskIdentifier != .invalid else {
|
2026-05-26 16:28:46 -07:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-09-03 11:49:15 -07:00
|
|
|
|
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
|
|
|
|
|
|
backgroundTaskIdentifier = .invalid
|
2019-10-31 19:20:52 -05:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-09-03 11:49:15 -07:00
|
|
|
|
let syncTask = Task { @MainActor in
|
2025-11-16 19:59:48 -08:00
|
|
|
|
await AccountManager.shared.syncArticleStatusAll()
|
2026-09-03 11:49:15 -07:00
|
|
|
|
isSyncArticleStatusRunning = false
|
|
|
|
|
|
endSyncBackgroundTask()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Sync Article Status") {
|
|
|
|
|
|
syncTask.cancel()
|
|
|
|
|
|
endSyncBackgroundTask()
|
|
|
|
|
|
Self.logger.info("Accounts sync processing terminated for running too long.")
|
2019-10-31 19:20:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-12-02 14:14:35 -06:00
|
|
|
|
func suspendApplication() {
|
2026-03-14 21:50:55 -07:00
|
|
|
|
guard UIApplication.shared.applicationState == .background else {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-04-14 09:37:52 -07:00
|
|
|
|
guard !AccountManager.shared.isSuspended else {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-12-04 17:27:39 -07:00
|
|
|
|
AccountManager.shared.suspendNetworkAll()
|
2026-09-01 08:58:31 -07:00
|
|
|
|
AccountManager.shared.saveAllIfNeeded()
|
2021-09-21 09:10:56 +08:00
|
|
|
|
ArticleThemeDownloader.shared.cleanUp()
|
2020-02-02 12:11:39 -08:00
|
|
|
|
|
2026-03-14 21:50:55 -07:00
|
|
|
|
AppNotification.postAppDidGoToBackground()
|
2026-03-07 17:18:03 -08:00
|
|
|
|
|
2019-12-02 14:14:35 -06:00
|
|
|
|
CoalescingQueue.standard.performCallsImmediately()
|
|
|
|
|
|
for scene in UIApplication.shared.connectedScenes {
|
|
|
|
|
|
if let sceneDelegate = scene.delegate as? SceneDelegate {
|
|
|
|
|
|
sceneDelegate.suspend()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-10-02 21:32:09 -07:00
|
|
|
|
Self.logger.info("Application processing suspended.")
|
2019-12-02 14:14:35 -06:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-10-31 19:20:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 20:42:34 -08:00
|
|
|
|
// MARK: - Background Tasks
|
2019-06-19 23:26:03 +08:00
|
|
|
|
|
2019-04-27 13:54:52 -05:00
|
|
|
|
private extension AppDelegate {
|
2019-06-19 23:26:03 +08:00
|
|
|
|
/// Register all background tasks.
|
2026-01-11 20:42:34 -08:00
|
|
|
|
nonisolated func registerBackgroundTasks() {
|
2019-06-19 23:26:03 +08:00
|
|
|
|
// Register background feed refresh.
|
2026-01-11 20:42:34 -08:00
|
|
|
|
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.ranchero.NetNewsWire.FeedRefresh", using: nil) { task in
|
2019-06-19 23:26:03 +08:00
|
|
|
|
self.performBackgroundFeedRefresh(with: task as! BGAppRefreshTask)
|
2019-04-29 09:29:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-08-11 11:27:15 -07:00
|
|
|
|
/// Ask the system for the next background app refresh.
|
|
|
|
|
|
/// The actual timing is up to the system.
|
2026-01-11 20:42:34 -08:00
|
|
|
|
nonisolated func scheduleBackgroundFeedRefresh() {
|
2019-11-13 17:13:06 -06:00
|
|
|
|
// We send this to a dedicated serial queue because as of 11/05/19 on iOS 13.2 the call to the
|
2019-11-05 19:14:26 -06:00
|
|
|
|
// task scheduler can hang indefinitely.
|
2025-11-03 21:32:24 -08:00
|
|
|
|
backgroundTaskDispatchQueue.async {
|
2019-11-05 19:14:26 -06:00
|
|
|
|
do {
|
2026-08-30 20:49:13 -07:00
|
|
|
|
let earliestBeginInterval: TimeInterval = 60 * 60
|
2025-11-18 19:07:17 -08:00
|
|
|
|
let request = BGAppRefreshTaskRequest(identifier: "com.ranchero.NetNewsWire.FeedRefresh")
|
2026-08-11 11:27:15 -07:00
|
|
|
|
request.earliestBeginDate = Date(timeIntervalSinceNow: earliestBeginInterval)
|
2019-11-05 19:14:26 -06:00
|
|
|
|
try BGTaskScheduler.shared.submit(request)
|
|
|
|
|
|
} catch {
|
2025-10-02 21:32:09 -07:00
|
|
|
|
Self.logger.error("Could not schedule app refresh: \(error.localizedDescription)")
|
2019-11-05 19:14:26 -06:00
|
|
|
|
}
|
2019-06-19 23:26:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-11 20:42:34 -08:00
|
|
|
|
nonisolated func performBackgroundFeedRefresh(with task: BGAppRefreshTask) {
|
2024-12-04 17:36:08 -08:00
|
|
|
|
|
2019-06-19 23:26:03 +08:00
|
|
|
|
scheduleBackgroundFeedRefresh() // schedule next refresh
|
2024-12-04 17:36:08 -08:00
|
|
|
|
|
2025-11-16 19:59:48 -08:00
|
|
|
|
Self.logger.info("Performing background refresh.")
|
2019-10-02 16:41:32 -05:00
|
|
|
|
|
2026-10-21 21:22:29 -07:00
|
|
|
|
let refreshTaskIsCompleted = OSAllocatedUnfairLock(initialState: false)
|
2026-08-30 21:23:08 -07:00
|
|
|
|
|
2026-09-05 10:08:28 -07:00
|
|
|
|
enum RefreshOutcome {
|
|
|
|
|
|
case completed
|
|
|
|
|
|
case noNetwork
|
|
|
|
|
|
case expired
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-30 21:23:08 -07:00
|
|
|
|
/// Make sure task.setTaskCompleted is called exactly once.
|
2026-09-05 10:08:28 -07:00
|
|
|
|
func completeRefreshTask(_ outcome: RefreshOutcome) {
|
2026-08-30 21:23:08 -07:00
|
|
|
|
let shouldComplete = refreshTaskIsCompleted.withLock { isCompleted -> Bool in
|
|
|
|
|
|
if isCompleted {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
isCompleted = true
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
guard shouldComplete else {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-09-05 10:08:28 -07:00
|
|
|
|
|
|
|
|
|
|
let success: Bool
|
|
|
|
|
|
switch outcome {
|
|
|
|
|
|
case .completed:
|
2026-08-30 21:23:08 -07:00
|
|
|
|
Self.logger.info("Background refresh completed.")
|
2026-09-05 10:08:28 -07:00
|
|
|
|
success = true
|
|
|
|
|
|
case .noNetwork:
|
|
|
|
|
|
Self.logger.info("Background refresh skipped — no network path.")
|
|
|
|
|
|
success = false
|
|
|
|
|
|
case .expired:
|
2026-08-30 21:23:08 -07:00
|
|
|
|
Self.logger.info("Background refresh terminated for running too long.")
|
2026-09-05 10:08:28 -07:00
|
|
|
|
success = false
|
2026-08-30 21:23:08 -07:00
|
|
|
|
}
|
2026-09-05 10:08:28 -07:00
|
|
|
|
|
2026-08-30 21:23:08 -07:00
|
|
|
|
task.setTaskCompleted(success: success)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let refreshTask = Task { @MainActor in
|
2019-12-01 16:51:25 -06:00
|
|
|
|
if AccountManager.shared.isSuspended {
|
|
|
|
|
|
AccountManager.shared.resumeAll()
|
|
|
|
|
|
}
|
2026-09-05 10:08:28 -07:00
|
|
|
|
let didRefresh = await AccountManager.shared.refreshAll(errorHandler: ErrorHandler.log)
|
2026-08-30 21:23:08 -07:00
|
|
|
|
if !Task.isCancelled {
|
2026-06-13 14:11:58 -07:00
|
|
|
|
await WidgetDataEncoder.shared?.encodeAndWait()
|
2019-06-19 23:26:03 +08:00
|
|
|
|
}
|
2026-08-30 21:23:08 -07:00
|
|
|
|
self.suspendApplication()
|
2026-09-05 10:08:28 -07:00
|
|
|
|
completeRefreshTask(didRefresh ? .completed : .noNetwork)
|
2019-06-19 23:26:03 +08:00
|
|
|
|
}
|
2024-12-04 17:36:08 -08:00
|
|
|
|
|
2026-08-30 21:23:08 -07:00
|
|
|
|
task.expirationHandler = {
|
|
|
|
|
|
refreshTask.cancel()
|
2026-09-05 10:08:28 -07:00
|
|
|
|
completeRefreshTask(.expired)
|
2026-01-11 20:42:34 -08:00
|
|
|
|
Task { @MainActor in
|
2024-12-04 17:36:08 -08:00
|
|
|
|
self.suspendApplication()
|
|
|
|
|
|
}
|
2019-06-19 23:26:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-12-23 20:15:25 +08:00
|
|
|
|
|
2026-01-11 20:42:34 -08:00
|
|
|
|
// MARK: - Handle Notification Actions
|
2020-12-23 20:15:25 +08:00
|
|
|
|
|
|
|
|
|
|
private extension AppDelegate {
|
|
|
|
|
|
func handleMarkAsRead(userInfo: [AnyHashable: Any]) {
|
2025-10-02 21:32:09 -07:00
|
|
|
|
handleStatusNotification(userInfo: userInfo, statusKey: .read)
|
2020-12-23 20:15:25 +08:00
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2020-12-23 20:15:25 +08:00
|
|
|
|
func handleMarkAsStarred(userInfo: [AnyHashable: Any]) {
|
2025-10-02 21:32:09 -07:00
|
|
|
|
handleStatusNotification(userInfo: userInfo, statusKey: .starred)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private func handleStatusNotification(userInfo: [AnyHashable: Any], statusKey: ArticleStatus.Key) {
|
2026-01-01 18:07:54 -08:00
|
|
|
|
guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable: Any],
|
2020-12-23 20:15:25 +08:00
|
|
|
|
let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String,
|
|
|
|
|
|
let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String else {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-02 21:32:09 -07:00
|
|
|
|
|
2026-06-06 16:04:56 -07:00
|
|
|
|
resumeIfNecessary()
|
2025-10-02 21:32:09 -07:00
|
|
|
|
|
2025-11-17 21:53:40 -08:00
|
|
|
|
guard let account = AccountManager.shared.existingAccount(accountID: accountID) else {
|
2025-10-02 21:32:09 -07:00
|
|
|
|
assertionFailure("Expected account with \(accountID)")
|
|
|
|
|
|
Self.logger.error("No account with accountID \(accountID) found from status notification")
|
2020-12-23 20:15:25 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-02 21:32:09 -07:00
|
|
|
|
|
2025-11-06 19:42:27 -08:00
|
|
|
|
Task { @MainActor in
|
2026-06-16 07:53:06 -07:00
|
|
|
|
try? await account.markArticles(articleIDs: [articleID], statusKey: statusKey, flag: true)
|
2026-05-30 11:12:29 -07:00
|
|
|
|
_ = try? await account.syncArticleStatus()
|
2025-11-06 19:42:27 -08:00
|
|
|
|
prepareAccountsForBackground()
|
|
|
|
|
|
suspendApplication()
|
|
|
|
|
|
}
|
2020-12-23 20:15:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|