Skip to content

Commit

Permalink
pull after terminate the app
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmer committed Sep 18, 2023
1 parent c26ce69 commit 0d86efb
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class _SplashScreenState extends ConsumerState<SplashScreen> {
@override
void initState() {
super.initState();

Logger.info('Starting app.', name: 'main.dart#initState');
Future.delayed(_splashScreenDelay, () {
if (mounted) {
setState(() {
Expand Down
6 changes: 2 additions & 4 deletions lib/state_notifiers/push_request_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ class PushRequestNotifier extends StateNotifier<PushRequest?> {
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();
}

Expand Down
65 changes: 53 additions & 12 deletions lib/state_notifiers/settings_notifier.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,39 +20,79 @@ class SettingsNotifier extends StateNotifier<SettingsState> {
});
}
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);
}
}
5 changes: 4 additions & 1 deletion lib/state_notifiers/token_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TokenNotifier extends StateNotifier<TokenState> {

Future<void> _loadTokenList() async {
List<Token> tokens = await StorageUtil.loadAllTokens();
Logger.info('Loaded tokens from storage: $tokens', name: 'token_notifier.dart#_loadTokenList');
final pushTokens = tokens.whereType<PushToken>();
if (pushTokens.isNotEmpty) {
checkNotificationPermission();
Expand All @@ -56,6 +57,7 @@ class TokenNotifier extends StateNotifier<TokenState> {
void refreshTokens() async {
List<Token> tokens = await StorageUtil.loadAllTokens();
final rolledOutPushToken = tokens.whereType<PushToken>().where((element) => element.isRolledOut).toList();
Logger.info('Refreshed Pushtokens from storage: $tokens', name: 'token_notifier.dart#refreshTokens');
state = state.updateTokens(rolledOutPushToken);
}

Expand Down Expand Up @@ -174,7 +176,7 @@ class TokenNotifier extends StateNotifier<TokenState> {

Future<void> addPushRequestToToken(PushRequest pr) async {
PushToken? token = state.tokens.whereType<PushToken>().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 {
Expand Down Expand Up @@ -235,6 +237,7 @@ class TokenNotifier extends StateNotifier<TokenState> {

Future<bool> 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 &&
Expand Down
1 change: 1 addition & 0 deletions lib/utils/push_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/riverpod_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ final tokenProvider = StateNotifierProvider<TokenNotifier, TokenState>((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;
Expand All @@ -57,12 +56,13 @@ final tokenProvider = StateNotifierProvider<TokenNotifier, TokenState>((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;
Expand Down Expand Up @@ -93,6 +93,7 @@ final pushRequestProvider = StateNotifierProvider<PushRequestNotifier, PushReque
ref.container,
(previous, next) {
if (previous == AppState.pause && next == AppState.resume) {
Logger.info('Polling for challenges on resume');
PushProvider.pollForChallenges();
}
},
Expand Down
4 changes: 2 additions & 2 deletions lib/views/main_view/main_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class _MainViewState extends ConsumerState<MainView> with LifecycleMixin {
final globalKey = GlobalKey<NestedScrollViewState>();
@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);
}

Expand Down

0 comments on commit 0d86efb

Please sign in to comment.