Skip to content

Commit

Permalink
fixes polling with zero tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmer committed Nov 3, 2023
1 parent 4f22703 commit 63c3a2b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
36 changes: 20 additions & 16 deletions lib/utils/push_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ class PushProvider {
// Start polling if enabled and not already polling
if (pollingEnabled && _pollTimer == null) {
Logger.info('Polling is enabled.', name: 'push_provider.dart#_startPollingIfEnabled');
_pollTimer = Timer.periodic(const Duration(seconds: 3), (_) => pollForChallenges());
pollForChallenges();
_pollTimer = Timer.periodic(const Duration(seconds: 3), (_) => pollForChallenges(isManually: false));
pollForChallenges(isManually: false);
return;
}
// Stop polling if it's disabled and currently polling
Expand All @@ -237,24 +237,28 @@ class PushProvider {
return;
}

Future<void> pollForChallenges() async {
final connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
Logger.info('Tried to poll without any internet connection available.', name: 'push_provider.dart#pollForChallenges');
globalRef?.read(statusMessageProvider.notifier).state = (
AppLocalizations.of(globalNavigatorKey.currentContext!)!.pollingFailed,
AppLocalizations.of(globalNavigatorKey.currentContext!)!.noNetworkConnection,
);
return;
}

Future<void> pollForChallenges({required bool isManually}) async {
// Get all push tokens
List<PushToken> pushTokens = globalRef?.read(tokenProvider).tokens.whereType<PushToken>().where((t) => t.isRolledOut && t.url != null).toList() ?? [];

// Disable polling if no push tokens exist
if (pushTokens.isEmpty && globalRef?.read(settingsProvider).enablePolling == true) {
Logger.info('No push token is available for polling, polling is disabled.', name: 'push_provider.dart#pollForChallenges');
globalRef?.read(settingsProvider.notifier).setPolling(false);
if (pushTokens.isEmpty) {
if (globalRef?.read(settingsProvider).enablePolling == true) {
Logger.info('No push token is available for polling, polling is disabled.', name: 'push_provider.dart#pollForChallenges');
globalRef?.read(settingsProvider.notifier).setPolling(false);
}
return;
}

final connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
if (isManually) {
Logger.info('Tried to poll without any internet connection available.', name: 'push_provider.dart#pollForChallenges');
globalRef?.read(statusMessageProvider.notifier).state = (
AppLocalizations.of(globalNavigatorKey.currentContext!)!.pollingFailed,
AppLocalizations.of(globalNavigatorKey.currentContext!)!.noNetworkConnection,
);
}
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/riverpod_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ final pushRequestProvider = StateNotifierProvider<PushRequestNotifier, PushReque
ref.listen(appStateProvider, (previous, next) {
if (previous == AppState.pause && next == AppState.resume) {
Logger.info('Polling for challenges on resume');
pushProvider.pollForChallenges();
pushProvider.pollForChallenges(isManually: false);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class _MainViewTokensListState extends ConsumerState<MainViewTokensList> {
message: AppLocalizations.of(context)!.pollingChallenges,
duration: const Duration(seconds: 1),
);
await PushProvider().pollForChallenges();
await PushProvider().pollForChallenges(isManually: true);
},
child: SlidableAutoCloseBehavior(
child: DragItemScroller(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class _PushTokensViwListState extends ConsumerState<PushTokensViwList> {
message: AppLocalizations.of(context)!.pollingChallenges,
duration: const Duration(seconds: 1),
);
await PushProvider().pollForChallenges();
await PushProvider().pollForChallenges(isManually: true);
},
child: SlidableAutoCloseBehavior(
child: DragItemScroller(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ publish_to: none
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 4.2.1+402103 # TODO Set the right version number
version: 4.2.1+402104 # TODO Set the right version number
# version: major.minor.build + 2x major|2x minor|3x build
# version: version number + build number (optional)
# android: build-name + versionCode
Expand Down

0 comments on commit 63c3a2b

Please sign in to comment.