Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Improve local/offline auto-switching

Barnabase608c1f0cf8d
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index f10feef7..f4d98bd4 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -5,6 +5,7 @@
"online": "Online",
"offline": "Offline",
+ "stillOffline": "Still offline. Tap to try again.",
"retry": "Retry",
"cancel": "Cancel",
"delete": "Delete",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 65018fe6..a3de5efb 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -116,6 +116,12 @@ abstract class AppLocalizations {
/// **'Offline'**
String get offline;
+ /// No description provided for @stillOffline.
+ ///
+ /// In en, this message translates to:
+ /// **'Still offline. Tap to try again.'**
+ String get stillOffline;
+
/// No description provided for @retry.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index af7dde73..794ce603 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -17,6 +17,9 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get offline => 'Offline';
+ @override
+ String get stillOffline => 'Still offline. Tap to try again.';
+
@override
String get retry => 'Retry';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index ace6b950..4872ec83 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -17,6 +17,9 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get offline => '离线';
+ @override
+ String get stillOffline => '仍处于离线状态。点击重试。';
+
@override
String get retry => '重试';
diff --git a/lib/l10n/app_zh.arb b/lib/l10n/app_zh.arb
index d0ed4f71..dc2c6531 100644
--- a/lib/l10n/app_zh.arb
+++ b/lib/l10n/app_zh.arb
@@ -5,6 +5,7 @@
"online": "在线",
"offline": "离线",
+ "stillOffline": "仍处于离线状态。点击重试。",
"retry": "重试",
"cancel": "取消",
"delete": "删除",
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 86711d6d..8e1587d6 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -134,6 +134,60 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
_manualOffline = prefs.getBool('manual_offline_mode') ?? false;
}
+ /// User-triggered reconnect attempt for the offline cloud icon. If currently
+ /// offline (manual or network), pings the server (local first when enabled)
+ /// and flips back online on success. Returns true if online when the call
+ /// completes. Sets [isReconnecting] for the duration so the UI can spin.
+ Future<bool> tryReconnect() async {
+ if (!isOffline) return true;
+ if (_isReconnecting) return !isOffline;
+ debugPrint('[Library] tryReconnect: manual=$_manualOffline network=$_networkOffline');
+ _isReconnecting = true;
+ notifyListeners();
+ try {
+ if (_manualOffline) {
+ await setManualOffline(false);
+ return !isOffline;
+ }
+ final auth = _auth;
+ if (auth == null) return false;
+ if (auth.localServerEnabled && auth.localServerUrl.isNotEmpty) {
+ final localReachable = await ApiService.pingServer(
+ auth.localServerUrl,
+ customHeaders: auth.customHeaders,
+ ).timeout(const Duration(seconds: 5), onTimeout: () => false);
+ if (localReachable) {
+ debugPrint('[Library] tryReconnect: local server reachable');
+ await auth.checkLocalServer();
+ _stopServerPingTimer();
+ setNetworkOffline(false);
+ return true;
+ }
+ if (auth.useLocalServer) {
+ debugPrint('[Library] tryReconnect: local unreachable, clearing override');
+ auth.clearLocalOverride();
+ }
+ }
+ final serverUrl = auth.serverUrl;
+ if (serverUrl == null || serverUrl.isEmpty) return false;
+ final reachable = await ApiService.pingServer(
+ serverUrl,
+ customHeaders: auth.customHeaders,
+ ).timeout(const Duration(seconds: 5), onTimeout: () => false);
+ if (reachable) {
+ debugPrint('[Library] tryReconnect: remote server reachable');
+ _stopServerPingTimer();
+ setNetworkOffline(false);
+ return true;
+ }
+ debugPrint('[Library] tryReconnect: still unreachable');
+ return false;
+ } finally {
+ _isReconnecting = false;
+ notifyListeners();
+ }
+ }
+
void setNetworkOffline(bool offline) {
final wasOffline = _networkOffline;
_networkOffline = offline;
diff --git a/lib/providers/_lp_state.dart b/lib/providers/_lp_state.dart
index f1e94344..8f2b9bbf 100644
--- a/lib/providers/_lp_state.dart
+++ b/lib/providers/_lp_state.dart
@@ -61,6 +61,7 @@ mixin _StateMixin on ChangeNotifier {
bool _manualOffline = false;
bool _networkOffline = false;
bool _deviceHasConnectivity = true;
+ bool _isReconnecting = false;
Timer? _serverPingTimer;
Timer? _healthCheckTimer;
Timer? _localProbeTimer;
@@ -105,6 +106,7 @@ mixin _StateMixin on ChangeNotifier {
Set<String> get hiddenSectionIds => _hiddenSectionIds;
bool get isOffline => _manualOffline || _networkOffline;
bool get isManualOffline => _manualOffline;
+ bool get isReconnecting => _isReconnecting;
Set<String> get manualAbsorbAdds => _manualAbsorbAdds;
Set<String> get manualAbsorbRemoves => _manualAbsorbRemoves;
List<String> get absorbingBookIds => _absorbingBookIds;
diff --git a/lib/screens/absorbing_screen.dart b/lib/screens/absorbing_screen.dart
index 9b728bdc..86fdf986 100644
--- a/lib/screens/absorbing_screen.dart
+++ b/lib/screens/absorbing_screen.dart
@@ -10,6 +10,7 @@ import '../services/scoped_prefs.dart';
import '../widgets/absorb_page_header.dart';
import '../main.dart' show oledNotifier;
import '../widgets/absorbing_card.dart';
+import '../widgets/offline_status_icon.dart';
import '../l10n/app_localizations.dart';
class AbsorbingScreen extends StatefulWidget {
@@ -522,26 +523,19 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
AbsorbPageHeader(
title: l.absorbingTitle,
padding: const EdgeInsets.fromLTRB(20, 12, 20, 0),
- trailing: GestureDetector(
- onTap: () {
- final newVal = !lib.isManualOffline;
- lib.setManualOffline(newVal);
- if (newVal) {
- final dl = DownloadService();
- final itemId = _player.currentItemId;
- final epId = _player.currentEpisodeId;
- final dlKey = epId != null && itemId != null
- ? '$itemId-$epId'
- : itemId;
- if (dlKey == null || !dl.isDownloaded(dlKey)) {
- _stopAndRefresh(lib);
- }
+ trailing: OfflineStatusIcon(
+ onTapWhenOnline: () {
+ lib.setManualOffline(true);
+ final dl = DownloadService();
+ final itemId = _player.currentItemId;
+ final epId = _player.currentEpisodeId;
+ final dlKey = epId != null && itemId != null
+ ? '$itemId-$epId'
+ : itemId;
+ if (dlKey == null || !dl.isDownloaded(dlKey)) {
+ _stopAndRefresh(lib);
}
},
- child: Icon(
- effectiveOffline ? Icons.cloud_off_rounded : Icons.cloud_done_rounded,
- size: 20, color: effectiveOffline ? Colors.orange : Colors.green,
- ),
),
actions: [
// Stop button (visible when playing)
diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart
index d0e501ca..e21dc8d4 100644
--- a/lib/screens/home_screen.dart
+++ b/lib/screens/home_screen.dart
@@ -20,6 +20,7 @@ import '../widgets/playlist_detail_sheet.dart';
import '../widgets/collection_detail_sheet.dart';
import '../widgets/section_detail_sheet.dart';
import '../widgets/feature_hint.dart';
+import '../widgets/offline_status_icon.dart';
import 'app_shell.dart';
import '../l10n/app_localizations.dart';
@@ -375,27 +376,20 @@ class HomeScreenState extends State<HomeScreen> {
SliverToBoxAdapter(
child: AbsorbPageHeader(
title: l.homeTitle,
- trailing: GestureDetector(
- onTap: () {
- final newVal = !lib.isManualOffline;
- lib.setManualOffline(newVal);
- if (newVal) {
- final dl = DownloadService();
- final player = AudioPlayerService();
- final itemId = player.currentItemId;
- final epId = player.currentEpisodeId;
- final dlKey = epId != null && itemId != null
- ? '$itemId-$epId'
- : itemId;
- if (dlKey == null || !dl.isDownloaded(dlKey)) {
- player.stop();
- }
+ trailing: OfflineStatusIcon(
+ onTapWhenOnline: () {
+ lib.setManualOffline(true);
+ final dl = DownloadService();
+ final player = AudioPlayerService();
+ final itemId = player.currentItemId;
+ final epId = player.currentEpisodeId;
+ final dlKey = epId != null && itemId != null
+ ? '$itemId-$epId'
+ : itemId;
+ if (dlKey == null || !dl.isDownloaded(dlKey)) {
+ player.stop();
}
},
- child: Icon(
- lib.isOffline ? Icons.cloud_off_rounded : Icons.cloud_done_rounded,
- size: 20, color: lib.isOffline ? Colors.orange : Colors.green,
- ),
),
actions: [
if (allLibraries.length > 1)
diff --git a/lib/screens/library_screen.dart b/lib/screens/library_screen.dart
index 252077dd..918b6c95 100644
--- a/lib/screens/library_screen.dart
+++ b/lib/screens/library_screen.dart
@@ -20,6 +20,7 @@ import '../widgets/library_authors_tab.dart';
import 'admin_podcasts_screen.dart';
import 'upcoming_releases_screen.dart';
import '../widgets/audible_series_sheet.dart' show showAudibleRegionPicker;
+import '../widgets/offline_status_icon.dart';
import '../l10n/app_localizations.dart';
/// Responsive grid column count based on available width.
@@ -1246,27 +1247,20 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi
child: Column(mainAxisSize: MainAxisSize.min, children: [
AbsorbPageHeader(
title: l.libraryTitle,
- trailing: GestureDetector(
- onTap: () {
- final newVal = !lib.isManualOffline;
- lib.setManualOffline(newVal);
- if (newVal) {
- final dl = DownloadService();
- final player = AudioPlayerService();
- final itemId = player.currentItemId;
- final epId = player.currentEpisodeId;
- final dlKey = epId != null && itemId != null
- ? '$itemId-$epId'
- : itemId;
- if (dlKey == null || !dl.isDownloaded(dlKey)) {
- player.stop();
- }
+ trailing: OfflineStatusIcon(
+ onTapWhenOnline: () {
+ lib.setManualOffline(true);
+ final dl = DownloadService();
+ final player = AudioPlayerService();
+ final itemId = player.currentItemId;
+ final epId = player.currentEpisodeId;
+ final dlKey = epId != null && itemId != null
+ ? '$itemId-$epId'
+ : itemId;
+ if (dlKey == null || !dl.isDownloaded(dlKey)) {
+ player.stop();
}
},
- child: Icon(
- lib.isOffline ? Icons.cloud_off_rounded : Icons.cloud_done_rounded,
- size: 20, color: lib.isOffline ? Colors.orange : Colors.green,
- ),
),
actions: hasMultipleLibraries ? [
GestureDetector(
diff --git a/lib/widgets/offline_status_icon.dart b/lib/widgets/offline_status_icon.dart
new file mode 100644
index 00000000..c6671f85
--- /dev/null
+++ b/lib/widgets/offline_status_icon.dart
@@ -0,0 +1,60 @@
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+import '../l10n/app_localizations.dart';
+import '../providers/library_provider.dart';
+import 'overlay_toast.dart';
+
+/// Cloud icon in page headers showing online/offline state.
+///
+/// Tap when offline triggers an immediate reconnect attempt; on failure shows
+/// a "still offline" toast. Tap when online runs [onTapWhenOnline] (typically
+/// switches to manual offline + stops the player when nothing is downloaded).
+class OfflineStatusIcon extends StatelessWidget {
+ final VoidCallback? onTapWhenOnline;
+
+ const OfflineStatusIcon({super.key, this.onTapWhenOnline});
+
+ @override
+ Widget build(BuildContext context) {
+ final lib = context.watch<LibraryProvider>();
+ final l = AppLocalizations.of(context)!;
+ final offline = lib.isOffline;
+ final reconnecting = lib.isReconnecting;
+ return GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: reconnecting
+ ? null
+ : () async {
+ if (offline) {
+ final ok = await lib.tryReconnect();
+ if (!ok && context.mounted) {
+ showOverlayToast(context, l.stillOffline,
+ icon: Icons.cloud_off_rounded);
+ }
+ } else {
+ onTapWhenOnline?.call();
+ }
+ },
+ child: Padding(
+ padding: const EdgeInsets.all(8),
+ child: SizedBox(
+ width: 20,
+ height: 20,
+ child: reconnecting
+ ? const Padding(
+ padding: EdgeInsets.all(1),
+ child: CircularProgressIndicator(
+ strokeWidth: 2,
+ color: Colors.orange,
+ ),
+ )
+ : Icon(
+ offline ? Icons.cloud_off_rounded : Icons.cloud_done_rounded,
+ size: 20,
+ color: offline ? Colors.orange : Colors.green,
+ ),
+ ),
+ ),
+ );
+ }
+}