2018-02-05 22:29:46 +01:00
|
|
|
//
|
|
|
|
// AppDelegate.swift
|
2019-04-15 22:03:05 +02:00
|
|
|
// NetNewsWire
|
2018-02-05 22:29:46 +01:00
|
|
|
//
|
2019-04-15 22:03:05 +02:00
|
|
|
// Created by Maurice Parker on 4/8/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
2018-02-05 22:29:46 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2019-04-15 22:03:05 +02:00
|
|
|
import RSCore
|
2019-04-24 14:30:35 +02:00
|
|
|
import RSWeb
|
2019-04-15 22:03:05 +02:00
|
|
|
import Account
|
2019-04-23 14:48:22 +02:00
|
|
|
import UserNotifications
|
2019-04-26 14:44:00 +02:00
|
|
|
import os.log
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
var appDelegate: AppDelegate!
|
2018-02-05 22:29:46 +01:00
|
|
|
|
|
|
|
@UIApplicationMain
|
2019-04-15 22:03:05 +02:00
|
|
|
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate, UnreadCountProvider {
|
2018-02-05 22:29:46 +01:00
|
|
|
|
2019-05-20 20:51:08 +02:00
|
|
|
private var refreshBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
|
|
|
|
private var syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
|
|
|
|
|
|
|
|
var syncTimer: ArticleStatusSyncTimer?
|
2019-04-24 14:30:35 +02:00
|
|
|
|
2019-05-20 20:51:08 +02:00
|
|
|
var shuttingDown = false {
|
|
|
|
didSet {
|
|
|
|
if shuttingDown {
|
|
|
|
syncTimer?.shuttingDown = shuttingDown
|
|
|
|
syncTimer?.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 14:44:00 +02:00
|
|
|
var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "application")
|
2018-02-05 22:29:46 +01:00
|
|
|
var window: UIWindow?
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
var faviconDownloader: FaviconDownloader!
|
|
|
|
var imageDownloader: ImageDownloader!
|
|
|
|
var authorAvatarDownloader: AuthorAvatarDownloader!
|
|
|
|
var feedIconDownloader: FeedIconDownloader!
|
|
|
|
|
|
|
|
var unreadCount = 0 {
|
|
|
|
didSet {
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
postUnreadCountDidChangeNotification()
|
2019-04-23 14:48:22 +02:00
|
|
|
UIApplication.shared.applicationIconBadgeNumber = unreadCount
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
appDelegate = self
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2019-04-26 22:24:39 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountRefreshDidFinish(_:)), name: .AccountRefreshDidFinish, object: nil)
|
2019-04-24 14:30:35 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
|
|
|
|
|
2019-04-26 21:21:17 +02:00
|
|
|
// Reinitialize the shared state as early as possible
|
|
|
|
_ = AccountManager.shared
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
}
|
2018-02-05 22:29:46 +01:00
|
|
|
|
2019-04-15 20:30:10 +02:00
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
// Set up the split view
|
2018-02-05 22:29:46 +01:00
|
|
|
let splitViewController = window!.rootViewController as! UISplitViewController
|
|
|
|
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
|
|
|
|
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
|
|
|
|
splitViewController.delegate = self
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
AppDefaults.registerDefaults()
|
|
|
|
let isFirstRun = AppDefaults.isFirstRun
|
|
|
|
if isFirstRun {
|
2019-04-26 14:44:00 +02:00
|
|
|
os_log("Is first run.", log: log, type: .info)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 12:53:18 +02:00
|
|
|
let localAccount = AccountManager.shared.defaultAccount
|
2019-04-15 22:03:05 +02:00
|
|
|
DefaultFeedsImporter.importIfNeeded(isFirstRun, account: localAccount)
|
|
|
|
|
|
|
|
let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
|
|
|
|
let faviconsFolderURL = tempDir.appendingPathComponent("Favicons")
|
|
|
|
try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
faviconDownloader = FaviconDownloader(folder: faviconsFolderURL.absoluteString)
|
|
|
|
|
|
|
|
let imagesFolderURL = tempDir.appendingPathComponent("Images")
|
|
|
|
try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
imageDownloader = ImageDownloader(folder: imagesFolderURL.absoluteString)
|
|
|
|
|
|
|
|
authorAvatarDownloader = AuthorAvatarDownloader(imageDownloader: imageDownloader)
|
|
|
|
feedIconDownloader = FeedIconDownloader(imageDownloader: imageDownloader)
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.unreadCount = AccountManager.shared.unreadCount
|
|
|
|
}
|
|
|
|
|
2019-04-27 20:54:52 +02:00
|
|
|
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in
|
2019-04-23 14:48:22 +02:00
|
|
|
if granted {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
UIApplication.shared.registerForRemoteNotifications()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 17:29:14 +02:00
|
|
|
|
2019-04-29 16:29:57 +02:00
|
|
|
updateBackgroundRefreshInterval()
|
2019-05-20 20:51:08 +02:00
|
|
|
|
|
|
|
syncTimer = ArticleStatusSyncTimer()
|
2019-04-23 17:29:14 +02:00
|
|
|
|
2019-05-20 20:51:08 +02:00
|
|
|
#if DEBUG
|
|
|
|
syncTimer!.update()
|
|
|
|
#endif
|
|
|
|
|
2018-02-05 22:29:46 +01:00
|
|
|
return true
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2018-02-05 22:29:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
|
|
|
|
//
|
|
|
|
// let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
|
|
|
|
// coder.encode(versionNumber, forKey: "VersionNumber")
|
|
|
|
//
|
|
|
|
// return true
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
|
|
|
|
// if let storedVersionNumber = coder.decodeObject(forKey: "VersionNumber") as? String {
|
|
|
|
// let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
|
|
|
|
// if versionNumber == storedVersionNumber {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
|
2018-02-05 22:29:46 +01:00
|
|
|
func applicationWillResignActive(_ application: UIApplication) {
|
|
|
|
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
|
|
|
|
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationDidEnterBackground(_ application: UIApplication) {
|
2019-05-20 20:51:08 +02:00
|
|
|
syncTimer?.invalidate()
|
|
|
|
|
|
|
|
let completeProcessing = { [unowned self] in
|
|
|
|
UIApplication.shared.endBackgroundTask(self.syncBackgroundUpdateTask)
|
|
|
|
self.syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.global(qos: .background).async { [unowned self] in
|
|
|
|
|
|
|
|
self.syncBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask {
|
|
|
|
completeProcessing()
|
|
|
|
os_log("Accounts sync processing terminated for running too long.", log: self.log, type: .info)
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
AccountManager.shared.syncArticleStatusAll() {
|
|
|
|
completeProcessing()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-02-05 22:29:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func applicationWillEnterForeground(_ application: UIApplication) {
|
2019-05-20 20:51:08 +02:00
|
|
|
AccountManager.shared.syncArticleStatusAll()
|
|
|
|
syncTimer?.update()
|
2018-02-05 22:29:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func applicationDidBecomeActive(_ application: UIApplication) {
|
2019-04-26 22:24:39 +02:00
|
|
|
|
|
|
|
// If we haven't refreshed the database for 15 minutes, run a refresh automatically
|
|
|
|
if let lastRefresh = AppDefaults.lastRefresh {
|
|
|
|
if Date() > lastRefresh.addingTimeInterval(15 * 60) {
|
|
|
|
AccountManager.shared.refreshAll()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AccountManager.shared.refreshAll()
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:29:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func applicationWillTerminate(_ application: UIApplication) {
|
2019-05-20 20:51:08 +02:00
|
|
|
shuttingDown = true
|
2018-02-05 22:29:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:43:41 +02:00
|
|
|
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
|
|
|
|
|
|
|
|
os_log("Woken to perform account refresh.", log: log, type: .info)
|
|
|
|
|
2019-04-27 20:54:52 +02:00
|
|
|
var startingUnreadCount = 0
|
2019-04-26 21:03:59 +02:00
|
|
|
|
|
|
|
let completeProcessing = { [unowned self] in
|
|
|
|
|
2019-05-20 20:51:08 +02:00
|
|
|
UIApplication.shared.endBackgroundTask(self.refreshBackgroundUpdateTask)
|
|
|
|
self.refreshBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
|
2019-04-26 21:03:59 +02:00
|
|
|
|
2019-04-27 20:54:52 +02:00
|
|
|
if startingUnreadCount < self.unreadCount {
|
|
|
|
self.sendReceivedArticlesUserNotification(newArticleCount: self.unreadCount - startingUnreadCount)
|
2019-04-26 16:42:00 +02:00
|
|
|
completionHandler(.newData)
|
|
|
|
} else {
|
|
|
|
completionHandler(.noData)
|
|
|
|
}
|
2019-04-26 21:03:59 +02:00
|
|
|
|
2019-04-26 16:42:00 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 23:29:32 +02:00
|
|
|
DispatchQueue.global(qos: .background).async { [unowned self] in
|
2019-04-24 18:04:20 +02:00
|
|
|
|
2019-05-20 20:51:08 +02:00
|
|
|
self.refreshBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask {
|
2019-04-26 21:03:59 +02:00
|
|
|
completeProcessing()
|
2019-04-27 20:59:57 +02:00
|
|
|
os_log("Accounts refresh processing terminated for running too long.", log: self.log, type: .info)
|
2019-04-24 14:30:35 +02:00
|
|
|
}
|
2019-04-24 18:04:20 +02:00
|
|
|
|
2019-04-27 23:16:46 +02:00
|
|
|
while(!AccountManager.shared.isUnreadCountsInitialized) {
|
2019-04-27 20:54:52 +02:00
|
|
|
os_log("Waiting for unread counts to be initialized...", log: self.log, type: .debug)
|
|
|
|
sleep(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
startingUnreadCount = self.unreadCount
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
AccountManager.shared.refreshAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
os_log("Accounts requested to begin refresh.", log: self.log, type: .debug)
|
|
|
|
|
2019-04-26 15:43:41 +02:00
|
|
|
sleep(1)
|
|
|
|
while(!AccountManager.shared.combinedRefreshProgress.isComplete) {
|
|
|
|
os_log("Waiting for account refresh processing to complete...", log: self.log, type: .debug)
|
|
|
|
sleep(1)
|
2019-04-24 18:04:20 +02:00
|
|
|
}
|
2019-04-26 15:43:41 +02:00
|
|
|
|
2019-04-26 21:03:59 +02:00
|
|
|
completeProcessing()
|
2019-04-26 15:43:41 +02:00
|
|
|
os_log("Accounts completed refresh processing.", log: self.log, type: .info)
|
2019-04-24 23:29:32 +02:00
|
|
|
|
2019-04-26 15:43:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 14:30:35 +02:00
|
|
|
}
|
2019-04-23 17:29:14 +02:00
|
|
|
|
2018-02-05 22:29:46 +01:00
|
|
|
// MARK: - Split view
|
|
|
|
|
|
|
|
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
|
|
|
|
guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false }
|
|
|
|
guard let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController else { return false }
|
2019-04-22 00:42:26 +02:00
|
|
|
if topAsDetailController.navState?.currentArticle == nil {
|
2018-02-05 22:29:46 +01:00
|
|
|
// Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
|
|
|
if note.object is AccountManager {
|
|
|
|
unreadCount = AccountManager.shared.unreadCount
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 17:29:14 +02:00
|
|
|
|
2019-04-24 14:30:35 +02:00
|
|
|
@objc func userDefaultsDidChange(_ note: Notification) {
|
2019-04-29 16:29:57 +02:00
|
|
|
updateBackgroundRefreshInterval()
|
2019-04-23 17:29:14 +02:00
|
|
|
}
|
2019-04-24 14:30:35 +02:00
|
|
|
|
2019-04-26 22:24:39 +02:00
|
|
|
@objc func accountRefreshDidFinish(_ note: Notification) {
|
|
|
|
AppDefaults.lastRefresh = Date()
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
// MARK: - API
|
|
|
|
|
|
|
|
func logMessage(_ message: String, type: LogItem.ItemType) {
|
2019-04-26 14:44:00 +02:00
|
|
|
print("logMessage: \(message) - \(type)")
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func logDebugMessage(_ message: String) {
|
|
|
|
logMessage(message, type: .debug)
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:29:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 20:54:52 +02:00
|
|
|
private extension AppDelegate {
|
|
|
|
|
2019-04-29 16:29:57 +02:00
|
|
|
func updateBackgroundRefreshInterval() {
|
|
|
|
let refreshInterval = AppDefaults.refreshInterval
|
|
|
|
if refreshInterval == .manually {
|
|
|
|
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalNever)
|
|
|
|
} else {
|
|
|
|
UIApplication.shared.setMinimumBackgroundFetchInterval(AppDefaults.refreshInterval.inSeconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 20:54:52 +02:00
|
|
|
func sendReceivedArticlesUserNotification(newArticleCount: Int) {
|
|
|
|
|
|
|
|
let content = UNMutableNotificationContent()
|
|
|
|
content.title = NSLocalizedString("Article Download", comment: "New Articles")
|
|
|
|
|
|
|
|
let body: String = {
|
|
|
|
if newArticleCount == 1 {
|
|
|
|
return NSLocalizedString("You have downloaded 1 new article.", comment: "Article Downloaded")
|
|
|
|
} else {
|
|
|
|
let formatString = NSLocalizedString("You have downloaded %d new articles.", comment: "Articles Downloaded")
|
|
|
|
return NSString.localizedStringWithFormat(formatString as NSString, newArticleCount) as String
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
content.body = body
|
|
|
|
content.sound = UNNotificationSound.default
|
|
|
|
|
|
|
|
let request = UNNotificationRequest.init(identifier: "NewArticlesReceived", content: content, trigger: nil)
|
|
|
|
UNUserNotificationCenter.current().add(request)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|