From 368bdbbfaa4adf347203f28dfc234da6ee519ddb Mon Sep 17 00:00:00 2001 From: Chris Li Date: Mon, 19 Sep 2016 17:31:16 -0400 Subject: [PATCH] ask notification when open lib --- Kiwix-iOS/AppDelegate.swift | 18 +++++++++++++--- Kiwix-iOS/Info.plist | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- .../xcdebugger/Breakpoints_v2.xcbkptlist | 16 ++++++++++++++ Kiwix/Network/Network.swift | 21 +++++++++++++++++++ .../Operations/RefreshLibraryOperation.swift | 4 ++++ Kiwix/libkiwix/ZimReader.mm | 1 + 7 files changed, 59 insertions(+), 5 deletions(-) diff --git a/Kiwix-iOS/AppDelegate.swift b/Kiwix-iOS/AppDelegate.swift index 26a09ce0..b838bb07 100644 --- a/Kiwix-iOS/AppDelegate.swift +++ b/Kiwix-iOS/AppDelegate.swift @@ -23,6 +23,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { Preference.activeUseHistory.append(NSDate()) } + func registerNotification() { + let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) + UIApplication.sharedApplication().registerUserNotificationSettings(settings) + } + // MARK: - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { @@ -30,8 +35,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { Network.shared // Register notification -// let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) // Here are the notification permission the app wants -// application.registerUserNotificationSettings(settings) + if let _ = Preference.libraryLastRefreshTime { registerNotification() } // Set background refresh interval application.setMinimumBackgroundFetchInterval(86400) @@ -140,6 +144,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + + if #available(iOS 10, *) { + let notification = UILocalNotification() + notification.alertTitle = NSLocalizedString("Book download finished", comment: "Notification: Book download finished") + notification.alertBody = NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished") + notification.soundName = UILocalNotificationDefaultSoundName + UIApplication.sharedApplication().presentLocalNotificationNow(notification) + } } func applicationWillEnterForeground(application: UIApplication) { @@ -147,7 +159,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) { -// Network.shared.rejoinSessionWithIdentifier(identifier, completionHandler: completionHandler) + Network.shared.rejoinSessionWithIdentifier(identifier, completionHandler: completionHandler) } // MARK: Background Refresh diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index cfcce4c4..6af6a79a 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.859 + 1.8.871 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index 7fac9262..236ac85b 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.862 + 1.8.874 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index dbfe494b..55d0ddea 100644 --- a/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -19,5 +19,21 @@ landmarkType = "5"> + + + + diff --git a/Kiwix/Network/Network.swift b/Kiwix/Network/Network.swift index 9ae05d9c..6787b207 100644 --- a/Kiwix/Network/Network.swift +++ b/Kiwix/Network/Network.swift @@ -16,6 +16,9 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe let context = NSManagedObjectContext.mainQueueContext private(set) var operations = [String: DownloadBookOperation]() + private var downloadedBookTitle = [String]() + private var completionHandler: (()-> Void)? + private override init() { super.init() queue.delegate = self @@ -29,6 +32,11 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe return NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil) }() + func rejoinSessionWithIdentifier(identifier: String, completionHandler: ()-> Void) { + guard identifier == session.configuration.identifier else {return} + self.completionHandler = completionHandler + } + // MARK: - OperationQueueDelegate func operationQueue(queue: OperationQueue, willAddOperation operation: NSOperation) { @@ -46,6 +54,19 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe func operationQueue(queue: OperationQueue, willProduceOperation operation: NSOperation) {} + // MARK: - NSURLSessionDelegate + + func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) { + NSOperationQueue.mainQueue().addOperationWithBlock { + self.completionHandler?() + + let notification = UILocalNotification() + notification.alertTitle = NSLocalizedString("Book download finished", comment: "Notification: Book download finished") + notification.alertBody = NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished") + notification.soundName = UILocalNotificationDefaultSoundName + UIApplication.sharedApplication().presentLocalNotificationNow(notification) + } + } // MARK: - NSURLSessionTaskDelegate func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { diff --git a/Kiwix/Operations/RefreshLibraryOperation.swift b/Kiwix/Operations/RefreshLibraryOperation.swift index dcadeee2..6b554e1a 100644 --- a/Kiwix/Operations/RefreshLibraryOperation.swift +++ b/Kiwix/Operations/RefreshLibraryOperation.swift @@ -20,6 +20,10 @@ class RefreshLibraryOperation: GroupOperation { process.injectResultFromDependency(retrive) super.init(operations: [retrive, process]) + addObserver(WillExecuteObserver { _ in + (UIApplication.sharedApplication().delegate as! AppDelegate).registerNotification() + }) + process.addObserver(DidFinishObserver { [unowned self] (operation, errors) in guard let operation = operation as? Process else {return} self.hasUpdate = operation.hasUpdate diff --git a/Kiwix/libkiwix/ZimReader.mm b/Kiwix/libkiwix/ZimReader.mm index e09f4059..c4efb61f 100755 --- a/Kiwix/libkiwix/ZimReader.mm +++ b/Kiwix/libkiwix/ZimReader.mm @@ -1,3 +1,4 @@ + // // ZimReader.m // KiwixTest