add label editing to reader view

This commit is contained in:
Satindar Dhillon 2022-04-13 10:48:25 -07:00
parent b57046b1d9
commit 1e2901c4e7
3 changed files with 21 additions and 3 deletions

View file

@ -103,7 +103,7 @@ import Views
}
}
.onChange(of: viewModel.selectedLinkItem) { _ in
viewModel.commitProgressUpdates()
viewModel.commitItemUpdates()
}
}
}

View file

@ -11,6 +11,9 @@ final class HomeFeedViewModel: ObservableObject {
/// Track progress updates to be committed when user navigates back to grid view
var uncommittedReadingProgressUpdates = [String: Double]()
/// Track label updates to be committed when user navigates back to grid view
var uncommittedLabelUpdates = [String: [FeedItemLabel]]()
@Published var items = [FeedItem]()
@Published var isLoading = false
@Published var showPushNotificationPrimer = false
@ -173,14 +176,18 @@ final class HomeFeedViewModel: ObservableObject {
.store(in: &subscriptions)
}
/// Update `FeedItem`s with the cached reading progress values so it can animate when the
/// Update `FeedItem`s with the cached reading progress and label values so it can animate when the
/// user navigates back to the grid view (and also avoid mutations of the grid items
/// that can cause the `NavigationView` to pop.
func commitProgressUpdates() {
func commitItemUpdates() {
for (key, value) in uncommittedReadingProgressUpdates {
updateProgress(itemID: key, progress: value)
}
for (key, value) in uncommittedLabelUpdates {
updateLabels(itemID: key, labels: value)
}
uncommittedReadingProgressUpdates = [:]
uncommittedLabelUpdates = [:]
}
private func updateProgress(itemID: String, progress: Double) {
@ -191,6 +198,13 @@ final class HomeFeedViewModel: ObservableObject {
}
func updateLabels(itemID: String, labels: [FeedItemLabel]) {
// If item is being being displayed then delay the state update of labels until
// user is no longer reading the item.
if selectedLinkItem != nil {
uncommittedLabelUpdates[itemID] = labels
return
}
guard let item = items.first(where: { $0.id == itemID }) else { return }
if let index = items.firstIndex(of: item) {
items[index].labels = labels

View file

@ -132,6 +132,10 @@ import WebKit
Menu(
content: {
Group {
Button(
action: { homeFeedViewModel.itemUnderLabelEdit = item },
label: { Label("Edit Labels", systemImage: "tag") }
)
Button(
action: {
homeFeedViewModel.setLinkArchived(