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
|
|
|
|
|
2021-10-20 02:57:04 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var model
|
2021-09-25 13:48:22 +05:30
|
|
|
@EnvironmentObject<InstancesModel> private var instances
|
|
|
|
|
|
|
|
var instance: Instance! {
|
2021-10-20 02:57:04 +05:30
|
|
|
InstancesModel.find(instanceID)
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2021-10-21 03:51:50 +05:30
|
|
|
VStack {
|
|
|
|
if instance.app.supportsAccounts {
|
2021-10-17 04:18:58 +05:30
|
|
|
accounts
|
|
|
|
} else {
|
|
|
|
Text("Accounts are not supported for the application of this instance")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationTitle(instance.shortDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
var accounts: some View {
|
2021-09-25 13:48:22 +05:30
|
|
|
List {
|
2021-09-29 15:44:43 +05:30
|
|
|
Section(header: Text("Accounts"), footer: sectionFooter) {
|
2021-10-20 02:57:04 +05:30
|
|
|
ForEach(InstancesModel.accounts(instanceID), id: \.self) { account in
|
|
|
|
#if os(tvOS)
|
|
|
|
Button(account.description) {}
|
|
|
|
.contextMenu {
|
|
|
|
Button("Remove", role: .destructive) { removeAccount(account) }
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
#else
|
2021-10-20 02:57:04 +05:30
|
|
|
Text(account.description)
|
|
|
|
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
|
|
|
Button("Remove", role: .destructive) { removeAccount(account) }
|
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
|
|
|
.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)
|
2021-10-20 02:57:04 +05:30
|
|
|
Text("Swipe to remove account")
|
2021-09-29 15:44:43 +05:30
|
|
|
#else
|
2021-10-20 02:57:04 +05:30
|
|
|
Text("Tap and hold to remove account")
|
2021-09-29 15:44:43 +05:30
|
|
|
.foregroundColor(.secondary)
|
|
|
|
#endif
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-09-27 03:33:33 +05:30
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
private func removeAccount(_ account: Account) {
|
2021-10-20 02:57:04 +05:30
|
|
|
AccountsModel.remove(account)
|
2021-09-27 03:33:33 +05:30
|
|
|
accountsChanged.toggle()
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|