2021-02-07 07:42:50 +01:00
|
|
|
//
|
|
|
|
// HomeTimelineViewController+StatusProvider.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/5.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2021-03-03 09:12:48 +01:00
|
|
|
import CoreData
|
2021-02-07 07:42:50 +01:00
|
|
|
import CoreDataStack
|
|
|
|
|
|
|
|
// MARK: - StatusProvider
|
|
|
|
extension HomeTimelineViewController: StatusProvider {
|
2021-02-24 09:11:48 +01:00
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
func status() -> Future<Status?, Never> {
|
2021-02-07 07:42:50 +01:00
|
|
|
return Future { promise in promise(.success(nil)) }
|
|
|
|
}
|
|
|
|
|
2021-04-13 13:46:42 +02:00
|
|
|
func status(for cell: UITableViewCell?, indexPath: IndexPath?) -> Future<Status?, Never> {
|
2021-02-07 07:42:50 +01:00
|
|
|
return Future { promise in
|
|
|
|
guard let diffableDataSource = self.viewModel.diffableDataSource else {
|
|
|
|
assertionFailure()
|
|
|
|
promise(.success(nil))
|
|
|
|
return
|
|
|
|
}
|
2021-04-13 13:46:42 +02:00
|
|
|
guard let indexPath = indexPath ?? cell.flatMap({ self.tableView.indexPath(for: $0) }),
|
2021-02-07 07:42:50 +01:00
|
|
|
let item = diffableDataSource.itemIdentifier(for: indexPath) else {
|
|
|
|
promise(.success(nil))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case .homeTimelineIndex(let objectID, _):
|
|
|
|
let managedObjectContext = self.viewModel.fetchedResultsController.managedObjectContext
|
|
|
|
managedObjectContext.perform {
|
|
|
|
let timelineIndex = managedObjectContext.object(with: objectID) as? HomeTimelineIndex
|
2021-04-01 08:39:15 +02:00
|
|
|
promise(.success(timelineIndex?.status))
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
promise(.success(nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
func status(for cell: UICollectionViewCell) -> Future<Status?, Never> {
|
2021-02-07 07:42:50 +01:00
|
|
|
return Future { promise in promise(.success(nil)) }
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:12:48 +01:00
|
|
|
var managedObjectContext: NSManagedObjectContext {
|
|
|
|
return viewModel.fetchedResultsController.managedObjectContext
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:11:48 +01:00
|
|
|
var tableViewDiffableDataSource: UITableViewDiffableDataSource<StatusSection, Item>? {
|
|
|
|
return viewModel.diffableDataSource
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:12:48 +01:00
|
|
|
func item(for cell: UITableViewCell?, indexPath: IndexPath?) -> Item? {
|
|
|
|
guard let diffableDataSource = self.viewModel.diffableDataSource else {
|
|
|
|
assertionFailure()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let indexPath = indexPath ?? cell.flatMap({ self.tableView.indexPath(for: $0) }),
|
|
|
|
let item = diffableDataSource.itemIdentifier(for: indexPath) else {
|
|
|
|
return nil
|
2021-02-24 09:11:48 +01:00
|
|
|
}
|
2021-03-03 09:12:48 +01:00
|
|
|
|
|
|
|
return item
|
2021-02-24 09:11:48 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 12:12:53 +01:00
|
|
|
func items(indexPaths: [IndexPath]) -> [Item] {
|
|
|
|
guard let diffableDataSource = self.viewModel.diffableDataSource else {
|
|
|
|
assertionFailure()
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
var items: [Item] = []
|
|
|
|
for indexPath in indexPaths {
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { continue }
|
|
|
|
items.append(item)
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|