Better cancelation of offline audio downloads, menu cleanup

This commit is contained in:
Jackson Harper 2022-12-01 13:52:58 +08:00
parent 3e4f9409df
commit ab64e56ab4
6 changed files with 40 additions and 14 deletions

View file

@ -279,7 +279,7 @@ import Views
}
Button(
action: { viewModel.itemUnderTitleEdit = item },
label: { Label("Edit Metadata", systemImage: "textbox") }
label: { Label("Edit Info", systemImage: "info.circle") }
)
Button(
action: { viewModel.itemUnderLabelEdit = item },

View file

@ -46,7 +46,7 @@ import Views
// TODO: add highlights view button
Button(
action: { viewModel.itemUnderTitleEdit = item },
label: { Label("Edit Metadata", systemImage: "textbox") }
label: { Label("Edit Info", systemImage: "info.circle") }
)
Button(
action: { viewModel.itemUnderLabelEdit = item },

View file

@ -150,7 +150,7 @@ struct LinkItemDetailView: View {
Group {
Button(
action: { showTitleEdit = true },
label: { Label("Edit Metadata", systemImage: "textbox") }
label: { Label("Edit Info", systemImage: "info.circle") }
)
Button(
action: { viewModel.handleArchiveAction(dataService: dataService) },

View file

@ -164,6 +164,17 @@ struct WebReaderContainerView: View {
}.foregroundColor(.appGrayTextContrast)
}
func audioMenuItem() -> some View {
Button(
action: {
viewModel.downloadAudio(audioController: audioController, item: item)
},
label: {
Label(viewModel.isDownloadingAudio ? "Downloading Audio" : "Download Audio", systemImage: "icloud.and.arrow.down")
}
)
}
func menuItems(for item: LinkedItem) -> some View {
let hasLabels = item.labels?.count == 0
let hasHighlights = (item.highlights?.count ?? 0) > 0
@ -176,7 +187,7 @@ struct WebReaderContainerView: View {
}
Button(
action: { showTitleEdit = true },
label: { Label("Edit Metadata", systemImage: "textbox") }
label: { Label("Edit Info", systemImage: "info.circle") }
)
Button(
action: editLabels,
@ -199,12 +210,8 @@ struct WebReaderContainerView: View {
},
label: { Label("Reset Read Location", systemImage: "arrow.counterclockwise.circle") }
)
Button(
action: {
viewModel.downloadAudio(audioController: audioController, item: item)
},
label: { Label("Download Audio", systemImage: "icloud.and.arrow.down") }
)
audioMenuItem()
if viewModel.hasOriginalUrl(item) {
Button(
action: share,

View file

@ -12,6 +12,12 @@ struct SafariWebLink: Identifiable {
@MainActor final class WebReaderViewModel: ObservableObject {
@Published var articleContent: ArticleContent?
@Published var errorMessage: String?
@Published var isDownloadingAudio: Bool = false
@Published var audioDownloadTask: Task<Void, Error>?
deinit {
print("deinit WebReaderViewModel")
}
func hasOriginalUrl(_ item: LinkedItem) -> Bool {
if let pageURLString = item.pageURLString, let host = URL(string: pageURLString)?.host {
@ -25,9 +31,22 @@ struct SafariWebLink: Identifiable {
func downloadAudio(audioController: AudioController, item: LinkedItem) {
Snackbar.show(message: "Downloading Offline Audio")
Task {
let downloaded = await audioController.downloadForOffline(itemID: item.unwrappedID)
Snackbar.show(message: downloaded ? "Audio file downloaded" : "Error downloading audio")
isDownloadingAudio = true
if let audioDownloadTask = audioDownloadTask {
audioDownloadTask.cancel()
}
let itemID = item.unwrappedID
audioDownloadTask = Task.detached(priority: .background) {
let canceled = Task.isCancelled
let downloaded = await audioController.downloadForOffline(itemID: itemID)
DispatchQueue.main.async {
self.isDownloadingAudio = false
if !canceled {
Snackbar.show(message: downloaded ? "Audio file downloaded" : "Error downloading audio")
}
}
}
}

View file

@ -53,7 +53,7 @@ public struct GridCard: View {
}
Button(
action: { menuActionHandler(.editTitle) },
label: { Label("Edit Metadata", systemImage: "textbox") }
label: { Label("Edit Info", systemImage: "info.circle") }
)
Button(
action: { menuActionHandler(.editLabels) },