2021-09-25 08:18:22 +00:00
|
|
|
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
|
2021-09-26 20:12:43 +00:00
|
|
|
@State private var validationDebounce = Debounce()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
|
|
|
@FocusState private var focused: Bool
|
|
|
|
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
|
|
|
|
@EnvironmentObject<InstancesModel> private var instances
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
2021-09-26 20:12:43 +00:00
|
|
|
header
|
|
|
|
form
|
|
|
|
footer
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.padding(.vertical)
|
|
|
|
#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 08:18:22 +00:00
|
|
|
}
|
2021-09-26 20:12:43 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.cancelAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
var form: some View {
|
|
|
|
Form {
|
|
|
|
TextField("Name", text: $name, prompt: Text("Account Name (optional)"))
|
|
|
|
.focused($focused)
|
|
|
|
|
|
|
|
TextField("SID", text: $sid, prompt: Text("Invidious SID Cookie"))
|
|
|
|
}
|
|
|
|
.onAppear(perform: initializeForm)
|
|
|
|
.onChange(of: sid) { _ in validate() }
|
|
|
|
#if os(macOS)
|
2021-09-25 08:18:22 +00:00
|
|
|
.padding(.horizontal)
|
2021-09-26 20:12:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
var footer: some View {
|
|
|
|
HStack {
|
|
|
|
validationStatus
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
Spacer()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
Button("Save", action: submitForm)
|
|
|
|
.disabled(!valid)
|
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.defaultAction)
|
2021-09-25 08:18:22 +00:00
|
|
|
#endif
|
2021-09-26 20:12:43 +00:00
|
|
|
}
|
|
|
|
.frame(minHeight: 35)
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
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 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 20:12:43 +00:00
|
|
|
.opacity(validated ? 1 : 0)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
private func initializeForm() {
|
2021-09-25 08:18:22 +00:00
|
|
|
focused = true
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
private func validate() {
|
|
|
|
validationDebounce.invalidate()
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
guard !sid.isEmpty else {
|
|
|
|
validator.reset()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
validationDebounce.debouncing(2) {
|
|
|
|
validator.validateAccount()
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
private func submitForm() {
|
2021-09-25 08:18:22 +00:00
|
|
|
guard valid else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
let account = instances.addAccount(instance: instance, name: name, sid: sid)
|
2021-09-25 08:18:22 +00:00
|
|
|
selectedAccount?.wrappedValue = account
|
|
|
|
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
private var validator: AccountValidator {
|
|
|
|
AccountValidator(
|
2021-09-25 08:18:22 +00:00
|
|
|
url: instance.url,
|
2021-09-26 20:39:27 +00:00
|
|
|
account: Instance.Account(instanceID: instance.id, url: instance.url, sid: sid),
|
2021-09-26 20:12:43 +00:00
|
|
|
id: $sid,
|
2021-09-25 08:18:22 +00:00
|
|
|
valid: $valid,
|
|
|
|
validated: $validated
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AccountFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
AccountFormView(instance: Instance.fixture)
|
|
|
|
}
|
|
|
|
}
|