metatext/ViewModels/Sources/ViewModels/View Models/ReportViewModel.swift

74 lines
2.4 KiB
Swift
Raw Normal View History

2020-11-30 02:54:11 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
import ServiceLayer
2021-03-03 05:02:07 +00:00
public final class ReportViewModel: CollectionItemsViewModel {
2020-11-30 02:54:11 +00:00
@Published public var elements: ReportElements
2021-03-03 05:02:07 +00:00
@Published public private(set) var reportingState = ReportingState.composing
2020-11-30 02:54:11 +00:00
private let accountService: AccountService
private var cancellables = Set<AnyCancellable>()
2021-03-03 05:02:07 +00:00
public init(accountService: AccountService, statusId: Status.Id? = nil, identityContext: IdentityContext) {
2020-11-30 02:54:11 +00:00
self.accountService = accountService
elements = ReportElements(accountId: accountService.account.id)
2021-03-03 05:02:07 +00: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 02:54:11 +00:00
}
}
2021-03-03 05:02:07 +00: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 02:54:11 +00:00
}
public extension ReportViewModel {
2021-03-03 05:02:07 +00:00
enum ReportingState {
case composing
case reporting
case done
2020-11-30 02:54:11 +00:00
}
var accountName: String { "@".appending(accountService.account.acct) }
var accountHost: String {
2021-01-31 21:59:26 +00:00
URL(string: accountService.account.url)?.host ?? ""
2020-11-30 02:54:11 +00:00
}
var isLocalAccount: Bool { accountService.isLocal }
func report() {
accountService.report(elements)
.receive(on: DispatchQueue.main)
2021-03-03 05:02:07 +00:00
.handleEvents(receiveSubscription: { [weak self] _ in self?.reportingState = .reporting })
2020-11-30 02:54:11 +00:00
.sink { [weak self] in
guard let self = self else { return }
2021-03-03 05:02:07 +00:00
switch $0 {
case .finished:
self.reportingState = .done
case let .failure(error):
self.alertItem = AlertItem(error: error)
self.reportingState = .composing
2020-11-30 02:54:11 +00:00
}
} receiveValue: { _ in }
.store(in: &cancellables)
}
}