Harden app for 24-7 offline-first operation
This commit is contained in:
@@ -44,12 +44,15 @@ class _HomeViewState extends ConsumerState<HomeView> {
|
||||
final List<LogicalKeyboardKey> _simulationShortcutKeys = [];
|
||||
Timer? _comboResetTimer;
|
||||
Timer? _simulationShortcutTimer;
|
||||
Timer? _autoRefreshTimer;
|
||||
bool _isAutoRefreshRunning = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_checkAutoSync();
|
||||
_startAutoRefreshMonitor();
|
||||
if (mounted) {
|
||||
_homeFocusNode.requestFocus();
|
||||
}
|
||||
@@ -60,21 +63,38 @@ class _HomeViewState extends ConsumerState<HomeView> {
|
||||
void dispose() {
|
||||
_comboResetTimer?.cancel();
|
||||
_simulationShortcutTimer?.cancel();
|
||||
_autoRefreshTimer?.cancel();
|
||||
_homeFocusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startAutoRefreshMonitor() {
|
||||
_autoRefreshTimer?.cancel();
|
||||
_autoRefreshTimer = Timer.periodic(
|
||||
const Duration(hours: 6),
|
||||
(_) => _checkAutoSync(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _checkAutoSync() async {
|
||||
final schedule = ref.read(todayScheduleProvider);
|
||||
if (schedule == null) {
|
||||
debugPrint('[AutoSync] No schedule found for today! Starting auto-sync...');
|
||||
final success = await SyncService.instance.syncMonthlyData();
|
||||
if (success && mounted) {
|
||||
debugPrint('[AutoSync] Success! Invalidating todayScheduleProvider.');
|
||||
if (_isAutoRefreshRunning || !mounted) return;
|
||||
_isAutoRefreshRunning = true;
|
||||
try {
|
||||
final result = await SyncService.instance.autoRefreshIfNeeded();
|
||||
if (!mounted) return;
|
||||
|
||||
if (result.synced) {
|
||||
debugPrint('[AutoSync] Cache refreshed successfully.');
|
||||
ref.invalidate(todayScheduleProvider);
|
||||
} else {
|
||||
debugPrint('[AutoSync] Failed or data remained empty.');
|
||||
ref.invalidate(scheduleCacheStatusProvider);
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.attempted) {
|
||||
debugPrint('[AutoSync] Refresh attempt failed. Staying on local cache.');
|
||||
}
|
||||
} finally {
|
||||
_isAutoRefreshRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user