-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6452b57
commit 8c5af24
Showing
11 changed files
with
135 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import BackgroundTasks | ||
import Foundation | ||
import SwiftUI | ||
import HackerNewsKit | ||
import UserNotifications | ||
|
||
class AppDelegate: NSObject, UIApplicationDelegate { | ||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { | ||
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.Download.backgroundTaskId, | ||
using: nil) { task in | ||
task.expirationHandler = { | ||
task.setTaskCompleted(success: false) | ||
} | ||
|
||
Task { | ||
await OfflineRepository.shared.downloadAllStories() | ||
|
||
task.setTaskCompleted(success: true) | ||
} | ||
|
||
OfflineRepository.shared.scheduleBackgroundDownload() | ||
} | ||
} | ||
|
||
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { | ||
UNUserNotificationCenter.current().delegate = self | ||
return true | ||
} | ||
|
||
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { | ||
// Register SceneDelegate. | ||
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role) | ||
sceneConfig.delegateClass = SceneDelegate.self | ||
return sceneConfig | ||
} | ||
|
||
private func configureUserNotifications() { | ||
UNUserNotificationCenter.current().delegate = self | ||
} | ||
} | ||
|
||
class SceneDelegate: NSObject, UIWindowSceneDelegate { | ||
func sceneDidEnterBackground(_ scene: UIScene) { | ||
OfflineRepository.shared.scheduleBackgroundDownload() | ||
} | ||
} | ||
|
||
extension AppDelegate: UNUserNotificationCenterDelegate { | ||
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | ||
completionHandler([.banner, .sound, .list]) | ||
} | ||
|
||
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | ||
Task { | ||
let content = response.notification.request.content | ||
if let id = Int(content.targetContentIdentifier ?? ""), | ||
id != 0, | ||
let item = await StoriesRepository.shared.fetchComment(id) { | ||
Router.shared.to(item) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters