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 = { viewModel.dataController.records = {
var oldRecords = viewModel.dataController.records var oldRecords = viewModel.dataController.records
var newRecords: [MastodonFeed] = newStatuses.map { status in var newRecords = [MastodonFeed]()
let hasMore: Bool = { for (index, status) in newStatuses.enumerated() {
guard if index < newStatuses.count - 1 {
let firstOldStatus = oldRecords.first?.status newRecords.append(
else { MastodonFeed.fromStatus(.fromEntity(status), kind: .home, hasMore: false)
return false )
} continue
/// 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 let hasMore = status != oldRecords.first?.status?.entity
}()
return MastodonFeed.fromStatus(.fromEntity(status), kind: .home, hasMore: hasMore) newRecords.append(
MastodonFeed.fromStatus(.fromEntity(status), kind: .home, hasMore: hasMore)
)
} }
for (i, record) in newRecords.enumerated() { for (i, record) in newRecords.enumerated() {
if let index = oldRecords.firstIndex(where: { $0.status?.reblog?.id == record.id || $0.status?.id == record.id }) { 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 @MainActor
func loadMore(item: StatusItem, indexPath: IndexPath) async { func loadMore(item: StatusItem, indexPath: IndexPath) async {
guard case let .feedLoader(record) = item else { return } guard case let .feedLoader(record) = item else { return }
guard let diffableDataSource = diffableDataSource else { return }
guard let status = record.status else { return } guard let status = record.status else { return }
record.isLoadingMore = true record.isLoadingMore = true
@ -158,7 +157,7 @@ extension HomeTimelineViewModel {
// fetch data // fetch data
let response = try? await context.apiService.homeTimeline( let response = try? await context.apiService.homeTimeline(
maxID: status.id, sinceID: status.id,
authenticationBox: authContext.mastodonAuthenticationBox authenticationBox: authContext.mastodonAuthenticationBox
) )