fix: Load missing posts
This commit is contained in:
parent
d55800d71a
commit
d334f39446
|
@ -108,6 +108,8 @@ extension HomeTimelineViewModel.LoadLatestState {
|
||||||
if newStatuses.isEmpty {
|
if newStatuses.isEmpty {
|
||||||
viewModel.didLoadLatest.send()
|
viewModel.didLoadLatest.send()
|
||||||
} else {
|
} else {
|
||||||
|
var oldRecords = viewModel.dataController.records
|
||||||
|
|
||||||
if !latestStatusIDs.isEmpty {
|
if !latestStatusIDs.isEmpty {
|
||||||
viewModel.homeTimelineNavigationBarTitleViewModel.newPostsIncoming()
|
viewModel.homeTimelineNavigationBarTitleViewModel.newPostsIncoming()
|
||||||
}
|
}
|
||||||
|
@ -115,13 +117,20 @@ extension HomeTimelineViewModel.LoadLatestState {
|
||||||
var newRecords: [MastodonFeed] = newStatuses.map {
|
var newRecords: [MastodonFeed] = newStatuses.map {
|
||||||
MastodonFeed.fromStatus(.fromEntity($0), kind: .home)
|
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 = {
|
viewModel.dataController.records = {
|
||||||
var oldRecords = viewModel.dataController.records
|
|
||||||
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 }) {
|
||||||
oldRecords[index] = record
|
oldRecords[index] = record
|
||||||
if newRecords.count > index {
|
if newRecords.count > index {
|
||||||
newRecords.remove(at: i)
|
newRecords.remove(at: index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,16 +150,21 @@ extension HomeTimelineViewModel {
|
||||||
|
|
||||||
// fetch data
|
// fetch data
|
||||||
let maxID = status.id
|
let maxID = status.id
|
||||||
_ = try? await context.apiService.homeTimeline(
|
let missingStatuses = (try? await context.apiService.homeTimeline(
|
||||||
maxID: maxID,
|
maxID: maxID,
|
||||||
authenticationBox: authContext.mastodonAuthenticationBox
|
authenticationBox: authContext.mastodonAuthenticationBox
|
||||||
)
|
).value) ?? []
|
||||||
|
|
||||||
record.isLoadingMore = false
|
record.isLoadingMore = false
|
||||||
|
|
||||||
// reconfigure item again
|
// reconfigure item again
|
||||||
snapshot.reconfigureItems([item])
|
snapshot.reconfigureItems([item])
|
||||||
await updateSnapshotUsingReloadData(snapshot: snapshot)
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ public final class MastodonFeed {
|
||||||
|
|
||||||
public let kind: Feed.Kind
|
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.id = notification?.id ?? status?.id ?? UUID().uuidString
|
||||||
self.hasMore = hasMore
|
self.hasMore = hasMore
|
||||||
self.isLoadingMore = isLoadingMore
|
self.isLoadingMore = isLoadingMore
|
||||||
|
|
Loading…
Reference in New Issue