2021-09-25 08:18:22 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2021-10-23 11:51:02 +00:00
|
|
|
struct InstanceForm: View {
|
2021-09-25 08:18:22 +00:00
|
|
|
@Binding var savedInstanceID: Instance.ID?
|
|
|
|
|
|
|
|
@State private var name = ""
|
|
|
|
@State private var url = ""
|
|
|
|
|
2022-07-01 21:28:32 +00:00
|
|
|
@State private var app: VideosApp?
|
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-25 08:18:22 +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
|
2021-11-28 14:37:55 +00:00
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
2022-11-11 18:23:58 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .leading) {
|
2021-09-28 18:06:05 +00:00
|
|
|
Group {
|
|
|
|
header
|
|
|
|
form
|
2022-12-17 14:57:59 +00:00
|
|
|
#if os(macOS)
|
|
|
|
VStack {
|
|
|
|
validationStatus
|
|
|
|
}
|
|
|
|
.frame(minHeight: 60, alignment: .topLeading)
|
|
|
|
.padding(.horizontal, 15)
|
|
|
|
#endif
|
2021-09-28 18:06:05 +00:00
|
|
|
footer
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
.frame(maxWidth: 1000)
|
|
|
|
}
|
|
|
|
.onChange(of: url) { _ in validate() }
|
|
|
|
#if os(iOS)
|
|
|
|
.padding(.vertical)
|
|
|
|
#elseif os(tvOS)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
2021-12-05 17:09:25 +00:00
|
|
|
.background(Color.background(scheme: colorScheme))
|
2021-09-28 18:06:05 +00:00
|
|
|
#else
|
2022-12-17 14:57:59 +00:00
|
|
|
.frame(width: 400, height: 180)
|
|
|
|
.padding(.vertical)
|
2021-09-28 18:06:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
private var header: some View {
|
2022-11-25 18:31:48 +00:00
|
|
|
HStack {
|
2022-07-01 21:28:32 +00:00
|
|
|
Text("Add Location")
|
2021-09-28 18:06:05 +00:00
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
Button("Cancel") {
|
2021-11-28 14:37:55 +00:00
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
#if !os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.keyboardShortcut(.cancelAction)
|
2021-09-25 08:18:22 +00:00
|
|
|
#endif
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
private var form: some View {
|
|
|
|
#if !os(tvOS)
|
|
|
|
Form {
|
|
|
|
formFields
|
|
|
|
#if os(macOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.padding(.horizontal)
|
2021-09-25 08:18:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2021-09-28 18:06:05 +00:00
|
|
|
formFields
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var formFields: some View {
|
|
|
|
Group {
|
2021-11-28 14:37:55 +00:00
|
|
|
TextField("Name", text: $name)
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2022-11-13 11:14:05 +00:00
|
|
|
TextField("Address", text: $url)
|
2021-10-27 21:11:38 +00:00
|
|
|
|
2021-09-28 20:33:12 +00:00
|
|
|
#if !os(macOS)
|
|
|
|
.autocapitalization(.none)
|
|
|
|
.keyboardType(.URL)
|
|
|
|
#endif
|
2022-12-17 14:57:59 +00:00
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
VStack {
|
|
|
|
validationStatus
|
|
|
|
}
|
|
|
|
.frame(minHeight: 100)
|
|
|
|
#elseif os(iOS)
|
|
|
|
validationStatus
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder var validationStatus: some View {
|
|
|
|
if !url.isEmpty {
|
|
|
|
Section {
|
|
|
|
AccountValidationStatus(
|
|
|
|
app: $app,
|
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
|
|
|
isValidating: $isValidating,
|
|
|
|
error: $validationError
|
|
|
|
)
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var footer: some View {
|
2022-11-25 18:31:48 +00:00
|
|
|
HStack {
|
2021-09-28 18:06:05 +00:00
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Save", action: submitForm)
|
2021-09-28 20:33:12 +00:00
|
|
|
.disabled(!isValid)
|
2021-09-28 18:06:05 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.padding(.top, 30)
|
2021-09-25 08:18:22 +00:00
|
|
|
#endif
|
2021-09-28 18:06:05 +00:00
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
var validator: AccountValidator {
|
|
|
|
AccountValidator(
|
2021-10-16 22:48:58 +00:00
|
|
|
app: $app,
|
2021-09-25 08:18:22 +00:00
|
|
|
url: url,
|
2021-09-26 20:12:43 +00:00
|
|
|
id: $url,
|
2021-09-28 20:33:12 +00:00
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
|
|
|
isValidating: $isValidating,
|
2021-09-25 08:18:22 +00:00
|
|
|
error: $validationError
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate() {
|
2021-11-07 21:39:28 +00:00
|
|
|
isValid = false
|
2021-09-26 20:12:43 +00:00
|
|
|
validationDebounce.invalidate()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
|
|
|
guard !url.isEmpty else {
|
2021-09-26 20:12:43 +00:00
|
|
|
validator.reset()
|
2021-09-25 08:18:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:33:12 +00:00
|
|
|
isValidating = true
|
|
|
|
|
2021-09-26 20:12:43 +00:00
|
|
|
validationDebounce.debouncing(2) {
|
|
|
|
validator.validateInstance()
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func submitForm() {
|
2022-09-28 14:27:01 +00:00
|
|
|
guard isValid, let app else {
|
2021-09-25 08:18:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
let savedInstance = InstancesModel.shared.add(app: app, name: name, url: url)
|
2022-11-11 18:23:58 +00:00
|
|
|
savedInstanceID = savedInstance.id
|
|
|
|
|
|
|
|
if accounts.isEmpty {
|
|
|
|
accounts.setCurrent(savedInstance.anonymousAccount)
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InstanceFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-10-23 11:51:02 +00:00
|
|
|
InstanceForm(savedInstanceID: .constant(nil))
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|