Skip to content

Commit

Permalink
mobile profile setup
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Oct 12, 2024
1 parent e1d042f commit 575121d
Show file tree
Hide file tree
Showing 4 changed files with 414 additions and 42 deletions.
7 changes: 4 additions & 3 deletions mobile/mobile/DashboardNavbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SwiftUI

struct DashboardNavbar: View {
var onLogout: () -> Void
@Binding var showProfile: Bool

var body: some View {
HStack {
Expand All @@ -16,9 +17,9 @@ struct DashboardNavbar: View {

Spacer()

Menu {
Button("Logout", action: onLogout)
} label: {
Button(action: {
showProfile.toggle()
}) {
Image(systemName: "person.circle")
.resizable()
.frame(width: 30, height: 30)
Expand Down
75 changes: 63 additions & 12 deletions mobile/mobile/DashboardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,56 @@ 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 {
NavigationView {
VStack(spacing: 0) {
DashboardNavbar(onLogout: handleLogout)
.background(ColorTheme.superDarkGrey)
.zIndex(1)
ZStack {
VStack(spacing: 0) {
DashboardNavbar(onLogout: handleLogout, showProfile: $showProfile)
.background(ColorTheme.superDarkGrey)
.zIndex(1)

ScrollView {
if isLoadingTrainingWeek {
ScrollView {
if isLoadingTrainingWeek {
LoadingView()
} else if let data = trainingWeekData {
TrainingWeekView(data: data)
} else {
Text("No training data available")
.font(.headline)
.foregroundColor(ColorTheme.lightGrey)
}
}
}
.background(ColorTheme.superDarkGrey.edgesIgnoringSafeArea(.all))
.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()
} else if let data = trainingWeekData {
TrainingWeekView(data: data)
.zIndex(2)
} else {
Text("No training data available")
.font(.headline)
Text("Failed to load profile")
.foregroundColor(ColorTheme.lightGrey)
.zIndex(2)
}
}
}
.background(ColorTheme.superDarkGrey.edgesIgnoringSafeArea(.all))
.navigationBarHidden(true)
}
.onAppear(perform: fetchData)
}
Expand All @@ -37,6 +64,11 @@ struct DashboardView: View {
}

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

private func fetchTrainingWeekData() {
guard let token = appState.jwtToken else {
isLoadingTrainingWeek = false
return
Expand All @@ -54,4 +86,23 @@ 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)")
}
}
}
}
}
54 changes: 48 additions & 6 deletions mobile/mobile/Models.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,61 @@
import Foundation

struct ProfileData: Codable {
let firstname: String
let lastname: String
let email: String
let profile: String
let isActive: Bool
let preferences: String
var firstname: String
var lastname: String
var email: String
var profile: String
var isActive: Bool
var preferences: String?

enum CodingKeys: String, CodingKey {
case firstname, lastname, email, profile, preferences
case isActive = "is_active"
}
}

enum Day: String, CaseIterable, Codable {
case mon = "Mon"
case tues = "Tues"
case wed = "Wed"
case thurs = "Thurs"
case fri = "Fri"
case sat = "Sat"
case sun = "Sun"
}

struct TrainingDay: Codable {
let day: Day
let sessionType: String

enum CodingKeys: String, CodingKey {
case day
case sessionType = "session_type"
}
}

struct Preferences: Codable {
var raceDistance: String?
var idealTrainingWeek: [TrainingDay]?

enum CodingKeys: String, CodingKey {
case raceDistance = "race_distance"
case idealTrainingWeek = "ideal_training_week"
}

init(fromJSON json: String) {
let decoder = JSONDecoder()
if let jsonData = json.data(using: .utf8),
let decoded = try? decoder.decode(Preferences.self, from: jsonData)
{
self = decoded
} else {
self.raceDistance = nil
self.idealTrainingWeek = nil
}
}
}

struct TrainingWeekData: Codable {
let sessions: [TrainingSession]
}
Expand Down
Loading

0 comments on commit 575121d

Please sign in to comment.