update reading progress only when user has navigated back to the grid view

This commit is contained in:
Satindar Dhillon 2022-04-04 12:05:06 -07:00
parent 87c79febeb
commit b79c71ff65
5 changed files with 21 additions and 6 deletions

View file

@ -100,6 +100,9 @@ import Views
viewModel.loadItems(dataService: dataService, searchQuery: searchQuery, isRefresh: true)
}
}
.onChange(of: selectedLinkItem) { _ in
viewModel.commitProgressUpdates()
}
}
}
@ -326,7 +329,7 @@ import Views
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 325), spacing: 24)], spacing: 24) {
ForEach(viewModel.items, id: \.renderID) { item in
ForEach(viewModel.items) { item in
let link = GridCardNavigationLink(
item: item,
searchQuery: searchQuery,

View file

@ -8,6 +8,9 @@ import Views
final class HomeFeedViewModel: ObservableObject {
var currentDetailViewModel: LinkItemDetailViewModel?
/// Track progress updates to be committed when user navigates back to grid view
var uncommittedReadingProgressUpdates = [String: Double]()
@Published var items = [FeedItem]()
@Published var isLoading = false
@Published var showPushNotificationPrimer = false
@ -163,7 +166,17 @@ final class HomeFeedViewModel: ObservableObject {
.store(in: &subscriptions)
}
func updateProgress(itemID: String, progress: Double) {
/// Update `FeedItem`s with the cached reading progress 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() {
for (key, value) in uncommittedReadingProgressUpdates {
updateProgress(itemID: key, progress: value)
}
uncommittedReadingProgressUpdates = [:]
}
private func updateProgress(itemID: String, progress: Double) {
guard sendProgressUpdates, let item = items.first(where: { $0.id == itemID }) else { return }
if let index = items.firstIndex(of: item) {
items[index].readingProgress = progress

View file

@ -92,7 +92,7 @@ final class LinkItemDetailViewModel: ObservableObject {
case let .shareHighlight(highlightID):
print("show share modal for highlight with id: \(highlightID)")
case let .updateReadingProgess(progress: progress):
self?.homeFeedViewModel.updateProgress(itemID: self?.item.id ?? "", progress: Double(progress))
self?.homeFeedViewModel.uncommittedReadingProgressUpdates[self?.item.id ?? ""] = Double(progress)
}
}
.store(in: &newWebAppWrapperViewModel.subscriptions)

View file

@ -36,7 +36,7 @@ struct WebReaderContainerView: View {
let messageBody = message.body as? [String: Double]
if let messageBody = messageBody, let progress = messageBody["progress"] {
homeFeedViewModel.updateProgress(itemID: item.id, progress: Double(progress))
homeFeedViewModel.uncommittedReadingProgressUpdates[item.id] = Double(progress)
}
}
@ -56,7 +56,7 @@ struct WebReaderContainerView: View {
if message.name == WebViewAction.readingProgressUpdate.rawValue {
guard let messageBody = message.body as? [String: Double] else { return }
guard let progress = messageBody["progress"] else { return }
homeFeedViewModel.updateProgress(itemID: item.id, progress: Double(progress))
homeFeedViewModel.uncommittedReadingProgressUpdates[item.id] = Double(progress)
}
}

View file

@ -12,7 +12,6 @@ public struct HomeFeedData {
public struct FeedItem: Identifiable, Hashable, Decodable {
public let id: String
public let renderID = UUID()
public let title: String
public let createdAt: Date
public let savedAt: Date