2021-09-25 13:48:22 +05:30
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
2021-10-23 17:21:02 +05:30
|
|
|
struct AccountForm: View {
|
2021-09-25 13:48:22 +05:30
|
|
|
let instance: Instance
|
2021-10-21 03:51:50 +05:30
|
|
|
var selectedAccount: Binding<Account?>?
|
2021-09-25 13:48:22 +05:30
|
|
|
|
|
|
|
@State private var name = ""
|
2021-11-15 04:36:01 +05:30
|
|
|
@State private var username = ""
|
|
|
|
@State private var password = ""
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-29 02:03:12 +05:30
|
|
|
@State private var isValid = false
|
|
|
|
@State private var isValidated = false
|
|
|
|
@State private var isValidating = false
|
2021-11-15 04:36:01 +05:30
|
|
|
@State private var validationError: String?
|
2021-09-27 01:42:43 +05:30
|
|
|
@State private var validationDebounce = Debounce()
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-12-05 22:39:25 +05:30
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2022-01-06 20:26:03 +05:30
|
|
|
@Environment(\.openURL) private var openURL
|
2021-11-28 20:07:55 +05:30
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2021-09-25 13:48:22 +05:30
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
2021-09-28 23:36:05 +05:30
|
|
|
Group {
|
|
|
|
header
|
|
|
|
form
|
|
|
|
footer
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 1000)
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
|
|
|
#if os(iOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.padding(.vertical)
|
2021-09-28 23:36:05 +05:30
|
|
|
#elseif os(tvOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
2021-12-05 22:39:25 +05:30
|
|
|
.background(Color.background(scheme: colorScheme))
|
2021-09-27 01:42:43 +05:30
|
|
|
#else
|
2021-11-08 21:59:35 +05:30
|
|
|
.frame(width: 400, height: 145)
|
2021-09-27 01:42:43 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var header: some View {
|
2022-11-26 00:01:48 +05:30
|
|
|
HStack {
|
2021-09-27 01:42:43 +05:30
|
|
|
Text("Add Account")
|
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Cancel") {
|
2021-11-28 20:07:55 +05:30
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-09-27 01:42:43 +05:30
|
|
|
#if !os(tvOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.keyboardShortcut(.cancelAction)
|
2021-09-27 01:42:43 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
private var form: some View {
|
|
|
|
Group {
|
|
|
|
#if !os(tvOS)
|
|
|
|
Form {
|
|
|
|
formFields
|
|
|
|
#if os(macOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.padding(.horizontal)
|
2021-09-28 23:36:05 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
formFields
|
|
|
|
#endif
|
|
|
|
}
|
2021-11-15 04:36:01 +05:30
|
|
|
.onChange(of: username) { _ in validate() }
|
|
|
|
.onChange(of: password) { _ in validate() }
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var formFields: some View {
|
|
|
|
Group {
|
2022-08-26 05:06:46 +05:30
|
|
|
TextField("Username", text: $username)
|
|
|
|
SecureField("Password", text: $password)
|
2021-11-15 04:36:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
var usernamePrompt: String {
|
2021-11-15 04:36:01 +05:30
|
|
|
switch instance.app {
|
|
|
|
case .invidious:
|
2021-11-28 20:07:55 +05:30
|
|
|
return "SID Cookie"
|
2021-11-15 04:36:01 +05:30
|
|
|
default:
|
2021-11-28 20:07:55 +05:30
|
|
|
return "Username"
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
var footer: some View {
|
|
|
|
HStack {
|
2021-11-15 04:36:01 +05:30
|
|
|
AccountValidationStatus(
|
2022-07-02 02:58:32 +05:30
|
|
|
app: .constant(instance.app),
|
2021-11-15 04:36:01 +05:30
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
|
|
|
isValidating: $isValidating,
|
|
|
|
error: $validationError
|
|
|
|
)
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
Spacer()
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
Button("Save", action: submitForm)
|
2021-09-29 02:03:12 +05:30
|
|
|
.disabled(!isValid)
|
2021-09-27 01:42:43 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.defaultAction)
|
2021-09-25 13:48:22 +05:30
|
|
|
#endif
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
|
|
|
.frame(minHeight: 35)
|
2021-09-28 23:36:05 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.top, 30)
|
|
|
|
#endif
|
2021-11-08 21:59:35 +05:30
|
|
|
.padding(.horizontal)
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
private func validate() {
|
2021-11-08 03:09:28 +05:30
|
|
|
isValid = false
|
2021-09-27 01:42:43 +05:30
|
|
|
validationDebounce.invalidate()
|
|
|
|
|
2022-08-26 05:06:46 +05:30
|
|
|
guard !username.isEmpty, !password.isEmpty else {
|
2021-09-25 13:48:22 +05:30
|
|
|
validator.reset()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-29 02:03:12 +05:30
|
|
|
isValidating = true
|
|
|
|
|
|
|
|
validationDebounce.debouncing(1) {
|
2021-11-15 04:36:01 +05:30
|
|
|
validator.validateAccount()
|
2021-09-27 01:42:43 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
private func submitForm() {
|
2021-09-29 02:03:12 +05:30
|
|
|
guard isValid else {
|
2021-09-25 13:48:22 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:36:01 +05:30
|
|
|
let account = AccountsModel.add(instance: instance, name: name, username: username, password: password)
|
2021-09-25 13:48:22 +05:30
|
|
|
selectedAccount?.wrappedValue = account
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
private var validator: AccountValidator {
|
|
|
|
AccountValidator(
|
2021-10-17 04:18:58 +05:30
|
|
|
app: .constant(instance.app),
|
2021-10-28 02:41:38 +05:30
|
|
|
url: instance.apiURL,
|
2021-11-15 04:36:01 +05:30
|
|
|
account: Account(instanceID: instance.id, url: instance.apiURL, username: username, password: password),
|
|
|
|
id: $username,
|
2021-09-29 02:03:12 +05:30
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
2021-11-15 04:36:01 +05:30
|
|
|
isValidating: $isValidating,
|
|
|
|
error: $validationError
|
2021-09-25 13:48:22 +05:30
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AccountFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-10-23 17:21:02 +05:30
|
|
|
AccountForm(instance: Instance.fixture)
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
}
|