2020-11-30 03:54:11 +01:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import Mastodon
|
|
|
|
import ServiceLayer
|
|
|
|
|
2021-03-03 06:02:07 +01:00
|
|
|
public final class ReportViewModel: CollectionItemsViewModel {
|
2020-11-30 03:54:11 +01:00
|
|
|
@Published public var elements: ReportElements
|
2021-03-03 06:02:07 +01:00
|
|
|
@Published public private(set) var reportingState = ReportingState.composing
|
2020-11-30 03:54:11 +01:00
|
|
|
|
|
|
|
private let accountService: AccountService
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-03-03 06:02:07 +01:00
|
|
|
public init(accountService: AccountService, statusId: Status.Id? = nil, identityContext: IdentityContext) {
|
2020-11-30 03:54:11 +01:00
|
|
|
self.accountService = accountService
|
|
|
|
elements = ReportElements(accountId: accountService.account.id)
|
|
|
|
|
2021-03-03 06:02:07 +01:00
|
|
|
super.init(
|
|
|
|
collectionService: identityContext.service.navigationService.timelineService(
|
|
|
|
timeline: .profile(accountId: accountService.account.id, profileCollection: .statusesAndReplies)),
|
|
|
|
identityContext: identityContext)
|
|
|
|
|
|
|
|
if let statusId = statusId {
|
|
|
|
elements.statusIds.insert(statusId)
|
2020-11-30 03:54:11 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-03 06:02:07 +01:00
|
|
|
|
|
|
|
public override func viewModel(indexPath: IndexPath) -> Any {
|
|
|
|
let viewModel = super.viewModel(indexPath: indexPath)
|
|
|
|
|
|
|
|
if let statusViewModel = viewModel as? StatusViewModel {
|
|
|
|
statusViewModel.showReportSelectionToggle = true
|
|
|
|
statusViewModel.selectedForReport = elements.statusIds.contains(statusViewModel.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return viewModel
|
|
|
|
}
|
2020-11-30 03:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public extension ReportViewModel {
|
2021-03-03 06:02:07 +01:00
|
|
|
enum ReportingState {
|
|
|
|
case composing
|
|
|
|
case reporting
|
|
|
|
case done
|
2020-11-30 03:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var accountName: String { "@".appending(accountService.account.acct) }
|
|
|
|
|
|
|
|
var accountHost: String {
|
2021-01-31 22:59:26 +01:00
|
|
|
URL(string: accountService.account.url)?.host ?? ""
|
2020-11-30 03:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var isLocalAccount: Bool { accountService.isLocal }
|
|
|
|
|
|
|
|
func report() {
|
|
|
|
accountService.report(elements)
|
|
|
|
.receive(on: DispatchQueue.main)
|
2021-03-03 06:02:07 +01:00
|
|
|
.handleEvents(receiveSubscription: { [weak self] _ in self?.reportingState = .reporting })
|
2020-11-30 03:54:11 +01:00
|
|
|
.sink { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
2021-03-03 06:02:07 +01:00
|
|
|
switch $0 {
|
|
|
|
case .finished:
|
|
|
|
self.reportingState = .done
|
|
|
|
case let .failure(error):
|
|
|
|
self.alertItem = AlertItem(error: error)
|
|
|
|
self.reportingState = .composing
|
2020-11-30 03:54:11 +01:00
|
|
|
}
|
|
|
|
} receiveValue: { _ in }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
}
|