import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:worldhopper/providers/download_provider.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 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 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); final chaptersRepo = ref.watch(downloadedChaptersRepositoryProvider); return OfflineContentChecker( epubService: epubService, chaptersRepo: chaptersRepo, ); }