diff --git a/Mastodon/Diffiable/Section/TimelineSection.swift b/Mastodon/Diffiable/Section/TimelineSection.swift index 1d8406c9..2712194b 100644 --- a/Mastodon/Diffiable/Section/TimelineSection.swift +++ b/Mastodon/Diffiable/Section/TimelineSection.swift @@ -22,7 +22,7 @@ extension TimelineSection { managedObjectContext: NSManagedObjectContext, timestampUpdatePublisher: AnyPublisher, timelinePostTableViewCellDelegate: TimelinePostTableViewCellDelegate, - timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate? + timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate ) -> UITableViewDiffableDataSource { UITableViewDiffableDataSource(tableView: tableView) { [weak timelinePostTableViewCellDelegate, weak timelineMiddleLoaderTableViewCellDelegate] tableView, indexPath, item -> UITableViewCell? in guard let timelinePostTableViewCellDelegate = timelinePostTableViewCellDelegate else { return UITableViewCell() } diff --git a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+Diffable.swift b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+Diffable.swift index 13f3eeaa..9ec745da 100644 --- a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+Diffable.swift +++ b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+Diffable.swift @@ -15,7 +15,7 @@ extension PublicTimelineViewModel { for tableView: UITableView, dependency: NeedsDependency, timelinePostTableViewCellDelegate: TimelinePostTableViewCellDelegate, - timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate? + timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate ) { let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common) .autoconnect() @@ -52,10 +52,10 @@ extension PublicTimelineViewModel: NSFetchedResultsControllerDelegate { .sorted { $0.0 < $1.0 } var items = [Item]() for tuple in indexTootTuples { + items.append(Item.toot(objectID: tuple.1.objectID)) if tootIDsWhichHasGap.contains(tuple.1.id) { items.append(Item.middleLoader(tootID: tuple.1.id)) } - items.append(Item.toot(objectID: tuple.1.objectID)) } self.items.value = items diff --git a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+LoadMiddleState.swift b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+LoadMiddleState.swift index f7400d1c..cdaddfe7 100644 --- a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+LoadMiddleState.swift +++ b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+LoadMiddleState.swift @@ -54,7 +54,7 @@ extension PublicTimelineViewModel.LoadMiddleState { } viewModel.context.apiService.publicTimeline( domain: activeMastodonAuthenticationBox.domain, - minID: upperTimelineTootID + maxID: upperTimelineTootID ) .receive(on: DispatchQueue.main) .sink { completion in @@ -70,8 +70,8 @@ extension PublicTimelineViewModel.LoadMiddleState { let addedToots = toots.filter { !viewModel.tootIDs.value.contains($0.id) } guard let gapIndex = viewModel.tootIDs.value.firstIndex(of: self.upperTimelineTootID) else { return } - let upToots = Array(viewModel.tootIDs.value[0...(gapIndex-1)]) - let downToots = Array(viewModel.tootIDs.value[gapIndex...viewModel.tootIDs.value.count-1]) + let upToots = Array(viewModel.tootIDs.value[...gapIndex]) + let downToots = Array(viewModel.tootIDs.value[(gapIndex + 1)...]) // construct newTootIDs var newTootIDs = upToots @@ -82,9 +82,9 @@ extension PublicTimelineViewModel.LoadMiddleState { viewModel.tootIDsWhichHasGap.remove(at: index) } // add new gap from viewmodel if need - let intersection = toots.filter { upToots.contains($0.id) } + let intersection = toots.filter { downToots.contains($0.id) } if intersection.isEmpty { - toots.first.flatMap { viewModel.tootIDsWhichHasGap.append($0.id) } + addedToots.last.flatMap { viewModel.tootIDsWhichHasGap.append($0.id) } } viewModel.tootIDs.value = newTootIDs diff --git a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+State.swift b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+State.swift index 6b3c8d3d..e9182dfe 100644 --- a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+State.swift +++ b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+State.swift @@ -69,7 +69,8 @@ extension PublicTimelineViewModel.State { } } receiveValue: { response in viewModel.isFetchingLatestTimeline.value = false - var newTootsIDs = response.value.compactMap { $0.id } + let resposeTootIDs = response.value.compactMap { $0.id } + var newTootsIDs = resposeTootIDs let oldTootsIDs = viewModel.tootIDs.value var hasGap = true for tootID in oldTootsIDs { @@ -79,8 +80,8 @@ extension PublicTimelineViewModel.State { hasGap = false } } - if hasGap { - oldTootsIDs.first.flatMap { viewModel.tootIDsWhichHasGap.append($0) } + if hasGap && oldTootsIDs.count > 0 { + resposeTootIDs.last.flatMap { viewModel.tootIDsWhichHasGap.append($0) } } viewModel.tootIDs.value = newTootsIDs stateMachine.enter(Idle.self) diff --git a/Mastodon/Service/APIService/APIService+PublicTimeline.swift b/Mastodon/Service/APIService/APIService+PublicTimeline.swift index 0f3d6032..aa45dbb9 100644 --- a/Mastodon/Service/APIService/APIService+PublicTimeline.swift +++ b/Mastodon/Service/APIService/APIService+PublicTimeline.swift @@ -29,7 +29,7 @@ extension APIService { onlyMedia: nil, maxID: maxID, sinceID: sinceID, - minID: minID, // prefer sinceID + minID: nil, // prefer sinceID limit: limit )