mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-25 04:48:16 -04:00
Replace keyPath listening, as the DownloadTask can go out of scope on iPad, leading to a crash
This commit is contained in:
parent
99356e3e25
commit
ceed6a4299
@ -27,6 +27,10 @@ struct DownloadState: Codable {
|
||||
let total: Int64
|
||||
let resumeData: Data?
|
||||
|
||||
static func empty() -> DownloadState {
|
||||
.init(downloaded: 0, total: 1, resumeData: nil)
|
||||
}
|
||||
|
||||
init(downloaded: Int64, total: Int64, resumeData: Data?) {
|
||||
guard total >= downloaded, total > 0 else {
|
||||
assertionFailure("invalid download progress values: downloaded \(downloaded) total: \(total)")
|
||||
|
@ -256,7 +256,10 @@ private struct FileLocator: ViewModifier {
|
||||
private struct DownloadTaskDetail: View {
|
||||
@ObservedObject var downloadTask: DownloadTask
|
||||
@EnvironmentObject var viewModel: LibraryViewModel
|
||||
@State private var downloadState = DownloadState(downloaded: 0, total: 1, resumeData: nil)
|
||||
@State private var downloadState = DownloadState.empty()
|
||||
private var downloadFileID: UUID? {
|
||||
viewModel.selectedZimFile?.fileID
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
@ -283,16 +286,12 @@ private struct DownloadTaskDetail: View {
|
||||
}
|
||||
Attribute(title: "zim_file.download_task.action.paused".localized, detail: detail)
|
||||
}
|
||||
}.onReceive(
|
||||
downloadTask.publisher(for: \.fileID)
|
||||
.combineLatest(DownloadService.shared.progress.publisher, {
|
||||
// swiftlint:disable:next closure_parameter_position
|
||||
(fileID: UUID, states: [UUID: DownloadState]) -> DownloadState? in
|
||||
states[fileID]
|
||||
})
|
||||
) { [self] (state: DownloadState?) in
|
||||
if let state {
|
||||
self.downloadState = state
|
||||
}.onReceive(DownloadService.shared.progress.publisher) { [self] (states: [UUID: DownloadState]) in
|
||||
if let downloadFileID,
|
||||
let state = states[downloadFileID] {
|
||||
downloadState = state
|
||||
} else {
|
||||
downloadState = .empty()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user