Impressia/Vernissage/Views/ReportView.swift

107 lines
3.9 KiB
Swift
Raw Normal View History

2023-04-04 09:14:07 +02:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the Apache License 2.0.
//
import SwiftUI
import PixelfedKit
2023-04-07 14:20:12 +02:00
import ClientKit
2023-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import WidgetsKit
2023-04-04 09:14:07 +02:00
2023-10-19 13:24:02 +02:00
@MainActor
2023-04-04 09:14:07 +02:00
struct ReportView: View {
2023-10-19 13:24:02 +02:00
@Environment(Client.self) var client
2023-04-04 09:14:07 +02:00
@Environment(\.dismiss) private var dismiss
@State private var publishDisabled = false
@State private var reportType = Report.ReportType.sensitive
private let objectType: Report.ObjectType
private let objectId: String
private let reportTypes: [(reportType: Report.ReportType, name: LocalizedStringKey)] = [
(Report.ReportType.spam, "report.title.spam"),
(Report.ReportType.sensitive, "report.title.sensitive"),
(Report.ReportType.abusive, "report.title.abusive"),
(Report.ReportType.underage, "report.title.underage"),
(Report.ReportType.violence, "report.title.violence"),
(Report.ReportType.copyright, "report.title.copyright"),
(Report.ReportType.impersonation, "report.title.impersonation"),
(Report.ReportType.scam, "report.title.scam"),
(Report.ReportType.terrorism, "report.title.terrorism")
]
init(objectType: Report.ObjectType, objectId: String) {
self.objectType = objectType
self.objectId = objectId
}
var body: some View {
NavigationView {
Form {
Section("report.title.reportType") {
ForEach(self.reportTypes, id: \.reportType) { item in
Button {
self.reportType = item.reportType
} label: {
HStack(alignment: .center) {
Text(item.name, comment: "Report type")
.font(.subheadline)
.foregroundColor(.mainTextColor)
Spacer()
if self.reportType == item.reportType {
Image(systemName: "checkmark")
}
}
}
}
}
}
.frame(alignment: .topLeading)
.toolbar {
ToolbarItem(placement: .primaryAction) {
ActionButton {
await onSendReport()
} label: {
Text("report.title.send", comment: "Send")
}
.disabled(self.publishDisabled)
.buttonStyle(.borderedProminent)
}
ToolbarItem(placement: .cancellationAction) {
Button(NSLocalizedString("report.title.close", comment: "Close"), role: .cancel) {
self.dismiss()
}
}
}
.navigationTitle("report.navigationBar.title")
.navigationBarTitleDisplayMode(.inline)
}
}
private func onSendReport() async {
do {
if try await self.client.reports?.report(objectType: self.objectType,
objectId: self.objectId,
reportType: self.reportType) != nil {
switch self.objectType {
case .post:
ToastrService.shared.showSuccess("report.title.postReported", imageSystemName: "exclamationmark.triangle")
case .user:
ToastrService.shared.showSuccess("report.title.userReported", imageSystemName: "exclamationmark.triangle")
}
self.dismiss()
}
} catch {
ErrorService.shared.handle(error, message: "report.error.notReported", showToastr: true)
}
}
}