2021-04-01 08:39:15 +02:00
|
|
|
//
|
|
|
|
// UserTimelineViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-29.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import GameplayKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
2021-04-07 08:24:28 +02:00
|
|
|
final class UserTimelineViewModel {
|
2021-04-01 08:39:15 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-05-13 11:23:35 +02:00
|
|
|
@Published var userIdentifier: UserIdentifier?
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published var queryFilter: QueryFilter
|
2021-04-01 08:39:15 +02:00
|
|
|
let statusFetchedResultsController: StatusFetchedResultsController
|
2021-11-10 10:55:12 +01:00
|
|
|
let listBatchFetchViewModel = ListBatchFetchViewModel()
|
2021-07-15 11:26:04 +02:00
|
|
|
|
2021-04-06 10:43:08 +02:00
|
|
|
let isBlocking = CurrentValueSubject<Bool, Never>(false)
|
|
|
|
let isBlockedBy = CurrentValueSubject<Bool, Never>(false)
|
2021-04-08 10:53:32 +02:00
|
|
|
let isSuspended = CurrentValueSubject<Bool, Never>(false)
|
|
|
|
let userDisplayName = CurrentValueSubject<String?, Never>(nil) // for suspended prompt label
|
2021-07-15 11:26:04 +02:00
|
|
|
var dataSourceDidUpdate = PassthroughSubject<Void, Never>()
|
2021-04-01 08:39:15 +02:00
|
|
|
|
|
|
|
// output
|
2022-01-27 14:23:39 +01:00
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, StatusItem>?
|
2021-04-01 08:39:15 +02:00
|
|
|
private(set) lazy var stateMachine: GKStateMachine = {
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
State.Initial(viewModel: self),
|
|
|
|
State.Reloading(viewModel: self),
|
|
|
|
State.Fail(viewModel: self),
|
|
|
|
State.Idle(viewModel: self),
|
2021-04-07 08:24:28 +02:00
|
|
|
State.Loading(viewModel: self),
|
2021-04-01 08:39:15 +02:00
|
|
|
State.NoMore(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(State.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
init(
|
|
|
|
context: AppContext,
|
|
|
|
queryFilter: QueryFilter
|
|
|
|
) {
|
2021-04-01 08:39:15 +02:00
|
|
|
self.context = context
|
|
|
|
self.statusFetchedResultsController = StatusFetchedResultsController(
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
2022-05-13 11:23:35 +02:00
|
|
|
domain: nil,
|
|
|
|
additionalTweetPredicate: nil
|
2021-04-01 08:39:15 +02:00
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
self.queryFilter = queryFilter
|
2021-04-07 08:24:28 +02:00
|
|
|
// super.init()
|
2021-04-01 08:39:15 +02:00
|
|
|
|
2022-05-13 11:23:35 +02:00
|
|
|
context.authenticationService.activeMastodonAuthenticationBox
|
|
|
|
.map { $0?.domain }
|
2021-04-01 08:39:15 +02:00
|
|
|
.assign(to: \.value, on: statusFetchedResultsController.domain)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2022-05-13 11:23:35 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UserTimelineViewModel {
|
|
|
|
struct QueryFilter {
|
|
|
|
let excludeReplies: Bool?
|
|
|
|
let excludeReblogs: Bool?
|
|
|
|
let onlyMedia: Bool?
|
|
|
|
|
|
|
|
init(
|
|
|
|
excludeReplies: Bool? = nil,
|
|
|
|
excludeReblogs: Bool? = nil,
|
|
|
|
onlyMedia: Bool? = nil
|
|
|
|
) {
|
|
|
|
self.excludeReplies = excludeReplies
|
|
|
|
self.excludeReblogs = excludeReblogs
|
|
|
|
self.onlyMedia = onlyMedia
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|