mastodon-ios/Mastodon/Scene/PublicTimeline/PublicTimelineViewControlle...

96 lines
3.1 KiB
Swift
Raw Normal View History

2021-01-28 09:10:30 +01:00
//
// PublicTimelineViewController+Provider.swift
2021-01-28 09:10:30 +01:00
// Mastodon
//
// Created by sxiaojian on 2021/1/27.
//
import os.log
import UIKit
import Combine
import CoreData
2021-01-28 09:10:30 +01:00
import CoreDataStack
import MastodonSDK
// MARK: - StatusProvider
2021-02-08 11:29:27 +01:00
extension PublicTimelineViewController: StatusProvider {
2021-04-01 08:39:15 +02:00
func status() -> Future<Status?, Never> {
2021-01-28 09:10:30 +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-01-28 09:10:30 +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-01-28 09:10:30 +01:00
let item = diffableDataSource.itemIdentifier(for: indexPath) else {
promise(.success(nil))
return
}
switch item {
2021-04-01 08:39:15 +02:00
case .status(let objectID, _):
2021-01-28 09:10:30 +01:00
let managedObjectContext = self.viewModel.fetchedResultsController.managedObjectContext
managedObjectContext.perform {
2021-04-01 08:39:15 +02:00
let status = managedObjectContext.object(with: objectID) as? Status
promise(.success(status))
2021-01-28 09:10:30 +01:00
}
2021-02-03 06:01:50 +01:00
default:
promise(.success(nil))
2021-01-28 09:10:30 +01:00
}
}
}
2021-04-01 08:39:15 +02:00
func status(for cell: UICollectionViewCell) -> Future<Status?, Never> {
2021-01-28 09:10:30 +01:00
return Future { promise in promise(.success(nil)) }
}
var managedObjectContext: NSManagedObjectContext {
return viewModel.fetchedResultsController.managedObjectContext
}
var tableViewDiffableDataSource: UITableViewDiffableDataSource<StatusSection, Item>? {
return viewModel.diffableDataSource
}
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
}
return item
}
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
}
func statusObjectItems(indexPaths: [IndexPath]) -> [StatusObjectItem] {
guard let diffableDataSource = self.viewModel.diffableDataSource else { return [] }
let items = indexPaths.compactMap { diffableDataSource.itemIdentifier(for: $0)?.statusObjectItem }
return items
}
2021-03-10 12:12:53 +01:00
2021-01-28 09:10:30 +01:00
}
extension PublicTimelineViewController: UserProvider {}