2021-09-25 08:18:22 +00:00
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AccountFormView: View {
|
|
|
|
let instance: Instance
|
2021-10-20 22:21:50 +00:00
|
|
|
var selectedAccount: Binding<Account?>?
|
2021-09-25 08:18:22 +00:00
|
|
|
|
|
|
|
@State private var name = ""
|
|
|
|
@State private var sid = ""
|
|
|
|
|
2021-09-28 20:33:12 +00:00
|
|
|
@State private var isValid = false
|
|
|
|
@State private var isValidated = false
|
|
|
|
@State private var isValidating = 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-28 18:06:05 +00:00
|
|
|
Group {
|
|
|
|
header
|
|
|
|
form
|
|
|
|
footer
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 1000)
|
2021-09-26 20:12:43 +00:00
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.padding(.vertical)
|
2021-09-28 18:06:05 +00:00
|
|
|
#elseif os(tvOS)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
|
|
|
.background(.thickMaterial)
|
2021-09-26 20:12:43 +00:00
|
|
|
#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)
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
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 {
|
2021-09-26 20:12:43 +00:00
|
|
|
TextField("Name", text: $name, prompt: Text("Account Name (optional)"))
|
|
|
|
.focused($focused)
|
|
|
|
|
|
|
|
TextField("SID", text: $sid, prompt: Text("Invidious SID Cookie"))
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
var footer: some View {
|
|
|
|
HStack {
|
2021-09-28 20:33:12 +00:00
|
|
|
ValidationStatusView(isValid: $isValid, isValidated: $isValidated, isValidating: $isValidating, error: .constant(nil))
|
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)
|
2021-09-28 20:33:12 +00:00
|
|
|
.disabled(!isValid)
|
2021-09-26 20:12:43 +00:00
|
|
|
#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)
|
2021-09-28 18:06:05 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.top, 30)
|
|
|
|
#endif
|
2021-09-26 20:12:43 +00:00
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
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-28 20:33:12 +00:00
|
|
|
isValidating = true
|
|
|
|
|
|
|
|
validationDebounce.debouncing(1) {
|
2021-10-16 22:48:58 +00:00
|
|
|
validator.validateInvidiousAccount()
|
2021-09-26 20:12:43 +00:00
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
private func submitForm() {
|
2021-09-28 20:33:12 +00:00
|
|
|
guard isValid else {
|
2021-09-25 08:18:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-19 21:27:04 +00:00
|
|
|
let account = AccountsModel.add(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-10-16 22:48:58 +00:00
|
|
|
app: .constant(instance.app),
|
2021-09-25 08:18:22 +00:00
|
|
|
url: instance.url,
|
2021-10-20 22:21:50 +00:00
|
|
|
account: Account(instanceID: instance.id, url: instance.url, sid: sid),
|
2021-09-26 20:12:43 +00:00
|
|
|
id: $sid,
|
2021-09-28 20:33:12 +00:00
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
|
|
|
isValidating: $isValidating
|
2021-09-25 08:18:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AccountFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
AccountFormView(instance: Instance.fixture)
|
|
|
|
}
|
|
|
|
}
|