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

105 lines
3.7 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<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) {
ForEach(instances.accounts(instanceID), id: \.self) { account in
2021-09-29 15:44:43 +05:30
#if os(iOS)
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 {
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) {
Button("Remove", role: .destructive) { removeAccount(account) }
2021-09-27 03:33:33 +05:30
}
#else
Button(action: { toggleDefault(account) }) {
HStack(spacing: 2) {
Text(account.description)
if instances.defaultAccount == account {
Text("— default")
.foregroundColor(.secondary)
}
}
}
.contextMenu {
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-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)
#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()
}
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
}