Tusker/Tusker/Screens/Mute/MuteAccountView.swift

162 lines
5.5 KiB
Swift

//
// MuteAccountView.swift
// Tusker
//
// Created by Shadowfacts on 11/11/22.
// Copyright © 2022 Shadowfacts. All rights reserved.
//
import SwiftUI
import Pachyderm
struct MuteAccountView: View {
private static let durationOptions: [MenuPicker<TimeInterval>.Option] = {
let f = DateComponentsFormatter()
f.maximumUnitCount = 1
f.unitsStyle = .full
f.allowedUnits = [.weekOfMonth, .day, .hour, .minute]
let durations: [TimeInterval] = [
30 * 60,
60 * 60,
6 * 60 * 60,
24 * 60 * 60,
3 * 24 * 60 * 60,
7 * 24 * 60 * 60,
]
return [
.init(value: 0, title: "Forever")
] + durations.map { .init(value: $0, title: f.string(from: $0)!) }
}()
let account: AccountMO
let mastodonController: MastodonController
@Environment(\.dismiss) private var dismiss: DismissAction
@ObservedObject private var preferences = Preferences.shared
@State private var muteNotifications = true
@State private var duration: TimeInterval = 0
@State private var isMuting = false
@State private var error: Error?
var body: some View {
if #available(iOS 16.0, *) {
NavigationStack {
navigationViewContent
.scrollContentBackground(.hidden)
.background(Color.appGroupedBackground)
}
} else {
NavigationView {
navigationViewContent
.onAppear {
UITableView.appearance(whenContainedInInstancesOf: [UIHostingController<MuteAccountView>.self]).backgroundColor = .appGroupedBackground
}
}
.navigationViewStyle(.stack)
}
}
private var navigationViewContent: some View {
Form {
Section {
HStack {
ComposeAvatarImageView(url: account.avatar)
.frame(width: 50, height: 50)
.cornerRadius(preferences.avatarStyle.cornerRadiusFraction * 50)
VStack(alignment: .leading) {
AccountDisplayNameLabel(account: account, textStyle: .headline, emojiSize: 17)
Text("@\(account.acct)")
.fontWeight(.light)
.foregroundColor(.secondary)
}
}
.frame(height: 50)
.listRowBackground(EmptyView())
// vertical insets are so the rounded corners fo the section don't affect the avatar
.listRowInsets(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0))
}
.accessibilityHidden(true)
Section {
Toggle(isOn: $muteNotifications) {
Text("Hide notifications from this person")
}
} footer: {
if muteNotifications {
Text("This user's posts and notifications will be hidden.")
} else {
Text("This user's posts will be hidden from your timeline. You can still receive notifications from them.")
}
}
.listRowBackground(Color.appGroupedCellBackground)
Section {
Picker(selection: $duration) {
ForEach(MuteAccountView.durationOptions, id: \.value) { option in
Text(option.title).tag(option.value)
}
} label: {
Text("Duration")
}
} footer: {
if duration != 0 {
Text("The mute will automatically be removed after the selected time.")
}
}
.listRowBackground(Color.appGroupedCellBackground)
Button(action: self.mute) {
if isMuting {
HStack {
Text("Muting User")
Spacer()
ProgressView()
.progressViewStyle(.circular)
}
} else {
Text("Mute User")
}
}
.disabled(isMuting)
.listRowBackground(Color.appGroupedCellBackground)
}
.alertWithData("Erorr Muting", data: $error, actions: { error in
Button("OK") {}
}, message: { error in
Text(error.localizedDescription)
})
.navigationTitle("Mute \(account.displayOrUserName)")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
self.dismiss()
}
}
}
}
private func mute() {
isMuting = true
let req = Account.mute(account.id, notifications: muteNotifications)
Task {
do {
let (relationship, _) = try await mastodonController.run(req)
mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
self.dismiss()
} catch {
self.error = error
isMuting = false
}
}
}
}
//struct MuteAccountView_Previews: PreviewProvider {
// static var previews: some View {
// MuteAccountView()
// }
//}