2024-06-19 15:15:27 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2024-10-17 17:20:03 +00:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-19 15:15:27 +00:00
|
|
|
struct SettingsScreen: View {
|
2024-10-04 15:58:04 +00:00
|
|
|
@EnvironmentObject var clientsStore: ClientsStore
|
2024-10-07 15:21:26 +00:00
|
|
|
@Environment(\.router) var router
|
2024-10-17 17:20:03 +00:00
|
|
|
@Environment(\.settingsParent) var parent
|
2024-10-04 15:58:04 +00:00
|
|
|
|
2024-06-19 15:15:27 +00:00
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
2024-10-07 12:54:07 +00:00
|
|
|
// Background color
|
2024-07-04 08:21:12 +00:00
|
|
|
Color.Material.Background.light
|
2024-06-19 15:15:27 +00:00
|
|
|
.ignoresSafeArea()
|
|
|
|
|
2024-10-07 12:54:07 +00:00
|
|
|
// Content
|
|
|
|
VStack(spacing: 0) {
|
|
|
|
// Header
|
2024-10-17 17:20:03 +00:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
}
|
2024-10-07 12:54:07 +00:00
|
|
|
|
|
|
|
// List
|
|
|
|
List {
|
|
|
|
// Accounts section
|
|
|
|
SharedSectionTitle(text: L10n.Settings.Section.Accounts.title)
|
|
|
|
|
2024-10-15 17:47:51 +00:00
|
|
|
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 {
|
|
|
|
try? await client.credentials.setActive(flag: new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
)
|
|
|
|
.onTapGesture {
|
|
|
|
print("Tapped account \(client.credentials.bareJid)")
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 12:54:07 +00:00
|
|
|
|
2024-10-18 17:37:49 +00:00
|
|
|
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
|
|
|
|
}
|
2024-10-07 15:21:26 +00:00
|
|
|
}
|
2024-10-07 12:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dev section
|
|
|
|
#if DEBUG
|
|
|
|
SharedSectionTitle(text: "Dev tools")
|
|
|
|
|
|
|
|
SharedListRow(
|
2024-10-07 15:09:55 +00:00
|
|
|
iconType: .image(Image(systemName: "xmark.octagon"), .Rainbow.red500),
|
2024-10-07 12:54:07 +00:00
|
|
|
text: "Clean all data",
|
|
|
|
controlType: .none
|
|
|
|
)
|
|
|
|
.onTapGesture {
|
2024-10-07 16:16:00 +00:00
|
|
|
router.showAlert(
|
|
|
|
.alert,
|
|
|
|
title: "Delete data",
|
|
|
|
subtitle: "Delete all data from the app?"
|
|
|
|
) {
|
|
|
|
Button("Delete", role: .destructive) {
|
|
|
|
clientsStore.flushAllData()
|
|
|
|
Database.shared.flushAllData()
|
2024-10-07 16:58:02 +00:00
|
|
|
UserSettings.reset()
|
2024-10-07 16:16:00 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-07 12:54:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
2024-10-04 15:58:04 +00:00
|
|
|
}
|
2024-10-07 12:54:07 +00:00
|
|
|
.listStyle(.plain)
|
|
|
|
.environment(\.defaultMinListRowHeight, 10)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
2024-06-19 15:15:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-07 15:21:26 +00:00
|
|
|
|
|
|
|
@ViewBuilder private var addAccountSelector: some View {
|
|
|
|
Button {
|
2024-10-17 17:41:57 +00:00
|
|
|
router.showScreen(.push) { _ in
|
|
|
|
LoginScreen()
|
|
|
|
.navigationBarHidden(true)
|
|
|
|
}
|
2024-10-07 15:21:26 +00:00
|
|
|
} label: {
|
|
|
|
Text(L10n.Settings.Section.Accounts.addExists)
|
|
|
|
}
|
|
|
|
|
|
|
|
Button {
|
2024-10-17 17:41:57 +00:00
|
|
|
router.showScreen(.push) { _ in
|
|
|
|
RegistrationScreen()
|
|
|
|
.navigationBarHidden(true)
|
|
|
|
}
|
2024-10-07 15:21:26 +00:00
|
|
|
} label: {
|
|
|
|
Text(L10n.Settings.Section.Accounts.addNew)
|
|
|
|
}
|
|
|
|
|
|
|
|
Button(role: .cancel) {} label: {
|
|
|
|
Text(L10n.Global.cancel)
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 15:15:27 +00:00
|
|
|
}
|