From faac99cd102beca375de778bcf4da45e1a295ec2 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Mon, 27 Nov 2023 11:54:01 +0100 Subject: [PATCH] Fix duplicate entry after reblog (IOS-176) --- .../HomeTimelineViewModel+LoadLatestState.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift index b107f8efc..19a58c80b 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift @@ -112,10 +112,21 @@ extension HomeTimelineViewModel.LoadLatestState { viewModel.homeTimelineNavigationBarTitleViewModel.newPostsIncoming() } - let newRecords: [MastodonFeed] = newStatuses.map { + var newRecords: [MastodonFeed] = newStatuses.map { MastodonFeed.fromStatus(.fromEntity($0), kind: .home) } - viewModel.fetchedResultsController.records = newRecords + viewModel.fetchedResultsController.records + viewModel.fetchedResultsController.records = { + var oldRecords = viewModel.fetchedResultsController.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) + } + } + } + return (newRecords + oldRecords).removingDuplicates() + }() } viewModel.timelineIsEmpty.value = latestStatusIDs.isEmpty && statuses.isEmpty