Skip to content

Commit 97786c8

Browse files
Refactored to new backend schema
1 parent 80817e7 commit 97786c8

3 files changed

Lines changed: 36 additions & 52 deletions

File tree

app/src/main/graphql/User.graphql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ fragment userFields on User {
1111
lastGoalChange
1212
lastStreak
1313
totalGymDays
14+
workoutHistory {
15+
...workoutFields
16+
}
1417
}
1518

1619
fragment workoutFields on Workout {
@@ -43,14 +46,14 @@ query getUserByNetId($netId: String!) {
4346
}
4447
}
4548

46-
mutation SetWorkoutGoals($id: Int!, $workoutGoal: Int!) {
47-
setWorkoutGoals(userId: $id, workoutGoal: $workoutGoal) {
49+
mutation SetWorkoutGoals($userId: Int!, $workoutGoal: Int!) {
50+
setWorkoutGoals(userId: $userId, workoutGoal: $workoutGoal) {
4851
...userFields
4952
}
5053
}
5154

52-
mutation LogWorkout($facilityId: Int!, $workoutTime: DateTime!, $id: Int!) {
53-
logWorkout(facilityId: $facilityId, userId: $id, workoutTime: $workoutTime) {
55+
mutation LogWorkout($facilityId: Int!, $workoutTime: DateTime!, $userId: Int!) {
56+
logWorkout(facilityId: $facilityId, userId: $userId, workoutTime: $workoutTime) {
5457
...workoutFields
5558
}
5659
}

app/src/main/java/com/cornellappdev/uplift/data/repositories/ProfileRepository.kt

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import android.util.Log
44
import com.apollographql.apollo.ApolloClient
55
import com.cornellappdev.uplift.GetUserByNetIdQuery
66
import com.cornellappdev.uplift.GetWeeklyWorkoutDaysQuery
7-
import com.cornellappdev.uplift.GetWorkoutsByIdQuery
87
import com.cornellappdev.uplift.SetWorkoutGoalsMutation
98
import com.cornellappdev.uplift.data.models.ProfileData
109
import com.cornellappdev.uplift.data.models.WorkoutDomain
11-
import kotlinx.coroutines.async
12-
import kotlinx.coroutines.coroutineScope
1310
import java.time.Instant
1411
import javax.inject.Inject
1512
import javax.inject.Singleton
@@ -39,51 +36,36 @@ class ProfileRepository @Inject constructor(
3936
val userId = user.id.toIntOrNull()
4037
?: throw IllegalStateException("Invalid user ID: ${user.id}")
4138

42-
coroutineScope {
43-
val workoutDeferred = async {
44-
apolloClient.query(GetWorkoutsByIdQuery(userId)).execute()
45-
}
46-
val weeklyDeferred = async {
47-
apolloClient.query(GetWeeklyWorkoutDaysQuery(userId)).execute()
48-
}
49-
50-
val workoutResponse = workoutDeferred.await()
51-
if (workoutResponse.hasErrors()) {
52-
Log.e("ProfileRepo", "Workout query errors: ${workoutResponse.errors}")
53-
throw IllegalStateException("Workout query failed")
54-
}
55-
56-
val workouts = workoutResponse.data?.getWorkoutsById?.filterNotNull().orEmpty()
57-
58-
val workoutDomain = workouts.map {
59-
WorkoutDomain(
60-
gymName = it.workoutFields.gymName,
61-
timestamp = Instant.parse(it.workoutFields.workoutTime.toString())
62-
.toEpochMilli()
63-
)
64-
}
65-
66-
val weeklyResponse = weeklyDeferred.await()
67-
if (weeklyResponse.hasErrors()) {
68-
Log.e("ProfileRepo", "Weekly query errors: ${weeklyResponse.errors}")
69-
throw IllegalStateException("Weekly workout days query failed")
70-
}
71-
72-
val weeklyDays = weeklyResponse.data?.getWeeklyWorkoutDays?.filterNotNull().orEmpty()
73-
74-
ProfileData(
75-
name = user.name,
76-
netId = user.netId,
77-
encodedImage = user.encodedImage,
78-
totalGymDays = user.totalGymDays,
79-
activeStreak = user.activeStreak,
80-
maxStreak = user.maxStreak,
81-
streakStart = user.streakStart?.toString(),
82-
workoutGoal = user.workoutGoal ?: 0,
83-
workouts = workoutDomain,
84-
weeklyWorkoutDays = weeklyDays
39+
val weeklyResponse = apolloClient.query(GetWeeklyWorkoutDaysQuery(userId)).execute()
40+
41+
if (weeklyResponse.hasErrors()) {
42+
throw IllegalStateException("Weekly workout days query failed: ${weeklyResponse.errors}")
43+
}
44+
45+
val workouts = user.workoutHistory?.filterNotNull().orEmpty()
46+
47+
val workoutDomain = workouts.map {
48+
WorkoutDomain(
49+
gymName = it.workoutFields.gymName,
50+
timestamp = Instant.parse(it.workoutFields.workoutTime.toString())
51+
.toEpochMilli()
8552
)
8653
}
54+
55+
val weeklyDays = weeklyResponse.data?.getWeeklyWorkoutDays?.filterNotNull().orEmpty()
56+
57+
ProfileData(
58+
name = user.name,
59+
netId = user.netId,
60+
encodedImage = user.encodedImage,
61+
totalGymDays = user.totalGymDays,
62+
activeStreak = user.activeStreak,
63+
maxStreak = user.maxStreak,
64+
streakStart = user.streakStart?.toString(),
65+
workoutGoal = user.workoutGoal ?: 0,
66+
workouts = workoutDomain,
67+
weeklyWorkoutDays = weeklyDays
68+
)
8769
}.onFailure { e ->
8870
Log.e("ProfileRepo", "Failed to load profile", e)
8971
}
@@ -95,7 +77,7 @@ class ProfileRepository @Inject constructor(
9577
val response = apolloClient
9678
.mutation(
9779
SetWorkoutGoalsMutation(
98-
id = userId,
80+
userId = userId,
9981
workoutGoal = goal
10082
)
10183
)

app/src/main/java/com/cornellappdev/uplift/ui/viewmodels/profile/ProfileViewModel.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class ProfileViewModel @Inject constructor(
7979
time = formatTime.format(workoutInstant),
8080
date = formatDate.format(workoutInstant),
8181
timestamp = it.timestamp,
82-
dayOfWeek = formatDayOfWeek.format(workoutInstant),
8382
ago = calendar.timeAgoString()
8483
)
8584
}

0 commit comments

Comments
 (0)