From da2c8490da430878e65ea1fc75ffd6e76bcd3c41 Mon Sep 17 00:00:00 2001 From: Jonas Acker Date: Mon, 30 Nov 2026 20:13:04 +0100 Subject: [PATCH] Ask for the automatic rating only with BikeControl in the foreground The prompt fired the moment a qualifying session ended, usually with the trainer app in front. iOS/Play then show nothing but the request still counted as the one ask. Wait for the app to be resumed first. Co-Authored-By: Claude Opus 5.5 --- lib/utils/rate_app.dart | 27 +++++++++++++++++ test/utils/rate_app_test.dart | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/lib/utils/rate_app.dart b/lib/utils/rate_app.dart index d175f3c4..757b8a9c 100644 --- a/lib/utils/rate_app.dart +++ b/lib/utils/rate_app.dart @@ -1,6 +1,9 @@ import 'package:bike_control/main.dart' show recordError; import 'package:bike_control/services/feedback_prompt_service.dart'; +import 'dart:async'; + import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; import 'package:in_app_review/in_app_review.dart'; /// Asks for a store rating after the user tapped a "rate" / "leave a review" @@ -29,6 +32,23 @@ Future requestAppRating() async { /// may silently show nothing — an acceptable no-op for a prompt nobody /// asked for; the ask simply comes round again in a later launch. /// +/// Completes once the app is in the foreground ([AppLifecycleState.resumed]), +/// right away when it already is. An unknown state (nothing reported yet, as +/// on some desktops) counts as foreground. +Future waitUntilAppResumed() { + final state = WidgetsBinding.instance.lifecycleState; + if (state == null || state == AppLifecycleState.resumed) return Future.value(); + final resumed = Completer(); + late final AppLifecycleListener listener; + listener = AppLifecycleListener( + onResume: () { + listener.dispose(); + if (!resumed.isCompleted) resumed.complete(); + }, + ); + return resumed.future; +} + /// Returns whether the sheet was actually requested. Future requestAppRatingSheet() async { final review = InAppReview.instance; @@ -45,11 +65,18 @@ Future requestAppRatingSheet() async { /// us whether the rider went through with a rating — "we asked" is the most /// we can know — but it does tell us when it showed nothing at all, and /// burning the one-and-only ask on that would be a silent loss. +/// +/// The session that makes a rider eligible usually ends with the trainer app +/// in front and BikeControl in the background, where iOS and Play silently +/// show nothing while still reporting the request — so the sheet is only +/// requested once BikeControl is back in the foreground ([untilForeground]). Future showAutomaticRatingPrompt( FeedbackPromptService service, { Future Function() requestSheet = requestAppRatingSheet, + Future Function() untilForeground = waitUntilAppResumed, }) async { try { + await untilForeground(); if (!await requestSheet()) return; await service.markCompleted(); } catch (e, s) { diff --git a/test/utils/rate_app_test.dart b/test/utils/rate_app_test.dart index 79cfc2af..2c833020 100644 --- a/test/utils/rate_app_test.dart +++ b/test/utils/rate_app_test.dart @@ -1,8 +1,11 @@ +import 'dart:async'; + import 'package:bike_control/services/feedback_prompt_service.dart'; import 'package:bike_control/utils/rate_app.dart'; import 'package:bike_control/utils/settings/settings.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -95,6 +98,60 @@ void main() { s.dispose(); }); + // The session that makes the rider eligible often ends while BikeControl + // is in the background (the trainer app is in front). iOS/Play show + // nothing then, but the ask would count as done. + test('waits for the app to be in the foreground before asking', () async { + final binding = TestWidgetsFlutterBinding.instance; + binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden); + binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); + addTearDown(() => binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed)); + + final s = await service(); + var asked = 0; + var done = false; + unawaited( + showAutomaticRatingPrompt( + s, + requestSheet: () async { + asked++; + return true; + }, + ).then((_) => done = true), + ); + await pumpEventQueue(); + expect(asked, 0, reason: 'nothing can show while backgrounded'); + expect(s.settings.getReviewCompleted(), isFalse); + + binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden); + binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + await pumpEventQueue(); + expect(asked, 0, reason: 'inactive is not in front yet'); + + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await pumpEventQueue(); + expect(asked, 1); + expect(done, isTrue); + expect(s.settings.getReviewCompleted(), isTrue); + s.dispose(); + }); + + test('asks right away when already in the foreground', () async { + TestWidgetsFlutterBinding.instance.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + final s = await service(); + var asked = 0; + await showAutomaticRatingPrompt( + s, + requestSheet: () async { + asked++; + return true; + }, + ); + expect(asked, 1); + s.dispose(); + }); + test('a failing store API does not mark the ask as done', () async { final s = await service(); await showAutomaticRatingPrompt(s, requestSheet: () async => throw PlatformException(code: 'boom'));