getStreakSteps method

Future<int> getStreakSteps(
  1. String userEmail
)

Returns the number of active streak steps (days) the user has maintained.

Implementation

Future<int> getStreakSteps(String userEmail) async {
  final userRef = await _getUserDocRef(userEmail);
  final streaks = await userRef
      .collection('streaks')
      .where('isActive', isEqualTo: true)
      .limit(1)
      .get();

  if (streaks.docs.isEmpty) return 0;

  final startedAt = (streaks.docs.first['startedAt'] as Timestamp).toDate();
  final now = DateTime.now();

  final today = DateTime(now.year, now.month, now.day);
  final start = DateTime(startedAt.year, startedAt.month, startedAt.day);

  if (await getDoneExercisesInSeconds(userEmail) < 300) {
    return today.difference(start).inDays;
  }
  return today.difference(start).inDays + 1;
}