2021-09-25 13:48:22 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
2021-09-29 15:44:43 +05:30
|
|
|
struct AccountsSettingsView: View {
|
2021-09-25 13:48:22 +05:30
|
|
|
let instanceID: Instance.ID?
|
|
|
|
|
|
|
|
@State private var accountsChanged = false
|
|
|
|
@State private var presentingAccountForm = false
|
|
|
|
|
|
|
|
@EnvironmentObject<InstancesModel> private var instances
|
|
|
|
|
|
|
|
var instance: Instance! {
|
|
|
|
instances.find(instanceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
List {
|
2021-09-29 15:44:43 +05:30
|
|
|
Section(header: Text("Accounts"), footer: sectionFooter) {
|
2021-09-28 23:36:05 +05:30
|
|
|
ForEach(instances.accounts(instanceID), id: \.self) { account in
|
2021-09-29 15:44:43 +05:30
|
|
|
#if os(iOS)
|
2021-09-28 23:36:05 +05:30
|
|
|
HStack(spacing: 2) {
|
|
|
|
Text(account.description)
|
|
|
|
if instances.defaultAccount == account {
|
|
|
|
Text("— default")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 03:33:33 +05:30
|
|
|
.swipeActions(edge: .leading, allowsFullSwipe: true) {
|
|
|
|
if instances.defaultAccount != account {
|
2021-09-29 02:03:12 +05:30
|
|
|
Button("Make Default") { makeDefault(account) }
|
2021-09-27 03:33:33 +05:30
|
|
|
} else {
|
|
|
|
Button("Reset Default", action: resetDefaultAccount)
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-27 03:33:33 +05:30
|
|
|
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
2021-09-29 02:03:12 +05:30
|
|
|
Button("Remove", role: .destructive) { removeAccount(account) }
|
2021-09-27 03:33:33 +05:30
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
|
|
|
|
#else
|
|
|
|
Button(action: { toggleDefault(account) }) {
|
|
|
|
HStack(spacing: 2) {
|
|
|
|
Text(account.description)
|
|
|
|
if instances.defaultAccount == account {
|
|
|
|
Text("— default")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.contextMenu {
|
2021-09-29 02:03:12 +05:30
|
|
|
Button("Toggle Default") { toggleDefault(account) }
|
|
|
|
Button("Remove", role: .destructive) { removeAccount(account) }
|
2021-09-29 18:06:52 +05:30
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.redrawOn(change: accountsChanged)
|
|
|
|
|
|
|
|
Button("Add account...") {
|
|
|
|
presentingAccountForm = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 15:44:43 +05:30
|
|
|
.navigationTitle(instance.shortDescription)
|
|
|
|
.sheet(isPresented: $presentingAccountForm, onDismiss: { accountsChanged.toggle() }) {
|
|
|
|
AccountFormView(instance: instance)
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
#if os(iOS)
|
|
|
|
.listStyle(.insetGrouped)
|
2021-09-28 23:36:05 +05:30
|
|
|
#elseif os(tvOS)
|
|
|
|
.frame(maxWidth: 1000)
|
2021-09-25 13:48:22 +05:30
|
|
|
#endif
|
2021-09-29 15:44:43 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-29 15:44:43 +05:30
|
|
|
private var sectionFooter: some View {
|
|
|
|
#if os(iOS)
|
|
|
|
Text("Swipe right to toggle default account, swipe left to remove")
|
|
|
|
#else
|
|
|
|
Text("Tap to toggle default account, tap and hold to remove")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
#endif
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-09-27 03:33:33 +05:30
|
|
|
|
|
|
|
private func makeDefault(_ account: Instance.Account) {
|
|
|
|
instances.setDefaultAccount(account)
|
|
|
|
accountsChanged.toggle()
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
private func toggleDefault(_ account: Instance.Account) {
|
|
|
|
if account == instances.defaultAccount {
|
|
|
|
resetDefaultAccount()
|
|
|
|
} else {
|
|
|
|
makeDefault(account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 03:33:33 +05:30
|
|
|
private func resetDefaultAccount() {
|
|
|
|
instances.resetDefaultAccount()
|
|
|
|
accountsChanged.toggle()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func removeAccount(_ account: Instance.Account) {
|
|
|
|
instances.removeAccount(account)
|
|
|
|
accountsChanged.toggle()
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|