worldhopper/lib/providers/connectivity_provider.dart
Felipe M. 64eee6b81b
feat: implement internet connectivity tracking with offline indicators
Adds real-time connectivity monitoring and visual feedback across the app. Offline icon appears in all app bars when disconnected, cached content shows download badges, and access control prevents browsing servers or opening uncached content without internet connection.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-28 20:13:21 +01:00

55 lines
1.8 KiB
Dart

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worldhopper/services/connectivity_service.dart';
import 'package:worldhopper/services/epub_download_service.dart';
import 'package:worldhopper/services/offline_content_checker.dart';
part 'connectivity_provider.g.dart';
/// Provider for ConnectivityService singleton
@riverpod
ConnectivityService connectivityService(ConnectivityServiceRef ref) {
return ConnectivityService();
}
/// Stream provider for connectivity status
///
/// Emits true when online, false when offline.
/// Automatically updates all watching widgets when connectivity changes.
@riverpod
Stream<bool> connectivityStatus(ConnectivityStatusRef ref) {
final service = ref.watch(connectivityServiceProvider);
return service.connectivityStream;
}
/// Provider for current connectivity state
///
/// This provider caches the current connectivity status and updates
/// automatically when the status changes.
@riverpod
class ConnectivityState extends _$ConnectivityState {
@override
Future<bool> build() async {
final service = ref.watch(connectivityServiceProvider);
// Subscribe to connectivity changes
ref.listen(connectivityStatusProvider, (_, next) {
next.whenData((isOnline) => state = AsyncValue.data(isOnline));
});
// Return initial connectivity status
return await service.isConnected();
}
}
/// Provider for EpubDownloadService singleton
@riverpod
EpubDownloadService epubDownloadService(EpubDownloadServiceRef ref) {
return EpubDownloadService();
}
/// Provider for OfflineContentChecker singleton
@riverpod
OfflineContentChecker offlineContentChecker(OfflineContentCheckerRef ref) {
final epubService = ref.watch(epubDownloadServiceProvider);
return OfflineContentChecker(epubService: epubService);
}