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
|
|
|
|
|
2021-04-25 09:36:40 +02:00
|
|
|
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
|
|
|
|
2021-04-25 12:45:17 +02:00
|
|
|
var statusIDs = [Mastodon.Entity.Status.ID]()
|
|
|
|
var comment: String?
|
|
|
|
|
2021-04-22 17:02:24 +02:00
|
|
|
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>
|
2021-04-25 09:36:40 +02:00
|
|
|
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)
|
2021-04-25 09:36:40 +02:00
|
|
|
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(),
|
2021-04-25 09:36:40 +02:00
|
|
|
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 }
|
2021-04-23 03:37:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-23 03:37:18 +02:00
|
|
|
attribute.isSelected = !attribute.isSelected
|
|
|
|
if attribute.isSelected {
|
2021-04-25 12:45:17 +02:00
|
|
|
self.append(statusID: status.id)
|
2021-04-19 14:34:08 +02:00
|
|
|
} else {
|
2021-04-25 12:45:17 +02:00
|
|
|
self.remove(statusID: status.id)
|
2021-04-19 14:34:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-25 12:45:17 +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
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:45:17 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-20 10:40:04 +02:00
|
|
|
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 }
|
2021-04-23 03:37:18 +02:00
|
|
|
self.reportQuery.comment = nil
|
2021-04-19 14:34:08 +02:00
|
|
|
return value
|
|
|
|
}
|
2021-04-25 12:45:17 +02:00
|
|
|
|
|
|
|
let step2Continue = input.step2Continue.map { [weak self] value -> Void in
|
|
|
|
guard let self = self else { return value }
|
|
|
|
self.reportQuery.comment = self.comment
|
|
|
|
return value
|
|
|
|
}
|
2021-04-25 09:36:40 +02:00
|
|
|
|
2021-04-25 12:45:17 +02:00
|
|
|
return Publishers.Merge(skip, step2Continue)
|
2021-04-25 09:36:40 +02:00
|
|
|
.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
|
|
|
|
)
|
2021-04-25 09:36:40 +02:00
|
|
|
.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
|
|
|
}
|
2021-04-25 09:36:40 +02:00
|
|
|
.eraseToAnyPublisher()
|
2021-04-19 14:34:08 +02:00
|
|
|
}
|
2021-04-25 12:45:17 +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
|
|
|
}
|