simplify Exercise to only hold on to the unit of the metric, remove ValueKeyboard as input

This commit is contained in:
Felix Förtsch
2024-10-02 12:41:04 +02:00
parent 41a82f081a
commit b7f5caf9dd
15 changed files with 630 additions and 243 deletions

View File

@@ -51,9 +51,9 @@ struct SetListItem: View {
#Preview {
@Previewable @State var set = WorkoutItem(set: [
WorkoutItem(reps: 10, "Squat"),
WorkoutItem(reps: 10, "Squat"),
WorkoutItem(reps: 10, "Squat")])
WorkoutItem(Exercise("Squat")),
WorkoutItem(Exercise("Squat")),
WorkoutItem(Exercise("Squat"))])
List {
SetListItem(workout: Workout(name: "RR"), set: $set)
}

View File

@@ -0,0 +1,33 @@
//
// TextEditorWithPlaceholder.swift
// WorkoutsPlus
//
// Created by Felix Förtsch on 01.10.24.
//
import SwiftUI
struct TextEditorWithPlaceholder: View {
@Binding var text: String
var placeholder: String
var body: some View {
ZStack(alignment: .topLeading) {
// TODO: If focused, hide placeholder.
if text.isEmpty {
Text(placeholder)
.foregroundColor(Color(UIColor.placeholderText))
}
TextEditor(text: $text)
.frame(minHeight: 40, maxHeight: .infinity)
.cornerRadius(8)
}
}
}
#Preview {
@Previewable @State var text = ""
Form {
TextEditorWithPlaceholder(text: $text, placeholder: "Description (optional)")
}
}

View File

@@ -5,41 +5,12 @@
// Created by Felix Förtsch on 24.09.24.
//
// TODO: Value keyboard binds to a metric (value + unit). You type in the $value and select the unit [m, s, kg] on the wheel.
// Example: Push-up (value: reps, unit: nil), Push-up (value: additional weight, unit: kg), Run (value: distance/time, unit: m/s)
// This means to use the valuekeyboard you would need to know which unit is allowed and then style the value depending on the unit
import SwiftUI
protocol ValueType {
associatedtype UnitType: CaseIterable & Hashable & CustomStringConvertible
var value: String { get set }
var unit: UnitType { get set }
}
enum ExerciseUnit: String, CaseIterable, CustomStringConvertible {
case reps = "reps"
case meter = "m"
case second = "s"
case speed = "m/s"
case pace = "s/m"
var description: String { rawValue }
}
struct ExerciseValue: ValueType {
var value: String = ""
var unit: ExerciseUnit = .reps
}
enum FoodUnit: String, CaseIterable, CustomStringConvertible {
case gram = "g"
case milliliter = "ml"
var description: String { rawValue }
}
struct FoodValue: ValueType {
var value: String = ""
var unit: FoodUnit = .gram
}
struct KeyboardButtonStyle: ButtonStyle {
var width: CGFloat = 100
var height: CGFloat = 50
@@ -57,9 +28,16 @@ struct KeyboardButtonStyle: ButtonStyle {
}
}
struct ValueKeyboard<Value: ValueType>: View {
protocol ValueKeyboardProtocol {
var value: String { get set }
var availableUnits: [String] { get set }
}
struct ValueKeyboard: View {
@Binding var isPresented: Bool
@Binding var value: Value
@Binding var value: String
@Binding var unit: [String]
var body: some View {
VStack {
@@ -69,12 +47,12 @@ struct ValueKeyboard<Value: ValueType>: View {
.frame(height: 80)
HStack {
TextField("0", text: $value.value)
TextField("0", text: $value)
.font(.system(size: 32, weight: .bold, design: .rounded))
.padding()
Picker("Unit", selection: $value.unit) {
ForEach(Array(Value.UnitType.allCases), id: \.self) { unit in
Picker("Unit", selection: $unit) {
ForEach(unit, id: \.self) { unit in
Text(unit.description).tag(unit)
}
}
@@ -145,31 +123,28 @@ struct ValueKeyboard<Value: ValueType>: View {
}
private func handleButtonTap(_ button: String) {
switch button {
case "":
if !value.value.isEmpty {
value.value.removeLast()
}
case "":
isPresented.toggle()
default:
value.value.append(button)
}
// switch button {
// case "":
// if !value.value.isEmpty {
// value.value.removeLast()
// }
// case "":
// isPresented.toggle()
// default:
// value.value.append(button)
// }
// }
}
}
#Preview("ExerciseValue") {
@Previewable @State var exerciseValue: ExerciseValue = .init()
@Previewable @State var isPresented: Bool = true
Text(exerciseValue.value)
Text(exerciseValue.unit.rawValue)
ValueKeyboard(isPresented: $isPresented, value: $exerciseValue)
}
#Preview("FoodValue") {
@Previewable @State var foodValue: FoodValue = .init()
@Previewable @State var isPresented: Bool = true
Text(foodValue.value)
Text(foodValue.unit.rawValue)
ValueKeyboard(isPresented: $isPresented, value: $foodValue)
let value = 0
let unit = ExerciseMetric.allCases
VStack {
// ValueKeyboard(isPresented: $isPresented, value: value, unit: unit)
}
}