yattee/Shared/Settings/AccountsSettings.swift

76 lines
2.4 KiB
Swift
Raw Normal View History

2021-09-25 08:18:22 +00:00
import SwiftUI
2021-10-23 11:51:02 +00:00
struct AccountsSettings: View {
2021-09-25 08:18:22 +00:00
let instanceID: Instance.ID?
@State private var accountsChanged = false
@State private var presentingAccountForm = false
@EnvironmentObject<AccountsModel> private var model
2021-09-25 08:18:22 +00:00
@EnvironmentObject<InstancesModel> private var instances
var instance: Instance! {
InstancesModel.find(instanceID)
2021-09-25 08:18:22 +00:00
}
var body: some View {
2021-10-20 22:21:50 +00:00
VStack {
if instance.app.supportsAccounts {
2021-10-16 22:48:58 +00:00
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 08:18:22 +00:00
List {
2021-09-29 10:14:43 +00:00
Section(header: Text("Accounts"), footer: sectionFooter) {
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) {}
}
#else
Text(account.description)
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button("Remove", role: .destructive) { removeAccount(account) }
}
2021-09-25 08:18:22 +00:00
#endif
}
.redrawOn(change: accountsChanged)
Button("Add account...") {
presentingAccountForm = true
}
}
}
2021-09-29 10:14:43 +00:00
.sheet(isPresented: $presentingAccountForm, onDismiss: { accountsChanged.toggle() }) {
2021-10-23 11:51:02 +00:00
AccountForm(instance: instance)
2021-09-29 10:14:43 +00:00
}
2021-09-25 08:18:22 +00:00
#if os(iOS)
.listStyle(.insetGrouped)
#elseif os(tvOS)
.frame(maxWidth: 1000)
2021-09-25 08:18:22 +00:00
#endif
2021-09-29 10:14:43 +00:00
}
2021-09-25 08:18:22 +00:00
2021-09-29 10:14:43 +00:00
private var sectionFooter: some View {
#if os(iOS)
Text("Swipe to remove account")
2021-09-29 10:14:43 +00:00
#else
Text("Tap and hold to remove account")
2021-09-29 10:14:43 +00:00
.foregroundColor(.secondary)
#endif
2021-09-25 08:18:22 +00:00
}
2021-09-26 22:03:33 +00:00
2021-10-20 22:21:50 +00:00
private func removeAccount(_ account: Account) {
AccountsModel.remove(account)
2021-09-26 22:03:33 +00:00
accountsChanged.toggle()
}
2021-09-25 08:18:22 +00:00
}