mastodon-ios/Mastodon/Scene/Report/ReportResult/ReportResultViewController....

171 lines
6.2 KiB
Swift
Raw Normal View History

2022-02-08 05:36:06 +01:00
//
// ReportResultViewController.swift
// Mastodon
//
// Created by MainasuK on 2022-2-8.
//
import os.log
import UIKit
2022-05-11 10:09:52 +02:00
import SwiftUI
2022-02-08 05:36:06 +01:00
import Combine
import MastodonAsset
2022-10-08 07:43:06 +02:00
import MastodonCore
2022-02-08 05:36:06 +01:00
import MastodonLocalization
final class ReportResultViewController: UIViewController, NeedsDependency, ReportViewControllerAppearance {
var disposeBag = Set<AnyCancellable>()
private var observations = Set<NSKeyValueObservation>()
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
var viewModel: ReportResultViewModel!
2022-05-11 10:09:52 +02:00
private(set) lazy var reportResultView = ReportResultView(viewModel: viewModel)
lazy var doneBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .done,
target: self,
action: #selector(ReportResultViewController.doneBarButtonItemDidPressed(_:))
)
2022-02-08 05:36:06 +01:00
let navigationActionView: NavigationActionView = {
let navigationActionView = NavigationActionView()
2022-02-15 11:15:58 +01:00
navigationActionView.backgroundColor = Asset.Scene.Onboarding.background.color
2022-02-08 05:36:06 +01:00
navigationActionView.hidesBackButton = true
navigationActionView.nextButton.setTitle(L10n.Common.Controls.Actions.done, for: .normal)
return navigationActionView
}()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension ReportResultViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupAppearance()
defer { setupNavigationBarBackgroundView() }
navigationItem.hidesBackButton = true
navigationItem.rightBarButtonItem = doneBarButtonItem
2022-02-08 05:36:06 +01:00
2022-05-11 10:09:52 +02:00
let hostingViewController = UIHostingController(rootView: reportResultView)
hostingViewController.view.preservesSuperviewLayoutMargins = true
addChild(hostingViewController)
hostingViewController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(hostingViewController.view)
hostingViewController.view.pinToParent()
2022-02-08 05:36:06 +01:00
navigationActionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(navigationActionView)
defer {
view.bringSubviewToFront(navigationActionView)
}
NSLayoutConstraint.activate([
navigationActionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
navigationActionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
view.bottomAnchor.constraint(equalTo: navigationActionView.bottomAnchor),
])
navigationActionView
.observe(\.bounds, options: [.initial, .new]) { [weak self] navigationActionView, _ in
guard let self = self else { return }
let inset = navigationActionView.frame.height
2022-05-11 10:09:52 +02:00
self.viewModel.bottomPaddingHeight = inset
2022-02-08 05:36:06 +01:00
}
.store(in: &observations)
navigationActionView.nextButton.addTarget(self, action: #selector(ReportSupplementaryViewController.nextButtonDidPressed(_:)), for: .touchUpInside)
2022-05-11 10:09:52 +02:00
viewModel.followActionPublisher
.throttle(for: 0.3, scheduler: DispatchQueue.main, latest: false)
.sink { [weak self] in
guard let self = self else { return }
Task { @MainActor in
guard !self.viewModel.isRequestFollow else { return }
self.viewModel.isRequestFollow = true
do {
try await DataSourceFacade.responseToUserFollowAction(
dependency: self,
user: self.viewModel.user
2022-05-11 10:09:52 +02:00
)
} catch {
// handle error
}
self.viewModel.isRequestFollow = false
} // end Task
}
.store(in: &disposeBag)
viewModel.muteActionPublisher
.throttle(for: 0.3, scheduler: DispatchQueue.main, latest: false)
.sink { [weak self] in
guard let self = self else { return }
Task { @MainActor in
guard !self.viewModel.isRequestMute else { return }
self.viewModel.isRequestMute = true
do {
try await DataSourceFacade.responseToUserMuteAction(
dependency: self,
user: self.viewModel.user
2022-05-11 10:09:52 +02:00
)
} catch {
// handle error
}
self.viewModel.isRequestMute = false
} // end Task
}
.store(in: &disposeBag)
viewModel.blockActionPublisher
.throttle(for: 0.3, scheduler: DispatchQueue.main, latest: false)
.sink { [weak self] in
guard let self = self else { return }
Task { @MainActor in
guard !self.viewModel.isRequestBlock else { return }
self.viewModel.isRequestBlock = true
do {
try await DataSourceFacade.responseToUserBlockAction(
dependency: self,
user: self.viewModel.user
2022-05-11 10:09:52 +02:00
)
} catch {
// handle error
}
self.viewModel.isRequestBlock = false
} // end Task
}
.store(in: &disposeBag)
2022-02-08 05:36:06 +01:00
}
}
extension ReportResultViewController {
@objc func doneBarButtonItemDidPressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
2022-02-08 05:36:06 +01:00
@objc func nextButtonDidPressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
// MARK: - AuthContextProvider
extension ReportResultViewController: AuthContextProvider {
var authContext: AuthContext { viewModel.authContext }
}
// MARK: - PanPopableViewController
extension ReportResultViewController: PanPopableViewController {
var isPanPopable: Bool { false }
}