2021-04-01 08:39:15 +02:00
|
|
|
//
|
|
|
|
// StatusFetchedResultsController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
public final class StatusFetchedResultsController: NSObject {
|
2021-04-01 08:39:15 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
let fetchedResultsController: NSFetchedResultsController<Status>
|
|
|
|
|
|
|
|
// input
|
2022-10-08 07:43:06 +02:00
|
|
|
@Published public var domain: String? = nil
|
|
|
|
@Published public var statusIDs: [Mastodon.Entity.Status.ID] = []
|
2021-04-01 08:39:15 +02:00
|
|
|
|
|
|
|
// output
|
2022-01-27 14:23:39 +01:00
|
|
|
let _objectIDs = CurrentValueSubject<[NSManagedObjectID], Never>([])
|
2022-10-08 07:43:06 +02:00
|
|
|
@Published public private(set) var records: [ManagedObjectRecord<Status>] = []
|
2021-04-01 08:39:15 +02:00
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
public init(managedObjectContext: NSManagedObjectContext, domain: String?, additionalTweetPredicate: NSPredicate?) {
|
|
|
|
self.domain = domain ?? ""
|
2021-04-01 08:39:15 +02:00
|
|
|
self.fetchedResultsController = {
|
|
|
|
let fetchRequest = Status.sortedFetchRequest
|
|
|
|
fetchRequest.predicate = Status.predicate(domain: domain ?? "", ids: [])
|
|
|
|
fetchRequest.returnsObjectsAsFaults = false
|
|
|
|
fetchRequest.fetchBatchSize = 20
|
|
|
|
let controller = NSFetchedResultsController(
|
|
|
|
fetchRequest: fetchRequest,
|
|
|
|
managedObjectContext: managedObjectContext,
|
|
|
|
sectionNameKeyPath: nil,
|
|
|
|
cacheName: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
return controller
|
|
|
|
}()
|
|
|
|
super.init()
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// debounce output to prevent UI update issues
|
|
|
|
_objectIDs
|
|
|
|
.throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true)
|
|
|
|
.map { objectIDs in objectIDs.map { ManagedObjectRecord(objectID: $0) } }
|
|
|
|
.assign(to: &$records)
|
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
fetchedResultsController.delegate = self
|
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
2022-10-08 07:43:06 +02:00
|
|
|
self.$domain.removeDuplicates(),
|
|
|
|
self.$statusIDs.removeDuplicates()
|
2021-04-01 08:39:15 +02:00
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] domain, ids in
|
|
|
|
guard let self = self else { return }
|
2021-04-07 10:37:05 +02:00
|
|
|
var predicates = [Status.predicate(domain: domain ?? "", ids: ids)]
|
|
|
|
if let additionalPredicate = additionalTweetPredicate {
|
|
|
|
predicates.append(additionalPredicate)
|
|
|
|
}
|
|
|
|
self.fetchedResultsController.fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
|
2021-04-01 08:39:15 +02:00
|
|
|
do {
|
|
|
|
try self.fetchedResultsController.performFetch()
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
extension StatusFetchedResultsController {
|
|
|
|
|
|
|
|
public func append(statusIDs: [Mastodon.Entity.Status.ID]) {
|
2022-10-08 07:43:06 +02:00
|
|
|
var result = self.statusIDs
|
2022-01-27 14:23:39 +01:00
|
|
|
for statusID in statusIDs where !result.contains(statusID) {
|
|
|
|
result.append(statusID)
|
|
|
|
}
|
2022-10-08 07:43:06 +02:00
|
|
|
self.statusIDs = result
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
// MARK: - NSFetchedResultsControllerDelegate
|
|
|
|
extension StatusFetchedResultsController: NSFetchedResultsControllerDelegate {
|
2022-10-08 07:43:06 +02:00
|
|
|
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
|
2021-04-01 08:39:15 +02:00
|
|
|
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
let indexes = statusIDs
|
2021-04-01 08:39:15 +02:00
|
|
|
let objects = fetchedResultsController.fetchedObjects ?? []
|
|
|
|
|
|
|
|
let items: [NSManagedObjectID] = objects
|
|
|
|
.compactMap { object in
|
|
|
|
indexes.firstIndex(of: object.id).map { index in (index, object) }
|
|
|
|
}
|
|
|
|
.sorted { $0.0 < $1.0 }
|
|
|
|
.map { $0.1.objectID }
|
2022-01-27 14:23:39 +01:00
|
|
|
self._objectIDs.value = items
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
}
|