Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add backup/restore settings and reorganize settings page

Nathan Poulson6b4582258f58
diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart
index ed34d4d3..a0d666af 100644
--- a/lib/screens/login_screen.dart
+++ b/lib/screens/login_screen.dart
@@ -1,10 +1,14 @@
import 'dart:async';
+import 'dart:convert';
+import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
+import 'package:file_picker/file_picker.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
import '../services/api_service.dart';
+import '../services/backup_service.dart';
import '../services/oidc_service.dart';
import '../services/user_account_service.dart';
import '../widgets/absorb_wave_icon.dart';
@@ -592,16 +596,35 @@ class _LoginScreenState extends State<LoginScreen>
),
),
- // ── Version label ──
+ // ── Version + Restore pill ──
const SizedBox(height: 32),
FadeTransition(
opacity: _fadeAnim,
- child: Text(
- _appVersion,
- style: tt.labelSmall?.copyWith(
- color: cs.onSurfaceVariant.withValues(alpha: 0.3),
- letterSpacing: 1,
- ),
+ child: Column(
+ children: [
+ Text(
+ _appVersion,
+ style: tt.labelSmall?.copyWith(
+ color: cs.onSurfaceVariant.withValues(alpha: 0.3),
+ letterSpacing: 1,
+ ),
+ ),
+ const SizedBox(height: 12),
+ ActionChip(
+ avatar: Icon(Icons.restore_rounded, size: 16,
+ color: cs.onSurfaceVariant.withValues(alpha: 0.6)),
+ label: Text('Restore from backup',
+ style: tt.labelSmall?.copyWith(
+ color: cs.onSurfaceVariant.withValues(alpha: 0.6),
+ )),
+ backgroundColor: cs.surfaceContainerHighest.withValues(alpha: 0.3),
+ side: BorderSide(color: cs.outlineVariant.withValues(alpha: 0.15)),
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(20),
+ ),
+ onPressed: _restoreFromBackup,
+ ),
+ ],
),
),
@@ -700,6 +723,94 @@ class _LoginScreenState extends State<LoginScreen>
);
}
+ Future<void> _restoreFromBackup() async {
+ try {
+ final result = await FilePicker.platform.pickFiles(type: FileType.any);
+ if (result == null || result.files.isEmpty) return;
+
+ final file = File(result.files.single.path!);
+ final jsonStr = await file.readAsString();
+ final data = jsonDecode(jsonStr) as Map<String, dynamic>;
+
+ if (!data.containsKey('version')) {
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(
+ const SnackBar(content: Text('Invalid backup file')),
+ );
+ }
+ return;
+ }
+
+ final accounts = data['accounts'] as List<dynamic>?;
+ final accountCount = accounts?.length ?? 0;
+ final hasAccounts = accountCount > 0;
+
+ if (!mounted) return;
+ final confirm = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ title: const Text('Restore backup?'),
+ content: Text(hasAccounts
+ ? 'This will restore all settings and $accountCount saved account(s). You\'ll be signed in automatically.'
+ : 'This will restore all settings. No accounts were included in this backup.'),
+ actions: [
+ TextButton(
+ onPressed: () => Navigator.pop(ctx, false),
+ child: const Text('Cancel'),
+ ),
+ FilledButton(
+ onPressed: () => Navigator.pop(ctx, true),
+ child: const Text('Restore'),
+ ),
+ ],
+ ),
+ );
+
+ if (confirm != true) return;
+
+ await BackupService.importSettings(data);
+
+ // Auto-login with the first restored account
+ if (hasAccounts && mounted) {
+ final restoredAccounts = UserAccountService().accounts;
+ if (restoredAccounts.isNotEmpty) {
+ final auth = context.read<AuthProvider>();
+ await auth.switchToAccount(restoredAccounts.first);
+
+ if (mounted) {
+ if (auth.isAuthenticated && auth.serverReachable) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: Text('Restored settings and signed in as ${restoredAccounts.first.username}'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
+ } else {
+ // Token expired — accounts are saved but need re-auth
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: const Text('Settings restored. Session expired \u2014 sign in to continue.'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
+ setState(() {}); // Refresh to show saved accounts list
+ }
+ }
+ }
+ } else if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: const Text('Settings restored'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
+ }
+ } catch (e) {
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(
+ SnackBar(content: Text('Restore failed: $e')),
+ );
+ }
+ }
+ }
+
Future<void> _quickSwitch(SavedAccount account) async {
setState(() => _isConnecting = true);
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index 2b5c4ce5..969c541d 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -1,4 +1,6 @@
+import 'dart:convert';
import 'dart:io';
+import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:permission_handler/permission_handler.dart';
@@ -11,6 +13,7 @@ import '../services/audio_player_service.dart';
import '../services/download_service.dart';
import '../services/sleep_timer_service.dart';
import '../services/user_account_service.dart';
+import '../services/backup_service.dart';
import '../services/log_service.dart';
import '../screens/login_screen.dart';
import '../screens/app_shell.dart';
@@ -51,6 +54,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
int _totalDownloadSizeBytes = 0;
AutoSleepSettings _autoSleepSettings = const AutoSleepSettings();
String _appVersion = '';
+ String? _expandedSection;
@override
void initState() {
@@ -416,6 +420,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
icon: Icons.palette_outlined,
title: 'Appearance',
cs: cs,
+ isExpanded: _expandedSection == 'Appearance',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Appearance' : null),
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
@@ -451,11 +457,107 @@ class _SettingsScreenState extends State<SettingsScreen> {
),
const SizedBox(height: 16),
+ // ── Absorbing Cards ──
+ _CollapsibleSection(
+ icon: Icons.style_rounded,
+ title: 'Absorbing Cards',
+ cs: cs,
+ isExpanded: _expandedSection == 'Absorbing Cards',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Absorbing Cards' : null),
+ children: [
+ SwitchListTile(
+ title: const Text('Full screen player'),
+ subtitle: Text(
+ _fullScreenPlayer ? 'On — books open in full screen when played' : 'Off — play within card view',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _fullScreenPlayer,
+ onChanged: _loaded ? (v) {
+ setState(() => _fullScreenPlayer = v);
+ PlayerSettings.setFullScreenPlayer(v);
+ } : null,
+ ),
+ const Divider(height: 1, indent: 16, endIndent: 16),
+ SwitchListTile(
+ title: const Text('Full book scrubber'),
+ subtitle: Text(
+ _showBookSlider ? 'On — seekable slider across entire book' : 'Off — progress bar only',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _showBookSlider,
+ onChanged: _loaded ? (v) {
+ setState(() => _showBookSlider = v);
+ PlayerSettings.setShowBookSlider(v);
+ } : null,
+ ),
+ const Divider(height: 1, indent: 16, endIndent: 16),
+ SwitchListTile(
+ title: const Text('Speed-adjusted time'),
+ subtitle: Text(
+ _speedAdjustedTime ? 'On — remaining time reflects playback speed' : 'Off — showing raw audio duration',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _speedAdjustedTime,
+ onChanged: _loaded ? (v) {
+ setState(() => _speedAdjustedTime = v);
+ PlayerSettings.setSpeedAdjustedTime(v);
+ } : null,
+ ),
+ const Divider(height: 1, indent: 16, endIndent: 16),
+ Padding(
+ padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
+ child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
+ Text('When finished', style: tt.bodyMedium?.copyWith(color: cs.onSurface)),
+ const SizedBox(height: 4),
+ Text('What happens to the absorbing card when a book or episode finishes',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ const SizedBox(height: 8),
+ SizedBox(width: double.infinity, child: SegmentedButton<String>(
+ segments: const [
+ ButtonSegment(value: 'overlay', icon: Icon(Icons.layers_rounded), label: Text('Show Overlay')),
+ ButtonSegment(value: 'auto_remove', icon: Icon(Icons.auto_delete_rounded), label: Text('Auto-remove')),
+ ],
+ selected: {_whenFinished},
+ onSelectionChanged: _loaded ? (s) {
+ setState(() => _whenFinished = s.first);
+ PlayerSettings.setWhenFinished(s.first);
+ } : null,
+ style: const ButtonStyle(visualDensity: VisualDensity.compact),
+ )),
+ ]),
+ ),
+ const Divider(height: 1, indent: 16, endIndent: 16),
+ SwitchListTile(
+ title: const Text('Auto-play next book in series'),
+ subtitle: Text(
+ _autoPlayNextBook ? 'On — next book plays automatically' : 'Off',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _autoPlayNextBook,
+ onChanged: _loaded ? (v) {
+ setState(() => _autoPlayNextBook = v);
+ PlayerSettings.setAutoPlayNextBook(v);
+ } : null,
+ ),
+ const Divider(height: 1, indent: 16, endIndent: 16),
+ SwitchListTile(
+ title: const Text('Auto-play next podcast episode'),
+ subtitle: Text(
+ _autoPlayNextPodcast ? 'On — next episode plays automatically' : 'Off',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _autoPlayNextPodcast,
+ onChanged: _loaded ? (v) {
+ setState(() => _autoPlayNextPodcast = v);
+ PlayerSettings.setAutoPlayNextPodcast(v);
+ } : null,
+ ),
+ ],
+ ),
+ const SizedBox(height: 16),
+
// ── Playback ──
_CollapsibleSection(
icon: Icons.play_circle_outline_rounded,
title: 'Playback',
cs: cs,
+ isExpanded: _expandedSection == 'Playback',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Playback' : null),
children: [
// Default speed
Padding(
@@ -549,90 +651,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
} : null,
),
const Divider(height: 1, indent: 16, endIndent: 16),
- // Toggles
- SwitchListTile(
- title: const Text('Full book scrubber'),
- subtitle: Text(
- _showBookSlider ? 'On — seekable slider across entire book' : 'Off — progress bar only',
- style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
- value: _showBookSlider,
- onChanged: _loaded ? (v) {
- setState(() => _showBookSlider = v);
- PlayerSettings.setShowBookSlider(v);
- } : null,
- ),
- const Divider(height: 1, indent: 16, endIndent: 16),
- SwitchListTile(
- title: const Text('Speed-adjusted time'),
- subtitle: Text(
- _speedAdjustedTime ? 'On — remaining time reflects playback speed' : 'Off — showing raw audio duration',
- style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
- value: _speedAdjustedTime,
- onChanged: _loaded ? (v) {
- setState(() => _speedAdjustedTime = v);
- PlayerSettings.setSpeedAdjustedTime(v);
- } : null,
- ),
- const Divider(height: 1, indent: 16, endIndent: 16),
- SwitchListTile(
- title: const Text('Full screen player'),
- subtitle: Text(
- _fullScreenPlayer ? 'On — books open in full screen when played' : 'Off — play within card view',
- style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
- value: _fullScreenPlayer,
- onChanged: _loaded ? (v) {
- setState(() => _fullScreenPlayer = v);
- PlayerSettings.setFullScreenPlayer(v);
- } : null,
- ),
- const Divider(height: 1, indent: 16, endIndent: 16),
- SwitchListTile(
- title: const Text('Auto-play next book in series'),
- subtitle: Text(
- _autoPlayNextBook ? 'On — next book plays automatically' : 'Off',
- style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
- value: _autoPlayNextBook,
- onChanged: _loaded ? (v) {
- setState(() => _autoPlayNextBook = v);
- PlayerSettings.setAutoPlayNextBook(v);
- } : null,
- ),
- const Divider(height: 1, indent: 16, endIndent: 16),
- SwitchListTile(
- title: const Text('Auto-play next podcast episode'),
- subtitle: Text(
- _autoPlayNextPodcast ? 'On — next episode plays automatically' : 'Off',
- style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
- value: _autoPlayNextPodcast,
- onChanged: _loaded ? (v) {
- setState(() => _autoPlayNextPodcast = v);
- PlayerSettings.setAutoPlayNextPodcast(v);
- } : null,
- ),
- const Divider(height: 1, indent: 16, endIndent: 16),
- Padding(
- padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
- child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
- Text('When finished', style: tt.bodyMedium?.copyWith(color: cs.onSurface)),
- const SizedBox(height: 4),
- Text('What happens to the absorbing card when a book or episode finishes',
- style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
- const SizedBox(height: 8),
- SizedBox(width: double.infinity, child: SegmentedButton<String>(
- segments: const [
- ButtonSegment(value: 'overlay', icon: Icon(Icons.layers_rounded), label: Text('Show Overlay')),
- ButtonSegment(value: 'auto_remove', icon: Icon(Icons.auto_delete_rounded), label: Text('Auto-remove')),
- ],
- selected: {_whenFinished},
- onSelectionChanged: _loaded ? (s) {
- setState(() => _whenFinished = s.first);
- PlayerSettings.setWhenFinished(s.first);
- } : null,
- style: const ButtonStyle(visualDensity: VisualDensity.compact),
- )),
- ]),
- ),
- const Divider(height: 1, indent: 16, endIndent: 16),
// ── Auto-Rewind ──
SwitchListTile(
title: const Text('Auto-rewind on resume'),
@@ -732,6 +750,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
icon: Icons.bedtime_outlined,
title: 'Sleep Timer',
cs: cs,
+ isExpanded: _expandedSection == 'Sleep Timer',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Sleep Timer' : null),
children: [
SwitchListTile(
title: const Text('Shake to add time'),
@@ -913,6 +933,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
icon: Icons.download_outlined,
title: 'Downloads & Storage',
cs: cs,
+ isExpanded: _expandedSection == 'Downloads & Storage',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Downloads & Storage' : null),
children: [
SwitchListTile(
title: const Text('Download over Wi-Fi only'),
@@ -964,6 +986,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
icon: Icons.auto_stories_outlined,
title: 'Library',
cs: cs,
+ isExpanded: _expandedSection == 'Library',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Library' : null),
children: [
SwitchListTile(
title: const Text('Hide eBook-only titles'),
@@ -1019,6 +1043,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
icon: Icons.shield_outlined,
title: 'Permissions',
cs: cs,
+ isExpanded: _expandedSection == 'Permissions',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Permissions' : null),
children: [
ListTile(
leading: const Icon(Icons.notifications_outlined),
@@ -1078,6 +1104,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
icon: Icons.support_agent_rounded,
title: 'Issues & Support',
cs: cs,
+ isExpanded: _expandedSection == 'Issues & Support',
+ onExpansionChanged: (v) => setState(() => _expandedSection = v ? 'Issues & Support' : null),
children: [
ListTile(
leading: Icon(Icons.bug_report_outlined, color: cs.onSurfaceVariant),
@@ -1199,6 +1227,48 @@ class _SettingsScreenState extends State<SettingsScreen> {
),
),
+ const SizedBox(height: 16),
+
+ // ── Backup & Restore ──
+ Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 16),
+ child: Card(
+ elevation: 0,
+ color: cs.surfaceContainerHigh,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
+ child: Padding(
+ padding: const EdgeInsets.all(16),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(children: [
+ Icon(Icons.settings_backup_restore_rounded, color: cs.primary, size: 22),
+ const SizedBox(width: 10),
+ Text('Backup & Restore', style: tt.titleSmall?.copyWith(fontWeight: FontWeight.w600)),
+ ]),
+ const SizedBox(height: 4),
+ Text('Save or restore all your settings to a file',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ const SizedBox(height: 14),
+ Row(children: [
+ Expanded(child: FilledButton.tonalIcon(
+ icon: const Icon(Icons.upload_rounded, size: 18),
+ label: const Text('Back up'),
+ onPressed: () => _backupSettings(context, cs, tt),
+ )),
+ const SizedBox(width: 10),
+ Expanded(child: OutlinedButton.icon(
+ icon: const Icon(Icons.download_rounded, size: 18),
+ label: const Text('Restore'),
+ onPressed: () => _restoreSettings(context, cs, tt),
+ )),
+ ]),
+ ],
+ ),
+ ),
+ ),
+ ),
+
const SizedBox(height: 24),
// ── Accounts ──
@@ -1654,6 +1724,186 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
}
+ void _backupSettings(BuildContext context, ColorScheme cs, TextTheme tt) {
+ showDialog(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ icon: const Icon(Icons.shield_rounded),
+ title: const Text('Include login info?'),
+ content: const Text(
+ 'Would you like to include login credentials for all your saved accounts in the backup?\n\n'
+ 'This makes it easy to restore on a new device, but the file will contain your auth tokens.',
+ ),
+ actions: [
+ TextButton(
+ onPressed: () {
+ Navigator.pop(ctx);
+ _performBackup(context, includeAccounts: false);
+ },
+ child: const Text('No, settings only'),
+ ),
+ FilledButton(
+ onPressed: () {
+ Navigator.pop(ctx);
+ _performBackup(context, includeAccounts: true);
+ },
+ child: const Text('Yes, include accounts'),
+ ),
+ ],
+ ),
+ );
+ }
+
+ Future<void> _performBackup(BuildContext context, {required bool includeAccounts}) async {
+ try {
+ final data = await BackupService.exportSettings(includeAccounts: includeAccounts);
+ final jsonStr = const JsonEncoder.withIndent(' ').convert(data);
+ final now = DateTime.now();
+ final datePart = '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}';
+ final fileName = 'absorb_backup_$datePart.absorb';
+
+ final bytes = Uint8List.fromList(utf8.encode(jsonStr));
+
+ final result = await FilePicker.platform.saveFile(
+ dialogTitle: 'Save Absorb backup',
+ fileName: fileName,
+ type: FileType.any,
+ bytes: bytes,
+ );
+
+ if (result != null) {
+ // Desktop platforms need manual file write; mobile writes via bytes param
+ if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
+ await File(result).writeAsString(jsonStr);
+ }
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: Text(includeAccounts
+ ? 'Backup saved (with accounts)'
+ : 'Backup saved (settings only)'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
+ }
+ }
+ } catch (e) {
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(
+ SnackBar(content: Text('Backup failed: $e')),
+ );
+ }
+ }
+ }
+
+ void _restoreSettings(BuildContext context, ColorScheme cs, TextTheme tt) async {
+ try {
+ final result = await FilePicker.platform.pickFiles(type: FileType.any);
+ if (result == null || result.files.single.path == null) return;
+
+ final file = File(result.files.single.path!);
+ final jsonStr = await file.readAsString();
+ final data = jsonDecode(jsonStr) as Map<String, dynamic>;
+
+ if (data['version'] == null) {
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(
+ const SnackBar(content: Text('Invalid backup file')),
+ );
+ }
+ return;
+ }
+
+ if (!mounted) return;
+
+ final accounts = data['accounts'] as List<dynamic>?;
+ final hasAccounts = accounts != null && accounts.isNotEmpty;
+ final createdAt = data['createdAt'] as String?;
+ final appVersion = data['appVersion'] as String?;
+
+ String details = '';
+ if (appVersion != null) details += 'From Absorb v$appVersion';
+ if (createdAt != null) {
+ final dt = DateTime.tryParse(createdAt);
+ if (dt != null) {
+ details += details.isEmpty ? '' : ' · ';
+ details += '${dt.month}/${dt.day}/${dt.year}';
+ }
+ }
+
+ final confirmed = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ icon: const Icon(Icons.restore_rounded),
+ title: const Text('Restore backup?'),
+ content: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ const Text('This will replace all your current settings with the backup values.'),
+ if (details.isNotEmpty) ...[
+ const SizedBox(height: 12),
+ Text(details, style: TextStyle(color: cs.onSurfaceVariant, fontSize: 12)),
+ ],
+ if (hasAccounts) ...[
+ const SizedBox(height: 12),
+ Container(
+ padding: const EdgeInsets.all(10),
+ decoration: BoxDecoration(
+ color: cs.primaryContainer.withValues(alpha: 0.15),
+ borderRadius: BorderRadius.circular(8),
+ ),
+ child: Row(children: [
+ Icon(Icons.people_rounded, size: 16, color: cs.primary),
+ const SizedBox(width: 8),
+ Text('Includes ${accounts.length} saved account(s)',
+ style: TextStyle(fontSize: 12, color: cs.primary)),
+ ]),
+ ),
+ ],
+ ],
+ ),
+ actions: [
+ TextButton(
+ onPressed: () => Navigator.pop(ctx, false),
+ child: const Text('Cancel'),
+ ),
+ FilledButton(
+ onPressed: () => Navigator.pop(ctx, true),
+ child: const Text('Restore'),
+ ),
+ ],
+ ),
+ );
+
+ if (confirmed != true || !mounted) return;
+
+ await BackupService.importSettings(data);
+
+ // Apply theme immediately
+ final theme = data['settings']?['themeMode'] as String?;
+ if (theme != null) {
+ themeNotifier.value = parseThemeMode(theme);
+ }
+
+ // Refresh UI
+ await _loadSettings();
+
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: const Text('Settings restored successfully'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
+ }
+ } catch (e) {
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(
+ SnackBar(content: Text('Restore failed: $e')),
+ );
+ }
+ }
+ }
+
void _confirmLogout(BuildContext context) {
final cs = Theme.of(context).colorScheme;
showDialog(
@@ -1756,35 +2006,67 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
}
-class _CollapsibleSection extends StatelessWidget {
+class _CollapsibleSection extends StatefulWidget {
final IconData icon;
final String title;
final ColorScheme cs;
final List<Widget> children;
+ final bool isExpanded;
+ final ValueChanged<bool>? onExpansionChanged;
const _CollapsibleSection({
required this.icon,
required this.title,
required this.cs,
required this.children,
+ this.isExpanded = false,
+ this.onExpansionChanged,
});
+ @override
+ State<_CollapsibleSection> createState() => _CollapsibleSectionState();
+}
+
+class _CollapsibleSectionState extends State<_CollapsibleSection> {
+ final ExpansibleController _controller = ExpansibleController();
+ bool _isBuilt = false;
+
+ @override
+ void didUpdateWidget(covariant _CollapsibleSection oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (_isBuilt && widget.isExpanded != oldWidget.isExpanded) {
+ if (widget.isExpanded) {
+ _controller.expand();
+ } else {
+ _controller.collapse();
+ }
+ }
+ }
+
@override
Widget build(BuildContext context) {
+ _isBuilt = true;
+ final isDark = Theme.of(context).brightness == Brightness.dark;
return Card(
- elevation: 0,
+ elevation: isDark ? 0 : 2,
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
- color: cs.surfaceContainerHigh,
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
+ color: widget.cs.surfaceContainerHighest,
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(16),
+ side: isDark ? BorderSide(color: widget.cs.outlineVariant.withValues(alpha: 0.3)) : BorderSide.none,
+ ),
clipBehavior: Clip.antiAlias,
child: Theme(
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: ExpansionTile(
- leading: Icon(icon, color: cs.primary, size: 22),
- title: Text(title, style: TextStyle(fontWeight: FontWeight.w600)),
+ controller: _controller,
+ initiallyExpanded: widget.isExpanded,
+ onExpansionChanged: widget.onExpansionChanged,
+ leading: Icon(widget.icon, color: widget.cs.primary, size: 22),
+ title: Text(widget.title, style: TextStyle(fontWeight: FontWeight.w600)),
childrenPadding: EdgeInsets.zero,
tilePadding: const EdgeInsets.symmetric(horizontal: 16),
- children: children,
+ children: widget.children,
),
),
);
diff --git a/lib/services/backup_service.dart b/lib/services/backup_service.dart
new file mode 100644
index 00000000..4f0ed541
--- /dev/null
+++ b/lib/services/backup_service.dart
@@ -0,0 +1,201 @@
+import 'package:shared_preferences/shared_preferences.dart';
+import 'package:package_info_plus/package_info_plus.dart';
+import 'audio_player_service.dart';
+import 'sleep_timer_service.dart';
+import 'user_account_service.dart';
+
+class BackupService {
+ static Future<Map<String, dynamic>> exportSettings({
+ required bool includeAccounts,
+ }) async {
+ final prefs = await SharedPreferences.getInstance();
+ final pkgInfo = await PackageInfo.fromPlatform();
+
+ // PlayerSettings
+ final settings = <String, dynamic>{
+ 'defaultSpeed': await PlayerSettings.getDefaultSpeed(),
+ 'wifiOnlyDownloads': await PlayerSettings.getWifiOnlyDownloads(),
+ 'autoPlayNextBook': await PlayerSettings.getAutoPlayNextBook(),
+ 'autoPlayNextPodcast': await PlayerSettings.getAutoPlayNextPodcast(),
+ 'whenFinished': await PlayerSettings.getWhenFinished(),
+ 'showBookSlider': await PlayerSettings.getShowBookSlider(),
+ 'speedAdjustedTime': await PlayerSettings.getSpeedAdjustedTime(),
+ 'forwardSkip': await PlayerSettings.getForwardSkip(),
+ 'backSkip': await PlayerSettings.getBackSkip(),
+ 'shakeToResetSleep': await PlayerSettings.getShakeToResetSleep(),
+ 'shakeAddMinutes': await PlayerSettings.getShakeAddMinutes(),
+ 'resetSleepOnPause': await PlayerSettings.getResetSleepOnPause(),
+ 'sleepFadeOut': await PlayerSettings.getSleepFadeOut(),
+ 'hideEbookOnly': await PlayerSettings.getHideEbookOnly(),
+ 'collapseSeries': await PlayerSettings.getCollapseSeries(),
+ 'librarySort': await PlayerSettings.getLibrarySort(),
+ 'librarySortAsc': await PlayerSettings.getLibrarySortAsc(),
+ 'libraryFilter': await PlayerSettings.getLibraryFilter(),
+ 'libraryGenreFilter': await PlayerSettings.getLibraryGenreFilter(),
+ 'showGoodreadsButton': await PlayerSettings.getShowGoodreadsButton(),
+ 'loggingEnabled': await PlayerSettings.getLoggingEnabled(),
+ 'fullScreenPlayer': await PlayerSettings.getFullScreenPlayer(),
+ 'themeMode': await PlayerSettings.getThemeMode(),
+ 'cardButtonOrder': await PlayerSettings.getCardButtonOrder(),
+ };
+
+ // AutoRewind
+ final rewind = await AutoRewindSettings.load();
+ final autoRewind = <String, dynamic>{
+ 'enabled': rewind.enabled,
+ 'min': rewind.minRewind,
+ 'max': rewind.maxRewind,
+ 'delay': rewind.activationDelay,
+ };
+
+ // AutoSleep
+ final sleep = await AutoSleepSettings.load();
+ final autoSleep = <String, dynamic>{
+ 'enabled': sleep.enabled,
+ 'startHour': sleep.startHour,
+ 'startMinute': sleep.startMinute,
+ 'endHour': sleep.endHour,
+ 'endMinute': sleep.endMinute,
+ 'durationMinutes': sleep.durationMinutes,
+ };
+
+ // Equalizer
+ final equalizer = <String, dynamic>{
+ 'enabled': prefs.getBool('eq_enabled') ?? false,
+ 'preset': prefs.getString('eq_preset') ?? 'flat',
+ 'bassBoost': prefs.getDouble('eq_bassBoost') ?? 0.0,
+ 'virtualizer': prefs.getDouble('eq_virtualizer') ?? 0.0,
+ 'loudnessGain': prefs.getDouble('eq_loudnessGain') ?? 0.0,
+ 'bands': prefs.getString('eq_bands'),
+ };
+
+ // Per-book speeds
+ final bookSpeeds = <String, double>{};
+ for (final key in prefs.getKeys()) {
+ if (key.startsWith('bookSpeed_')) {
+ final itemId = key.substring('bookSpeed_'.length);
+ final speed = prefs.getDouble(key);
+ if (speed != null) bookSpeeds[itemId] = speed;
+ }
+ }
+
+ // Offline mode
+ final offlineMode = prefs.getBool('manual_offline_mode') ?? false;
+
+ // Accounts (optional)
+ List<Map<String, dynamic>>? accounts;
+ if (includeAccounts) {
+ accounts = UserAccountService()
+ .accounts
+ .map((a) => a.toJson())
+ .toList();
+ }
+
+ return {
+ 'version': 1,
+ 'createdAt': DateTime.now().toIso8601String(),
+ 'appVersion': pkgInfo.version,
+ 'settings': settings,
+ 'autoRewind': autoRewind,
+ 'autoSleep': autoSleep,
+ 'equalizer': equalizer,
+ 'bookSpeeds': bookSpeeds,
+ 'offlineMode': offlineMode,
+ 'accounts': accounts,
+ };
+ }
+
+ static Future<void> importSettings(Map<String, dynamic> data) async {
+ final prefs = await SharedPreferences.getInstance();
+
+ // PlayerSettings
+ final s = data['settings'] as Map<String, dynamic>? ?? {};
+ if (s['defaultSpeed'] != null) PlayerSettings.setDefaultSpeed((s['defaultSpeed'] as num).toDouble());
+ if (s['wifiOnlyDownloads'] != null) PlayerSettings.setWifiOnlyDownloads(s['wifiOnlyDownloads'] as bool);
+ if (s['autoPlayNextBook'] != null) PlayerSettings.setAutoPlayNextBook(s['autoPlayNextBook'] as bool);
+ if (s['autoPlayNextPodcast'] != null) PlayerSettings.setAutoPlayNextPodcast(s['autoPlayNextPodcast'] as bool);
+ if (s['whenFinished'] != null) PlayerSettings.setWhenFinished(s['whenFinished'] as String);
+ if (s['showBookSlider'] != null) PlayerSettings.setShowBookSlider(s['showBookSlider'] as bool);
+ if (s['speedAdjustedTime'] != null) PlayerSettings.setSpeedAdjustedTime(s['speedAdjustedTime'] as bool);
+ if (s['forwardSkip'] != null) PlayerSettings.setForwardSkip(s['forwardSkip'] as int);
+ if (s['backSkip'] != null) PlayerSettings.setBackSkip(s['backSkip'] as int);
+ if (s['shakeToResetSleep'] != null) PlayerSettings.setShakeToResetSleep(s['shakeToResetSleep'] as bool);
+ if (s['shakeAddMinutes'] != null) PlayerSettings.setShakeAddMinutes(s['shakeAddMinutes'] as int);
+ if (s['resetSleepOnPause'] != null) PlayerSettings.setResetSleepOnPause(s['resetSleepOnPause'] as bool);
+ if (s['sleepFadeOut'] != null) PlayerSettings.setSleepFadeOut(s['sleepFadeOut'] as bool);
+ if (s['hideEbookOnly'] != null) PlayerSettings.setHideEbookOnly(s['hideEbookOnly'] as bool);
+ if (s['collapseSeries'] != null) PlayerSettings.setCollapseSeries(s['collapseSeries'] as bool);
+ if (s['librarySort'] != null) PlayerSettings.setLibrarySort(s['librarySort'] as String);
+ if (s['librarySortAsc'] != null) PlayerSettings.setLibrarySortAsc(s['librarySortAsc'] as bool);
+ if (s['libraryFilter'] != null) PlayerSettings.setLibraryFilter(s['libraryFilter'] as String);
+ if (s.containsKey('libraryGenreFilter')) PlayerSettings.setLibraryGenreFilter(s['libraryGenreFilter'] as String?);
+ if (s['showGoodreadsButton'] != null) PlayerSettings.setShowGoodreadsButton(s['showGoodreadsButton'] as bool);
+ if (s['loggingEnabled'] != null) PlayerSettings.setLoggingEnabled(s['loggingEnabled'] as bool);
+ if (s['fullScreenPlayer'] != null) PlayerSettings.setFullScreenPlayer(s['fullScreenPlayer'] as bool);
+ if (s['themeMode'] != null) PlayerSettings.setThemeMode(s['themeMode'] as String);
+ if (s['cardButtonOrder'] != null) {
+ PlayerSettings.setCardButtonOrder(
+ (s['cardButtonOrder'] as List<dynamic>).cast<String>(),
+ );
+ }
+
+ // AutoRewind
+ final r = data['autoRewind'] as Map<String, dynamic>?;
+ if (r != null) {
+ await AutoRewindSettings(
+ enabled: r['enabled'] as bool? ?? true,
+ minRewind: (r['min'] as num?)?.toDouble() ?? 1.0,
+ maxRewind: (r['max'] as num?)?.toDouble() ?? 30.0,
+ activationDelay: (r['delay'] as num?)?.toDouble() ?? 0.0,
+ ).save();
+ }
+
+ // AutoSleep
+ final sl = data['autoSleep'] as Map<String, dynamic>?;
+ if (sl != null) {
+ await AutoSleepSettings(
+ enabled: sl['enabled'] as bool? ?? false,
+ startHour: sl['startHour'] as int? ?? 22,
+ startMinute: sl['startMinute'] as int? ?? 0,
+ endHour: sl['endHour'] as int? ?? 6,
+ endMinute: sl['endMinute'] as int? ?? 0,
+ durationMinutes: sl['durationMinutes'] as int? ?? 30,
+ ).save();
+ }
+
+ // Equalizer
+ final eq = data['equalizer'] as Map<String, dynamic>?;
+ if (eq != null) {
+ await prefs.setBool('eq_enabled', eq['enabled'] as bool? ?? false);
+ await prefs.setString('eq_preset', eq['preset'] as String? ?? 'flat');
+ await prefs.setDouble('eq_bassBoost', (eq['bassBoost'] as num?)?.toDouble() ?? 0.0);
+ await prefs.setDouble('eq_virtualizer', (eq['virtualizer'] as num?)?.toDouble() ?? 0.0);
+ await prefs.setDouble('eq_loudnessGain', (eq['loudnessGain'] as num?)?.toDouble() ?? 0.0);
+ if (eq['bands'] != null) {
+ await prefs.setString('eq_bands', eq['bands'] as String);
+ }
+ }
+
+ // Per-book speeds
+ final bookSpeeds = data['bookSpeeds'] as Map<String, dynamic>?;
+ if (bookSpeeds != null) {
+ for (final entry in bookSpeeds.entries) {
+ await PlayerSettings.setBookSpeed(entry.key, (entry.value as num).toDouble());
+ }
+ }
+
+ // Offline mode
+ if (data['offlineMode'] != null) {
+ await prefs.setBool('manual_offline_mode', data['offlineMode'] as bool);
+ }
+
+ // Accounts
+ final accounts = data['accounts'] as List<dynamic>?;
+ if (accounts != null) {
+ for (final a in accounts) {
+ final map = a as Map<String, dynamic>;
+ await UserAccountService().saveAccount(SavedAccount.fromJson(map));
+ }
+ }
+ }
+}