1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Shared/Settings/InstanceForm.swift

155 lines
3.8 KiB
Swift
Raw Normal View History

2021-09-25 13:48:22 +05:30
import SwiftUI
2021-10-23 17:21:02 +05:30
struct InstanceForm: View {
2021-09-25 13:48:22 +05:30
@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
@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?
@State private var validationDebounce = Debounce()
2021-09-25 13:48:22 +05:30
@Environment(\.colorScheme) private var colorScheme
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(alignment: .leading) {
Group {
header
2021-09-25 13:48:22 +05:30
form
2021-09-25 13:48:22 +05:30
footer
2021-09-25 13:48:22 +05:30
}
.frame(maxWidth: 1000)
}
2021-11-08 03:09:28 +05:30
.onChange(of: app) { _ in validate() }
.onChange(of: url) { _ in validate() }
#if os(iOS)
.padding(.vertical)
#elseif os(tvOS)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.background(scheme: colorScheme))
#else
2021-10-17 04:18:58 +05:30
.frame(width: 400, height: 190)
#endif
}
2021-09-25 13:48:22 +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
Button("Cancel") {
2021-11-28 20:07:55 +05:30
presentationMode.wrappedValue.dismiss()
2021-09-25 13:48:22 +05:30
}
#if !os(tvOS)
2021-11-08 21:59:35 +05:30
.keyboardShortcut(.cancelAction)
2021-09-25 13:48:22 +05:30
#endif
}
.padding(.horizontal)
}
2021-09-25 13:48:22 +05:30
private var form: some View {
#if !os(tvOS)
Form {
formFields
#if os(macOS)
2021-11-08 21:59:35 +05:30
.padding(.horizontal)
2021-09-25 13:48:22 +05:30
#endif
}
#else
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-12-03 00:52:55 +05:30
.labelsHidden()
2021-10-17 04:18:58 +05:30
2021-11-28 20:07:55 +05:30
TextField("Name", text: $name)
2021-11-28 20:07:55 +05:30
TextField("API URL", text: $url)
2021-10-28 02:41:38 +05:30
#if !os(macOS)
.autocapitalization(.none)
.keyboardType(.URL)
#endif
}
}
private var footer: some View {
HStack(alignment: .center) {
2021-10-23 17:21:02 +05:30
AccountValidationStatus(isValid: $isValid, isValidated: $isValidated, isValidating: $isValidating, error: $validationError)
Spacer()
Button("Save", action: submitForm)
.disabled(!isValid)
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
}
#if os(tvOS)
2021-11-08 21:59:35 +05:30
.padding(.top, 30)
2021-09-25 13:48:22 +05:30
#endif
.padding(.horizontal)
}
var validator: AccountValidator {
AccountValidator(
2021-10-17 04:18:58 +05:30
app: $app,
2021-09-25 13:48:22 +05:30
url: url,
id: $url,
isValid: $isValid,
isValidated: $isValidated,
isValidating: $isValidating,
2021-09-25 13:48:22 +05:30
error: $validationError
)
}
func validate() {
2021-11-08 03:09:28 +05:30
isValid = false
validationDebounce.invalidate()
2021-09-25 13:48:22 +05:30
guard !url.isEmpty else {
validator.reset()
2021-09-25 13:48:22 +05:30
return
}
isValidating = true
validationDebounce.debouncing(2) {
validator.validateInstance()
}
2021-09-25 13:48:22 +05:30
}
func submitForm() {
guard isValid else {
2021-09-25 13:48:22 +05:30
return
}
savedInstanceID = InstancesModel.add(app: app, name: name, url: url).id
2021-09-25 13:48:22 +05:30
2021-11-28 20:07:55 +05:30
presentationMode.wrappedValue.dismiss()
2021-09-25 13:48:22 +05:30
}
}
struct InstanceFormView_Previews: PreviewProvider {
static var previews: some View {
2021-10-23 17:21:02 +05:30
InstanceForm(savedInstanceID: .constant(nil))
2021-09-25 13:48:22 +05:30
}
}