Fixes focus states

release
Laurent 3 years ago
parent 31f4d2963f
commit 9aaeba7cf5
  1. 2
      LeCountdown/Views/Countdown/CountdownFormView.swift
  2. 2
      LeCountdown/Views/NewDataView.swift
  3. 12
      LeCountdown/Views/Stopwatch/NewStopwatchView.swift
  4. 25
      LeCountdown/Views/Stopwatch/StopwatchFormView.swift

@ -50,7 +50,7 @@ struct CountdownFormView : View {
TextField("Name (activates tracking)", text: nameBinding) TextField("Name (activates tracking)", text: nameBinding)
.focused($focusedField, equals: .name) .focused($focusedField, equals: .name)
.onSubmit { .onSubmit {
self.focusNextField($focusedField) self.focusedField = nil
} }
} }

@ -52,7 +52,7 @@ struct NewDataView: View {
NewStopwatchView(isPresented: $isPresented) NewStopwatchView(isPresented: $isPresented)
.tag(1) .tag(1)
.environment(\.managedObjectContext, viewContext) .environment(\.managedObjectContext, viewContext)
} }.tabViewStyle(.page(indexDisplayMode: .never))
} }
} }

@ -49,8 +49,6 @@ struct StopwatchEditView: View {
@State var errorShown: Bool = false @State var errorShown: Bool = false
@State var error: Error? = nil @State var error: Error? = nil
@FocusState private var textFieldIsFocused: Bool
@State var _isAdding: Bool = false @State var _isAdding: Bool = false
@Environment(\.isPresented) var envIsPresented @Environment(\.isPresented) var envIsPresented
@ -69,8 +67,7 @@ struct StopwatchEditView: View {
} }
StopwatchFormView(nameBinding: self.$nameString, StopwatchFormView(nameBinding: self.$nameString,
imageBinding: self.$image, imageBinding: self.$image,
playSoundBinding: self.$playSound, playSoundBinding: self.$playSound).environmentObject(self.model)
textFieldIsFocused: $textFieldIsFocused).environmentObject(self.model)
.onAppear { .onAppear {
self._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") .navigationTitle("Edit stopwatch")
} }

@ -9,26 +9,44 @@ import SwiftUI
struct StopwatchFormView: View { struct StopwatchFormView: View {
enum StopwatchField: Int, Hashable {
case name
}
@FocusState private var focusedField: StopwatchField?
var nameBinding: Binding<String> var nameBinding: Binding<String>
var imageBinding: Binding<CoolPic> var imageBinding: Binding<CoolPic>
var playSoundBinding: Binding<Bool> var playSoundBinding: Binding<Bool>
@EnvironmentObject var model: TimerModel @EnvironmentObject var model: TimerModel
var textFieldIsFocused: FocusState<Bool>.Binding // var textFieldIsFocused: FocusState<Bool>.Binding
var body: some View { var body: some View {
Form { Form {
Section(header: Text("Name for tracking the activity")) { Section(header: Text("Name for tracking the activity")) {
TextField("name", text: nameBinding) TextField("name", text: nameBinding)
.focused(textFieldIsFocused) .focused($focusedField, equals: .name)
.onSubmit {
self.focusedField = nil
}
} }
SoundImageFormView(imageBinding: imageBinding, SoundImageFormView(imageBinding: imageBinding,
optionalSound: playSoundBinding) optionalSound: playSoundBinding)
.environmentObject(self.model) .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( StopwatchFormView(
nameBinding: .constant(""), nameBinding: .constant(""),
imageBinding: .constant(.pic1), imageBinding: .constant(.pic1),
playSoundBinding: .constant(true), playSoundBinding: .constant(true))
textFieldIsFocused: $textFieldIsFocused)
.environmentObject(TimerModel()) .environmentObject(TimerModel())
} }
} }

Loading…
Cancel
Save