Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix auto-advance not working due to unscoped settings migration

Nathan Poulson74a759323ada
diff --git a/lib/main.dart b/lib/main.dart
index 78524538..59b6a84e 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -18,6 +18,7 @@ import 'services/download_notification_service.dart';
import 'services/progress_sync_service.dart';
import 'services/equalizer_service.dart';
import 'services/sleep_timer_service.dart';
+import 'services/scoped_prefs.dart';
import 'services/user_account_service.dart';
import 'services/android_auto_service.dart';
import 'services/chromecast_service.dart';
@@ -303,6 +304,10 @@ class _AuthGateState extends State<AuthGate> {
// fall back to un-scoped keys and appear as defaults.
await UserAccountService().init();
+ // One-time migration: copy any settings that were written under unscoped
+ // keys (before scope was active) to the current user's scoped keys.
+ await ScopedPrefs.migrateToScope();
+
// Start auth restoration immediately — it doesn't depend on audio/cast/
// download services and must not be blocked by a hanging service init.
if (mounted) {
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index 7d76a769..c43a3336 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -1612,6 +1612,7 @@ class LibraryProvider extends ChangeNotifier {
// Skip when user manually marks an item finished (not natural playback completion).
if (!skipAutoAdvance) {
PlayerSettings.getQueueMode().then((mode) {
+ debugPrint('[AutoAdvance] queueMode=$mode for finished item $itemId');
if (mode == 'manual') {
_manualQueueAdvance(itemId);
} else if (mode == 'auto_next') {
@@ -1814,7 +1815,10 @@ class LibraryProvider extends ChangeNotifier {
/// Manual queue auto-advance: scan absorbing list from after the finished
/// item, find the first non-finished card, and play it.
void _manualQueueAdvance(String finishedKey) async {
- if (AudioPlayerService.wasNoisyPause) return;
+ if (AudioPlayerService.wasNoisyPause) {
+ debugPrint('[AutoAdvance] Skipping manual advance - noisy pause active');
+ return;
+ }
final merged = await PlayerSettings.getMergeAbsorbingLibraries();
@@ -1874,15 +1878,19 @@ class LibraryProvider extends ChangeNotifier {
chapters: chapters,
);
}
+ debugPrint('[AutoAdvance] Manual queue: starting next item $key');
return; // started the next item
}
- // All remaining cards are finished — playback stops naturally.
+ debugPrint('[AutoAdvance] Manual queue: no next item found after $finishedKey');
}
/// Offline auto-advance: find the next downloaded book in series or next
/// downloaded podcast episode and auto-play it without any server calls.
void _autoAdvanceOffline(String finishedKey) {
- if (AudioPlayerService.wasNoisyPause) return;
+ if (AudioPlayerService.wasNoisyPause) {
+ debugPrint('[AutoAdvance] Skipping auto advance - noisy pause active');
+ return;
+ }
final isCompound = finishedKey.length > 36;
if (isCompound) {
diff --git a/lib/services/scoped_prefs.dart b/lib/services/scoped_prefs.dart
index bbf4f3ca..cd38f561 100644
--- a/lib/services/scoped_prefs.dart
+++ b/lib/services/scoped_prefs.dart
@@ -1,3 +1,4 @@
+import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'user_account_service.dart';
@@ -17,6 +18,69 @@ class ScopedPrefs {
/// the account has no data — not that it should inherit another account's.
static bool get _shouldFallback => !UserAccountService().hasScope;
+ /// Keys that are global (not per-user) and should NOT be copied during
+ /// the unscoped-to-scoped migration.
+ static const _globalKeys = <String>{
+ 'saved_accounts', 'active_account_scope',
+ 'server_url', 'token', 'username', 'user_id', 'default_library_id',
+ 'custom_headers',
+ 'loggingEnabled', 'manual_offline_mode',
+ 'custom_download_path', 'downloads',
+ 'absorb_device_id',
+ 'widget_item_id', 'widget_episode_id',
+ 'cached_stats', 'cached_sessions',
+ 'update_last_check', 'update_dismissed_version',
+ };
+
+ /// One-time migration: copy unscoped settings to the active scope.
+ /// This handles the case where settings were written before scope was
+ /// active (first login, or old init order where UserAccountService
+ /// initialized after settings reads).
+ static Future<void> migrateToScope() async {
+ final scope = UserAccountService().activeScopeKey;
+ if (scope.isEmpty) return;
+
+ final prefs = await SharedPreferences.getInstance();
+ final flag = '$scope:_scope_migrated';
+ if (prefs.getBool(flag) == true) return;
+
+ int copied = 0;
+ for (final key in prefs.getKeys()) {
+ // Skip keys that already have a scope prefix (contain ':')
+ if (key.contains(':')) continue;
+ // Skip known global keys
+ if (_globalKeys.contains(key)) continue;
+ // Skip Flutter framework keys
+ if (key.startsWith('flutter.')) continue;
+ // Skip per-podcast sort keys (global, keyed by item ID)
+ if (key.startsWith('podcast_sort_newest_')) continue;
+
+ final scopedKey = '$scope:$key';
+ if (prefs.containsKey(scopedKey)) continue; // already exists
+
+ final value = prefs.get(key);
+ if (value is String) {
+ await prefs.setString(scopedKey, value);
+ } else if (value is bool) {
+ await prefs.setBool(scopedKey, value);
+ } else if (value is int) {
+ await prefs.setInt(scopedKey, value);
+ } else if (value is double) {
+ await prefs.setDouble(scopedKey, value);
+ } else if (value is List<String>) {
+ await prefs.setStringList(scopedKey, value);
+ } else {
+ continue;
+ }
+ copied++;
+ }
+
+ await prefs.setBool(flag, true);
+ if (copied > 0) {
+ debugPrint('[ScopedPrefs] Migrated $copied unscoped settings to scope: $scope');
+ }
+ }
+
// ── String ──
static Future<String?> getString(String key) async {