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-03-03 09:12:48 +01:00
|
|
|
statusTableViewCellDelegate: StatusTableViewCellDelegate,
|
2021-02-05 09:04:59 +01:00
|
|
|
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
|
|
|
|
2021-02-24 09:11:48 +01:00
|
|
|
diffableDataSource = StatusSection.tableViewDiffableDataSource(
|
2021-01-28 09:10:30 +01:00
|
|
|
for: tableView,
|
|
|
|
dependency: dependency,
|
|
|
|
managedObjectContext: fetchedResultsController.managedObjectContext,
|
|
|
|
timestampUpdatePublisher: timestampUpdatePublisher,
|
2021-03-03 09:12:48 +01:00
|
|
|
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 = []
|
2021-02-04 07:45:44 +01:00
|
|
|
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] = [:]
|
2021-02-24 09:11:48 +01:00
|
|
|
for item in self.items.value {
|
2021-04-01 08:39:15 +02:00
|
|
|
guard case let .status(objectID, attribute) = item else { continue }
|
2021-02-24 09:11:48 +01:00
|
|
|
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 {
|
2021-04-16 14:06:36 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|