Skip to content

Commit

Permalink
trying to simplify mobile preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Oct 13, 2024
1 parent 575121d commit 0842c07
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 261 deletions.
4 changes: 4 additions & 0 deletions mobile/mobile.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
80777D192C973F02007565FC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 80777D182C973F02007565FC /* Assets.xcassets */; };
80777D1C2C973F02007565FC /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 80777D1B2C973F02007565FC /* Preview Assets.xcassets */; };
808B3E022C9CFB9B005BC890 /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 808B3E012C9CFB9B005BC890 /* ProfileView.swift */; };
809E44762CBB42D6000BED21 /* PreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 809E44752CBB42D6000BED21 /* PreferencesView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -43,6 +44,7 @@
80777D182C973F02007565FC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
80777D1B2C973F02007565FC /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
808B3E012C9CFB9B005BC890 /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; };
809E44752CBB42D6000BED21 /* PreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesView.swift; sourceTree = "<group>"; };
80A6AD222C9A508D005145B3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -93,6 +95,7 @@
8021CCD12C9B6BAB00CB9B8D /* TrainingWeek.swift */,
80777D182C973F02007565FC /* Assets.xcassets */,
80777D1A2C973F02007565FC /* Preview Content */,
809E44752CBB42D6000BED21 /* PreferencesView.swift */,
);
path = mobile;
sourceTree = "<group>";
Expand Down Expand Up @@ -185,6 +188,7 @@
8021CCC62C9AFAA400CB9B8D /* AppDelegate.swift in Sources */,
80777D172C973F01007565FC /* ContentView.swift in Sources */,
80777D152C973F01007565FC /* mobileApp.swift in Sources */,
809E44762CBB42D6000BED21 /* PreferencesView.swift in Sources */,
8031CD792C9DC59C005CED34 /* APIManager.swift in Sources */,
8021CCD62C9B6DF900CB9B8D /* ColorTheme.swift in Sources */,
8021CCD22C9B6BAB00CB9B8D /* TrainingWeek.swift in Sources */,
Expand Down
6 changes: 2 additions & 4 deletions mobile/mobile/DashboardNavbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ struct DashboardNavbar: View {
var body: some View {
HStack {
Text("Track")
.font(.title)
.fontWeight(.semibold)
.font(.system(size: 24, weight: .black))
.foregroundColor(ColorTheme.primaryLight)
+ Text("Flow")
.font(.title)
.fontWeight(.semibold)
.font(.system(size: 24, weight: .black))
.foregroundColor(ColorTheme.primary)

Spacer()
Expand Down
144 changes: 144 additions & 0 deletions mobile/mobile/PreferencesView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import SwiftUI

struct PreferencesContainer: View {
@Binding var preferences: Preferences
@State private var isSaving: Bool = false

var body: some View {
VStack(alignment: .leading) {
Text("Preferences")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(ColorTheme.white)

PreferencesContent(preferences: $preferences, onUpdate: savePreferences)
}
.padding()
.background(ColorTheme.darkDarkGrey)
.cornerRadius(12)
.overlay(
Group {
if isSaving {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: ColorTheme.primary))
}
}
)
}

private func savePreferences() {
isSaving = true
// Simulate API call
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
isSaving = false
}
}
}

struct PreferencesContent: View {
@Binding var preferences: Preferences
var onUpdate: () -> Void

var body: some View {
VStack(alignment: .leading, spacing: 20) {
preferenceSectionView(title: "Race Details") {
preferenceRowView(
title: "Race Distance",
value: Binding(
get: { preferences.raceDistance ?? "" },
set: {
preferences.raceDistance = $0.isEmpty ? nil : $0
onUpdate()
}
)
) {
Picker("Race Distance", selection: $0) {
Text("Select distance").tag("")
Text("5K").tag("5k")
Text("10K").tag("10k")
Text("Half Marathon").tag("half marathon")
Text("Marathon").tag("marathon")
Text("Ultra Marathon").tag("ultra marathon")
Text("None").tag("none")
}
.pickerStyle(MenuPickerStyle())
}
}

preferenceSectionView(title: "Ideal Training Week") {
ForEach(Day.allCases, id: \.self) { day in
preferenceRowView(
title: day.rawValue,
value: sessionTypeBinding(for: day)
) {
Picker("Session Type", selection: $0) {
Text("No Preference").tag("")
Text("Easy Run").tag("easy run")
Text("Long Run").tag("long run")
Text("Speed Workout").tag("speed workout")
Text("Rest Day").tag("rest day")
Text("Moderate Run").tag("moderate run")
}
.pickerStyle(MenuPickerStyle())
}
}
}
}
}

private func sessionTypeBinding(for day: Day) -> Binding<String> {
Binding(
get: { sessionType(for: day) },
set: {
updateSessionType(for: day, with: $0)
onUpdate()
}
)
}

private func sessionType(for day: Day) -> String {
preferences.idealTrainingWeek?.first(where: { $0.day == day })?.sessionType ?? ""
}

private func updateSessionType(for day: Day, with newValue: String) {
if var week = preferences.idealTrainingWeek {
if let index = week.firstIndex(where: { $0.day == day }) {
week[index] = TrainingDay(day: day, sessionType: newValue)
} else {
week.append(TrainingDay(day: day, sessionType: newValue))
}
preferences.idealTrainingWeek = week
} else {
preferences.idealTrainingWeek = [TrainingDay(day: day, sessionType: newValue)]
}
}

private func preferenceSectionView<Content: View>(
title: String, @ViewBuilder content: () -> Content
) -> some View {
VStack(alignment: .leading, spacing: 4) {
Text(title)
.font(.headline)
.fontWeight(.semibold)
.foregroundColor(ColorTheme.lightGrey)
content()
}
}

private func preferenceRowView<Value, EditContent: View>(
title: String,
value: Binding<Value>,
@ViewBuilder editContent: (Binding<Value>) -> EditContent
) -> some View {
HStack {
Text(title)
.font(.subheadline)
.foregroundColor(ColorTheme.lightGrey)
Spacer()
editContent(value)
.foregroundColor(ColorTheme.white)
.frame(height: 30)
}
.frame(height: 44)
}
}
Loading

0 comments on commit 0842c07

Please sign in to comment.