another.im-ios/ConversationsClassic/View/Main/Settings/SettingsScreen.swift
2024-10-21 17:26:36 +02:00

142 lines
4.8 KiB
Swift

import SwiftUI
enum SettingsScreenParent {
case main
case welcome
}
struct SettingsScreenParentKey: EnvironmentKey {
static let defaultValue = SettingsScreenParent.main
}
extension EnvironmentValues {
var settingsParent: SettingsScreenParent {
get { self[SettingsScreenParentKey.self] }
set { self[SettingsScreenParentKey.self] = newValue }
}
}
struct SettingsScreen: View {
@EnvironmentObject var clientsStore: ClientsStore
@Environment(\.router) var router
@Environment(\.settingsParent) var parent
var body: some View {
ZStack {
// Background color
Color.Material.Background.light
.ignoresSafeArea()
// Content
VStack(spacing: 0) {
// Header
if parent == .main {
SharedNavigationBar(
centerText: .init(text: L10n.Settings.Main.title)
)
} else {
SharedNavigationBar(
leftButton: .init(
image: Image(systemName: "chevron.left"),
action: {
router.dismissScreen()
}
),
centerText: .init(text: L10n.Settings.Main.title)
)
}
// List
List {
// Accounts section
SharedSectionTitle(text: L10n.Settings.Section.Accounts.title)
ForEach(clientsStore.clients) { client in
SharedListRow(
iconType: .charCircle(client.credentials.bareJid),
text: client.credentials.bareJid,
controlType: .switcher(isOn: Binding(
get: { client.credentials.isActive },
set: { new in
Task {
await client.updActivity(new)
}
}
))
)
}
if parent == .main {
SharedListRow(
iconType: .image(Image(systemName: "plus"), .Material.Elements.active),
text: L10n.Settings.Section.Accounts.add,
controlType: .none
)
.onTapGesture {
router.showAlert(
.confirmationDialog,
title: L10n.Settings.Section.Accounts.add,
subtitle: L10n.Settings.Section.Accounts.addHint
) {
addAccountSelector
}
}
}
// Dev section
#if DEBUG
SharedSectionTitle(text: "Dev tools")
SharedListRow(
iconType: .image(Image(systemName: "xmark.octagon"), .Rainbow.red500),
text: "Clean all data",
controlType: .none
)
.onTapGesture {
router.showAlert(
.alert,
title: "Delete data",
subtitle: "Delete all data from the app?"
) {
Button("Delete", role: .destructive) {
clientsStore.flushAllData()
Database.shared.flushAllData()
UserSettings.reset()
}
}
}
#endif
}
.listStyle(.plain)
.environment(\.defaultMinListRowHeight, 10)
Spacer()
}
}
}
@ViewBuilder private var addAccountSelector: some View {
Button {
router.showScreen(.push) { _ in
LoginScreen()
.navigationBarHidden(true)
}
} label: {
Text(L10n.Settings.Section.Accounts.addExists)
}
Button {
router.showScreen(.push) { _ in
RegistrationScreen()
.navigationBarHidden(true)
}
} label: {
Text(L10n.Settings.Section.Accounts.addNew)
}
Button(role: .cancel) {} label: {
Text(L10n.Global.cancel)
}
}
}