1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 06:10:32 +05:30
yattee/Shared/Settings/AccountsSettingsView.swift

76 lines
2.4 KiB
Swift
Raw Normal View History

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<AccountsModel> private var model
2021-09-25 13:48:22 +05:30
@EnvironmentObject<InstancesModel> private var instances
var instance: Instance! {
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) {
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 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)
#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 to remove account")
2021-09-29 15:44:43 +05:30
#else
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) {
AccountsModel.remove(account)
2021-09-27 03:33:33 +05:30
accountsChanged.toggle()
}
2021-09-25 13:48:22 +05:30
}