diff --git a/lib/main.dart b/lib/main.dart index 21f9f4d..f29f3b5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -42,7 +42,17 @@ class App extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - _checkConnectivityAndRedirect(context, ref); + final connectivityStatus = ref.watch(connectivityProvider); + connectivityStatus.whenData((result) { + if(result == ConnectivityResult.none) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (ModalRoute.of(context)?.settings.name != '/downloads') { + Navigator.of(context).pushNamed('/downloads'); + return; + } + }); + } + }); final userState = ref.watch(userViewModelProvider); @@ -52,6 +62,7 @@ class App extends ConsumerWidget { _setupNotifications(ref, userState); return MaterialApp( + title: 'GoCast', debugShowCheckedModeBanner: false, localizationsDelegates: const [ AppLocalizations.delegate, @@ -71,16 +82,6 @@ class App extends ConsumerWidget { ); } - void _checkConnectivityAndRedirect(BuildContext context, WidgetRef ref) { - Connectivity().checkConnectivity().then((connectivityResult) { - if (connectivityResult == ConnectivityResult.none) { - WidgetsBinding.instance.addPostFrameCallback((_) { - Navigator.of(context).pushNamed('/downloads'); - }); - } - }); - } - void _handleErrors(WidgetRef ref, UserState userState) { // Check for errors in userState and show a SnackBar if there are any if (userState.error != null) { diff --git a/lib/providers.dart b/lib/providers.dart index c3af3b4..baecf02 100644 --- a/lib/providers.dart +++ b/lib/providers.dart @@ -1,3 +1,4 @@ +import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:gocast_mobile/config/app_config.dart'; @@ -84,3 +85,8 @@ final isSearchActiveProvider = StateProvider((ref) => false); final playbackSpeedsProvider = StateProvider>((ref) { return [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]; }); + +final connectivityProvider = StreamProvider((ref) { + return Connectivity().onConnectivityChanged; +}); + diff --git a/lib/view_models/user_view_model.dart b/lib/view_models/user_view_model.dart index c04a03f..f6d70d6 100644 --- a/lib/view_models/user_view_model.dart +++ b/lib/view_models/user_view_model.dart @@ -17,7 +17,6 @@ import 'package:logger/logger.dart'; class UserViewModel extends StateNotifier { final Logger _logger = Logger(); - bool _isTokenChecked = false; // Flag to track token check final GrpcHandler _grpcHandler; @@ -154,7 +153,6 @@ class UserViewModel extends StateNotifier { } Future _checkToken() async { - if (_isTokenChecked) return; String token = await _getToken(); if (token.isNotEmpty && !Jwt.isExpired(token)) { _logger.i('Token found, fetching user: $token'); @@ -162,7 +160,6 @@ class UserViewModel extends StateNotifier { } else { _logger.i('Token not found or expired'); } - _isTokenChecked = true; } Future _getToken() async {