add a render ID to FeedItem to identify it in ForEach renders (solves stale item issue after refresh)

This commit is contained in:
Satindar Dhillon
2022-03-07 17:42:53 -08:00
parent 35bd189e2e
commit 2c1d010d6a
4 changed files with 3 additions and 7 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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