mastodon-ios/Mastodon/Scene/Profile/Timeline/UserTimelineViewModel.swift

92 lines
2.6 KiB
Swift
Raw Normal View History

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
2022-10-08 07:43:06 +02:00
import MastodonCore
2021-04-01 08:39:15 +02:00
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
let authContext: AuthContext
let title: String
2021-04-01 08:39:15 +02:00
let statusFetchedResultsController: StatusFetchedResultsController
let listBatchFetchViewModel = ListBatchFetchViewModel()
@Published var userIdentifier: UserIdentifier?
@Published var queryFilter: QueryFilter
@Published var isBlocking = false
@Published var isBlockedBy = false
@Published var isSuspended = false
// let userDisplayName = CurrentValueSubject<String?, Never>(nil) // for suspended prompt label
// var dataSourceDidUpdate = PassthroughSubject<Void, Never>()
2021-04-01 08:39:15 +02:00
// output
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
}()
init(
context: AppContext,
authContext: AuthContext,
title: String,
queryFilter: QueryFilter
) {
2021-04-01 08:39:15 +02:00
self.context = context
self.authContext = authContext
self.title = title
2021-04-01 08:39:15 +02:00
self.statusFetchedResultsController = StatusFetchedResultsController(
managedObjectContext: context.managedObjectContext,
domain: authContext.mastodonAuthenticationBox.domain,
2022-05-13 11:23:35 +02:00
additionalTweetPredicate: nil
2021-04-01 08:39:15 +02:00
)
self.queryFilter = queryFilter
2021-04-01 08:39:15 +02:00
}
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
}
}
}