Merge pull request #1145 from kiwix/1107-liveactivities-background-mode-fix2

Fix background mode scheduler for live activities
This commit is contained in:
Kelson 2025-03-24 06:58:20 +01:00 committed by GitHub
commit d15086468e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -103,6 +103,13 @@ struct Kiwix: App {
} }
} }
.modifier(DonationViewModifier()) .modifier(DonationViewModifier())
.onReceive(
NotificationCenter.default.publisher(
for: UIApplication.didEnterBackgroundNotification
)
) { _ in
appDelegate.reScheduleBackgroundDownloadTask()
}
} }
.commands { .commands {
@ -133,22 +140,16 @@ struct Kiwix: App {
} }
// Background download task // Background download task
func applicationDidFinishLaunching(_ application: UIApplication) { func registerBackgroundTask() {
registerBackgroundTask()
}
func applicationDidEnterBackground(_ application: UIApplication) {
registerBackgroundTask()
}
private func registerBackgroundTask() {
guard case .kiwix = AppType.current else { return } guard case .kiwix = AppType.current else { return }
let isRegistered = BGTaskScheduler.shared.register( let isRegistered = BGTaskScheduler.shared.register(
forTaskWithIdentifier: BackgroundDownloads.identifier, forTaskWithIdentifier: BackgroundDownloads.identifier,
using: .main) { [self] _ in using: .main) { [self] task in
// update the live activities, if any // update the live activities, if any
ActivityService.shared().start() ActivityService.shared().start()
// reschedule // reschedule
reScheduleBackgroundDownloadTask() reScheduleBackgroundDownloadTask()
task.setTaskCompleted(success: true)
} }
if isRegistered { if isRegistered {
os_log("BackgroundDownloads registered", log: Log.DownloadService, type: .debug) os_log("BackgroundDownloads registered", log: Log.DownloadService, type: .debug)
@ -157,28 +158,29 @@ struct Kiwix: App {
} }
} }
private func reScheduleBackgroundDownloadTask() { func reScheduleBackgroundDownloadTask() {
do { guard case .kiwix = AppType.current else { return }
let date = BackgroundDownloads.nextDate() do {
let request = BGAppRefreshTaskRequest(identifier: BackgroundDownloads.identifier) let date = BackgroundDownloads.nextDate()
request.earliestBeginDate = date let request = BGAppRefreshTaskRequest(identifier: BackgroundDownloads.identifier)
os_log( request.earliestBeginDate = date
"BackgroundDownloads task re-scheduled for: %s", os_log(
log: Log.DownloadService, "BackgroundDownloads task re-scheduled for: %s",
type: .debug, log: Log.DownloadService,
date.formatted() type: .debug,
) date.formatted()
)
try BGTaskScheduler.shared.submit(request)
} catch { try BGTaskScheduler.shared.submit(request)
os_log( } catch {
"BackgroundDownloads re-schedule failed: %s", os_log(
log: Log.DownloadService, "BackgroundDownloads re-schedule failed: %s",
type: .error, log: Log.DownloadService,
error.localizedDescription type: .error,
) error.localizedDescription
} )
} }
}
/// Handling file download complete notification /// Handling file download complete notification
func userNotificationCenter(_ center: UNUserNotificationCenter, func userNotificationCenter(_ center: UNUserNotificationCenter,