Skip to content

Commit

Permalink
Fixes an issue with listening to the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmer committed Nov 3, 2023
1 parent 2ce2245 commit 4f22703
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
14 changes: 14 additions & 0 deletions lib/utils/riverpod_providers.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -126,6 +128,18 @@ final draggingSortableProvider = StateProvider<SortableMixin?>((ref) {

final connectivityProvider = StreamProvider<ConnectivityResult>((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;
});

Expand Down
2 changes: 1 addition & 1 deletion lib/views/main_view/main_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
27 changes: 27 additions & 0 deletions lib/views/main_view/main_view_widgets/connectivity_listener.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 0 additions & 24 deletions lib/widgets/app_wrapper.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
}

0 comments on commit 4f22703

Please sign in to comment.