From 0d86efb897f17064883c5dbf201746a403a49ab9 Mon Sep 17 00:00:00 2001 From: Frank Merkel <138444693+frankmer@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:04:03 +0200 Subject: [PATCH] pull after terminate the app --- lib/main.dart | 2 +- .../push_request_notifier.dart | 6 +- lib/state_notifiers/settings_notifier.dart | 65 +++++++++++++++---- lib/state_notifiers/token_notifier.dart | 5 +- lib/utils/push_provider.dart | 1 + lib/utils/riverpod_providers.dart | 9 +-- lib/views/main_view/main_view.dart | 4 +- 7 files changed, 68 insertions(+), 24 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 90c057774..0c6f2ff68 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -109,7 +109,7 @@ class _SplashScreenState extends ConsumerState { @override void initState() { super.initState(); - + Logger.info('Starting app.', name: 'main.dart#initState'); Future.delayed(_splashScreenDelay, () { if (mounted) { setState(() { diff --git a/lib/state_notifiers/push_request_notifier.dart b/lib/state_notifiers/push_request_notifier.dart index a480850ad..da2334e41 100644 --- a/lib/state_notifiers/push_request_notifier.dart +++ b/lib/state_notifiers/push_request_notifier.dart @@ -53,10 +53,8 @@ class PushRequestNotifier extends StateNotifier { handleIncomingMessage: (RemoteMessage message) => _handleIncomingAuthRequest(message), backgroundMessageHandler: _firebaseMessagingBackgroundHandler, ); - - if (pollingEnabled) { - PushProvider.pollForChallenges(); - } + Logger.info('PushProvider initialized. Polling for Challenges', name: 'main_screen.dart#_initStateAsync'); + PushProvider.pollForChallenges(); _startOrStopPolling(); } diff --git a/lib/state_notifiers/settings_notifier.dart b/lib/state_notifiers/settings_notifier.dart index fc45465b2..040ed3ce4 100644 --- a/lib/state_notifiers/settings_notifier.dart +++ b/lib/state_notifiers/settings_notifier.dart @@ -1,6 +1,7 @@ import 'dart:ui'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:privacyidea_authenticator/utils/logger.dart'; import '../model/states/settings_state.dart'; import '../repo/settings_repository.dart'; @@ -19,39 +20,79 @@ class SettingsNotifier extends StateNotifier { }); } void _loadFromRepo() async { + Logger.info('Loading settings from repo: $state', name: 'settings_notifier.dart#_loadFromRepo'); state = await _repo.loadSettings(); } void _saveToRepo() async { + Logger.info('Saving settings to repo: $state', name: 'settings_notifier.dart#_saveToRepo'); await _repo.saveSettings(state); } void addCrashReportRecipient(String email) { + Logger.info('Crash report recipient added: $email', name: 'settings_notifier.dart#addCrashReportRecipient'); var updatedSet = state.crashReportRecipients..add(email); state = state.copyWith(crashReportRecipients: updatedSet); } - set isFirstRun(bool value) => state = state.copyWith(isFirstRun: value); + set isFirstRun(bool value) { + Logger.info('First run set to $value', name: 'settings_notifier.dart#setFirstRun'); + state = state.copyWith(isFirstRun: value); + } - set hideOTPs(bool value) => state = state.copyWith(hideOpts: value); + set hideOTPs(bool value) { + Logger.info('Hide OTPs set to $value', name: 'settings_notifier.dart#setHideOTPs'); + state = state.copyWith(hideOpts: value); + } - set showGuideOnStart(bool value) => state = state.copyWith(showGuideOnStart: value); + set showGuideOnStart(bool value) { + Logger.info('Show guide on start set to $value', name: 'settings_notifier.dart#setShowGuideOnStart'); + state = state.copyWith(showGuideOnStart: value); + } - void setLocalePreference(Locale locale) => state = state.copyWith(localePreference: locale); + void setLocalePreference(Locale locale) { + Logger.info('Locale set to $locale', name: 'settings_notifier.dart#setLocalePreference'); + state = state.copyWith(localePreference: locale); + } - void setUseSystemLocale(bool value) => state = state.copyWith(useSystemLocale: value); + void setUseSystemLocale(bool value) { + Logger.info('Use system locale set to $value', name: 'settings_notifier.dart#setUseSystemLocale'); + state = state.copyWith(useSystemLocale: value); + } - void enablePolling() => state = state.copyWith(enablePolling: true); + void enablePolling() { + Logger.info('Polling set to true', name: 'settings_notifier.dart#setPolling'); + state = state.copyWith(enablePolling: true); + } - void disablePolling() => state = state.copyWith(enablePolling: false); + void disablePolling() { + Logger.info('Polling set to false', name: 'settings_notifier.dart#setPolling'); + state = state.copyWith(enablePolling: false); + } - void setPolling(bool value) => state = state.copyWith(enablePolling: value); + void setPolling(bool value) { + Logger.info('Polling set to $value', name: 'settings_notifier.dart#setPolling'); + state = state.copyWith(enablePolling: value); + } - void setLocale(Locale locale) => state = state.copyWith(localePreference: locale); + void setLocale(Locale locale) { + Logger.info('Locale set to $locale', name: 'settings_notifier.dart#setLocale'); + state = state.copyWith(localePreference: locale); + } - void setVerboseLogging(bool value) => state = state.copyWith(verboseLogging: value); + void setVerboseLogging(bool value) { + Logger.info('Verbose logging set to $value', name: 'settings_notifier.dart#setVerboseLogging'); + state = state.copyWith(verboseLogging: value); + } - void toggleVerboseLogging() => state = state.copyWith(verboseLogging: !state.verboseLogging); + void toggleVerboseLogging() { + final value = !state.verboseLogging; + Logger.info('Verbose logging set to $value', name: 'settings_notifier.dart#setVerboseLogging'); + state = state.copyWith(verboseLogging: value); + } - void setFirstRun(bool value) => state = state.copyWith(isFirstRun: value); + void setFirstRun(bool value) { + Logger.info('First run set to $value', name: 'settings_notifier.dart#setFirstRun'); + state = state.copyWith(isFirstRun: value); + } } diff --git a/lib/state_notifiers/token_notifier.dart b/lib/state_notifiers/token_notifier.dart index b3e41776e..a6be31b16 100644 --- a/lib/state_notifiers/token_notifier.dart +++ b/lib/state_notifiers/token_notifier.dart @@ -41,6 +41,7 @@ class TokenNotifier extends StateNotifier { Future _loadTokenList() async { List tokens = await StorageUtil.loadAllTokens(); + Logger.info('Loaded tokens from storage: $tokens', name: 'token_notifier.dart#_loadTokenList'); final pushTokens = tokens.whereType(); if (pushTokens.isNotEmpty) { checkNotificationPermission(); @@ -56,6 +57,7 @@ class TokenNotifier extends StateNotifier { void refreshTokens() async { List tokens = await StorageUtil.loadAllTokens(); final rolledOutPushToken = tokens.whereType().where((element) => element.isRolledOut).toList(); + Logger.info('Refreshed Pushtokens from storage: $tokens', name: 'token_notifier.dart#refreshTokens'); state = state.updateTokens(rolledOutPushToken); } @@ -174,7 +176,7 @@ class TokenNotifier extends StateNotifier { Future addPushRequestToToken(PushRequest pr) async { PushToken? token = state.tokens.whereType().firstWhereOrNull((t) => t.serial == pr.serial && t.isRolledOut); - + Logger.info('Adding push request ${pr.id} to token ${token?.id}', name: 'main_screen.dart#_handleIncomingChallenge', error: pr.serial); if (token == null) { Logger.warning('The requested token does not exist or is not rolled out.', name: 'main_screen.dart#_handleIncomingChallenge', error: pr.serial); } else { @@ -235,6 +237,7 @@ class TokenNotifier extends StateNotifier { Future rolloutPushToken(PushToken token) async { token = getTokenFromId(token.id) as PushToken? ?? token; + Logger.info('Rolling out token ${token.serial}', name: 'token_widgets.dart#rolloutPushToken'); if (token.isRolledOut) return true; if (token.rolloutState != PushTokenRollOutState.rolloutNotStarted && token.rolloutState != PushTokenRollOutState.generatingRSAKeyPairFailed && diff --git a/lib/utils/push_provider.dart b/lib/utils/push_provider.dart index f26e097ca..3633a49d8 100644 --- a/lib/utils/push_provider.dart +++ b/lib/utils/push_provider.dart @@ -197,6 +197,7 @@ abstract class PushProvider { } // Start request for each token + Logger.info('Polling for challenges: ${pushTokens.length} Tokens', name: 'push_provider.dart#pollForChallenges'); for (PushToken p in pushTokens) { pollForChallenge(p).then((errorMessage) { if (errorMessage != null && showMessageForEachToken) { diff --git a/lib/utils/riverpod_providers.dart b/lib/utils/riverpod_providers.dart index 4e62ce64b..006f204f3 100644 --- a/lib/utils/riverpod_providers.dart +++ b/lib/utils/riverpod_providers.dart @@ -32,15 +32,14 @@ final tokenProvider = StateNotifierProvider((ref) { (previous, next) { switch (next) { case AppState.resume: - //startPolling(); if (previous == AppState.pause) { - Logger.info('refreshing tokens on resume'); + Logger.info('Refreshing tokens on resume'); tokenNotifier.refreshTokens(); ref.read(appStateProvider.notifier).setAppState(AppState.running); } break; case AppState.pause: - //stopPolling(); + //stopPolling() break; default: break; @@ -57,12 +56,13 @@ final tokenProvider = StateNotifierProvider((ref) { ref.container, (previous, next) { if (next == null) return; - Logger.warning('next: $next'); if (next.accepted == null) { + Logger.info("tokenProvider received new pushRequest"); tokenNotifier.addPushRequestToToken(next); return; } if (next.accepted != null) { + Logger.info("tokenProvider received pushRequest with accepted=${next.accepted}... removing from state."); tokenNotifier.removePushRequest(next); FlutterLocalNotificationsPlugin().cancelAll(); return; @@ -93,6 +93,7 @@ final pushRequestProvider = StateNotifierProvider with LifecycleMixin { final globalKey = GlobalKey(); @override void onResume() { - Logger.info('onResume'); + Logger.info('MainView Resume', name: 'main_view.dart#onResume'); globalRef?.read(appStateProvider.notifier).setAppState(AppState.resume); } @override void onPause() { - Logger.info('onPause'); + Logger.info('MainView Pause', name: 'main_view.dart#onPause'); globalRef?.read(appStateProvider.notifier).setAppState(AppState.pause); }