mastodon-ios/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+Dif...

73 lines
2.9 KiB
Swift
Raw Normal View History

2021-01-28 09:10:30 +01:00
//
// PublicTimelineViewModel+Diffable.swift
// Mastodon
//
// Created by sxiaojian on 2021/1/27.
//
import CoreData
import CoreDataStack
2021-02-02 12:04:24 +01:00
import os.log
import UIKit
2021-01-28 09:10:30 +01:00
extension PublicTimelineViewModel {
func setupDiffableDataSource(
for tableView: UITableView,
dependency: NeedsDependency,
statusTableViewCellDelegate: StatusTableViewCellDelegate,
timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate
2021-01-28 09:10:30 +01:00
) {
let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common)
.autoconnect()
.share()
.eraseToAnyPublisher()
2021-02-02 12:04:24 +01:00
diffableDataSource = StatusSection.tableViewDiffableDataSource(
2021-01-28 09:10:30 +01:00
for: tableView,
2021-07-09 13:07:12 +02:00
timelineContext: .public,
2021-01-28 09:10:30 +01:00
dependency: dependency,
managedObjectContext: fetchedResultsController.managedObjectContext,
timestampUpdatePublisher: timestampUpdatePublisher,
statusTableViewCellDelegate: statusTableViewCellDelegate,
2021-04-13 13:46:42 +02:00
timelineMiddleLoaderTableViewCellDelegate: timelineMiddleLoaderTableViewCellDelegate,
threadReplyLoaderTableViewCellDelegate: nil
2021-02-02 12:04:24 +01:00
)
2021-01-28 09:10:30 +01:00
items.value = []
stateMachine.enter(PublicTimelineViewModel.State.Loading.self)
2021-01-28 09:10:30 +01:00
}
}
// MARK: - NSFetchedResultsControllerDelegate
2021-02-02 12:04:24 +01:00
2021-01-28 09:10:30 +01:00
extension PublicTimelineViewModel: NSFetchedResultsControllerDelegate {
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
2021-02-02 12:04:24 +01:00
os_log("%{public}s[%{public}ld], %{public}s", (#file as NSString).lastPathComponent, #line, #function)
2021-04-01 08:39:15 +02:00
let indexes = statusIDs.value
let statuses = fetchedResultsController.fetchedObjects ?? []
guard statuses.count == indexes.count else { return }
let indexStatusTuples: [(Int, Status)] = statuses
.compactMap { status -> (Int, Status)? in
guard status.deletedAt == nil else { return nil }
return indexes.firstIndex(of: status.id).map { index in (index, status) }
2021-01-28 09:10:30 +01:00
}
.sorted { $0.0 < $1.0 }
2021-03-05 06:41:48 +01:00
var oldSnapshotAttributeDict: [NSManagedObjectID: Item.StatusAttribute] = [:]
for item in self.items.value {
2021-04-01 08:39:15 +02:00
guard case let .status(objectID, attribute) = item else { continue }
oldSnapshotAttributeDict[objectID] = attribute
}
2021-02-04 08:09:58 +01:00
var items = [Item]()
2021-04-01 08:39:15 +02:00
for (_, status) in indexStatusTuples {
let attribute = oldSnapshotAttributeDict[status.objectID] ?? Item.StatusAttribute()
2021-04-01 08:39:15 +02:00
items.append(Item.status(objectID: status.objectID, attribute: attribute))
if statusIDsWhichHasGap.contains(status.id) {
items.append(Item.publicMiddleLoader(statusID: status.id))
2021-02-04 08:09:58 +01:00
}
}
2021-01-28 09:10:30 +01:00
self.items.value = items
}
}