2021-09-25 08:18:22 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct InstanceDetailsSettingsView: View {
|
|
|
|
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 {
|
|
|
|
Section(header: Text("Accounts")) {
|
2021-09-28 18:06:05 +00:00
|
|
|
ForEach(instances.accounts(instanceID), id: \.self) { account in
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
#if !os(tvOS)
|
2021-09-28 18:06:05 +00:00
|
|
|
HStack(spacing: 2) {
|
|
|
|
Text(account.description)
|
|
|
|
if instances.defaultAccount == account {
|
|
|
|
Text("— default")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 22:03:33 +00:00
|
|
|
.swipeActions(edge: .leading, allowsFullSwipe: true) {
|
|
|
|
if instances.defaultAccount != account {
|
|
|
|
Button("Make Default", action: { makeDefault(account) })
|
|
|
|
} else {
|
|
|
|
Button("Reset Default", action: resetDefaultAccount)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 22:03:33 +00:00
|
|
|
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
|
|
|
Button("Remove", role: .destructive, action: { removeAccount(account) })
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
Button(action: { toggleDefault(account) }) {
|
|
|
|
HStack(spacing: 2) {
|
|
|
|
Text(account.description)
|
|
|
|
if instances.defaultAccount == account {
|
|
|
|
Text("— default")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
Button("Toggle Default", action: { toggleDefault(account) })
|
|
|
|
Button("Remove", role: .destructive, action: { removeAccount(account) })
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.redrawOn(change: accountsChanged)
|
|
|
|
|
|
|
|
Button("Add account...") {
|
|
|
|
presentingAccountForm = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.listStyle(.insetGrouped)
|
2021-09-28 18:06:05 +00:00
|
|
|
#elseif os(tvOS)
|
|
|
|
.frame(maxWidth: 1000)
|
2021-09-25 08:18:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
.navigationTitle(instance.shortDescription)
|
|
|
|
.sheet(isPresented: $presentingAccountForm, onDismiss: { accountsChanged.toggle() }) {
|
|
|
|
AccountFormView(instance: instance)
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 22:03:33 +00:00
|
|
|
|
|
|
|
private func makeDefault(_ account: Instance.Account) {
|
|
|
|
instances.setDefaultAccount(account)
|
|
|
|
accountsChanged.toggle()
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
private func toggleDefault(_ account: Instance.Account) {
|
|
|
|
if account == instances.defaultAccount {
|
|
|
|
resetDefaultAccount()
|
|
|
|
} else {
|
|
|
|
makeDefault(account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:03:33 +00:00
|
|
|
private func resetDefaultAccount() {
|
|
|
|
instances.resetDefaultAccount()
|
|
|
|
accountsChanged.toggle()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func removeAccount(_ account: Instance.Account) {
|
|
|
|
instances.removeAccount(account)
|
|
|
|
accountsChanged.toggle()
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|