mastodon-ios/Mastodon/Scene/Report/ReportStatus/ReportStatusViewController....

213 lines
8.0 KiB
Swift
Raw Normal View History

2022-02-08 05:36:06 +01:00
//
// ReportStatusViewController.swift
2022-02-08 05:36:06 +01:00
// Mastodon
//
// Created by MainasuK on 2022-5-10.
2022-02-08 05:36:06 +01:00
//
import os.log
import UIKit
import Combine
import CoreDataStack
import MastodonAsset
2022-10-08 07:43:06 +02:00
import MastodonCore
2022-02-08 05:36:06 +01:00
import MastodonLocalization
protocol ReportStatusViewControllerDelegate: AnyObject {
func reportStatusViewController(_ viewController: ReportStatusViewController, skipButtonDidPressed button: UIButton)
func reportStatusViewController(_ viewController: ReportStatusViewController, nextButtonDidPressed button: UIButton)
}
class ReportStatusViewController: UIViewController, NeedsDependency, ReportViewControllerAppearance {
let logger = Logger(subsystem: "ReportStatusViewController", category: "ViewController")
2022-02-08 05:36:06 +01:00
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: ReportStatusViewModel!
2022-02-08 05:36:06 +01:00
// MAKK: - UI
2022-02-08 05:36:06 +01:00
lazy var cancelBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .cancel,
target: self,
action: #selector(ReportStatusViewController.cancelBarButtonItemDidPressed(_:))
2022-02-08 05:36:06 +01:00
)
let tableView: UITableView = {
let tableView = ControlContainableTableView()
tableView.backgroundColor = Asset.Scene.Report.background.color
tableView.rowHeight = UITableView.automaticDimension
tableView.separatorStyle = .none
tableView.backgroundColor = .clear
tableView.keyboardDismissMode = .onDrag
tableView.allowsMultipleSelection = true
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = .leastNonzeroMagnitude
} else {
// Fallback on earlier versions
}
return tableView
}()
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.backButton.setTitle(L10n.Common.Controls.Actions.skip, for: .normal)
return navigationActionView
}()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension ReportStatusViewController {
2022-02-08 05:36:06 +01:00
override func viewDidLoad() {
super.viewDidLoad()
setupAppearance()
defer { setupNavigationBarBackgroundView() }
navigationItem.rightBarButtonItem = cancelBarButtonItem
2022-02-08 05:36:06 +01:00
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
tableView.delegate = self
viewModel.setupDiffableDataSource(
tableView: tableView
)
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
self.tableView.contentInset.bottom = inset
self.tableView.verticalScrollIndicatorInsets.bottom = inset
}
.store(in: &observations)
// setup batch fetch
viewModel.listBatchFetchViewModel.setup(scrollView: tableView)
viewModel.listBatchFetchViewModel.shouldFetch
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
guard self.view.window != nil else { return }
self.viewModel.stateMachine.enter(ReportStatusViewModel.State.Loading.self)
2022-02-08 05:36:06 +01:00
}
.store(in: &disposeBag)
viewModel.$isNextButtonEnabled
.receive(on: DispatchQueue.main)
.assign(to: \.isEnabled, on: navigationActionView.nextButton)
.store(in: &disposeBag)
navigationActionView.backButton.addTarget(self, action: #selector(ReportStatusViewController.skipButtonDidPressed(_:)), for: .touchUpInside)
navigationActionView.nextButton.addTarget(self, action: #selector(ReportStatusViewController.nextButtonDidPressed(_:)), for: .touchUpInside)
2022-02-08 05:36:06 +01:00
}
}
extension ReportStatusViewController {
2022-02-08 05:36:06 +01:00
@objc private func cancelBarButtonItemDidPressed(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
@objc private func skipButtonDidPressed(_ sender: UIButton) {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
2022-02-08 05:36:06 +01:00
assert(viewModel.delegate != nil)
viewModel.isSkip = true
viewModel.delegate?.reportStatusViewController(self, skipButtonDidPressed: sender)
2022-02-08 05:36:06 +01:00
}
@objc private func nextButtonDidPressed(_ sender: UIButton) {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
2022-02-08 05:36:06 +01:00
assert(viewModel.delegate != nil)
viewModel.isSkip = false
viewModel.delegate?.reportStatusViewController(self, nextButtonDidPressed: sender)
2022-02-08 05:36:06 +01:00
}
}
// MARK: - UITableViewDelegate
extension ReportStatusViewController: UITableViewDelegate {
2022-02-08 05:36:06 +01:00
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
guard let item = viewModel.diffableDataSource?.itemIdentifier(for: indexPath),
case .status = item
else {
return nil
}
return indexPath
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = viewModel.diffableDataSource?.itemIdentifier(for: indexPath),
case let .status(record) = item
else {
tableView.deselectRow(at: indexPath, animated: true)
return
}
viewModel.selectStatuses.append(record)
}
func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? {
guard let item = viewModel.diffableDataSource?.itemIdentifier(for: indexPath),
case let .status(record) = item
else {
return nil
}
// disallow deselect initial selection
guard record != viewModel.status else { return nil }
return indexPath
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
guard let item = viewModel.diffableDataSource?.itemIdentifier(for: indexPath),
case let .status(record) = item
else {
return
}
viewModel.selectStatuses.remove(record)
}
}
// MARK: - UIAdaptivePresentationControllerDelegate
extension ReportStatusViewController: UIAdaptivePresentationControllerDelegate {
2022-02-08 05:36:06 +01:00
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
return false
}
}