yattee/Shared/Settings/InstanceFormView.swift

160 lines
3.9 KiB
Swift
Raw Normal View History

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