Skip to content

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
wger-project:masterfrom
pankaj-basnet:fix/midnight-workout-session-split--backend-2379
Open

fix(sessions): resolve cross-midnight split bug by migrating to full DateTime (wger-project/wger#2379)#1248
pankaj-basnet wants to merge 2 commits into
wger-project:masterfrom
pankaj-basnet:fix/midnight-workout-session-split--backend-2379

Conversation

@pankaj-basnet

Copy link
Copy Markdown
Contributor

still WIP

  • TODOs: fix test files

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 DateTime objects (UTC ISO8601 strings in the database).

Key Architectural Changes

  • Time Tracking Migration: Replaced TimeOfDay with DateTime for timeStart and timeEnd to natively support date-spanning logic.
  • Intelligent Lazy Sessions: Introduced a sessionMaxDuration (5 hours) constant. The WorkoutLogRepository now accurately queries for open sessions within this rolling time window rather than relying on static calendar-day boundary checks (isSameDayAs).
  • Cross-Midnight UI Logic: The SessionForm now 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 the DateTime object.

File Modifications

# Code File Changed Description of Code Changes
1 lib/database/converters/date_only_text_converter.dart Created DateTimeTextConverter to serialize/deserialize DateTime objects to ISO8601 strings for SQLite.
2 lib/database/powersync/tables/routines.dart Updated WorkoutSessionTable schema columns (time_start, time_end) to leverage the new DateTimeTextConverter.
3 lib/helpers/routines/validators.dart Updated validateWorkoutSessionTimes signature to accept DateTime parameters instead of TimeOfDay.
4 lib/models/workouts/session.dart Migrated timeStart and timeEnd model properties. Added sessionMaxDuration constant. Refactored duration and string formatter getters for null-safety.
5 lib/providers/gym_state.dart Migrated GymModeState startTime to DateTime, defaulting to clock.now().
6 lib/providers/workout_logs_repository.dart Refactored addLocalDrift lazy 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).
7 lib/widgets/dashboard/calendar.dart Adapted the UI layer to extract TimeOfDay from the new DateTime models for correct visual rendering.
8 lib/widgets/routines/forms/session.dart Added null-safe, local state handling for time inputs. Implemented the end.isBefore(start) check to gracefully add a 24-hour offset for midnight-crossing logs.
9 lib/widgets/routines/gym_mode/session_page.dart Overhauled the active session lookup query. It now finds unclosed sessions within sessionMaxDuration instead of strictly checking if the session shares the exact calendar date.

Related Issues

Related Issue(s)

Please check that the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Set a 100-character limit to avoid white space diffs (run dart format .)

- 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Training over night breaks routine into two

1 participant