fix: Load missing posts

This commit is contained in:
Marcus Kida 2024-01-15 15:03:49 +01:00
parent d55800d71a
commit d334f39446
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
3 changed files with 19 additions and 5 deletions

View File

@ -108,6 +108,8 @@ extension HomeTimelineViewModel.LoadLatestState {
if newStatuses.isEmpty {
viewModel.didLoadLatest.send()
} else {
var oldRecords = viewModel.dataController.records
if !latestStatusIDs.isEmpty {
viewModel.homeTimelineNavigationBarTitleViewModel.newPostsIncoming()
}
@ -115,13 +117,20 @@ extension HomeTimelineViewModel.LoadLatestState {
var newRecords: [MastodonFeed] = newStatuses.map {
MastodonFeed.fromStatus(.fromEntity($0), kind: .home)
}
if let lastNewId = newRecords.last?.status?.id, let firstOld = oldRecords.first?.status, lastNewId != firstOld.id {
// Add "Load more"
newRecords.append(
MastodonFeed(hasMore: true, isLoadingMore: false, status: firstOld, notification: nil, kind: .none)
)
}
viewModel.dataController.records = {
var oldRecords = viewModel.dataController.records
for (i, record) in newRecords.enumerated() {
if let index = oldRecords.firstIndex(where: { $0.status?.reblog?.id == record.id || $0.status?.id == record.id }) {
oldRecords[index] = record
if newRecords.count > index {
newRecords.remove(at: i)
newRecords.remove(at: index)
}
}
}

View File

@ -150,16 +150,21 @@ extension HomeTimelineViewModel {
// fetch data
let maxID = status.id
_ = try? await context.apiService.homeTimeline(
let missingStatuses = (try? await context.apiService.homeTimeline(
maxID: maxID,
authenticationBox: authContext.mastodonAuthenticationBox
)
).value) ?? []
record.isLoadingMore = false
// reconfigure item again
snapshot.reconfigureItems([item])
await updateSnapshotUsingReloadData(snapshot: snapshot)
let newItems: [MastodonFeed] = missingStatuses.map({ MastodonFeed.fromStatus(.fromEntity($0), kind: .home) })
var existingItems = Array(dataController.records)
existingItems.removeAll(where: { $0.id == status.id }) // Remove loader item
dataController.records = (newItems + existingItems).removingDuplicates()
}
}

View File

@ -20,7 +20,7 @@ public final class MastodonFeed {
public let kind: Feed.Kind
init(hasMore: Bool, isLoadingMore: Bool, status: MastodonStatus?, notification: Mastodon.Entity.Notification?, kind: Feed.Kind) {
public init(hasMore: Bool, isLoadingMore: Bool, status: MastodonStatus?, notification: Mastodon.Entity.Notification?, kind: Feed.Kind) {
self.id = notification?.id ?? status?.id ?? UUID().uuidString
self.hasMore = hasMore
self.isLoadingMore = isLoadingMore