add a render ID to FeedItem to identify it in ForEach renders (solves stale item issue after refresh)
This commit is contained in:
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user