diff --git a/App/App_iOS.swift b/App/App_iOS.swift index c75a39ac..417b4d12 100644 --- a/App/App_iOS.swift +++ b/App/App_iOS.swift @@ -103,14 +103,6 @@ struct Kiwix: App { } } .modifier(DonationViewModifier()) - .onReceive( - NotificationCenter.default.publisher( - for: UIApplication.didEnterBackgroundNotification - ) - ) { _ in - appDelegate.reScheduleBackgroundDownloadTask() - } - } .commands { CommandGroup(replacing: .undoRedo) { @@ -130,57 +122,6 @@ struct Kiwix: App { completionHandler: @escaping () -> Void) { DownloadService.shared.backgroundCompletionHandler = completionHandler } - - func application( - _ application: UIApplication, - didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil - ) -> Bool { - registerBackgroundTask() - return true - } - - // Background download task - func registerBackgroundTask() { - guard case .kiwix = AppType.current else { return } - let isRegistered = BGTaskScheduler.shared.register( - forTaskWithIdentifier: BackgroundDownloads.identifier, - using: .main) { [self] task in - // update the live activities, if any - ActivityService.shared().start() - // reschedule - reScheduleBackgroundDownloadTask() - task.setTaskCompleted(success: true) - } - if isRegistered { - os_log("BackgroundDownloads registered", log: Log.DownloadService, type: .debug) - } else { - os_log("BackgroundDownloads registering failed: %s", log: Log.DownloadService, type: .error) - } - } - - func reScheduleBackgroundDownloadTask() { - guard case .kiwix = AppType.current else { return } - do { - let date = BackgroundDownloads.nextDate() - let request = BGAppRefreshTaskRequest(identifier: BackgroundDownloads.identifier) - request.earliestBeginDate = date - os_log( - "BackgroundDownloads task re-scheduled for: %s", - log: Log.DownloadService, - type: .debug, - date.formatted() - ) - - try BGTaskScheduler.shared.submit(request) - } catch { - os_log( - "BackgroundDownloads re-schedule failed: %s", - log: Log.DownloadService, - type: .error, - error.localizedDescription - ) - } - } /// Handling file download complete notification func userNotificationCenter(_ center: UNUserNotificationCenter, diff --git a/Model/Downloads/BackgroundDownloads.swift b/Model/Downloads/BackgroundDownloads.swift deleted file mode 100644 index 6df2201e..00000000 --- a/Model/Downloads/BackgroundDownloads.swift +++ /dev/null @@ -1,28 +0,0 @@ -// This file is part of Kiwix for iOS & macOS. -// -// Kiwix is free software; you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 3 of the License, or -// any later version. -// -// Kiwix is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Kiwix; If not, see https://www.gnu.org/licenses/. - -#if os(iOS) -import Foundation -import BackgroundTasks - -enum BackgroundDownloads { - static let identifier = "org.kiwix.downloads_to_liveactivity" - - static func nextDate() -> Date { - .now + 2 // after 2 seconds - } -} - -#endif diff --git a/Views/LiveActivity/ActivityService.swift b/Views/LiveActivity/ActivityService.swift index 025499e5..259f89b6 100644 --- a/Views/LiveActivity/ActivityService.swift +++ b/Views/LiveActivity/ActivityService.swift @@ -111,17 +111,7 @@ final class ActivityService { start(with: state, downloadTimes: downloadTimes) return } - let now = CACurrentMediaTime() - // make sure we don't update too frequently - // unless there's a pause, we do want immediate update - let isTooEarlyToUpdate = if hasAnyPause(in: state) { - false - } else { - (now - lastUpdate) <= updateFrequency - } - guard let activity, !isTooEarlyToUpdate else { - return - } + guard let activity else { return } Task { let activityState = await activityState(from: state, downloadTimes: downloadTimes) let newContent = ActivityContent( @@ -136,7 +126,6 @@ final class ActivityService { await activity.update(newContent) } } - lastUpdate = now } private func updatedDownloadTimes(from states: [UUID: DownloadState]) -> [UUID: CFTimeInterval] { diff --git a/Widgets/DownloadsLiveActivity.swift b/Widgets/DownloadsLiveActivity.swift index c735025e..a9c6ea8a 100644 --- a/Widgets/DownloadsLiveActivity.swift +++ b/Widgets/DownloadsLiveActivity.swift @@ -108,25 +108,20 @@ struct DownloadsLiveActivity: Widget { state: DownloadActivityAttributes.ContentState ) -> some View { let timeInterval = currentTimeInterval(state: state) - if !state.isAllPaused { - ProgressView(timerInterval: timeInterval, countsDown: false, label: { - progressText(state.progressDescription) - }, currentValueLabel: { + ProgressView(value: state.progress, label: { + progressText(state.progressDescription) + }, currentValueLabel: { + if !state.isAllPaused { Text(timerInterval: timeInterval) .font(.caption) .tint(.secondary) - }) - .tint(Color.primary) - } else { - ProgressView(value: state.progress, label: { - progressText(state.progressDescription) - }, currentValueLabel: { + } else { Label("", systemImage: "pause.fill") .font(.caption) .tint(.secondary) - }) - .tint(Color.primary) - } + } + }) + .tint(Color.primary) } }