Implement subsequent "Load More" (IOS-266)

This commit is contained in:
Marcus Kida 2024-05-03 14:43:00 +02:00
parent 5a18fe73f7
commit 2387d25598
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
1 changed files with 31 additions and 13 deletions

View File

@ -173,22 +173,40 @@ extension HomeTimelineViewModel {
}
// insert missing items
if let items = response?.value {
let feedItems: [MastodonFeed] = items.map { .fromStatus($0.asMastodonStatus, kind: .home, hasMore: false) }
let firstIndex = indexPath.row
let count = dataController.records.count
let head = dataController.records[..<firstIndex]
let tail = dataController.records[firstIndex..<count]
let combinedRecords = Array(head + feedItems + tail)
dataController.records = combinedRecords
record.isLoadingMore = false
record.hasMore = false
} else {
guard let items = response?.value else {
record.isLoadingMore = false
return
}
let firstIndex = indexPath.row
let oldRecords = dataController.records
let count = oldRecords.count
let head = oldRecords[..<firstIndex]
let tail = oldRecords[firstIndex..<count]
var feedItems = [MastodonFeed]()
for (index, item) in items.enumerated() {
let hasMore: Bool
/// there can only be a gap after the last items
if index < items.count - 1 {
hasMore = false
} else {
/// if fetched items and first item after gap don't match -> we got another gap
hasMore = item != head.first?.status?.entity
}
feedItems.append(
.fromStatus(item.asMastodonStatus, kind: .home, hasMore: hasMore)
)
}
let combinedRecords = Array(head + feedItems + tail)
dataController.records = combinedRecords
record.isLoadingMore = false
record.hasMore = false
}
}