mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-02 10:09:09 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<void> 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<void> waitUntilAppResumed() {
|
||||
final state = WidgetsBinding.instance.lifecycleState;
|
||||
if (state == null || state == AppLifecycleState.resumed) return Future.value();
|
||||
final resumed = Completer<void>();
|
||||
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<bool> requestAppRatingSheet() async {
|
||||
final review = InAppReview.instance;
|
||||
@@ -45,11 +65,18 @@ Future<bool> 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<void> showAutomaticRatingPrompt(
|
||||
FeedbackPromptService service, {
|
||||
Future<bool> Function() requestSheet = requestAppRatingSheet,
|
||||
Future<void> Function() untilForeground = waitUntilAppResumed,
|
||||
}) async {
|
||||
try {
|
||||
await untilForeground();
|
||||
if (!await requestSheet()) return;
|
||||
await service.markCompleted();
|
||||
} catch (e, s) {
|
||||
|
||||
@@ -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'));
|
||||
|
||||
Reference in New Issue
Block a user