Improve load more (IOS-266)

This commit is contained in:
Marcus Kida 2024-05-02 12:45:12 +02:00
parent 7b5b0dacc7
commit d9a7bbe00d
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
2 changed files with 15 additions and 14 deletions

View File

@ -147,18 +147,20 @@ extension HomeTimelineViewModel.LoadLatestState {
viewModel.dataController.records = {
var oldRecords = viewModel.dataController.records
var newRecords: [MastodonFeed] = newStatuses.map { status in
let hasMore: Bool = {
guard
let firstOldStatus = oldRecords.first?.status
else {
return false
}
/// if the most recent cached item overlaps with the last returned status we know that there
/// is no gap in the timline
return status == newStatuses.last && status != firstOldStatus.entity
}()
return MastodonFeed.fromStatus(.fromEntity(status), kind: .home, hasMore: hasMore)
var newRecords = [MastodonFeed]()
for (index, status) in newStatuses.enumerated() {
if index < newStatuses.count - 1 {
newRecords.append(
MastodonFeed.fromStatus(.fromEntity(status), kind: .home, hasMore: false)
)
continue
}
let hasMore = status != oldRecords.first?.status?.entity
newRecords.append(
MastodonFeed.fromStatus(.fromEntity(status), kind: .home, hasMore: hasMore)
)
}
for (i, record) in newRecords.enumerated() {
if let index = oldRecords.firstIndex(where: { $0.status?.reblog?.id == record.id || $0.status?.id == record.id }) {

View File

@ -149,7 +149,6 @@ extension HomeTimelineViewModel {
@MainActor
func loadMore(item: StatusItem, indexPath: IndexPath) async {
guard case let .feedLoader(record) = item else { return }
guard let diffableDataSource = diffableDataSource else { return }
guard let status = record.status else { return }
record.isLoadingMore = true
@ -158,7 +157,7 @@ extension HomeTimelineViewModel {
// fetch data
let response = try? await context.apiService.homeTimeline(
maxID: status.id,
sinceID: status.id,
authenticationBox: authContext.mastodonAuthenticationBox
)