2021-04-01 04:12:57 +02:00
|
|
|
//
|
|
|
|
// HashtagTimelineViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by BradGao on 2021/3/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
final class HashtagTimelineViewModel: NSObject {
|
|
|
|
|
2021-04-07 10:56:31 +02:00
|
|
|
let hashtag: String
|
2021-04-01 04:12:57 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
var needLoadMiddleIndex: Int? = nil
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2021-04-07 10:37:05 +02:00
|
|
|
let fetchedResultsController: StatusFetchedResultsController
|
2021-04-01 04:12:57 +02:00
|
|
|
let isFetchingLatestTimeline = CurrentValueSubject<Bool, Never>(false)
|
|
|
|
let timelinePredicate = CurrentValueSubject<NSPredicate?, Never>(nil)
|
2021-04-02 04:21:51 +02:00
|
|
|
let hashtagEntity = CurrentValueSubject<Mastodon.Entity.Tag?, Never>(nil)
|
2021-04-01 04:12:57 +02:00
|
|
|
|
|
|
|
weak var contentOffsetAdjustableTimelineViewControllerDelegate: ContentOffsetAdjustableTimelineViewControllerDelegate?
|
|
|
|
weak var tableView: UITableView?
|
|
|
|
|
|
|
|
// output
|
|
|
|
// top loader
|
|
|
|
private(set) lazy var loadLatestStateMachine: GKStateMachine = {
|
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
LoadLatestState.Initial(viewModel: self),
|
|
|
|
LoadLatestState.Loading(viewModel: self),
|
|
|
|
LoadLatestState.Fail(viewModel: self),
|
|
|
|
LoadLatestState.Idle(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(LoadLatestState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
lazy var loadLatestStateMachinePublisher = CurrentValueSubject<LoadLatestState?, Never>(nil)
|
|
|
|
// bottom loader
|
2021-07-23 13:10:27 +02:00
|
|
|
private(set) lazy var loadOldestStateMachine: GKStateMachine = {
|
2021-04-01 04:12:57 +02:00
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
LoadOldestState.Initial(viewModel: self),
|
|
|
|
LoadOldestState.Loading(viewModel: self),
|
|
|
|
LoadOldestState.Fail(viewModel: self),
|
|
|
|
LoadOldestState.Idle(viewModel: self),
|
|
|
|
LoadOldestState.NoMore(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(LoadOldestState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
lazy var loadOldestStateMachinePublisher = CurrentValueSubject<LoadOldestState?, Never>(nil)
|
|
|
|
// middle loader
|
|
|
|
let loadMiddleSateMachineList = CurrentValueSubject<[NSManagedObjectID: GKStateMachine], Never>([:]) // TimelineIndex.objectID : middle loading state machine
|
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, Item>?
|
|
|
|
var cellFrameCache = NSCache<NSNumber, NSValue>()
|
|
|
|
|
|
|
|
|
2021-04-07 10:56:31 +02:00
|
|
|
init(context: AppContext, hashtag: String) {
|
2021-04-01 04:12:57 +02:00
|
|
|
self.context = context
|
2021-04-07 10:56:31 +02:00
|
|
|
self.hashtag = hashtag
|
2021-04-07 10:37:05 +02:00
|
|
|
let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value
|
|
|
|
self.fetchedResultsController = StatusFetchedResultsController(managedObjectContext: context.managedObjectContext, domain: activeMastodonAuthenticationBox?.domain, additionalTweetPredicate: nil)
|
2021-04-01 04:12:57 +02:00
|
|
|
super.init()
|
|
|
|
|
2021-04-07 10:37:05 +02:00
|
|
|
fetchedResultsController.objectIDs
|
2021-04-01 04:12:57 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2021-04-07 10:37:05 +02:00
|
|
|
.sink { [weak self] objectIds in
|
|
|
|
self?.generateStatusItems(newObjectIDs: objectIds)
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
2021-04-02 04:21:51 +02:00
|
|
|
func fetchTag() {
|
|
|
|
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else {
|
|
|
|
return
|
|
|
|
}
|
2021-04-20 09:40:10 +02:00
|
|
|
let query = Mastodon.API.V2.Search.Query(q: hashtag, type: .hashtags)
|
2021-09-26 12:29:08 +02:00
|
|
|
context.apiService.search(
|
|
|
|
domain: activeMastodonAuthenticationBox.domain,
|
|
|
|
query: query,
|
|
|
|
mastodonAuthenticationBox: activeMastodonAuthenticationBox
|
|
|
|
)
|
|
|
|
.sink { _ in
|
|
|
|
|
|
|
|
} receiveValue: { [weak self] response in
|
|
|
|
let matchedTag = response.value.hashtags.first { tag -> Bool in
|
|
|
|
return tag.name == self?.hashtag
|
2021-04-02 04:21:51 +02:00
|
|
|
}
|
2021-09-26 12:29:08 +02:00
|
|
|
self?.hashtagEntity.send(matchedTag)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-02 04:21:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s:", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|