fix(sessions): resolve cross-midnight split bug by migrating to full DateTime (wger-project/wger#2379)#1248
Open
pankaj-basnet wants to merge 2 commits into
Conversation
- Convert timeStart and timeEnd from TimeOfDay to DateTime for better precision and easier duration calculation. - Includes overnight session detection logic - Limits active session fetching to a new sessionMaxDuration constant (5 hours). - Relates to: wger-project/wger#2379
Refactors WorkoutSession storage from TimeOfDay to DateTime to enable accurate cross-midnight session tracking. - Adds DateTimeTextConverter to handle ISO8601 serialization/deserialization. - Updates WorkoutSessionTable to use the new DateTime converter. - Refactors WorkoutLogRepository to use UTC ISO8601 strings for time range queries in Drift. - Improves lazy session creation logic to handle closed vs. open session states.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
still WIP
PR Title
fix(sessions): resolve cross-midnight split bug by migrating to full DateTime (#2379)PR Description
Overview
This PR resolves the persistent "midnight session split" bug (Issue #2379). Previously, workout sessions were tracked using
TimeOfDay, which inherently strips date context. This caused duration calculation failures and split active sessions when a user worked out across midnight.Architecturally, this PR migrates the entire session time-tracking pipeline—from the Drift database layer up to the Riverpod providers and UI forms—to use fully qualified
DateTimeobjects (UTC ISO8601 strings in the database).Key Architectural Changes
TimeOfDaywithDateTimefortimeStartandtimeEndto natively support date-spanning logic.sessionMaxDuration(5 hours) constant. TheWorkoutLogRepositorynow accurately queries for open sessions within this rolling time window rather than relying on static calendar-day boundary checks (isSameDayAs).SessionFormnow automatically detects if an end time is chronologically earlier than a start time (e.g., 23:00 to 01:00) and safely adds a 1-day offset to theDateTimeobject.File Modifications
lib/database/converters/date_only_text_converter.dartDateTimeTextConverterto serialize/deserializeDateTimeobjects to ISO8601 strings for SQLite.lib/database/powersync/tables/routines.dartWorkoutSessionTableschema columns (time_start,time_end) to leverage the newDateTimeTextConverter.lib/helpers/routines/validators.dartvalidateWorkoutSessionTimessignature to acceptDateTimeparameters instead ofTimeOfDay.lib/models/workouts/session.darttimeStartandtimeEndmodel properties. AddedsessionMaxDurationconstant. Refactoreddurationand string formatter getters for null-safety.lib/providers/gym_state.dartGymModeStatestartTimetoDateTime, defaulting toclock.now().lib/providers/workout_logs_repository.dartaddLocalDriftlazy session creation. It now checks for sessions within the 5-hour window and explicitly creates new sessions if the previous matching session is already closed (timeEnd != null).lib/widgets/dashboard/calendar.dartTimeOfDayfrom the newDateTimemodels for correct visual rendering.lib/widgets/routines/forms/session.dartend.isBefore(start)check to gracefully add a 24-hour offset for midnight-crossing logs.lib/widgets/routines/gym_mode/session_page.dartsessionMaxDurationinstead of strictly checking if the session shares the exact calendar date.Related Issues
Related Issue(s)
Please check that the PR fulfills these requirements
dart format .)