Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Count this week from Sunday instead of the last seven days

Barnabas78bd6c3b1ed3
diff --git a/lib/screens/stats_screen.dart b/lib/screens/stats_screen.dart
index 4fe93634..bd11a86f 100644
--- a/lib/screens/stats_screen.dart
+++ b/lib/screens/stats_screen.dart
@@ -1628,11 +1628,15 @@ class _StatsScreenState extends State<StatsScreen>
double _todaySeconds(Map<String, dynamic> dailyMap) =>
_daySeconds(dailyMap, _dateKey(DateTime.now()));
+ /// Sunday through today, so the week total resets on Sunday morning instead
+ /// of sliding. Days are built from year/month/day rather than by subtracting
+ /// Durations, or a DST change would repeat or skip a date key.
double _weekSeconds(Map<String, dynamic> dailyMap) {
final now = DateTime.now();
double total = 0;
- for (int i = 0; i < 7; i++) {
- total += _daySeconds(dailyMap, _dateKey(now.subtract(Duration(days: i))));
+ for (int i = now.weekday % 7; i >= 0; i--) {
+ total += _daySeconds(
+ dailyMap, _dateKey(DateTime(now.year, now.month, now.day - i)));
}
return total;
}
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index 634bdcc3..c037e2c3 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -54,6 +54,10 @@ class HomeWidgetService {
int _weekBase = 0;
int _localAddedToday = 0;
int _localAddedWeek = 0;
+ // Date the bases were captured on. Ticking local seconds onto a base from a
+ // previous day (or, since the week is Sun-Sat, a previous week) shows a total
+ // that should have reset.
+ String? _statsBaseDay;
/// Call after AudioPlayerService is initialized to start pushing state.
Future<void> init() async {
@@ -863,6 +867,7 @@ class HomeWidgetService {
_weekBase = week;
_localAddedToday = 0;
_localAddedWeek = 0;
+ _statsBaseDay = _dateKey(DateTime.now());
await HomeWidget.saveWidgetData<int>('widget_stats_today', today);
await HomeWidget.saveWidgetData<int>('widget_stats_week', week);
@@ -886,6 +891,16 @@ class HomeWidgetService {
/// the 15-min stats timer is throttled by Android Doze.
Future<void> addLocalListeningSeconds(int seconds) async {
if (seconds <= 0) return;
+ // Crossed midnight since the bases were taken: they belong to yesterday's
+ // "today" and possibly last week. Drop the tick and let a refresh reset
+ // them - not forced, so an offline night can't turn every sync into an
+ // unthrottled fetch. Worst case the widget stops ticking until the 15-min
+ // timer lands, which beats showing a total that should have reset.
+ if (_statsBaseDay != null && _statsBaseDay != _dateKey(DateTime.now())) {
+ debugPrint('[StatsWidget] Base is from $_statsBaseDay, refreshing before tick');
+ await refreshStats();
+ return;
+ }
_localAddedToday += seconds;
_localAddedWeek += seconds;
try {
@@ -981,11 +996,14 @@ class HomeWidgetService {
double _todaySeconds(Map<String, dynamic> dailyMap) =>
_daySeconds(dailyMap, _dateKey(DateTime.now()));
+ /// Sunday through today. Must match `_weekSeconds` on the stats page, or the
+ /// widget and the app disagree about the same "This week" number.
double _weekSeconds(Map<String, dynamic> dailyMap) {
final now = DateTime.now();
double total = 0;
- for (int i = 0; i < 7; i++) {
- total += _daySeconds(dailyMap, _dateKey(now.subtract(Duration(days: i))));
+ for (int i = now.weekday % 7; i >= 0; i--) {
+ total += _daySeconds(
+ dailyMap, _dateKey(DateTime(now.year, now.month, now.day - i)));
}
return total;
}