From 2c1d010d6a7163f6448eba0dcbaa0e8f4e346cc7 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 7 Mar 2022 17:42:53 -0800 Subject: [PATCH] add a render ID to FeedItem to identify it in ForEach renders (solves stale item issue after refresh) --- .../App/Views/Home/Components/FeedCardNavigationLink.swift | 3 --- .../OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift | 4 ++-- .../Sources/App/Views/Home/HomeFeedViewModel.swift | 2 -- apple/OmnivoreKit/Sources/Models/FeedItem.swift | 1 + 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/Components/FeedCardNavigationLink.swift b/apple/OmnivoreKit/Sources/App/Views/Home/Components/FeedCardNavigationLink.swift index 502efbfc6..3d60094ff 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/Components/FeedCardNavigationLink.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/Components/FeedCardNavigationLink.swift @@ -53,9 +53,6 @@ struct GridCardNavigationLink: View { ) { EmptyView() } -// .onAppear { -// viewModel.itemAppeared(item: item, searchQuery: searchQuery, dataService: dataService) -// } GridCard(item: item, isContextMenuOpen: $isContextMenuOpen, actionHandler: actionHandler, tapAction: { withAnimation { scale = 0.95 diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index 43126d025..cacd54ce8 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -301,7 +301,7 @@ import Views var body: some View { ScrollView { LazyVGrid(columns: columns, spacing: 20) { - ForEach(viewModel.items) { item in + ForEach(viewModel.items, id: \.renderID) { item in let link = GridCardNavigationLink( item: item, searchQuery: searchQuery, @@ -338,7 +338,7 @@ import Views } ) .onPreferenceChange(ScrollViewOffsetPreferenceKey.self) { offset in - if !viewModel.isLoading, abs(offset) > 240 { + if !viewModel.isLoading, offset > 240 { viewModel.loadItems(dataService: dataService, searchQuery: searchQuery, isRefresh: true) } } diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index 540ae9399..1d7c11fed 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -27,10 +27,8 @@ final class HomeFeedViewModel: ObservableObject { let itemIndex = items.firstIndex(where: { $0.id == item.id }) let thresholdIndex = items.index(items.endIndex, offsetBy: -5) - print("itemIndex", itemIndex) // Check if user has scrolled to the last five items in the list if let itemIndex = itemIndex, itemIndex > thresholdIndex, items.count < thresholdIndex + 10 { - print("loading more items") loadItems(dataService: dataService, searchQuery: searchQuery, isRefresh: false) } } diff --git a/apple/OmnivoreKit/Sources/Models/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/FeedItem.swift index 4b4010ab5..b61022b01 100644 --- a/apple/OmnivoreKit/Sources/Models/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/FeedItem.swift @@ -12,6 +12,7 @@ public struct HomeFeedData { public struct FeedItem: Identifiable, Hashable, Decodable { public let id: String + public let renderID = UUID() public let title: String public var readingProgress: Double public var readingProgressAnchor: Int