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

View File

@ -15,7 +15,7 @@ extension PublicTimelineViewModel {
for tableView: UITableView, for tableView: UITableView,
dependency: NeedsDependency, dependency: NeedsDependency,
timelinePostTableViewCellDelegate: TimelinePostTableViewCellDelegate, timelinePostTableViewCellDelegate: TimelinePostTableViewCellDelegate,
timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate? timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate
) { ) {
let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common) let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common)
.autoconnect() .autoconnect()
@ -52,10 +52,10 @@ extension PublicTimelineViewModel: NSFetchedResultsControllerDelegate {
.sorted { $0.0 < $1.0 } .sorted { $0.0 < $1.0 }
var items = [Item]() var items = [Item]()
for tuple in indexTootTuples { for tuple in indexTootTuples {
items.append(Item.toot(objectID: tuple.1.objectID))
if tootIDsWhichHasGap.contains(tuple.1.id) { if tootIDsWhichHasGap.contains(tuple.1.id) {
items.append(Item.middleLoader(tootID: tuple.1.id)) items.append(Item.middleLoader(tootID: tuple.1.id))
} }
items.append(Item.toot(objectID: tuple.1.objectID))
} }
self.items.value = items self.items.value = items

View File

@ -54,7 +54,7 @@ extension PublicTimelineViewModel.LoadMiddleState {
} }
viewModel.context.apiService.publicTimeline( viewModel.context.apiService.publicTimeline(
domain: activeMastodonAuthenticationBox.domain, domain: activeMastodonAuthenticationBox.domain,
minID: upperTimelineTootID maxID: upperTimelineTootID
) )
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { completion in .sink { completion in
@ -70,8 +70,8 @@ extension PublicTimelineViewModel.LoadMiddleState {
let addedToots = toots.filter { !viewModel.tootIDs.value.contains($0.id) } let addedToots = toots.filter { !viewModel.tootIDs.value.contains($0.id) }
guard let gapIndex = viewModel.tootIDs.value.firstIndex(of: self.upperTimelineTootID) else { return } guard let gapIndex = viewModel.tootIDs.value.firstIndex(of: self.upperTimelineTootID) else { return }
let upToots = Array(viewModel.tootIDs.value[0...(gapIndex-1)]) let upToots = Array(viewModel.tootIDs.value[...gapIndex])
let downToots = Array(viewModel.tootIDs.value[gapIndex...viewModel.tootIDs.value.count-1]) let downToots = Array(viewModel.tootIDs.value[(gapIndex + 1)...])
// construct newTootIDs // construct newTootIDs
var newTootIDs = upToots var newTootIDs = upToots
@ -82,9 +82,9 @@ extension PublicTimelineViewModel.LoadMiddleState {
viewModel.tootIDsWhichHasGap.remove(at: index) viewModel.tootIDsWhichHasGap.remove(at: index)
} }
// add new gap from viewmodel if need // 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 { if intersection.isEmpty {
toots.first.flatMap { viewModel.tootIDsWhichHasGap.append($0.id) } addedToots.last.flatMap { viewModel.tootIDsWhichHasGap.append($0.id) }
} }
viewModel.tootIDs.value = newTootIDs viewModel.tootIDs.value = newTootIDs

View File

@ -69,7 +69,8 @@ extension PublicTimelineViewModel.State {
} }
} receiveValue: { response in } receiveValue: { response in
viewModel.isFetchingLatestTimeline.value = false 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 let oldTootsIDs = viewModel.tootIDs.value
var hasGap = true var hasGap = true
for tootID in oldTootsIDs { for tootID in oldTootsIDs {
@ -79,8 +80,8 @@ extension PublicTimelineViewModel.State {
hasGap = false hasGap = false
} }
} }
if hasGap { if hasGap && oldTootsIDs.count > 0 {
oldTootsIDs.first.flatMap { viewModel.tootIDsWhichHasGap.append($0) } resposeTootIDs.last.flatMap { viewModel.tootIDsWhichHasGap.append($0) }
} }
viewModel.tootIDs.value = newTootsIDs viewModel.tootIDs.value = newTootsIDs
stateMachine.enter(Idle.self) stateMachine.enter(Idle.self)

View File

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