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

76 lines
3.0 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,
dependency: dependency,
managedObjectContext: fetchedResultsController.managedObjectContext,
timestampUpdatePublisher: timestampUpdatePublisher,
statusTableViewCellDelegate: statusTableViewCellDelegate,
2021-02-04 08:09:58 +01:00
timelineMiddleLoaderTableViewCellDelegate: timelineMiddleLoaderTableViewCellDelegate
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)
let indexes = tootIDs.value
2021-01-28 09:10:30 +01:00
let toots = fetchedResultsController.fetchedObjects ?? []
2021-02-04 04:12:17 +01:00
guard toots.count == indexes.count else { return }
2021-02-04 08:09:58 +01:00
let indexTootTuples: [(Int, Toot)] = toots
2021-01-28 09:10:30 +01:00
.compactMap { toot -> (Int, Toot)? in
guard toot.deletedAt == nil else { return nil }
return indexes.firstIndex(of: toot.id).map { index in (index, toot) }
}
.sorted { $0.0 < $1.0 }
2021-03-05 06:41:48 +01:00
var oldSnapshotAttributeDict: [NSManagedObjectID: Item.StatusAttribute] = [:]
for item in self.items.value {
guard case let .toot(objectID, attribute) = item else { continue }
oldSnapshotAttributeDict[objectID] = attribute
}
2021-02-04 08:09:58 +01:00
var items = [Item]()
for (_, toot) in indexTootTuples {
let targetToot = toot.reblog ?? toot
let isStatusTextSensitive: Bool = {
guard let spoilerText = targetToot.spoilerText, !spoilerText.isEmpty else { return false }
return true
}()
2021-03-05 06:41:48 +01:00
let attribute = oldSnapshotAttributeDict[toot.objectID] ?? Item.StatusAttribute(isStatusTextSensitive: isStatusTextSensitive, isStatusSensitive: targetToot.sensitive)
items.append(Item.toot(objectID: toot.objectID, attribute: attribute))
if tootIDsWhichHasGap.contains(toot.id) {
items.append(Item.publicMiddleLoader(tootID: toot.id))
2021-02-04 08:09:58 +01:00
}
}
2021-01-28 09:10:30 +01:00
self.items.value = items
}
}