mastodon-ios/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel.swift

81 lines
2.2 KiB
Swift
Raw Normal View History

2022-02-08 05:36:06 +01:00
//
// ReportStatusViewModel.swift
2022-02-08 05:36:06 +01:00
// Mastodon
//
// Created by MainasuK on 2022-5-10.
2022-02-08 05:36:06 +01:00
//
import Combine
import CoreData
import CoreDataStack
import Foundation
import GameplayKit
import MastodonSDK
import OrderedCollections
import os.log
import UIKit
2022-10-08 07:43:06 +02:00
import MastodonCore
2022-02-08 05:36:06 +01:00
class ReportStatusViewModel {
2022-02-08 05:36:06 +01:00
var disposeBag = Set<AnyCancellable>()
weak var delegate: ReportStatusViewControllerDelegate?
2022-02-08 05:36:06 +01:00
// input
let context: AppContext
let authContext: AuthContext
2022-02-08 05:36:06 +01:00
let user: ManagedObjectRecord<MastodonUser>
let status: ManagedObjectRecord<Status>?
let statusFetchedResultsController: StatusFetchedResultsController
let listBatchFetchViewModel = ListBatchFetchViewModel()
@Published var isSkip = false
2022-02-08 05:36:06 +01:00
@Published var selectStatuses = OrderedSet<ManagedObjectRecord<Status>>()
// output
var diffableDataSource: UITableViewDiffableDataSource<ReportSection, ReportItem>?
private(set) lazy var stateMachine: GKStateMachine = {
let stateMachine = GKStateMachine(states: [
State.Initial(viewModel: self),
State.Fail(viewModel: self),
State.Idle(viewModel: self),
State.Loading(viewModel: self),
State.NoMore(viewModel: self),
])
stateMachine.enter(State.Initial.self)
return stateMachine
}()
@Published var isNextButtonEnabled = false
init(
context: AppContext,
authContext: AuthContext,
2022-02-08 05:36:06 +01:00
user: ManagedObjectRecord<MastodonUser>,
status: ManagedObjectRecord<Status>?
) {
self.context = context
self.authContext = authContext
2022-02-08 05:36:06 +01:00
self.user = user
self.status = status
self.statusFetchedResultsController = StatusFetchedResultsController(
managedObjectContext: context.managedObjectContext,
domain: authContext.mastodonAuthenticationBox.domain,
2022-02-08 05:36:06 +01:00
additionalTweetPredicate: nil
)
// end init
if let status = status {
selectStatuses.append(status)
}
2022-02-08 05:36:06 +01:00
$selectStatuses
.map { statuses -> Bool in
return status == nil ? !statuses.isEmpty : statuses.count > 1
}
.assign(to: &$isNextButtonEnabled)
}
}