Skip to content

Commit

Permalink
Fixed preference updates lets goooooo
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Oct 15, 2024
1 parent 8ceffcd commit 442e498
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 56 deletions.
47 changes: 6 additions & 41 deletions mobile/mobile/DashboardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import SwiftUI
struct DashboardView: View {
@EnvironmentObject var appState: AppState
@State private var trainingWeekData: TrainingWeekData?
@State private var profileData: ProfileData?
@State private var isLoadingTrainingWeek: Bool = true
@State private var isLoadingProfile: Bool = true
@State private var showProfile: Bool = false

var body: some View {
Expand All @@ -32,25 +30,12 @@ struct DashboardView: View {
.navigationBarHidden(true)

if showProfile {
if let profileData = profileData {
ProfileView(
isPresented: $showProfile,
profileData: Binding(
get: { profileData },
set: { self.profileData = $0 }
),
showProfile: $showProfile
)
.transition(.move(edge: .trailing))
.zIndex(2)
} else if isLoadingProfile {
LoadingView()
.zIndex(2)
} else {
Text("Failed to load profile")
.foregroundColor(ColorTheme.lightGrey)
.zIndex(2)
}
ProfileView(
isPresented: $showProfile,
showProfile: $showProfile
)
.transition(.move(edge: .trailing))
.zIndex(2)
}
}
}
Expand All @@ -65,7 +50,6 @@ struct DashboardView: View {

private func fetchData() {
fetchTrainingWeekData()
fetchProfileData()
}

private func fetchTrainingWeekData() {
Expand All @@ -86,23 +70,4 @@ struct DashboardView: View {
}
}
}

private func fetchProfileData() {
guard let token = appState.jwtToken else {
isLoadingProfile = false
return
}

APIManager.shared.fetchProfileData(token: token) { result in
DispatchQueue.main.async {
self.isLoadingProfile = false
switch result {
case .success(let profile):
self.profileData = profile
case .failure(let error):
print("Error fetching profile data: \(error)")
}
}
}
}
}
6 changes: 0 additions & 6 deletions mobile/mobile/PreferencesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct PreferencesContainer: View {

private func savePreferences() {
guard let token = appState.jwtToken else {
print("No JWT token available")
return
}

Expand All @@ -56,7 +55,6 @@ struct PreferencesContainer: View {
isSaving = false
switch result {
case .success:
print("Preferences saved successfully")
showingSavedPopup = true
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
showingSavedPopup = false
Expand Down Expand Up @@ -158,8 +156,6 @@ struct PreferencesContent: View {
}

private func updateSessionType(for day: Day, with newValue: String) {
print("Setting session type for \(day.rawValue) to \(newValue)")

if preferences.idealTrainingWeek == nil {
preferences.idealTrainingWeek = []
}
Expand All @@ -169,8 +165,6 @@ struct PreferencesContent: View {
} else {
preferences.idealTrainingWeek?.append(TrainingDay(day: day, sessionType: newValue))
}

print("Preferences after update: \(preferences.toJSON())")
onUpdate()
}

Expand Down
46 changes: 37 additions & 9 deletions mobile/mobile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ import SwiftUI

struct ProfileView: View {
@Binding var isPresented: Bool
@Binding var profileData: ProfileData
@State private var profileData: ProfileData?
@EnvironmentObject var appState: AppState
@Binding var showProfile: Bool
@State private var isSaving: Bool = false
@State private var isLoading: Bool = true

var body: some View {
VStack(spacing: 0) {
DashboardNavbar(onLogout: handleSignOut, showProfile: $showProfile)
.background(ColorTheme.superDarkGrey)
.zIndex(1)

ScrollView {
VStack {
ProfileHeader(profileData: profileData)
PreferencesContainer(preferences: preferencesBinding)
if isLoading {
LoadingView()
} else if let profileData = profileData {
ScrollView {
VStack {
ProfileHeader(profileData: profileData)
PreferencesContainer(preferences: preferencesBinding)
}
.padding()
SignOutButton(action: handleSignOut)
}
.padding()
SignOutButton(action: handleSignOut)
} else {
Text("Failed to load profile")
.foregroundColor(ColorTheme.lightGrey)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(ColorTheme.superDarkGrey.edgesIgnoringSafeArea(.all))
.onAppear(perform: fetchProfileData)
}

private func handleSignOut() {
Expand All @@ -36,17 +45,36 @@ struct ProfileView: View {
private var preferencesBinding: Binding<Preferences> {
Binding(
get: {
Preferences(fromJSON: profileData.preferences ?? "{}")
Preferences(fromJSON: profileData?.preferences ?? "{}")
},
set: { newValue in
if let preferencesJSON = try? JSONEncoder().encode(newValue),
let preferencesString = String(data: preferencesJSON, encoding: .utf8)
{
profileData.preferences = preferencesString
profileData?.preferences = preferencesString
}
}
)
}

private func fetchProfileData() {
guard let token = appState.jwtToken else {
isLoading = false
return
}

APIManager.shared.fetchProfileData(token: token) { result in
DispatchQueue.main.async {
self.isLoading = false
switch result {
case .success(let profile):
self.profileData = profile
case .failure(let error):
print("Error fetching profile data: \(error)")
}
}
}
}
}

struct ProfileHeader: View {
Expand Down

0 comments on commit 442e498

Please sign in to comment.