Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Ask for a store review after the first finished book

Barnabas038c1da29a3c
diff --git a/fdroid/com.barnabas.absorb.yml b/fdroid/com.barnabas.absorb.yml
index b04e3ca0..93318450 100644
--- a/fdroid/com.barnabas.absorb.yml
+++ b/fdroid/com.barnabas.absorb.yml
@@ -23,7 +23,8 @@ Builds:
- git -C $$flutter$$ checkout -f $flutterVersion
- mv lib/services/chromecast_service_stub.dart lib/services/chromecast_service.dart
- mv lib/widgets/chromecast_button_stub.dart lib/widgets/chromecast_button.dart
- - sed -i '/flutter_chrome_cast:/d' pubspec.yaml
+ - mv lib/services/review_service_stub.dart lib/services/review_service.dart
+ - sed -i '/flutter_chrome_cast:/d; /in_app_review:/d' pubspec.yaml
- export PUB_CACHE=$(pwd)/.pub-cache
- $$flutter$$/bin/flutter config --no-analytics
- $$flutter$$/bin/flutter pub get
@@ -46,7 +47,8 @@ Builds:
- git -C $$flutter$$ checkout -f $flutterVersion
- mv lib/services/chromecast_service_stub.dart lib/services/chromecast_service.dart
- mv lib/widgets/chromecast_button_stub.dart lib/widgets/chromecast_button.dart
- - sed -i '/flutter_chrome_cast:/d' pubspec.yaml
+ - mv lib/services/review_service_stub.dart lib/services/review_service.dart
+ - sed -i '/flutter_chrome_cast:/d; /in_app_review:/d' pubspec.yaml
- export PUB_CACHE=$(pwd)/.pub-cache
- $$flutter$$/bin/flutter config --no-analytics
- $$flutter$$/bin/flutter pub get
@@ -69,7 +71,8 @@ Builds:
- git -C $$flutter$$ checkout -f $flutterVersion
- mv lib/services/chromecast_service_stub.dart lib/services/chromecast_service.dart
- mv lib/widgets/chromecast_button_stub.dart lib/widgets/chromecast_button.dart
- - sed -i '/flutter_chrome_cast:/d' pubspec.yaml
+ - mv lib/services/review_service_stub.dart lib/services/review_service.dart
+ - sed -i '/flutter_chrome_cast:/d; /in_app_review:/d' pubspec.yaml
- export PUB_CACHE=$(pwd)/.pub-cache
- $$flutter$$/bin/flutter config --no-analytics
- $$flutter$$/bin/flutter pub get
diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dart
index 1d42f65c..2f37cc42 100644
--- a/lib/screens/app_shell.dart
+++ b/lib/screens/app_shell.dart
@@ -26,6 +26,7 @@ import 'library_screen.dart';
import 'stats_screen.dart';
import 'settings_screen.dart';
import '../widgets/welcome_sheet.dart';
+import '../services/review_service.dart';
import '../services/update_checker_service.dart';
import '../widgets/update_dialog.dart';
@@ -497,6 +498,7 @@ class _AppShellState extends State<AppShell> with WidgetsBindingObserver, Ticker
SleepTimerService().onAppForegrounded();
AudioPlayerService.onAppForegrounded();
HomeWidgetService().onAppForegrounded();
+ ReviewService.onAppForegrounded();
_refreshDataForTab(_currentIndex);
// Check auto sleep in case we resumed into the window
SleepTimerService().checkAutoSleep();
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 591ccd4d..27c68f71 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -22,6 +22,7 @@ import 'player_settings.dart';
import 'session_cache.dart';
import 'home_widget_service.dart';
import 'bookmark_service.dart';
+import 'review_service.dart';
export 'player_settings.dart';
// ─── AudioHandler (runs in background, controls notification) ───
@@ -1534,6 +1535,9 @@ class AudioPlayerService extends ChangeNotifier {
final cb = _onAutoQueueAdvancedCallback;
if (cb != null) cb(oldKey);
}
+ if (oldItemId != null && oldEpisodeId == null) {
+ unawaited(ReviewService.onBookFinished(isForeground: !_isBackgrounded));
+ }
notifyListeners();
_autoQueueAdvancing = false;
@@ -3973,6 +3977,9 @@ class AudioPlayerService extends ChangeNotifier {
debugPrint('[Player] Book complete: $_currentTitle');
_logEvent(PlaybackEventType.bookFinished);
+ if (_currentEpisodeId == null) {
+ unawaited(ReviewService.onBookFinished(isForeground: !_isBackgrounded));
+ }
// Cancel subscriptions first so we don't process stale events.
_syncSub?.cancel();
diff --git a/lib/services/review_service.dart b/lib/services/review_service.dart
new file mode 100644
index 00000000..965582e9
--- /dev/null
+++ b/lib/services/review_service.dart
@@ -0,0 +1,53 @@
+import 'package:flutter/foundation.dart';
+import 'package:in_app_review/in_app_review.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+/// One-time store review prompt, fired after the user naturally finishes a
+/// book. The stores own the display rules: iOS suppresses it entirely on
+/// TestFlight and caps prompts per year, Play only shows it for
+/// Play-installed builds (isAvailable is false for sideloaded APKs).
+///
+/// The F-Droid build replaces this file with review_service_stub.dart at
+/// build time - the Play Core review dependency would fail the F-Droid
+/// scanner (same treatment as the Chromecast plugin).
+class ReviewService {
+ static const _shownKey = 'review_prompt_shown';
+ static const _pendingKey = 'review_prompt_pending';
+
+ /// Call when a book (not a podcast episode) finishes naturally. Neither OS
+ /// will display the dialog from the background, so park a pending flag and
+ /// fire on the next foreground instead of burning the one shot invisibly.
+ static Future<void> onBookFinished({required bool isForeground}) async {
+ final prefs = await SharedPreferences.getInstance();
+ if (prefs.getBool(_shownKey) ?? false) return;
+ if (!isForeground) {
+ await prefs.setBool(_pendingKey, true);
+ return;
+ }
+ await _request(prefs);
+ }
+
+ /// Call on app resume to fire a prompt parked by a background finish.
+ static Future<void> onAppForegrounded() async {
+ final prefs = await SharedPreferences.getInstance();
+ if (prefs.getBool(_shownKey) ?? false) return;
+ if (!(prefs.getBool(_pendingKey) ?? false)) return;
+ // Let the resume settle so the dialog doesn't collide with launch UI.
+ await Future.delayed(const Duration(seconds: 2));
+ await _request(prefs);
+ }
+
+ static Future<void> _request(SharedPreferences prefs) async {
+ try {
+ final review = InAppReview.instance;
+ // Not a store install - keep the pending flag so a store build of the
+ // same install could still prompt later.
+ if (!await review.isAvailable()) return;
+ await prefs.setBool(_shownKey, true);
+ await prefs.remove(_pendingKey);
+ await review.requestReview();
+ } catch (e) {
+ debugPrint('[Review] requestReview failed: $e');
+ }
+ }
+}
diff --git a/lib/services/review_service_stub.dart b/lib/services/review_service_stub.dart
new file mode 100644
index 00000000..3c125b13
--- /dev/null
+++ b/lib/services/review_service_stub.dart
@@ -0,0 +1,8 @@
+/// F-Droid stand-in for review_service.dart - the build copies this file
+/// over it and strips the in_app_review dependency, since the Play Core
+/// review library fails the F-Droid scanner. No store, no review prompt.
+class ReviewService {
+ static Future<void> onBookFinished({required bool isForeground}) async {}
+
+ static Future<void> onAppForegrounded() async {}
+}
diff --git a/pubspec.lock b/pubspec.lock
index 45200a7e..db536407 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -443,6 +443,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.8.0"
+ in_app_review:
+ dependency: "direct main"
+ description:
+ name: in_app_review
+ sha256: "364db0c160b37fe7fad9cdaa18f968473924b17368869010af902760421b6bf8"
+ url: "https://pub.dev"
+ source: hosted
+ version: "2.0.12"
+ in_app_review_platform_interface:
+ dependency: transitive
+ description:
+ name: in_app_review_platform_interface
+ sha256: fed2c755f2125caa9ae10495a3c163aa7fab5af3585a9c62ef4a6920c5b45f10
+ url: "https://pub.dev"
+ source: hosted
+ version: "2.0.5"
intl:
dependency: "direct main"
description:
diff --git a/pubspec.yaml b/pubspec.yaml
index 08632646..7b2f8286 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -83,6 +83,9 @@ dependencies:
# App icon long-press shortcuts (Continue Listening / Downloads)
quick_actions: ^1.0.8
+ # One-time store review prompt (stripped for the F-Droid build)
+ in_app_review: ^2.0.9
+
# Splash screen (preserve during init)
flutter_native_splash: ^2.4.4
vibration: ^3.1.8