Show cached schedule coverage status in admin
This commit is contained in:
@@ -4,6 +4,75 @@ import 'package:intl/intl.dart';
|
||||
import '../local/models.dart';
|
||||
import 'myquran_service.dart';
|
||||
|
||||
class ScheduleCacheStatus {
|
||||
final DateTime? startDate;
|
||||
final DateTime? endDate;
|
||||
final int cachedDays;
|
||||
final int daysUntilRefresh;
|
||||
|
||||
const ScheduleCacheStatus({
|
||||
required this.startDate,
|
||||
required this.endDate,
|
||||
required this.cachedDays,
|
||||
required this.daysUntilRefresh,
|
||||
});
|
||||
|
||||
const ScheduleCacheStatus.empty()
|
||||
: startDate = null,
|
||||
endDate = null,
|
||||
cachedDays = 0,
|
||||
daysUntilRefresh = -1;
|
||||
|
||||
bool get hasData => startDate != null && endDate != null && cachedDays > 0;
|
||||
bool get isExpired => hasData && daysUntilRefresh < 0;
|
||||
bool get needsRefreshSoon => hasData && daysUntilRefresh <= 3;
|
||||
|
||||
static ScheduleCacheStatus fromSchedules(
|
||||
Iterable<DailyPrayerSchedule> schedules,
|
||||
DateTime referenceDate,
|
||||
) {
|
||||
DateTime? startDate;
|
||||
DateTime? endDate;
|
||||
var cachedDays = 0;
|
||||
|
||||
for (final schedule in schedules) {
|
||||
final parsedDate = DateTime.tryParse(schedule.date);
|
||||
if (parsedDate == null) continue;
|
||||
|
||||
final normalized = DateTime(
|
||||
parsedDate.year,
|
||||
parsedDate.month,
|
||||
parsedDate.day,
|
||||
);
|
||||
|
||||
cachedDays++;
|
||||
startDate = startDate == null || normalized.isBefore(startDate)
|
||||
? normalized
|
||||
: startDate;
|
||||
endDate = endDate == null || normalized.isAfter(endDate)
|
||||
? normalized
|
||||
: endDate;
|
||||
}
|
||||
|
||||
if (startDate == null || endDate == null || cachedDays == 0) {
|
||||
return const ScheduleCacheStatus.empty();
|
||||
}
|
||||
|
||||
final today = DateTime(
|
||||
referenceDate.year,
|
||||
referenceDate.month,
|
||||
referenceDate.day,
|
||||
);
|
||||
|
||||
return ScheduleCacheStatus(
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
cachedDays: cachedDays,
|
||||
daysUntilRefresh: endDate.difference(today).inDays,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Service to sync monthly prayer data from MyQuran API → Hive.
|
||||
class SyncService {
|
||||
SyncService._();
|
||||
@@ -90,4 +159,13 @@ class SyncService {
|
||||
final dateStr = DateFormat('yyyy-MM-dd').format(dateToFetch);
|
||||
return scheduleBox.get(dateStr);
|
||||
}
|
||||
|
||||
ScheduleCacheStatus getCacheStatus([DateTime? referenceDate]) {
|
||||
final scheduleBox =
|
||||
Hive.box<DailyPrayerSchedule>(HiveBoxes.prayerSchedule);
|
||||
return ScheduleCacheStatus.fromSchedules(
|
||||
scheduleBox.values,
|
||||
referenceDate ?? DateTime.now(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user