mastodon-ios/Mastodon/Scene/Report/ReportViewModel.swift

216 lines
7.3 KiB
Swift
Raw Normal View History

2021-04-19 14:34:08 +02:00
//
// ReportViewModel.swift
// Mastodon
//
// Created by ihugo on 2021/4/19.
//
import Combine
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
import UIKit
import os.log
class ReportViewModel: NSObject {
2021-04-19 14:34:08 +02:00
typealias FileReportQuery = Mastodon.API.Reports.FileReportQuery
enum Step: Int {
case one
case two
}
// confirm set only once
weak var context: AppContext! { willSet { precondition(context == nil) } }
2021-04-26 09:58:49 +02:00
var user: MastodonUser
var status: Status?
2021-04-19 14:34:08 +02:00
var statusIDs = [Mastodon.Entity.Status.ID]()
var comment: String?
var reportQuery: FileReportQuery
var disposeBag = Set<AnyCancellable>()
let currentStep = CurrentValueSubject<Step, Never>(.one)
let statusFetchedResultsController: StatusFetchedResultsController
var diffableDataSource: UITableViewDiffableDataSource<ReportSection, Item>?
let continueEnableSubject = CurrentValueSubject<Bool, Never>(false)
let sendEnableSubject = CurrentValueSubject<Bool, Never>(false)
2021-04-19 14:34:08 +02:00
struct Input {
let didToggleSelected: AnyPublisher<Item, Never>
let comment: AnyPublisher<String?, Never>
let step1Continue: AnyPublisher<Void, Never>
let step1Skip: AnyPublisher<Void, Never>
let step2Continue: AnyPublisher<Void, Never>
let step2Skip: AnyPublisher<Void, Never>
let cancel: AnyPublisher<Void, Never>
}
struct Output {
let currentStep: AnyPublisher<Step, Never>
let continueEnableSubject: AnyPublisher<Bool, Never>
let sendEnableSubject: AnyPublisher<Bool, Never>
let reportResult: AnyPublisher<(Bool, Error?), Never>
2021-04-19 14:34:08 +02:00
}
init(context: AppContext,
domain: String,
2021-04-26 09:58:49 +02:00
user: MastodonUser,
status: Status?
2021-04-19 14:34:08 +02:00
) {
self.context = context
2021-04-26 09:58:49 +02:00
self.user = user
self.status = status
2021-04-19 14:34:08 +02:00
self.statusFetchedResultsController = StatusFetchedResultsController(
managedObjectContext: context.managedObjectContext,
domain: domain,
additionalTweetPredicate: Status.notDeleted()
)
self.reportQuery = FileReportQuery(
2021-04-26 09:58:49 +02:00
accountID: user.id,
2021-04-25 09:48:37 +02:00
statusIDs: [],
2021-04-19 14:34:08 +02:00
comment: nil,
forward: nil
)
super.init()
}
func transform(input: Input?) -> Output? {
guard let input = input else { return nil }
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else {
return nil
}
let domain = activeMastodonAuthenticationBox.domain
// data binding
bindData(input: input)
// step1 and step2 binding
bindForStep1(input: input)
let reportResult = bindForStep2(
2021-04-19 14:34:08 +02:00
input: input,
domain: domain,
activeMastodonAuthenticationBox: activeMastodonAuthenticationBox
)
requestRecentStatus(
domain: domain,
2021-04-26 09:58:49 +02:00
accountId: self.user.id,
2021-04-19 14:34:08 +02:00
authorizationBox: activeMastodonAuthenticationBox
)
fetchStatus()
return Output(
currentStep: currentStep.eraseToAnyPublisher(),
continueEnableSubject: continueEnableSubject.eraseToAnyPublisher(),
sendEnableSubject: sendEnableSubject.eraseToAnyPublisher(),
reportResult: reportResult
2021-04-19 14:34:08 +02:00
)
}
// MARK: - Private methods
func bindData(input: Input) {
input.didToggleSelected.sink { [weak self] (item) in
guard let self = self else { return }
guard case let .reportStatus(objectID, attribute) = item else { return }
2021-04-19 14:34:08 +02:00
let managedObjectContext = self.statusFetchedResultsController.fetchedResultsController.managedObjectContext
guard let status = managedObjectContext.object(with: objectID) as? Status else {
return
}
attribute.isSelected = !attribute.isSelected
if attribute.isSelected {
self.append(statusID: status.id)
2021-04-19 14:34:08 +02:00
} else {
self.remove(statusID: status.id)
2021-04-19 14:34:08 +02:00
}
let continueEnable = self.statusIDs.count > 0
2021-04-19 14:34:08 +02:00
self.continueEnableSubject.send(continueEnable)
}
.store(in: &disposeBag)
input.comment.sink { [weak self] (comment) in
guard let self = self else { return }
2021-04-26 11:41:24 +02:00
self.comment = comment
2021-04-19 14:34:08 +02:00
let sendEnable = (comment?.length ?? 0) > 0
self.sendEnableSubject.send(sendEnable)
}
.store(in: &disposeBag)
}
func bindForStep1(input: Input) {
let skip = input.step1Skip.map { [weak self] value -> Void in
guard let self = self else { return value }
2021-04-25 09:48:37 +02:00
self.reportQuery.statusIDs?.removeAll()
2021-04-19 14:34:08 +02:00
return value
}
let step1Continue = input.step1Continue.map { [weak self] value -> Void in
guard let self = self else { return value }
self.reportQuery.statusIDs = self.statusIDs
return value
}
Publishers.Merge(skip, step1Continue)
2021-04-19 14:34:08 +02:00
.sink { [weak self] _ in
self?.currentStep.value = .two
self?.sendEnableSubject.send(false)
}
.store(in: &disposeBag)
}
func bindForStep2(input: Input, domain: String, activeMastodonAuthenticationBox: MastodonAuthenticationBox) -> AnyPublisher<(Bool, Error?), Never> {
2021-04-19 14:34:08 +02:00
let skip = input.step2Skip.map { [weak self] value -> Void in
guard let self = self else { return value }
self.reportQuery.comment = nil
2021-04-19 14:34:08 +02:00
return value
}
let step2Continue = input.step2Continue.map { [weak self] value -> Void in
guard let self = self else { return value }
self.reportQuery.comment = self.comment
return value
}
return Publishers.Merge(skip, step2Continue)
.flatMap { [weak self] (_) -> AnyPublisher<(Bool, Error?), Never> in
guard let self = self else {
return Empty(completeImmediately: true).eraseToAnyPublisher()
}
return self.context.apiService.report(
2021-04-19 14:34:08 +02:00
domain: domain,
query: self.reportQuery,
mastodonAuthenticationBox: activeMastodonAuthenticationBox
)
.map({ (content) -> (Bool, Error?) in
return (true, nil)
})
.eraseToAnyPublisher()
.tryCatch({ (error) -> AnyPublisher<(Bool, Error?), Never> in
return Just((false, error)).eraseToAnyPublisher()
})
// to covert to AnyPublisher<(Bool, Error?), Never>
.replaceError(with: (false, nil))
.eraseToAnyPublisher()
2021-04-19 14:34:08 +02:00
}
.eraseToAnyPublisher()
2021-04-19 14:34:08 +02:00
}
func append(statusID: Mastodon.Entity.Status.ID) {
guard self.statusIDs.contains(statusID) != true else { return }
self.statusIDs.append(statusID)
}
func remove(statusID: String) {
guard let index = self.statusIDs.firstIndex(of: statusID) else { return }
self.statusIDs.remove(at: index)
}
2021-04-19 14:34:08 +02:00
}