Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/PublicTimeline/PublicTimelineViewModel+Dif...

76 lines
3.1 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,
2021-02-23 08:16:55 +01:00
timelinePostTableViewCellDelegate: 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,
2021-02-04 08:09:58 +01:00
timelinePostTableViewCellDelegate: timelinePostTableViewCellDelegate,
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 }
var oldSnapshotAttributeDict: [NSManagedObjectID: Item.StatusTimelineAttribute] = [:]
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
}()
let attribute = oldSnapshotAttributeDict[toot.objectID] ?? Item.StatusTimelineAttribute(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
}
}