2021-09-25 13:48:22 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct InstanceFormView: View {
|
|
|
|
@Binding var savedInstanceID: Instance.ID?
|
|
|
|
|
|
|
|
@State private var name = ""
|
|
|
|
@State private var url = ""
|
2021-10-21 03:51:50 +05:30
|
|
|
@State private var app = VideosApp.invidious
|
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-09-25 13:48:22 +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
|
|
|
|
|
|
|
@FocusState private var nameFieldFocused: Bool
|
|
|
|
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
|
|
|
|
@EnvironmentObject<InstancesModel> private var instancesModel
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .leading) {
|
2021-09-28 23:36:05 +05:30
|
|
|
Group {
|
|
|
|
header
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
form
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
footer
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
.frame(maxWidth: 1000)
|
|
|
|
}
|
|
|
|
.onChange(of: url) { _ in validate() }
|
|
|
|
.onAppear(perform: initializeForm)
|
|
|
|
#if os(iOS)
|
|
|
|
.padding(.vertical)
|
|
|
|
#elseif os(tvOS)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
|
|
|
.background(.thickMaterial)
|
|
|
|
#else
|
2021-10-17 04:18:58 +05:30
|
|
|
.frame(width: 400, height: 190)
|
2021-09-28 23:36:05 +05:30
|
|
|
#endif
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
private var header: some View {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
Text("Add Instance")
|
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
Button("Cancel") {
|
|
|
|
dismiss()
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-09-28 23:36:05 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.cancelAction)
|
2021-09-25 13:48:22 +05:30
|
|
|
#endif
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
private var form: some View {
|
|
|
|
#if !os(tvOS)
|
|
|
|
Form {
|
|
|
|
formFields
|
|
|
|
#if os(macOS)
|
|
|
|
.padding(.horizontal)
|
2021-09-25 13:48:22 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2021-09-28 23:36:05 +05:30
|
|
|
formFields
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var formFields: some View {
|
|
|
|
Group {
|
2021-10-17 04:18:58 +05:30
|
|
|
Picker("Application", selection: $app) {
|
2021-10-21 03:51:50 +05:30
|
|
|
ForEach(VideosApp.allCases, id: \.self) { app in
|
2021-10-17 04:18:58 +05:30
|
|
|
Text(app.rawValue.capitalized).tag(app)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.pickerStyle(.segmented)
|
|
|
|
|
2021-09-28 23:36:05 +05:30
|
|
|
TextField("Name", text: $name, prompt: Text("Instance Name (optional)"))
|
|
|
|
.focused($nameFieldFocused)
|
|
|
|
|
|
|
|
TextField("URL", text: $url, prompt: Text("https://invidious.home.net"))
|
2021-09-29 02:03:12 +05:30
|
|
|
#if !os(macOS)
|
|
|
|
.autocapitalization(.none)
|
|
|
|
.keyboardType(.URL)
|
|
|
|
#endif
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var footer: some View {
|
|
|
|
HStack(alignment: .center) {
|
2021-09-29 02:03:12 +05:30
|
|
|
ValidationStatusView(isValid: $isValid, isValidated: $isValidated, isValidating: $isValidating, error: $validationError)
|
2021-09-28 23:36:05 +05:30
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Save", action: submitForm)
|
2021-09-29 02:03:12 +05:30
|
|
|
.disabled(!isValid)
|
2021-09-28 23:36:05 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.top, 30)
|
2021-09-25 13:48:22 +05:30
|
|
|
#endif
|
2021-09-28 23:36:05 +05:30
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
var validator: AccountValidator {
|
|
|
|
AccountValidator(
|
2021-10-17 04:18:58 +05:30
|
|
|
app: $app,
|
2021-09-25 13:48:22 +05:30
|
|
|
url: url,
|
2021-09-27 01:42:43 +05:30
|
|
|
id: $url,
|
2021-09-29 02:03:12 +05:30
|
|
|
isValid: $isValid,
|
|
|
|
isValidated: $isValidated,
|
|
|
|
isValidating: $isValidating,
|
2021-09-25 13:48:22 +05:30
|
|
|
error: $validationError
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate() {
|
2021-09-27 01:42:43 +05:30
|
|
|
validationDebounce.invalidate()
|
2021-09-25 13:48:22 +05:30
|
|
|
|
|
|
|
guard !url.isEmpty else {
|
2021-09-27 01:42:43 +05:30
|
|
|
validator.reset()
|
2021-09-25 13:48:22 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-29 02:03:12 +05:30
|
|
|
isValidating = true
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
validationDebounce.debouncing(2) {
|
|
|
|
validator.validateInstance()
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func initializeForm() {
|
|
|
|
nameFieldFocused = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func submitForm() {
|
2021-09-29 02:03:12 +05:30
|
|
|
guard isValid else {
|
2021-09-25 13:48:22 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-20 02:57:04 +05:30
|
|
|
savedInstanceID = InstancesModel.add(app: app, name: name, url: url).id
|
2021-09-25 13:48:22 +05:30
|
|
|
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InstanceFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
InstanceFormView(savedInstanceID: .constant(nil))
|
|
|
|
}
|
|
|
|
}
|