NetNewsWire/iOS/AppDelegate.swift

500 lines
16 KiB
Swift
Raw Normal View History

//
// AppDelegate.swift
// NetNewsWire
//
// Created by Maurice Parker on 4/8/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
import Web
import Account
2019-06-19 17:26:03 +02:00
import BackgroundTasks
2019-04-26 14:44:00 +02:00
import os.log
import Secrets
import WidgetKit
import Core
var appDelegate: AppDelegate!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, UnreadCountProvider {
2019-06-19 17:26:03 +02:00
private var bgTaskDispatchQueue = DispatchQueue.init(label: "BGTaskScheduler")
private var waitBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
2019-06-20 01:09:42 +02:00
private var syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
2019-05-20 20:51:08 +02:00
var syncTimer: ArticleStatusSyncTimer?
2019-05-20 20:51:08 +02:00
var shuttingDown = false {
didSet {
if shuttingDown {
syncTimer?.shuttingDown = shuttingDown
syncTimer?.invalidate()
}
}
}
2019-06-19 17:26:03 +02:00
2019-11-02 18:29:01 +01:00
var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Application")
2019-06-19 17:26:03 +02:00
var userNotificationManager: UserNotificationManager!
var faviconDownloader: FaviconDownloader!
var imageDownloader: ImageDownloader!
var authorAvatarDownloader: AuthorAvatarDownloader!
var feedIconDownloader: FeedIconDownloader!
var extensionContainersFile: ExtensionContainersFile!
var extensionFeedAddRequestFile: ExtensionFeedAddRequestFile!
2019-06-19 17:26:03 +02:00
var unreadCount = 0 {
didSet {
if unreadCount != oldValue {
postUnreadCountDidChangeNotification()
2024-02-26 03:45:44 +01:00
UNUserNotificationCenter.current().setBadgeCount(unreadCount)
}
}
}
var isSyncArticleStatusRunning = false
var isWaitingForSyncTasks = false
2024-03-20 04:33:54 +01:00
let accountManager: AccountManager
private var secretsProvider = Secrets()
override init() {
let documentFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let documentAccountsFolder = documentFolder.appendingPathComponent("Accounts").absoluteString
let documentAccountsFolderPath = String(documentAccountsFolder.suffix(from: documentAccountsFolder.index(documentAccountsFolder.startIndex, offsetBy: 7)))
2024-03-20 04:33:54 +01:00
self.accountManager = AccountManager(accountsFolder: documentAccountsFolderPath, secretsProvider: secretsProvider)
AccountManager.shared = accountManager
super.init()
appDelegate = self
2021-09-08 03:12:21 +02:00
let documentThemesFolder = documentFolder.appendingPathComponent("Themes").absoluteString
let documentThemesFolderPath = String(documentThemesFolder.suffix(from: documentAccountsFolder.index(documentThemesFolder.startIndex, offsetBy: 7)))
ArticleThemesManager.shared = ArticleThemesManager(folderPath: documentThemesFolderPath)
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(accountRefreshDidFinish(_:)), name: .AccountRefreshDidFinish, object: nil)
}
2019-06-19 17:26:03 +02:00
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AppDefaults.registerDefaults()
let isFirstRun = AppDefaults.shared.isFirstRun
if isFirstRun {
2019-04-26 14:44:00 +02:00
os_log("Is first run.", log: log, type: .info)
}
2024-03-20 04:33:54 +01:00
if isFirstRun && !accountManager.anyAccountHasAtLeastOneFeed() {
let localAccount = accountManager.defaultAccount
DefaultFeedsImporter.importDefaultFeeds(account: localAccount)
}
registerBackgroundTasks()
CacheCleaner.purgeIfNecessary()
2019-09-01 23:54:07 +02:00
initializeDownloaders()
initializeHomeScreenQuickActions()
2019-06-19 17:26:03 +02:00
2024-03-20 04:33:54 +01:00
Task { @MainActor in
self.unreadCount = accountManager.unreadCount
}
2024-03-20 04:33:54 +01:00
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in
if granted {
2024-03-20 04:33:54 +01:00
Task { @MainActor in
2019-04-23 14:48:22 +02:00
UIApplication.shared.registerForRemoteNotifications()
}
}
}
UNUserNotificationCenter.current().delegate = self
userNotificationManager = UserNotificationManager()
extensionContainersFile = ExtensionContainersFile()
extensionFeedAddRequestFile = ExtensionFeedAddRequestFile()
2019-05-20 20:51:08 +02:00
syncTimer = ArticleStatusSyncTimer()
2019-06-19 17:26:03 +02:00
2019-05-20 20:51:08 +02:00
#if DEBUG
2019-06-19 17:26:03 +02:00
syncTimer!.update()
2019-05-20 20:51:08 +02:00
#endif
2021-11-02 11:44:21 +01:00
return true
}
2019-06-19 17:26:03 +02:00
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
resumeDatabaseProcessingIfNecessary()
await accountManager.receiveRemoteNotification(userInfo: userInfo)
suspendApplication()
return .newData
}
2019-06-28 17:28:02 +02:00
func applicationWillTerminate(_ application: UIApplication) {
shuttingDown = true
}
func applicationDidEnterBackground(_ application: UIApplication) {
IconImageCache.shared.emptyCache()
}
2019-06-28 17:28:02 +02:00
// MARK: Notifications
@objc func unreadCountDidChange(_ note: Notification) {
if note.object is AccountManager {
2024-03-20 04:33:54 +01:00
unreadCount = accountManager.unreadCount
2019-06-28 17:28:02 +02:00
}
}
2019-06-19 17:26:03 +02:00
2019-06-28 17:28:02 +02:00
@objc func accountRefreshDidFinish(_ note: Notification) {
AppDefaults.shared.lastRefresh = Date()
2019-06-28 17:28:02 +02:00
}
// MARK: - API
func manualRefresh(errorHandler: @escaping (Error) -> ()) {
UIApplication.shared.connectedScenes.compactMap( { $0.delegate as? SceneDelegate } ).forEach {
$0.cleanUp(conditional: true)
}
2024-03-26 07:36:27 +01:00
Task { @MainActor in
await self.accountManager.refreshAll(errorHandler: errorHandler)
}
}
func resumeDatabaseProcessingIfNecessary() {
2024-03-20 04:33:54 +01:00
if accountManager.isSuspended {
accountManager.resumeAll()
os_log("Application processing resumed.", log: self.log, type: .info)
}
}
2019-06-28 17:28:02 +02:00
func prepareAccountsForBackground() {
extensionFeedAddRequestFile.suspend()
2019-05-20 20:51:08 +02:00
syncTimer?.invalidate()
2019-06-19 17:26:03 +02:00
scheduleBackgroundFeedRefresh()
syncArticleStatus()
waitForSyncTasksToFinish()
}
2019-06-19 17:26:03 +02:00
2019-06-28 17:28:02 +02:00
func prepareAccountsForForeground() {
extensionFeedAddRequestFile.resume()
syncTimer?.update()
2024-03-26 07:36:27 +01:00
Task { @MainActor in
if let lastRefresh = AppDefaults.shared.lastRefresh {
if Date() > lastRefresh.addingTimeInterval(15 * 60) {
await accountManager.refreshAll(errorHandler: ErrorHandler.log)
} else {
await accountManager.syncArticleStatusAll()
}
2024-03-26 07:36:27 +01:00
} else {
await accountManager.refreshAll(errorHandler: ErrorHandler.log)
}
}
}
2024-03-26 07:36:27 +01:00
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
2024-02-26 04:02:29 +01:00
completionHandler([.list, .banner, .badge, .sound])
}
2019-10-03 16:53:21 +02:00
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
defer { completionHandler() }
2020-12-23 13:15:25 +01:00
let userInfo = response.notification.request.content.userInfo
switch response.actionIdentifier {
case "MARK_AS_READ":
handleMarkAsRead(userInfo: userInfo)
case "MARK_AS_STARRED":
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()
})
2020-12-23 13:15:25 +01:00
}
2019-10-03 16:53:21 +02:00
}
2020-12-23 13:15:25 +01:00
2019-10-03 16:53:21 +02:00
}
}
2019-09-01 23:54:07 +02:00
// MARK: App Initialization
private extension AppDelegate {
private func initializeDownloaders() {
let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let faviconsFolderURL = tempDir.appendingPathComponent("Favicons")
let imagesFolderURL = tempDir.appendingPathComponent("Images")
2019-09-01 23:54:07 +02:00
try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil)
let faviconsFolder = faviconsFolderURL.absoluteString
let faviconsFolderPath = faviconsFolder.suffix(from: faviconsFolder.index(faviconsFolder.startIndex, offsetBy: 7))
faviconDownloader = FaviconDownloader(folder: String(faviconsFolderPath))
let imagesFolder = imagesFolderURL.absoluteString
let imagesFolderPath = imagesFolder.suffix(from: imagesFolder.index(imagesFolder.startIndex, offsetBy: 7))
try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)
imageDownloader = ImageDownloader(folder: String(imagesFolderPath))
authorAvatarDownloader = AuthorAvatarDownloader(imageDownloader: imageDownloader)
let tempFolder = tempDir.absoluteString
let tempFolderPath = tempFolder.suffix(from: tempFolder.index(tempFolder.startIndex, offsetBy: 7))
feedIconDownloader = FeedIconDownloader(imageDownloader: imageDownloader, folder: String(tempFolderPath))
2019-09-01 23:54:07 +02:00
}
private func initializeHomeScreenQuickActions() {
let unreadTitle = NSLocalizedString("First Unread", comment: "First Unread")
let unreadIcon = UIApplicationShortcutIcon(systemImageName: "chevron.down.circle")
2019-09-01 23:54:07 +02:00
let unreadItem = UIApplicationShortcutItem(type: "com.ranchero.NetNewsWire.FirstUnread", localizedTitle: unreadTitle, localizedSubtitle: nil, icon: unreadIcon, userInfo: nil)
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 22:45:09 +02:00
let addTitle = NSLocalizedString("Add Feed", comment: "Add Feed")
let addIcon = UIApplicationShortcutIcon(systemImageName: "plus")
let addItem = UIApplicationShortcutItem(type: "com.ranchero.NetNewsWire.ShowAdd", localizedTitle: addTitle, localizedSubtitle: nil, icon: addIcon, userInfo: nil)
2019-09-02 23:05:55 +02:00
UIApplication.shared.shortcutItems = [addItem, searchItem, unreadItem]
2019-09-01 23:54:07 +02:00
}
}
// MARK: Go To Background
private extension AppDelegate {
func waitForSyncTasksToFinish() {
guard !isWaitingForSyncTasks && UIApplication.shared.applicationState == .background else { return }
isWaitingForSyncTasks = true
self.waitBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask { [weak self] in
guard let self = self else { return }
self.completeProcessing(true)
os_log("Accounts wait for progress terminated for running too long.", log: self.log, type: .info)
}
DispatchQueue.main.async { [weak self] in
self?.waitToComplete() { [weak self] suspend in
self?.completeProcessing(suspend)
}
}
}
func waitToComplete(completion: @escaping (Bool) -> Void) {
guard UIApplication.shared.applicationState == .background else {
2022-01-04 23:25:20 +01:00
os_log("App came back to foreground, no longer waiting.", log: self.log, type: .info)
completion(false)
return
}
2024-03-20 04:33:54 +01:00
if accountManager.refreshInProgress || isSyncArticleStatusRunning {
os_log("Waiting for sync to finish...", log: self.log, type: .info)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
self?.waitToComplete(completion: completion)
}
} else {
os_log("Refresh progress complete.", log: self.log, type: .info)
completion(true)
}
}
func completeProcessing(_ suspend: Bool) {
if suspend {
suspendApplication()
}
UIApplication.shared.endBackgroundTask(self.waitBackgroundUpdateTask)
self.waitBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
isWaitingForSyncTasks = false
}
func syncArticleStatus() {
guard !isSyncArticleStatusRunning else { return }
isSyncArticleStatusRunning = true
let completeProcessing = { [unowned self] in
self.isSyncArticleStatusRunning = false
UIApplication.shared.endBackgroundTask(self.syncBackgroundUpdateTask)
self.syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
}
self.syncBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask {
completeProcessing()
os_log("Accounts sync processing terminated for running too long.", log: self.log, type: .info)
}
2024-03-20 04:33:54 +01:00
Task { @MainActor in
await self.accountManager.syncArticleStatusAll()
completeProcessing()
}
}
func suspendApplication() {
guard UIApplication.shared.applicationState == .background else { return }
2024-03-20 04:33:54 +01:00
accountManager.suspendNetworkAll()
accountManager.suspendDatabaseAll()
ArticleThemeDownloader.cleanUp()
CoalescingQueue.standard.performCallsImmediately()
for scene in UIApplication.shared.connectedScenes {
if let sceneDelegate = scene.delegate as? SceneDelegate {
sceneDelegate.suspend()
}
}
os_log("Application processing suspended.", log: self.log, type: .info)
}
}
2019-09-01 23:54:07 +02:00
// MARK: Background Tasks
2019-06-19 17:26:03 +02:00
private extension AppDelegate {
2019-06-28 17:28:02 +02:00
2019-06-19 17:26:03 +02:00
/// Register all background tasks.
func registerBackgroundTasks() {
// Register background feed refresh.
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.ranchero.NetNewsWire.FeedRefresh", using: nil) { (task) in
self.performBackgroundFeedRefresh(with: task as! BGAppRefreshTask)
}
}
2019-06-19 17:26:03 +02:00
/// Schedules a background app refresh based on `AppDefaults.refreshInterval`.
func scheduleBackgroundFeedRefresh() {
let request = BGAppRefreshTaskRequest(identifier: "com.ranchero.NetNewsWire.FeedRefresh")
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
// We send this to a dedicated serial queue because as of 11/05/19 on iOS 13.2 the call to the
// task scheduler can hang indefinitely.
bgTaskDispatchQueue.async {
do {
try BGTaskScheduler.shared.submit(request)
} catch {
os_log(.error, log: self.log, "Could not schedule app refresh: %@", error.localizedDescription)
}
2019-06-19 17:26:03 +02:00
}
}
/// Performs background feed refresh.
/// - Parameter task: `BGAppRefreshTask`
/// - Warning: As of Xcode 11 beta 2, when triggered from the debugger this doesn't work.
func performBackgroundFeedRefresh(with task: BGAppRefreshTask) {
scheduleBackgroundFeedRefresh() // schedule next refresh
os_log("Woken to perform account refresh.", log: self.log, type: .info)
2024-03-20 04:33:54 +01:00
Task { @MainActor in
if self.accountManager.isSuspended {
self.accountManager.resumeAll()
}
2024-03-26 07:36:27 +01:00
await self.accountManager.refreshAll(errorHandler: ErrorHandler.log)
if !self.accountManager.isSuspended {
try? WidgetDataEncoder.shared.encodeWidgetData()
self.suspendApplication()
os_log("Account refresh operation completed.", log: self.log, type: .info)
task.setTaskCompleted(success: true)
2019-06-19 17:26:03 +02:00
}
}
2019-06-19 17:26:03 +02:00
// set expiration handler
2019-11-28 01:03:19 +01:00
task.expirationHandler = { [weak task] in
DispatchQueue.main.sync {
self.suspendApplication()
}
2019-06-19 17:26:03 +02:00
os_log("Accounts refresh processing terminated for running too long.", log: self.log, type: .info)
2019-11-28 01:03:19 +01:00
task?.setTaskCompleted(success: false)
2019-06-19 17:26:03 +02:00
}
}
}
2020-12-23 13:15:25 +01:00
// Handle Notification Actions
private extension AppDelegate {
2024-03-19 18:15:30 +01:00
@MainActor func handleMarkAsRead(userInfo: [AnyHashable: Any]) {
guard let articlePathInfo = ArticlePathInfo(userInfo: userInfo) else {
return
2020-12-23 13:15:25 +01:00
}
2024-03-19 18:15:30 +01:00
2020-12-23 13:15:25 +01:00
resumeDatabaseProcessingIfNecessary()
2024-03-19 18:15:30 +01:00
guard let accountID = articlePathInfo.accountID, let account = accountManager.existingAccount(with: accountID) else {
2020-12-23 13:15:25 +01:00
os_log(.debug, "No account found from notification.")
return
}
guard let articleID = articlePathInfo.articleID else {
os_log(.debug, "No articleID found from notification.")
return
}
2024-03-19 18:15:30 +01:00
Task { @MainActor in
guard let articles = try? await account.articles(for: .articleIDs([articleID])) else {
os_log(.debug, "No article found from search using %@", articleID)
return
2020-12-23 13:15:25 +01:00
}
2024-03-19 18:15:30 +01:00
2024-03-27 02:48:44 +01:00
try? await account.markArticles(articles, statusKey: .read, flag: true)
2024-03-19 18:15:30 +01:00
self.prepareAccountsForBackground()
2024-03-27 02:48:44 +01:00
try? await account.syncArticleStatus()
if !self.accountManager.isSuspended {
try? WidgetDataEncoder.shared.encodeWidgetData()
self.prepareAccountsForBackground()
self.suspendApplication()
}
2024-03-19 18:15:30 +01:00
}
2020-12-23 13:15:25 +01:00
}
2024-03-19 18:15:30 +01:00
@MainActor func handleMarkAsStarred(userInfo: [AnyHashable: Any]) {
guard let articlePathInfo = ArticlePathInfo(userInfo: userInfo) else {
return
2020-12-23 13:15:25 +01:00
}
2024-03-19 18:15:30 +01:00
2020-12-23 13:15:25 +01:00
resumeDatabaseProcessingIfNecessary()
2024-03-19 18:15:30 +01:00
guard let accountID = articlePathInfo.accountID, let account = accountManager.existingAccount(with: accountID) else {
2020-12-23 13:15:25 +01:00
os_log(.debug, "No account found from notification.")
return
}
guard let articleID = articlePathInfo.articleID else {
os_log(.debug, "No articleID found from notification.")
return
}
2024-03-19 18:15:30 +01:00
Task { @MainActor in
guard let articles = try? await account.articles(for: .articleIDs([articleID])) else {
os_log(.debug, "No article found from search using %@", articleID)
return
2020-12-23 13:15:25 +01:00
}
2024-03-19 18:15:30 +01:00
2024-03-27 02:48:44 +01:00
try? await account.markArticles(articles, statusKey: .starred, flag: true)
2024-03-19 18:15:30 +01:00
try? await account.syncArticleStatus()
if !self.accountManager.isSuspended {
try? WidgetDataEncoder.shared.encodeWidgetData()
self.prepareAccountsForBackground()
self.suspendApplication()
}
2024-03-19 18:15:30 +01:00
}
2020-12-23 13:15:25 +01:00
}
}