A cross platform Audiobookshelf client for Android and iOS
Dart
Cache Audible rating so it sticks between book opens
Barnabasde71997b71df
diff --git a/lib/services/api_service.dart b/lib/services/api_service.dartindex a629b1b3..bda7e2f9 100644--- a/lib/services/api_service.dart+++ b/lib/services/api_service.dart@@ -1233,6 +1233,42 @@ class ApiService {return null;}+ /// Read a previously-fetched Audible rating from local cache, keyed by+ /// library item id. Used so the rating shows immediately on book detail+ /// open even when Audnexus is slow or unreachable, and so a transient+ /// network failure doesn't make a known rating disappear.+ static Future<Map<String, dynamic>?> getCachedAudibleRating(String itemId) async {+ try {+ final prefs = await SharedPreferences.getInstance();+ final raw = prefs.getString('audible_rating_$itemId');+ if (raw == null) return null;+ final data = jsonDecode(raw) as Map<String, dynamic>;+ final rating = (data['rating'] as num?)?.toDouble();+ if (rating == null || rating <= 0) return null;+ return {+ 'rating': rating,+ 'asin': data['asin'] as String?,+ };+ } catch (_) {+ return null;+ }+ }++ /// Persist a fresh Audible rating so subsequent book detail opens render+ /// the stars instantly without waiting on Audnexus.+ static Future<void> setCachedAudibleRating(+ String itemId, double rating, String? asin) async {+ if (rating <= 0) return;+ try {+ final prefs = await SharedPreferences.getInstance();+ await prefs.setString('audible_rating_$itemId', jsonEncode({+ 'rating': rating,+ 'asin': asin,+ 'fetchedAt': DateTime.now().millisecondsSinceEpoch,+ }));+ } catch (_) {}+ }+/// Search Audible via the audiobookshelf server for an ASIN by title+author,/// then fetch the rating from Audnexus. Used as a fallback when the book's/// stored ASIN returns no rating.diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dartindex da16c980..4fc5228b 100644--- a/lib/widgets/book_detail_sheet.dart+++ b/lib/widgets/book_detail_sheet.dart@@ -126,6 +126,17 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {final title = metadata['title'] as String? ?? '';final author = metadata['authorName'] as String?;+ // Show any cached rating immediately so the stars don't blink in+ // and out between detail opens. The fresh fetch below replaces it+ // if Audnexus answers; if it doesn't, the cached value stays.+ final cached = await ApiService.getCachedAudibleRating(widget.itemId);+ if (cached != null && mounted) {+ setState(() {+ _rating = cached;+ _asin = cached['asin'] as String? ?? asin;+ });+ }+Map<String, dynamic>? rating;if (asin != null && asin.isNotEmpty) {rating = await ApiService.getAudibleRating(asin);@@ -138,10 +149,14 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {}}if (rating != null && mounted) {+ final freshRating = (rating['rating'] as num).toDouble();+ final freshAsin = rating['asin'] as String? ?? asin;setState(() {_rating = rating;- _asin = rating?['asin'] as String? ?? asin;+ _asin = freshAsin;});+ await ApiService.setCachedAudibleRating(+ widget.itemId, freshRating, freshAsin);}return;}