Use instant updates

This commit is contained in:
Balazs Perlaki-Horvath 2025-03-26 20:16:28 +01:00
parent 0ea8330200
commit 9f7afe6809
4 changed files with 9 additions and 112 deletions

View File

@ -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,

View File

@ -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

View File

@ -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<DownloadActivityAttributes.ContentState>(
@ -136,7 +126,6 @@ final class ActivityService {
await activity.update(newContent)
}
}
lastUpdate = now
}
private func updatedDownloadTimes(from states: [UUID: DownloadState]) -> [UUID: CFTimeInterval] {

View File

@ -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)
}
}