1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/Settings/AccountFormView.swift

157 lines
3.8 KiB
Swift
Raw Normal View History

2021-09-25 13:48:22 +05:30
import Defaults
import SwiftUI
struct AccountFormView: View {
let instance: Instance
var selectedAccount: Binding<Instance.Account?>?
@State private var name = ""
@State private var sid = ""
@State private var valid = false
@State private var validated = false
@State private var validationDebounce = Debounce()
2021-09-25 13:48:22 +05:30
@FocusState private var focused: Bool
@Environment(\.dismiss) private var dismiss
@EnvironmentObject<InstancesModel> private var instances
var body: some View {
VStack {
Group {
header
form
footer
}
.frame(maxWidth: 1000)
}
#if os(iOS)
.padding(.vertical)
#elseif os(tvOS)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(.thickMaterial)
#else
.frame(width: 400, height: 145)
#endif
}
var header: some View {
HStack(alignment: .center) {
Text("Add Account")
.font(.title2.bold())
Spacer()
Button("Cancel") {
dismiss()
2021-09-25 13:48:22 +05:30
}
#if !os(tvOS)
.keyboardShortcut(.cancelAction)
#endif
}
.padding(.horizontal)
}
private var form: some View {
Group {
#if !os(tvOS)
Form {
formFields
#if os(macOS)
.padding(.horizontal)
#endif
}
#else
formFields
#endif
}
.onAppear(perform: initializeForm)
.onChange(of: sid) { _ in validate() }
}
var formFields: some View {
Group {
TextField("Name", text: $name, prompt: Text("Account Name (optional)"))
.focused($focused)
TextField("SID", text: $sid, prompt: Text("Invidious SID Cookie"))
}
}
2021-09-25 13:48:22 +05:30
var footer: some View {
HStack {
validationStatus
2021-09-25 13:48:22 +05:30
Spacer()
2021-09-25 13:48:22 +05:30
Button("Save", action: submitForm)
.disabled(!valid)
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
2021-09-25 13:48:22 +05:30
#endif
}
.frame(minHeight: 35)
#if os(tvOS)
.padding(.top, 30)
#endif
.padding(.horizontal)
}
2021-09-25 13:48:22 +05:30
var validationStatus: some View {
HStack(spacing: 4) {
Image(systemName: valid ? "checkmark.circle.fill" : "xmark.circle.fill")
.foregroundColor(valid ? .green : .red)
VStack(alignment: .leading) {
Text(valid ? "Account found" : "Invalid account details")
2021-09-25 13:48:22 +05:30
}
}
.opacity(validated ? 1 : 0)
2021-09-25 13:48:22 +05:30
}
private func initializeForm() {
2021-09-25 13:48:22 +05:30
focused = true
}
private func validate() {
validationDebounce.invalidate()
2021-09-25 13:48:22 +05:30
guard !sid.isEmpty else {
validator.reset()
return
}
validationDebounce.debouncing(2) {
validator.validateAccount()
}
2021-09-25 13:48:22 +05:30
}
private func submitForm() {
2021-09-25 13:48:22 +05:30
guard valid else {
return
}
let account = instances.addAccount(instance: instance, name: name, sid: sid)
2021-09-25 13:48:22 +05:30
selectedAccount?.wrappedValue = account
dismiss()
}
private var validator: AccountValidator {
AccountValidator(
2021-09-25 13:48:22 +05:30
url: instance.url,
account: Instance.Account(instanceID: instance.id, url: instance.url, sid: sid),
id: $sid,
2021-09-25 13:48:22 +05:30
valid: $valid,
validated: $validated
)
}
}
struct AccountFormView_Previews: PreviewProvider {
static var previews: some View {
AccountFormView(instance: Instance.fixture)
}
}