From 9aaeba7cf557822b953f01cf09d5b182aaa5e27c Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 16 Feb 2023 15:12:30 +0100 Subject: [PATCH] Fixes focus states --- .../Views/Countdown/CountdownFormView.swift | 2 +- LeCountdown/Views/NewDataView.swift | 2 +- .../Views/Stopwatch/NewStopwatchView.swift | 14 ++--------- .../Views/Stopwatch/StopwatchFormView.swift | 25 ++++++++++++++++--- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/LeCountdown/Views/Countdown/CountdownFormView.swift b/LeCountdown/Views/Countdown/CountdownFormView.swift index 1077ffd..5fd3907 100644 --- a/LeCountdown/Views/Countdown/CountdownFormView.swift +++ b/LeCountdown/Views/Countdown/CountdownFormView.swift @@ -50,7 +50,7 @@ struct CountdownFormView : View { TextField("Name (activates tracking)", text: nameBinding) .focused($focusedField, equals: .name) .onSubmit { - self.focusNextField($focusedField) + self.focusedField = nil } } diff --git a/LeCountdown/Views/NewDataView.swift b/LeCountdown/Views/NewDataView.swift index c1def1c..fd7b6b1 100644 --- a/LeCountdown/Views/NewDataView.swift +++ b/LeCountdown/Views/NewDataView.swift @@ -52,7 +52,7 @@ struct NewDataView: View { NewStopwatchView(isPresented: $isPresented) .tag(1) .environment(\.managedObjectContext, viewContext) - } + }.tabViewStyle(.page(indexDisplayMode: .never)) } } diff --git a/LeCountdown/Views/Stopwatch/NewStopwatchView.swift b/LeCountdown/Views/Stopwatch/NewStopwatchView.swift index 1b1564a..581a94c 100644 --- a/LeCountdown/Views/Stopwatch/NewStopwatchView.swift +++ b/LeCountdown/Views/Stopwatch/NewStopwatchView.swift @@ -48,9 +48,7 @@ struct StopwatchEditView: View { @State var errorShown: Bool = false @State var error: Error? = nil - - @FocusState private var textFieldIsFocused: Bool - + @State var _isAdding: Bool = false @Environment(\.isPresented) var envIsPresented @@ -69,8 +67,7 @@ struct StopwatchEditView: View { } StopwatchFormView(nameBinding: self.$nameString, imageBinding: self.$image, - playSoundBinding: self.$playSound, - textFieldIsFocused: $textFieldIsFocused).environmentObject(self.model) + playSoundBinding: self.$playSound).environmentObject(self.model) .onAppear { self._onAppear() } @@ -121,13 +118,6 @@ struct StopwatchEditView: View { } } } - ToolbarItemGroup(placement: .keyboard) { - Button { - textFieldIsFocused = false - } label: { - Image(systemName: "keyboard.chevron.compact.down") - } - } } .navigationTitle("Edit stopwatch") } diff --git a/LeCountdown/Views/Stopwatch/StopwatchFormView.swift b/LeCountdown/Views/Stopwatch/StopwatchFormView.swift index 2b60d42..0757d77 100644 --- a/LeCountdown/Views/Stopwatch/StopwatchFormView.swift +++ b/LeCountdown/Views/Stopwatch/StopwatchFormView.swift @@ -9,26 +9,44 @@ import SwiftUI struct StopwatchFormView: View { + enum StopwatchField: Int, Hashable { + case name + } + + @FocusState private var focusedField: StopwatchField? + var nameBinding: Binding var imageBinding: Binding var playSoundBinding: Binding @EnvironmentObject var model: TimerModel - var textFieldIsFocused: FocusState.Binding +// var textFieldIsFocused: FocusState.Binding var body: some View { Form { Section(header: Text("Name for tracking the activity")) { TextField("name", text: nameBinding) - .focused(textFieldIsFocused) + .focused($focusedField, equals: .name) + .onSubmit { + self.focusedField = nil + } } SoundImageFormView(imageBinding: imageBinding, optionalSound: playSoundBinding) .environmentObject(self.model) + }.toolbar { + ToolbarItemGroup(placement: .keyboard) { + Button { + self.focusedField = nil + } label: { + Image(systemName: "keyboard.chevron.compact.down") + } + Spacer() + } } } } @@ -41,8 +59,7 @@ struct StopwatchFormView_Previews: PreviewProvider { StopwatchFormView( nameBinding: .constant(""), imageBinding: .constant(.pic1), - playSoundBinding: .constant(true), - textFieldIsFocused: $textFieldIsFocused) + playSoundBinding: .constant(true)) .environmentObject(TimerModel()) } }