Replace keyPath listening, as the DownloadTask can go out of scope on iPad, leading to a crash

This commit is contained in:
Balazs Perlaki-Horvath 2024-08-06 22:38:27 +02:00
parent 99356e3e25
commit ceed6a4299
2 changed files with 14 additions and 11 deletions

View File

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

View File

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