yattee/Shared/Settings/AccountsSettings.swift

103 lines
3.2 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
2021-10-27 21:11:38 +00:00
@State private var frontendURL = ""
@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-27 21:11:38 +00:00
List {
if instance.app.hasFrontendURL {
Section(header: Text("Frontend URL")) {
TextField(
"Frontend URL",
text: $frontendURL,
prompt: Text("To enable videos, channels and playlists sharing")
)
.onAppear {
frontendURL = instance.frontendURL ?? ""
}
.onChange(of: frontendURL) { newValue in
InstancesModel.setFrontendURL(instance, newValue)
}
.labelsHidden()
.autocapitalization(.none)
.keyboardType(.URL)
}
}
Section(header: Text("Accounts"), footer: sectionFooter) {
if instance.app.supportsAccounts {
accounts
} else {
Text("Accounts are not supported for the application of this instance")
.foregroundColor(.secondary)
}
2021-10-16 22:48:58 +00:00
}
}
2021-10-27 21:11:38 +00:00
#if os(tvOS)
.frame(maxWidth: 1000)
#endif
.navigationTitle(instance.description)
2021-10-16 22:48:58 +00:00
}
var accounts: some View {
2021-10-27 21:11:38 +00:00
Group {
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) }
}
#endif
}
.redrawOn(change: accountsChanged)
2021-09-25 08:18:22 +00:00
2021-10-27 21:11:38 +00:00
Button("Add account...") {
presentingAccountForm = true
2021-09-25 08:18:22 +00:00
}
}
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-10-27 21:11:38 +00:00
#if !os(tvOS)
2021-09-25 08:18:22 +00:00
.listStyle(.insetGrouped)
#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 {
2021-10-27 21:11:38 +00:00
if !instance.app.supportsAccounts {
return Text("")
}
2021-09-29 10:14:43 +00:00
#if os(iOS)
2021-10-27 21:11:38 +00:00
return Text("Swipe to remove account")
2021-09-29 10:14:43 +00:00
#else
2021-10-27 21:11:38 +00:00
return 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
}