Hey guys,
I'm creating a fitness app on WatchOS and I need to realize a focus on a variable but it doesn't work.
I will show you my code to understand the problem :
@State var WeightChoose: Double = 0.0
@FocusState private var isWeightActive: Bool
var body: some View {
HStack {
Text("Poids:")
Spacer()
Text("\(String(format: "%.0f", WeightChoose)) KG")
.focused($isWeightActive)
.foregroundColor(isWeightActive ? .green : .white)
.digitalCrownRotation($WeightChoose, from: 0.0, through: 300.0, by: 1.0, sensitivity: .medium)
.animation(.easeIn(duration: 1), value: WeightChoose)
.padding()
Here my code don't show me any error but the focus doesn't work at all.
Before, I used the old way to focus with
@State private var isWeightActive = false
instead of @FocusState
and
.focusable(true) { focused in
isWeightActive = focused
}
Instead of .focused()
But now the .focusable() method is depreciated since watchOS 8 and we have to go on FocusState and .focused but it doesn't work for my code.
If you could help me with that it would be awesome.
Thank you
Cyrille
Post
Replies
Boosts
Views
Activity
Hello guys,
I have such a big coding problem since a half year now about data saving.
I have a fitness app where we can add our exercices depend of the muscle, so you can choose it and then select your weight and number of repetitions and valid your exercise.
But when I do my exercises and my watch screen is also turn off, my muscle and muscleExercise variables are going back to their default value.
Here some code for you :
@EnvironmentObject var dataManager: DataManager
@Environment(\.modelContext) private var context
@AppStorage("savedGroupName") private var savedGroupName: String = ""
@AppStorage("savedExerciceName") private var savedExerciceName: String = ""
@State var groupName: String = "À choisir"
@State var ExerciceChoose: String = "À choisir"
I use my variables here :
HStack {
Text("Muscle:")
Spacer()
NavigationLink(destination: MusclesView()) {
Text(savedGroupName.isEmpty ? "À choisir" : savedGroupName)
}
.buttonStyle(PlainButtonStyle())
}
.onAppear {
savedGroupName = groupName
}
HStack {
Text("Exercise:")
Spacer()
NavigationLink(destination: MuscleExercicesView(groupName: groupName, ExerciceChoose: ExerciceChoose)) {
Text(savedExerciceName.isEmpty ? "À choisir" : savedExerciceName)
}
.onAppear {
savedExerciceName = ExerciceChoose
}
.buttonStyle(PlainButtonStyle())
}
The value of my muscle variable is taking in an other view :
struct MusclesView: View {
let muscleGroup = ExerciceData.muscleGroups
var body: some View {
NavigationStack {
List(muscleGroup, id: \.name) { group in
NavigationLink(destination: MuscleExercicesView(groupName: group.name, ExerciceChoose: "À choisir")) {
Text(group.name)
}
}
}
}
}
#Preview {
MusclesView()
}
and the value of the exerciseMuscle variable is also taking in an other view :
```import SwiftUI
struct MuscleExercicesView: View {
let exerciceGroup = ExerciceData.muscleGroups
@State var groupName: String
@State var ExerciceChoose: String
var body: some View {
if let group = ExerciceData.muscleGroups.first(where: { $0.name == groupName }) {
List(group.exercices, id: \.id) { exercice in
NavigationLink(exercice.name, destination: CurrentInformationsView(groupName: groupName, ExerciceChoose: exercice.name))
}
/*.onTapGesture {
print("Selected exercise: \(ExerciceChoose) for muscle group: \(groupName)")
}*/
.navigationTitle(Text("Exercices pour \(groupName)"))
} else {
Text("Aucun exercice trouvé pour \(groupName)")
.navigationTitle(Text("Erreur"))
}
}
}
#Preview {
MuscleExercicesView(groupName: "Pectoraux", ExerciceChoose: "À choisir")
}
I tried many things (like userDefault, put my values in an array to save it etc..) to keep my variables with the same value during the session but nothing works.
I wish I could have some help from you guys.
Have a good day !
Cyrille