fix: change Middle Loader load sequence ,use the toot before the gap as the anchor.

This commit is contained in:
sunxiaojian 2021-02-05 16:04:59 +08:00
parent 61dc0cb2cd
commit 08dfe42aba
5 changed files with 13 additions and 12 deletions

View File

@ -22,7 +22,7 @@ extension TimelineSection {
managedObjectContext: NSManagedObjectContext,
timestampUpdatePublisher: AnyPublisher<Date, Never>,
timelinePostTableViewCellDelegate: TimelinePostTableViewCellDelegate,
timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate?
timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate
) -> UITableViewDiffableDataSource<TimelineSection, Item> {
UITableViewDiffableDataSource(tableView: tableView) { [weak timelinePostTableViewCellDelegate, weak timelineMiddleLoaderTableViewCellDelegate] tableView, indexPath, item -> UITableViewCell? in
guard let timelinePostTableViewCellDelegate = timelinePostTableViewCellDelegate else { return UITableViewCell() }

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -29,7 +29,7 @@ extension APIService {
onlyMedia: nil,
maxID: maxID,
sinceID: sinceID,
minID: minID, // prefer sinceID
minID: nil, // prefer sinceID
limit: limit
)