From ab64e56ab412d28f90778801db431c727b33847c Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 1 Dec 2022 13:52:58 +0800 Subject: [PATCH] Better cancelation of offline audio downloads, menu cleanup --- .../App/Views/Home/HomeFeedViewIOS.swift | 2 +- .../App/Views/Home/HomeFeedViewMac.swift | 2 +- .../App/Views/LinkItemDetailView.swift | 2 +- .../Views/WebReader/WebReaderContainer.swift | 21 ++++++++++------ .../Views/WebReader/WebReaderViewModel.swift | 25 ++++++++++++++++--- .../Sources/Views/FeedItem/GridCard.swift | 2 +- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index f319d3ff3..0db963073 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -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 }, diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift index 788131c4c..60d1c6098 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift @@ -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 }, diff --git a/apple/OmnivoreKit/Sources/App/Views/LinkItemDetailView.swift b/apple/OmnivoreKit/Sources/App/Views/LinkItemDetailView.swift index 0401a052f..ea5576b93 100644 --- a/apple/OmnivoreKit/Sources/App/Views/LinkItemDetailView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/LinkItemDetailView.swift @@ -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) }, diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift index cc3e0823f..311cf6830 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift @@ -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, diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift index 130b2278f..936516226 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift @@ -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? + + 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") + } + } } } diff --git a/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift b/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift index 82e1cf53a..4f9246d1e 100644 --- a/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift +++ b/apple/OmnivoreKit/Sources/Views/FeedItem/GridCard.swift @@ -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) },