metatext/Views/SwiftUI/ReportView.swift

100 lines
2.9 KiB
Swift
Raw Normal View History

2020-11-30 02:54:11 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
import ViewModels
struct ReportView: View {
@StateObject var viewModel: ReportViewModel
@Environment(\.presentationMode) private var presentationMode
fileprivate var dismissHostingController: (() -> Void)?
var body: some View {
Form {
if let statusViewModel = viewModel.statusViewModel {
Section {
ReportStatusView(viewModel: statusViewModel)
.frame(height: Self.statusHeight)
}
}
Section {
Text("report.hint")
2021-02-15 22:14:55 +00:00
}
Section(header: Text("report.additional-comments")) {
TextEditor(text: $viewModel.elements.comment)
.accessibility(label: Text("report.additional-comments"))
}
if !viewModel.isLocalAccount {
Section {
2020-11-30 02:54:11 +00:00
VStack(alignment: .leading) {
Text("report.forward.hint")
Toggle("report.forward-\(viewModel.accountHost)", isOn: $viewModel.elements.forward)
}
}
2021-02-15 22:14:55 +00:00
}
Section {
if viewModel.loading {
ProgressView()
} else {
Button("report.target-\(viewModel.accountName)") {
viewModel.report()
2020-11-30 02:54:11 +00:00
}
}
}
}
.alertItem($viewModel.alertItem)
.onReceive(viewModel.events) {
switch $0 {
case .reported:
dismiss()
}
}
.navigationTitle("report.target-\(viewModel.accountName)")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
2021-02-10 23:41:41 +00:00
Button("cancel") {
2020-11-30 02:54:11 +00:00
dismiss()
}
}
}
.navigationBarTitleDisplayMode(.inline)
}
}
private extension ReportView {
static let statusHeight: CGFloat = 100
func dismiss() {
if let dismissHostingController = dismissHostingController {
dismissHostingController()
} else {
presentationMode.wrappedValue.dismiss()
}
}
}
2021-02-10 23:41:41 +00:00
final class ReportViewController: UIHostingController<ReportView> {
2020-11-30 02:54:11 +00:00
init(viewModel: ReportViewModel) {
super.init(rootView: ReportView(viewModel: viewModel))
rootView.dismissHostingController = { [weak self] in self?.dismiss(animated: true) }
}
@available(*, unavailable)
@objc required dynamic init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
#if DEBUG
import PreviewViewModels
struct ReportView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
ReportView(viewModel: .preview)
}
}
}
#endif