2021-09-25 08:18:22 +00:00
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
2021-10-23 11:51:02 +00:00
|
|
|
struct AccountForm: View {
|
2021-09-25 08:18:22 +00:00
|
|
|
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 = ""
|
2021-11-14 23:06:01 +00:00
|
|
|
@State private var username = ""
|
|
|
|
@State private var password = ""
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-28 20:33:12 +00:00
|
|
|
@State private var isValid = false
|
|
|
|
@State private var isValidated = false
|
|
|
|
@State private var isValidating = false
|
2021-11-14 23:06:01 +00:00
|
|
|
@State private var validationError: String?
|
2021-09-26 20:12:43 +00:00
|
|
|
@State private var validationDebounce = Debounce()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-12-05 17:09:25 +00:00
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2022-01-06 14:56:03 +00:00
|
|
|
@Environment(\.openURL) private var openURL
|
2021-11-28 14:37:55 +00:00
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2021-09-25 08:18:22 +00:00
|
|
|
|
|
|
|
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)
|
2021-11-08 16:29:35 +00:00
|
|
|
.padding(.vertical)
|
2021-09-28 18:06:05 +00:00
|
|
|
#elseif os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
2021-12-05 17:09:25 +00:00
|
|
|
.background(Color.background(scheme: colorScheme))
|
2021-09-26 20:12:43 +00:00
|
|
|
#else
|
2021-11-08 16:29:35 +00:00
|
|
|
.frame(width: 400, height: 145)
|
2021-09-26 20:12:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var header: some View {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
Text("Add Account")
|
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Cancel") {
|
2021-11-28 14:37:55 +00:00
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
2021-09-26 20:12:43 +00:00
|
|
|
#if !os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.keyboardShortcut(.cancelAction)
|
2021-09-26 20:12:43 +00:00
|
|
|
#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)
|
2021-11-08 16:29:35 +00:00
|
|
|
.padding(.horizontal)
|
2021-09-28 18:06:05 +00:00
|
|
|
#endif
|
2022-01-06 14:56:03 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
helpButton
|
|
|
|
#endif
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
formFields
|
|
|
|
#endif
|
|
|
|
}
|
2021-11-14 23:06:01 +00:00
|
|
|
.onChange(of: username) { _ in validate() }
|
|
|
|
.onChange(of: password) { _ in validate() }
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 14:56:03 +00:00
|
|
|
var helpButton: some View {
|
|
|
|
Group {
|
|
|
|
if instance.app == .invidious {
|
|
|
|
Button {
|
|
|
|
openURL(URL(string: "https://github.com/yattee/yattee/wiki/Adding-Invidious-instance-and-account")!)
|
|
|
|
} label: {
|
|
|
|
Label("How to add Invidious account?", systemImage: "questionmark.circle")
|
|
|
|
#if os(macOS)
|
|
|
|
.help("How to add Invidious account?")
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
var formFields: some View {
|
|
|
|
Group {
|
2021-11-14 23:06:01 +00:00
|
|
|
if !instance.app.accountsUsePassword {
|
2021-11-28 14:37:55 +00:00
|
|
|
TextField("Name", text: $name)
|
2021-11-14 23:06:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
TextField(usernamePrompt, text: $username)
|
2021-09-26 20:12:43 +00:00
|
|
|
|
2021-11-14 23:06:01 +00:00
|
|
|
if instance.app.accountsUsePassword {
|
2021-11-28 14:37:55 +00:00
|
|
|
SecureField("Password", text: $password)
|
2021-11-14 23:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
var usernamePrompt: String {
|
2021-11-14 23:06:01 +00:00
|
|
|
switch instance.app {
|
|
|
|
case .invidious:
|
2021-11-28 14:37:55 +00:00
|
|
|
return "SID Cookie"
|
2021-11-14 23:06:01 +00:00
|
|
|
default:
|
2021-11-28 14:37:55 +00:00
|
|
|
return "Username"
|
2021-09-26 20:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
var footer: some View {
|
|
|
|
HStack {
|
2021-11-14 23:06:01 +00:00
|
|
|
AccountValidationStatus(
|
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
|
|
|
isValidating: $isValidating,
|
|
|
|
error: $validationError
|
|
|
|
)
|
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
|
|
|
|
2022-01-06 14:56:03 +00:00
|
|
|
#if os(macOS)
|
|
|
|
helpButton
|
|
|
|
#endif
|
|
|
|
|
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-11-08 16:29:35 +00:00
|
|
|
.padding(.horizontal)
|
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 validate() {
|
2021-11-07 21:39:28 +00:00
|
|
|
isValid = false
|
2021-09-26 20:12:43 +00:00
|
|
|
validationDebounce.invalidate()
|
|
|
|
|
2021-11-14 23:06:01 +00:00
|
|
|
let passwordIsValid = instance.app.accountsUsePassword ? !password.isEmpty : true
|
|
|
|
|
|
|
|
guard !username.isEmpty, passwordIsValid else {
|
2021-09-25 08:18:22 +00:00
|
|
|
validator.reset()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:33:12 +00:00
|
|
|
isValidating = true
|
|
|
|
|
|
|
|
validationDebounce.debouncing(1) {
|
2021-11-14 23:06:01 +00:00
|
|
|
validator.validateAccount()
|
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-11-14 23:06:01 +00:00
|
|
|
let account = AccountsModel.add(instance: instance, name: name, username: username, password: password)
|
2021-09-25 08:18:22 +00:00
|
|
|
selectedAccount?.wrappedValue = account
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
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-10-27 21:11:38 +00:00
|
|
|
url: instance.apiURL,
|
2021-11-14 23:06:01 +00:00
|
|
|
account: Account(instanceID: instance.id, url: instance.apiURL, username: username, password: password),
|
|
|
|
id: $username,
|
2021-09-28 20:33:12 +00:00
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
2021-11-14 23:06:01 +00:00
|
|
|
isValidating: $isValidating,
|
|
|
|
error: $validationError
|
2021-09-25 08:18:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AccountFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-10-23 11:51:02 +00:00
|
|
|
AccountForm(instance: Instance.fixture)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|