diff --git a/lib/utils/riverpod_providers.dart b/lib/utils/riverpod_providers.dart index d50119559..49a258a6b 100644 --- a/lib/utils/riverpod_providers.dart +++ b/lib/utils/riverpod_providers.dart @@ -1,6 +1,8 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:privacyidea_authenticator/utils/customizations.dart'; +import '../l10n/app_localizations.dart'; import '../model/mixins/sortable_mixin.dart'; import '../model/push_request.dart'; import '../model/states/app_state.dart'; @@ -126,6 +128,18 @@ final draggingSortableProvider = StateProvider((ref) { final connectivityProvider = StreamProvider((ref) { Logger.info("New connectivityProvider created"); + ref.read(tokenProvider.notifier).isLoading.then( + (_) { + final newState = ref.read(tokenProvider); + Connectivity().checkConnectivity().then((connectivity) { + Logger.info("First connectivity check: $connectivity"); + final hasNoConnection = connectivity == ConnectivityResult.none; + if (hasNoConnection && newState.hasPushTokens && globalNavigatorKey.currentContext != null) { + ref.read(statusMessageProvider.notifier).state = (AppLocalizations.of(globalNavigatorKey.currentContext!)!.noNetworkConnection, null); + } + }); + }, + ); return Connectivity().onConnectivityChanged; }); diff --git a/lib/views/main_view/main_view.dart b/lib/views/main_view/main_view.dart index 82a6df927..89ec3210b 100644 --- a/lib/views/main_view/main_view.dart +++ b/lib/views/main_view/main_view.dart @@ -5,8 +5,8 @@ import 'package:flutterlifecyclehooks/flutterlifecyclehooks.dart'; import '../../model/states/app_state.dart'; import '../../utils/logger.dart'; import '../../utils/riverpod_providers.dart'; -import '../../widgets/app_wrapper.dart'; import '../../widgets/status_bar.dart'; +import 'main_view_widgets/connectivity_listener.dart'; import 'main_view_widgets/main_view_navigation_bar.dart'; import 'main_view_widgets/main_view_tokens_list.dart'; diff --git a/lib/views/main_view/main_view_widgets/connectivity_listener.dart b/lib/views/main_view/main_view_widgets/connectivity_listener.dart new file mode 100644 index 000000000..995dd6b54 --- /dev/null +++ b/lib/views/main_view/main_view_widgets/connectivity_listener.dart @@ -0,0 +1,27 @@ +import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../../../l10n/app_localizations.dart'; +import '../../../utils/logger.dart'; +import '../../../utils/riverpod_providers.dart'; + +class ConnectivityListener extends ConsumerWidget { + final Widget child; + const ConnectivityListener({required this.child, super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final connectivity = ref.watch(connectivityProvider).asData?.value; + if (connectivity != null && connectivity == ConnectivityResult.none) { + ref.read(tokenProvider.notifier).isLoading.then((value) { + final tokenState = ref.read(tokenProvider); + if (tokenState.hasPushTokens) { + Logger.info("Connectivity changed: $connectivity"); + ref.read(statusMessageProvider.notifier).state = (AppLocalizations.of(context)!.noNetworkConnection, null); + } + }); + } + return child; + } +} diff --git a/lib/widgets/app_wrapper.dart b/lib/widgets/app_wrapper.dart index e4230e81e..2ea8b9fd8 100644 --- a/lib/widgets/app_wrapper.dart +++ b/lib/widgets/app_wrapper.dart @@ -1,11 +1,7 @@ -import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:easy_dynamic_theme/easy_dynamic_theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import '../l10n/app_localizations.dart'; -import '../utils/riverpod_providers.dart'; - class AppWrapper extends StatelessWidget { final Widget child; @@ -18,23 +14,3 @@ class AppWrapper extends StatelessWidget { ); } } - -class ConnectivityListener extends ConsumerWidget { - final Widget child; - const ConnectivityListener({required this.child, super.key}); - - @override - Widget build(BuildContext context, WidgetRef ref) { - final connectivity = ref.watch(connectivityProvider).asData?.value; - final hasConnection = connectivity != null && connectivity != ConnectivityResult.none; - ref.read(tokenProvider.notifier).isLoading.then( - (value) { - final hasPushTokens = ref.read(tokenProvider).hasPushTokens; - if (!hasConnection && hasPushTokens) { - ref.read(statusMessageProvider.notifier).state = (AppLocalizations.of(context)!.noNetworkConnection, null); - } - }, - ); - return child; - } -}