Skip to content

Commit

Permalink
fix connectivity_check
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Feb 4, 2024
1 parent 0ed512c commit a403fd7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
23 changes: 12 additions & 11 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -52,6 +62,7 @@ class App extends ConsumerWidget {
_setupNotifications(ref, userState);

return MaterialApp(
title: 'GoCast',
debugShowCheckedModeBanner: false,
localizationsDelegates: const [
AppLocalizations.delegate,
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions lib/providers.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -84,3 +85,8 @@ final isSearchActiveProvider = StateProvider<bool>((ref) => false);
final playbackSpeedsProvider = StateProvider<List<double>>((ref) {
return [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2];
});

final connectivityProvider = StreamProvider<ConnectivityResult>((ref) {
return Connectivity().onConnectivityChanged;
});

3 changes: 0 additions & 3 deletions lib/view_models/user_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:logger/logger.dart';

class UserViewModel extends StateNotifier<UserState> {
final Logger _logger = Logger();
bool _isTokenChecked = false; // Flag to track token check

final GrpcHandler _grpcHandler;

Expand Down Expand Up @@ -154,15 +153,13 @@ class UserViewModel extends StateNotifier<UserState> {
}

Future<void> _checkToken() async {
if (_isTokenChecked) return;
String token = await _getToken();
if (token.isNotEmpty && !Jwt.isExpired(token)) {
_logger.i('Token found, fetching user: $token');
fetchUser();
} else {
_logger.i('Token not found or expired');
}
_isTokenChecked = true;
}

Future<String> _getToken() async {
Expand Down

0 comments on commit a403fd7

Please sign in to comment.