diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index e61a373c9..515d047c1 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -79,7 +79,9 @@ import Views let thresholdIndex = items.index(items.endIndex, offsetBy: -5) // Check if user has scrolled to the last five items in the list - if let itemIndex = itemIndex, itemIndex > thresholdIndex, items.count < thresholdIndex + 10 { + // Make sure we aren't currently loading though, as this would get triggered when the first set + // of items are presented to the user. + if let itemIndex = itemIndex, itemIndex > thresholdIndex, items.count < thresholdIndex + 10, !isLoading { await loadItems(dataService: dataService, audioController: audioController, isRefresh: false) } } @@ -104,14 +106,15 @@ import Views } } - func syncItems(dataService: DataService, syncStartTime: Date) async { + func syncItems(dataService: DataService, syncStartTime _: Date) async { let lastSyncDate = dateFormatter.date(from: dataService.lastItemSyncTime) ?? Date(timeIntervalSinceReferenceDate: 0) let syncResult = try? await dataService.syncLinkedItems(since: lastSyncDate, cursor: nil, deferFetchingMore: true) - if syncResult != nil { - dataService.lastItemSyncTime = dateFormatter.string(from: syncStartTime) + if syncResult?.hasMore { + // dataService.lastItemSyncTime = dateFormatter.string(from: syncStartTime) + self.isLoading = true } // If possible start prefetching new pages in the background diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index bc14cc01b..eae67dbb7 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -15,10 +15,12 @@ public struct LinkedItemQueryResult { public struct LinkedItemSyncResult { public let updatedItemIDs: [String] public let cursor: String? + public let hasMore: Bool - public init(updatedItemIDs: [String], cursor: String?) { + public init(updatedItemIDs: [String], cursor: String?, hasMore: Bool) { self.updatedItemIDs = updatedItemIDs self.cursor = cursor + self.hasMore = hasMore } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Public/LinkedItemLoading.swift b/apple/OmnivoreKit/Sources/Services/DataService/Public/LinkedItemLoading.swift index 0c7b8f447..6ef36ea88 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Public/LinkedItemLoading.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Public/LinkedItemLoading.swift @@ -34,17 +34,19 @@ public extension DataService { let prev = previousQueryResult?.updatedItemIDs ?? [] let result = LinkedItemSyncResult( updatedItemIDs: prev + fetchResult.items.map(\.id), - cursor: fetchResult.cursor + cursor: fetchResult.cursor, + hasMore: fetchResult.hasMoreItems ) - if fetchResult.hasMoreItems, (previousQueryResult?.updatedItemIDs.count ?? 0) < 40 { + print("synced since:", date, "total synced items:", result.updatedItemIDs.count, ", fetchResult.hasMoreItems", fetchResult.hasMoreItems) + if fetchResult.hasMoreItems { if deferFetchingMore { Task.detached(priority: .background) { try await self.syncLinkedItems( since: date, cursor: fetchResult.cursor, previousQueryResult: result, - deferFetchingMore: deferFetchingMore + deferFetchingMore: false ) } } else { @@ -55,6 +57,11 @@ public extension DataService { deferFetchingMore: deferFetchingMore ) } + } else { + print("setting last synced time to: ", date) + DispatchQueue.main.async { + self.lastItemSyncTime = DateFormatter.formatterISO8601.string(from: date) + } } return result