Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private fun SmartInsightsContent(modifier: Modifier = Modifier) {
val activeProfile by userProfileRepository.activeProfile.collectAsState()
val profileId = activeProfile?.id ?: "default"

val nowMs = remember { currentTimeMillis() }
var nowMs by remember { mutableStateOf(currentTimeMillis()) }
val twentyEightDaysMs = 28L * 24 * 60 * 60 * 1000

var isLoading by remember { mutableStateOf(true) }
Expand All @@ -117,6 +117,7 @@ private fun SmartInsightsContent(modifier: Modifier = Modifier) {
var weightHistory by remember { mutableStateOf<List<SessionSummary>>(emptyList()) }

LaunchedEffect(profileId) {
nowMs = currentTimeMillis()
withContext(Dispatchers.IO) {
sessionSummaries =
repository.getSessionSummariesSince(nowMs - twentyEightDaysMs, profileId)
Comment on lines +120 to 123

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When profileId changes, isLoading should be reset to true at the start of the LaunchedEffect. Currently, if the user switches profiles, isLoading remains false (from the previous profile's state). The update to nowMs at line 120 will trigger a recomposition, causing insights to be recomputed using the new timestamp but stale sessionSummaries from the previous profile before the new data fetch completes. Using a local variable for the fetch also ensures consistency within the effect.

Suggested change
nowMs = currentTimeMillis()
withContext(Dispatchers.IO) {
sessionSummaries =
repository.getSessionSummariesSince(nowMs - twentyEightDaysMs, profileId)
isLoading = true
val freshNow = currentTimeMillis()
nowMs = freshNow
withContext(Dispatchers.IO) {
sessionSummaries =
repository.getSessionSummariesSince(freshNow - twentyEightDaysMs, profileId)

Expand All @@ -137,13 +138,13 @@ private fun SmartInsightsContent(modifier: Modifier = Modifier) {
}

// Compute all insights
val weeklyVolume = remember(sessionSummaries) {
val weeklyVolume = remember(sessionSummaries, nowMs) {
SmartSuggestionsEngine.computeWeeklyVolume(sessionSummaries, nowMs)
}
val balanceAnalysis = remember(sessionSummaries) {
val balanceAnalysis = remember(sessionSummaries, nowMs) {
SmartSuggestionsEngine.analyzeBalance(sessionSummaries, nowMs)
}
val neglectedExercises = remember(exerciseLastPerformed) {
val neglectedExercises = remember(exerciseLastPerformed, nowMs) {
SmartSuggestionsEngine.findNeglectedExercises(exerciseLastPerformed, nowMs)
}
val plateaus = remember(weightHistory) {
Expand Down Expand Up @@ -203,7 +204,7 @@ private fun SmartInsightsContent(modifier: Modifier = Modifier) {

// Section F: Training Readiness / ACWR (ACWR-01)
item {
val readiness = remember(sessionSummaries) {
val readiness = remember(sessionSummaries, nowMs) {
ReadinessEngine.computeReadiness(sessionSummaries, nowMs)
}
ReadinessBriefingCard(readinessResult = readiness)
Expand Down
Loading