mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-26 21:39:37 -04:00
Download Notification
This commit is contained in:
parent
555125c01e
commit
cd9c4d4c58
@ -8,6 +8,7 @@
|
||||
import UIKit
|
||||
import CoreData
|
||||
import Operations
|
||||
import UserNotifications
|
||||
|
||||
@UIApplicationMain
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
@ -24,8 +25,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
}
|
||||
|
||||
func registerNotification() {
|
||||
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
|
||||
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
|
||||
if #available(iOS 10, *) {
|
||||
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Alert, .Badge, .Sound], completionHandler: { _ in })
|
||||
} else {
|
||||
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
|
||||
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
@ -144,16 +149,6 @@ 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, *) {
|
||||
|
||||
} else {
|
||||
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) {
|
||||
|
@ -49,7 +49,7 @@
|
||||
</dict>
|
||||
</array>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1.8.873</string>
|
||||
<string>1.8.900</string>
|
||||
<key>ITSAppUsesNonExemptEncryption</key>
|
||||
<false/>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
|
@ -8,6 +8,8 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
class DownloadFinishedNotifications: {
|
||||
class DownloadFinishedNotifications: UILocalNotification {
|
||||
|
||||
}
|
||||
|
||||
//@available(iOS 10, *)
|
||||
|
@ -21,7 +21,7 @@
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1.8.876</string>
|
||||
<string>1.8.903</string>
|
||||
<key>NSExtension</key>
|
||||
<dict>
|
||||
<key>NSExtensionMainStoryboard</key>
|
||||
|
@ -19,21 +19,5 @@
|
||||
landmarkType = "5">
|
||||
</BreakpointContent>
|
||||
</BreakpointProxy>
|
||||
<BreakpointProxy
|
||||
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
|
||||
<BreakpointContent
|
||||
shouldBeEnabled = "Yes"
|
||||
ignoreCount = "0"
|
||||
continueAfterRunningActions = "No"
|
||||
filePath = "Kiwix-iOS/AppDelegate.swift"
|
||||
timestampString = "496013317.729722"
|
||||
startingColumnNumber = "9223372036854775807"
|
||||
endingColumnNumber = "9223372036854775807"
|
||||
startingLineNumber = "149"
|
||||
endingLineNumber = "149"
|
||||
landmarkName = "applicationDidEnterBackground(application:)"
|
||||
landmarkType = "7">
|
||||
</BreakpointContent>
|
||||
</BreakpointProxy>
|
||||
</Breakpoints>
|
||||
</Bucket>
|
||||
|
@ -9,13 +9,14 @@
|
||||
import UIKit
|
||||
import CoreData
|
||||
import Operations
|
||||
import UserNotifications
|
||||
|
||||
class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate, OperationQueueDelegate {
|
||||
static let shared = Network()
|
||||
let queue = OperationQueue()
|
||||
let context = NSManagedObjectContext.mainQueueContext
|
||||
private(set) var operations = [String: DownloadBookOperation]()
|
||||
|
||||
private(set) var operations = [String: DownloadBookOperation]()
|
||||
private var downloadedBookTitle = [String]()
|
||||
private var completionHandler: (()-> Void)?
|
||||
|
||||
@ -60,13 +61,42 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
|
||||
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)
|
||||
let title = NSLocalizedString("Book download finished", comment: "Notification: Book download finished")
|
||||
let body: String = {
|
||||
switch self.downloadedBookTitle.count {
|
||||
case 0:
|
||||
return NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished")
|
||||
case 1:
|
||||
return String(format: NSLocalizedString("%@ has been downloaded", comment: "Notification: Book download finished"), self.downloadedBookTitle[0])
|
||||
case 2:
|
||||
return String(format: NSLocalizedString("%@ and @% have been downloaded", comment: "Notification: Book download finished"),
|
||||
self.downloadedBookTitle[0], self.downloadedBookTitle[1])
|
||||
default:
|
||||
return String(format: NSLocalizedString("%@ and %d others have been downloaded", comment: "Notification: Book download finished"),
|
||||
self.downloadedBookTitle[0], self.downloadedBookTitle.count - 1)
|
||||
}
|
||||
}()
|
||||
|
||||
if #available(iOS 10, *) {
|
||||
UNUserNotificationCenter.currentNotificationCenter().getNotificationSettingsWithCompletionHandler({ (settings) in
|
||||
guard settings.alertSetting == .Enabled else {return}
|
||||
let content = UNMutableNotificationContent()
|
||||
content.title = title
|
||||
content.body = body
|
||||
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
|
||||
let request = UNNotificationRequest(identifier: "org.kiwix.downloadFinished", content: content, trigger: trigger)
|
||||
UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, withCompletionHandler: nil)
|
||||
})
|
||||
} else {
|
||||
let notification = UILocalNotification()
|
||||
notification.alertTitle = title
|
||||
notification.alertBody = body
|
||||
notification.soundName = UILocalNotificationDefaultSoundName
|
||||
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - NSURLSessionTaskDelegate
|
||||
|
||||
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
|
||||
@ -112,8 +142,9 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
|
||||
|
||||
// - Remove cache, if any
|
||||
// - Delete Download task Object
|
||||
context.performBlock {
|
||||
context.performBlockAndWait {
|
||||
guard let book = Book.fetch(bookID, context: self.context) else {return}
|
||||
if let title = book.title {self.downloadedBookTitle.append(title)}
|
||||
book.removeResumeData()
|
||||
guard let downloadTask = book.downloadTask else {return}
|
||||
self.context.deleteObject(downloadTask)
|
||||
|
Loading…
x
Reference in New Issue
Block a user