Sources: Fix input fields in source settings

Signed-off-by: kingbri <bdashore3@gmail.com>
This commit is contained in:
kingbri 2022-08-11 21:04:40 -04:00
parent 2792b61e9b
commit d99a065d3b

View file

@ -12,8 +12,6 @@ struct SourceSettingsView: View {
@EnvironmentObject var navModel: NavigationViewModel @EnvironmentObject var navModel: NavigationViewModel
@State private var tempBaseUrl: String = ""
var body: some View { var body: some View {
NavView { NavView {
Form { Form {
@ -45,24 +43,7 @@ struct SourceSettingsView: View {
} }
if selectedSource.dynamicBaseUrl { if selectedSource.dynamicBaseUrl {
Section( SourceSettingsBaseUrlView(selectedSource: selectedSource)
header: Text("Base URL"),
footer: Text("Enter the base URL of your server.")
) {
TextField("https://...", text: $tempBaseUrl)
.onAppear {
tempBaseUrl = selectedSource.baseUrl ?? ""
}
.onSubmit {
if tempBaseUrl.last == "/" {
selectedSource.baseUrl = String(tempBaseUrl.dropLast())
} else {
selectedSource.baseUrl = tempBaseUrl
}
PersistenceController.shared.save()
}
}
} }
if let sourceApi = selectedSource.api { if let sourceApi = selectedSource.api {
@ -72,6 +53,9 @@ struct SourceSettingsView: View {
SourceSettingsMethodView(selectedSource: selectedSource) SourceSettingsMethodView(selectedSource: selectedSource)
} }
} }
.onDisappear {
PersistenceController.shared.save()
}
.navigationTitle("Source settings") .navigationTitle("Source settings")
.toolbar { .toolbar {
ToolbarItem(placement: .navigationBarTrailing) { ToolbarItem(placement: .navigationBarTrailing) {
@ -84,14 +68,44 @@ struct SourceSettingsView: View {
} }
} }
struct SourceSettingsBaseUrlView: View {
@ObservedObject var selectedSource: Source
@FocusState var baseUrlFocused: Bool
@State private var tempBaseUrl: String = ""
var body: some View {
Section(
header: Text("Base URL"),
footer: Text("Enter the base URL of your server.")
) {
TextField("https://...", text: $tempBaseUrl)
.keyboardType(.URL)
.focused($baseUrlFocused)
.onChange(of: baseUrlFocused) { isFocused in
if !isFocused {
if tempBaseUrl.last == "/" {
selectedSource.baseUrl = String(tempBaseUrl.dropLast())
} else {
selectedSource.baseUrl = tempBaseUrl
}
}
}
.onAppear {
tempBaseUrl = selectedSource.baseUrl ?? ""
}
}
}
}
struct SourceSettingsApiView: View { struct SourceSettingsApiView: View {
@ObservedObject var selectedSourceApi: SourceApi @ObservedObject var selectedSourceApi: SourceApi
@FocusState var clientIdFieldFocused: Bool
@FocusState var tokenFieldFocused: Bool
@State private var tempClientId: String = "" @State private var tempClientId: String = ""
@State private var tempClientSecret: String = "" @State private var tempClientSecret: String = ""
@State private var showPassword = false
@FocusState var inFocus: Field?
enum Field { enum Field {
case secure, plain case secure, plain
@ -104,43 +118,30 @@ struct SourceSettingsApiView: View {
) { ) {
if selectedSourceApi.dynamicClientId { if selectedSourceApi.dynamicClientId {
TextField("Client ID", text: $tempClientId) TextField("Client ID", text: $tempClientId)
.textInputAutocapitalization(.never)
.focused($clientIdFieldFocused)
.onChange(of: clientIdFieldFocused) { isFocused in
if !isFocused {
selectedSourceApi.clientId = tempClientId
}
}
.onAppear { .onAppear {
tempClientId = selectedSourceApi.clientId ?? "" tempClientId = selectedSourceApi.clientId ?? ""
} }
.onSubmit {
selectedSourceApi.clientId = tempClientId
PersistenceController.shared.save()
}
} }
if selectedSourceApi.clientSecret != nil { if selectedSourceApi.clientSecret != nil {
HStack { TextField("Token", text: $tempClientSecret)
Group { .textInputAutocapitalization(.never)
if showPassword { .focused($tokenFieldFocused)
TextField("Token", text: $tempClientSecret) .onChange(of: clientIdFieldFocused) { isFocused in
.focused($inFocus, equals: .plain) if !isFocused {
} else { selectedSourceApi.clientSecret = tempClientSecret
SecureField("Token", text: $tempClientSecret)
.focused($inFocus, equals: .secure)
} }
} }
.onAppear { .onAppear {
tempClientSecret = selectedSourceApi.clientSecret ?? "" tempClientSecret = selectedSourceApi.clientSecret ?? ""
} }
.onSubmit {
selectedSourceApi.clientSecret = tempClientSecret
PersistenceController.shared.save()
}
Spacer()
Button {
showPassword.toggle()
inFocus = showPassword ? .plain : .secure
} label: {
Image(systemName: showPassword ? "eye.slash" : "eye")
}
}
} }
} }
} }
@ -169,7 +170,6 @@ struct SourceSettingsMethodView: View {
} }
.onChange(of: selectedTempParser) { _ in .onChange(of: selectedTempParser) { _ in
selectedSource.preferredParser = selectedTempParser.rawValue selectedSource.preferredParser = selectedTempParser.rawValue
PersistenceController.shared.save()
} }
} }
} }