mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-14 06:39:05 -04:00
Use instant updates
This commit is contained in:
parent
0ea8330200
commit
9f7afe6809
@ -103,14 +103,6 @@ struct Kiwix: App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.modifier(DonationViewModifier())
|
.modifier(DonationViewModifier())
|
||||||
.onReceive(
|
|
||||||
NotificationCenter.default.publisher(
|
|
||||||
for: UIApplication.didEnterBackgroundNotification
|
|
||||||
)
|
|
||||||
) { _ in
|
|
||||||
appDelegate.reScheduleBackgroundDownloadTask()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
.commands {
|
.commands {
|
||||||
CommandGroup(replacing: .undoRedo) {
|
CommandGroup(replacing: .undoRedo) {
|
||||||
@ -131,57 +123,6 @@ struct Kiwix: App {
|
|||||||
DownloadService.shared.backgroundCompletionHandler = completionHandler
|
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
|
/// Handling file download complete notification
|
||||||
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
||||||
didReceive response: UNNotificationResponse,
|
didReceive response: UNNotificationResponse,
|
||||||
|
@ -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
|
|
@ -111,17 +111,7 @@ final class ActivityService {
|
|||||||
start(with: state, downloadTimes: downloadTimes)
|
start(with: state, downloadTimes: downloadTimes)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let now = CACurrentMediaTime()
|
guard let activity else { return }
|
||||||
// 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
|
|
||||||
}
|
|
||||||
Task {
|
Task {
|
||||||
let activityState = await activityState(from: state, downloadTimes: downloadTimes)
|
let activityState = await activityState(from: state, downloadTimes: downloadTimes)
|
||||||
let newContent = ActivityContent<DownloadActivityAttributes.ContentState>(
|
let newContent = ActivityContent<DownloadActivityAttributes.ContentState>(
|
||||||
@ -136,7 +126,6 @@ final class ActivityService {
|
|||||||
await activity.update(newContent)
|
await activity.update(newContent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lastUpdate = now
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updatedDownloadTimes(from states: [UUID: DownloadState]) -> [UUID: CFTimeInterval] {
|
private func updatedDownloadTimes(from states: [UUID: DownloadState]) -> [UUID: CFTimeInterval] {
|
||||||
|
@ -108,26 +108,21 @@ struct DownloadsLiveActivity: Widget {
|
|||||||
state: DownloadActivityAttributes.ContentState
|
state: DownloadActivityAttributes.ContentState
|
||||||
) -> some View {
|
) -> some View {
|
||||||
let timeInterval = currentTimeInterval(state: state)
|
let timeInterval = currentTimeInterval(state: state)
|
||||||
if !state.isAllPaused {
|
|
||||||
ProgressView(timerInterval: timeInterval, countsDown: false, label: {
|
|
||||||
progressText(state.progressDescription)
|
|
||||||
}, currentValueLabel: {
|
|
||||||
Text(timerInterval: timeInterval)
|
|
||||||
.font(.caption)
|
|
||||||
.tint(.secondary)
|
|
||||||
})
|
|
||||||
.tint(Color.primary)
|
|
||||||
} else {
|
|
||||||
ProgressView(value: state.progress, label: {
|
ProgressView(value: state.progress, label: {
|
||||||
progressText(state.progressDescription)
|
progressText(state.progressDescription)
|
||||||
}, currentValueLabel: {
|
}, currentValueLabel: {
|
||||||
|
if !state.isAllPaused {
|
||||||
|
Text(timerInterval: timeInterval)
|
||||||
|
.font(.caption)
|
||||||
|
.tint(.secondary)
|
||||||
|
} else {
|
||||||
Label("", systemImage: "pause.fill")
|
Label("", systemImage: "pause.fill")
|
||||||
.font(.caption)
|
.font(.caption)
|
||||||
.tint(.secondary)
|
.tint(.secondary)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.tint(Color.primary)
|
.tint(Color.primary)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DownloadActivityAttributes {
|
extension DownloadActivityAttributes {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user