mastodon-ios/Mastodon/Scene/Report/ReportViewModel+Data.swift

101 lines
4.2 KiB
Swift
Raw Normal View History

2021-04-19 14:34:08 +02:00
//
// ReportViewModel+Data.swift
// Mastodon
//
// Created by ihugo on 2021/4/20.
//
import Combine
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
import UIKit
import os.log
extension ReportViewModel {
func requestRecentStatus(
domain: String,
accountId: String,
authorizationBox: AuthenticationService.MastodonAuthenticationBox
) {
context.apiService.userTimeline(
domain: domain,
accountID: accountId,
2021-04-26 09:58:49 +02:00
excludeReblogs: true,
2021-04-19 14:34:08 +02:00
authorizationBox: authorizationBox
)
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
switch completion {
case .failure(let error):
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: fetch user timeline fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
guard let self = self else { return }
2021-04-26 09:58:49 +02:00
guard let reportStatusId = self.status?.id else { return }
2021-04-19 14:34:08 +02:00
var statusIDs = self.statusFetchedResultsController.statusIDs.value
guard statusIDs.contains(reportStatusId) else { return }
statusIDs.append(reportStatusId)
self.statusFetchedResultsController.statusIDs.value = statusIDs
case .finished:
break
}
} receiveValue: { [weak self] response in
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
guard let self = self else { return }
var statusIDs = response.value.map { $0.id }
2021-04-26 09:58:49 +02:00
if let reportStatusId = self.status?.id, !statusIDs.contains(reportStatusId) {
2021-04-19 14:34:08 +02:00
statusIDs.append(reportStatusId)
}
self.statusFetchedResultsController.statusIDs.value = statusIDs
}
.store(in: &disposeBag)
}
func fetchStatus() {
let managedObjectContext = self.statusFetchedResultsController.fetchedResultsController.managedObjectContext
statusFetchedResultsController.objectIDs.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.sink { [weak self] objectIDs in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
var items: [Item] = []
var snapshot = NSDiffableDataSourceSnapshot<ReportSection, Item>()
snapshot.appendSections([.main])
defer {
// not animate when empty items fix loader first appear layout issue
diffableDataSource.apply(snapshot, animatingDifferences: !items.isEmpty)
}
var oldSnapshotAttributeDict: [NSManagedObjectID : Item.ReportStatusAttribute] = [:]
2021-04-19 14:34:08 +02:00
let oldSnapshot = diffableDataSource.snapshot()
for item in oldSnapshot.itemIdentifiers {
guard case let .reportStatus(objectID, attribute) = item else { continue }
2021-04-19 14:34:08 +02:00
oldSnapshotAttributeDict[objectID] = attribute
}
for objectID in objectIDs {
let attribute = oldSnapshotAttributeDict[objectID] ?? Item.ReportStatusAttribute()
let item = Item.reportStatus(objectID: objectID, attribute: attribute)
2021-04-19 14:34:08 +02:00
items.append(item)
guard let status = managedObjectContext.object(with: objectID) as? Status else {
continue
}
2021-04-26 09:58:49 +02:00
if status.id == self.status?.id {
attribute.isSelected = true
self.append(statusID: status.id)
2021-04-19 14:34:08 +02:00
self.continueEnableSubject.send(true)
}
}
snapshot.appendItems(items, toSection: .main)
}
.store(in: &disposeBag)
}
}