checkIfFirstExerciseInTimeWindow method
Implementation
Future<bool> checkIfFirstExerciseInTimeWindow(String userId) async {
final now = DateTime.now();
final fourHoursAgo = now.subtract(const Duration(hours: 4));
// Query for any completed exercises in the last 4 hours
final snapshot = await FirebaseFirestore.instance
.collection('users')
.doc(userId)
.collection('exerciseLogs')
.where('endTime', isGreaterThan: Timestamp.fromDate(fourHoursAgo))
.orderBy('endTime', descending: true)
.limit(2) // Get at most 2 to check if the current one is first
.get();
// If there's only 1 result (the one we just added), it's the first in 4 hours
return snapshot.docs.length <= 1;
}