Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix misleading login error, improve progress sync cache handling

Nathan Poulson1e3ef2ac55f8
diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dart
index ec5bc693..0ebadf42 100644
--- a/lib/providers/auth_provider.dart
+++ b/lib/providers/auth_provider.dart
@@ -142,7 +142,7 @@ class AuthProvider extends ChangeNotifier {
}
// Attempt login
- final result = await ApiService.login(
+ final (result, statusCode) = await ApiService.login(
serverUrl: url,
username: username,
password: password,
@@ -150,7 +150,9 @@ class AuthProvider extends ChangeNotifier {
);
if (result == null) {
- _errorMessage = 'Invalid username or password';
+ _errorMessage = statusCode == 401
+ ? 'Invalid username or password'
+ : 'Login failed - check your server address and credentials';
return false;
}
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index c43a3336..ed23d007 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -1038,7 +1038,7 @@ class LibraryProvider extends ChangeNotifier {
]);
// Clear stale local overrides — server data is now authoritative
_localProgressOverrides.clear();
- // Update local SharedPreferences from fresh server data so they stay in sync
+ // Cache fresh server data locally (without marking as pending syncs)
final sync = ProgressSyncService();
for (final entry in _progressMap.entries) {
final itemId = entry.key;
@@ -1046,16 +1046,13 @@ class LibraryProvider extends ChangeNotifier {
final currentTime = (mp['currentTime'] as num?)?.toDouble() ?? 0;
final duration = (mp['duration'] as num?)?.toDouble() ?? 0;
if (duration > 0 && currentTime > 0) {
- await sync.saveLocal(
+ await sync.cacheServerProgress(
itemId: itemId,
currentTime: currentTime,
duration: duration,
- speed: 1.0,
);
}
}
- // Clear pending syncs since we just pulled fresh server data
- await ScopedPrefs.setStringList('pending_syncs', []);
notifyListeners();
}
diff --git a/lib/services/api_service.dart b/lib/services/api_service.dart
index e242d67e..bf4417c3 100644
--- a/lib/services/api_service.dart
+++ b/lib/services/api_service.dart
@@ -63,8 +63,8 @@ class ApiService {
String get _cleanBaseUrl =>
baseUrl.endsWith('/') ? baseUrl.substring(0, baseUrl.length - 1) : baseUrl;
- /// Login and return the full response JSON (contains user, token, etc.)
- static Future<Map<String, dynamic>?> login({
+ /// Login and return the full response JSON (contains user, token, etc.) and HTTP status code.
+ static Future<(Map<String, dynamic>?, int)> login({
required String serverUrl,
required String username,
required String password,
@@ -82,11 +82,11 @@ class ApiService {
).timeout(const Duration(seconds: 15));
if (response.statusCode == 200) {
- return jsonDecode(response.body) as Map<String, dynamic>;
+ return (jsonDecode(response.body) as Map<String, dynamic>, 200);
}
- return null;
+ return (null, response.statusCode);
} catch (e) {
- return null;
+ return (null, 0);
}
}
diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dart
index a70929c0..c44315bd 100644
--- a/lib/services/progress_sync_service.dart
+++ b/lib/services/progress_sync_service.dart
@@ -35,7 +35,7 @@ class ProgressSyncService {
bool get isOnline => _isOnline;
- /// Save progress locally. Always succeeds.
+ /// Save progress locally and mark it as a pending change to sync.
Future<void> saveLocal({
required String itemId,
required double currentTime,
@@ -58,6 +58,22 @@ class ProgressSyncService {
}
}
+ /// Cache server-known progress locally without marking it as a pending sync.
+ Future<void> cacheServerProgress({
+ required String itemId,
+ required double currentTime,
+ required double duration,
+ }) async {
+ final data = {
+ 'itemId': itemId,
+ 'currentTime': currentTime,
+ 'duration': duration,
+ 'speed': 1.0,
+ 'timestamp': 0,
+ };
+ await ScopedPrefs.setString('progress_$itemId', jsonEncode(data));
+ }
+
/// Get locally saved progress for an item.
Future<Map<String, dynamic>?> getLocal(String itemId) async {
final json = await ScopedPrefs.getString('progress_$itemId');
@@ -178,11 +194,10 @@ class ProgressSyncService {
if (serverTimestamp > localTimestamp) {
debugPrint('[Sync] Server is newer for $itemId: server=$serverTime s ($serverTimestamp) vs local=$localTime s ($localTimestamp) — pulling');
- await saveLocal(
+ await cacheServerProgress(
itemId: itemId,
currentTime: serverTime,
duration: localDuration,
- speed: (data['speed'] as num?)?.toDouble() ?? 1.0,
);
final updated = await ScopedPrefs.getStringList('pending_syncs');
updated.remove(itemId);
@@ -220,6 +235,12 @@ class ProgressSyncService {
await ScopedPrefs.setStringList('pending_syncs', updated);
} catch (e) {
debugPrint('[Sync] Flush failed for $itemId: $e');
+ // Stop the batch if we lost connectivity
+ if (e.toString().contains('SocketException') ||
+ e.toString().contains('connection abort')) {
+ debugPrint('[Sync] Network error — stopping flush');
+ break;
+ }
}
}
} finally {