A cross platform Audiobookshelf client for Android and iOS
Dart
Restrict admin to root, account switcher sheet, settings cleanup
Nathan Poulsonbcdcd9f3afa3
diff --git a/lib/screens/admin_podcasts_screen.dart b/lib/screens/admin_podcasts_screen.dartindex 9e2bfb46..325090ab 100644--- a/lib/screens/admin_podcasts_screen.dart+++ b/lib/screens/admin_podcasts_screen.dart@@ -908,14 +908,23 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleT@overridevoid initState() {super.initState();- _item = Map<String, dynamic>.from(widget.item);- _tabCtrl = TabController(length: 3, vsync: this);+ _item = jsonDecode(jsonEncode(widget.item)) as Map<String, dynamic>;+ _tabCtrl = TabController(length: 3, vsync: this)+ ..addListener(_onTabChanged);_initCheckDate();_reloadItem(); // Load full item with episodes_loadFeed(); // Pre-load feed so it's ready when user switches tabs_pollDownloadQueue(); // Check for any in-progress downloads}+ int _lastTabIndex = 0;+ void _onTabChanged() {+ if (_tabCtrl.index != _lastTabIndex && !_tabCtrl.indexIsChanging) {+ _lastTabIndex = _tabCtrl.index;+ setState(() {});+ }+ }+void _initCheckDate() {final lastCheck = _media['lastEpisodeCheck'] as num?;if (lastCheck != null && lastCheck > 0) {@@ -926,7 +935,7 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleT}@override- void dispose() { _pollingQueue = false; _tabCtrl.dispose(); super.dispose(); }+ void dispose() { _pollingQueue = false; _tabCtrl.removeListener(_onTabChanged); _tabCtrl.dispose(); super.dispose(); }Future<void> _removeShow() async {final yes = await showDialog<bool>(context: context, builder: (ctx) => AlertDialog(@@ -948,7 +957,11 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleTfinal api = context.read<AuthProvider>().apiService; if (api == null) return;try {final found = await api.getLibraryItem(_podcastId);- if (found != null && mounted) setState(() => _item = Map<String, dynamic>.from(found));+ if (found != null && mounted) {+ // Deep copy to ensure nested maps are mutable+ final copy = jsonDecode(jsonEncode(found)) as Map<String, dynamic>;+ setState(() => _item = copy);+ }} catch (_) {}}@@ -1039,10 +1052,10 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleTchild: Row(children: [IconButton(icon: Icon(Icons.arrow_back_rounded, color: cs.onSurface.withValues(alpha: 0.54)), onPressed: () => Navigator.pop(context)),const Spacer(),- if (_tabCtrl.index == 0 && _episodes.isNotEmpty || _tabCtrl.index == 1 && _feedEpisodes.isNotEmpty)+ if ((_tabCtrl.index == 0 && _episodes.isNotEmpty) || (_tabCtrl.index == 1 && _feedEpisodes.isNotEmpty))IconButton(icon: Icon(- (_isSelectingDownloaded || _isSelecting) ? Icons.checklist_rounded : Icons.checklist_rounded,+ Icons.checklist_rounded,color: (_isSelectingDownloaded || _isSelecting) ? cs.primary : cs.onSurface.withValues(alpha: 0.3),size: 22,),@@ -1089,7 +1102,6 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleTtabs: const [Tab(text: 'Downloaded'), Tab(text: 'Feed'), Tab(text: 'Settings')],onTap: (i) {if (i == 1 && _feedEpisodes.isEmpty && !_loadingFeed) _loadFeed();- setState(() {}); // Rebuild header button for active tab},),@@ -1625,12 +1637,20 @@ class _PodcastDetailScreenState extends State<_PodcastDetailScreen> with SingleTif (api == null) return;final ok = await api.updatePodcastMedia(_podcastId, {'autoDownloadEpisodes': v,- if (!isOn) 'autoDownloadSchedule': '0 * * * *',+ if (v && !isOn) 'autoDownloadSchedule': '0 * * * *',});- if (ok && mounted) {- _media['autoDownloadEpisodes'] = v;- if (!v) _media.remove('autoDownloadSchedule');- setState(() {}); // rebuild entire settings tab+ if (mounted) {+ if (ok) {+ final media = _item['media'];+ if (media is Map<String, dynamic>) {+ media['autoDownloadEpisodes'] = v;+ if (!v) media.remove('autoDownloadSchedule');+ }+ } else {+ _msg('Failed to update auto-download setting');+ }+ setLocalState(() {});+ setState(() {});widget.onChanged();}},diff --git a/lib/screens/library_screen.dart b/lib/screens/library_screen.dartindex 41912227..6d9fc8d6 100644--- a/lib/screens/library_screen.dart+++ b/lib/screens/library_screen.dart@@ -1244,7 +1244,7 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMimainAxisSize: MainAxisSize.min,children: [_buildFloatingSortButton(cs, tt),- if (context.read<AuthProvider>().isAdmin) ...[+ if (context.read<AuthProvider>().isRoot) ...[const SizedBox(width: 8),_buildFloatingManageButton(cs),],diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dartindex 2a515d59..0e25b998 100644--- a/lib/screens/settings_screen.dart+++ b/lib/screens/settings_screen.dart@@ -382,7 +382,9 @@ class _SettingsScreenState extends State<SettingsScreen> {// ── User Profile ──Padding(padding: const EdgeInsets.fromLTRB(20, 8, 20, 0),- child: Column(+ child: GestureDetector(+ onTap: () => _showAccountSheet(context),+ child: Column(crossAxisAlignment: CrossAxisAlignment.start,children: [Row(@@ -399,6 +401,8 @@ class _SettingsScreenState extends State<SettingsScreen> {child: Text(auth.isRoot ? 'Root Admin' : 'Admin', style: tt.labelSmall?.copyWith(color: auth.isRoot ? Colors.amber : cs.primary, fontWeight: FontWeight.w600, fontSize: 10)),),+ const SizedBox(width: 4),+ Icon(Icons.unfold_more_rounded, size: 20, color: cs.onSurface.withValues(alpha: 0.3)),],),const SizedBox(height: 2),@@ -408,10 +412,11 @@ class _SettingsScreenState extends State<SettingsScreen> {style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),],),+ ),),// ── Admin Controls ──- if (auth.isAdmin)+ if (auth.isRoot)Padding(padding: const EdgeInsets.fromLTRB(16, 16, 16, 0),child: Material(@@ -1850,71 +1855,10 @@ class _SettingsScreenState extends State<SettingsScreen> {mode: LaunchMode.externalApplication),),),- const SizedBox(height: 12),- Row(- mainAxisSize: MainAxisSize.min,- children: [- Text(- 'Absorb v$_appVersion',- style: tt.bodySmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.4)),- ),- const SizedBox(width: 4),- Icon(- _isGithubBuild ? Icons.code_rounded : Icons.store_rounded,- size: 14,- color: cs.onSurfaceVariant.withValues(alpha: 0.3),- ),- if (auth.serverVersion != null)- Text(- ' · Server ${auth.serverVersion}',- style: tt.bodySmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.4)),- ),- ],- ),],),),- if (_isGithubBuild) ...[- const SizedBox(height: 8),- TextButton.icon(- onPressed: () async {- final info = await UpdateCheckerService.check(force: true);- if (!mounted) return;- if (info == null || !info.hasUpdate) {- ScaffoldMessenger.of(context).showSnackBar(- const SnackBar(content: Text('You\'re on the latest version')),- );- return;- }- showDialog(- context: context,- builder: (ctx) => AlertDialog(- title: const Text('Update available'),- content: Text('A new version of Absorb is available: ${info.latestVersion}\n\nYou are on ${info.currentVersion}.'),- actions: [- TextButton(- onPressed: () => Navigator.pop(ctx),- child: const Text('Later'),- ),- FilledButton(- onPressed: () {- Navigator.pop(ctx);- launchUrl(Uri.parse(info.downloadUrl), mode: LaunchMode.externalApplication);- },- child: const Text('Download'),- ),- ],- ),- );- },- icon: const Icon(Icons.system_update_rounded, size: 16),- label: const Text('Check for update'),- ),- ],-const SizedBox(height: 16),// ── Backup & Restore ──@@ -1979,136 +1923,68 @@ class _SettingsScreenState extends State<SettingsScreen> {),),- const SizedBox(height: 24),-- // ── Accounts ──- Builder(builder: (ctx) {- final accounts = UserAccountService().accounts;- final auth = ctx.read<AuthProvider>();- final otherAccounts = accounts.where((a) =>- !(a.serverUrl == auth.serverUrl && a.username == auth.username)- ).toList();- return Padding(- padding: const EdgeInsets.symmetric(horizontal: 16),- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- if (otherAccounts.isNotEmpty) ...[- Text('Switch Account',- style: tt.titleSmall?.copyWith(- color: cs.onSurfaceVariant,- fontWeight: FontWeight.w600,- )),- const SizedBox(height: 8),- ...otherAccounts.map((account) {- final shortUrl = account.serverUrl- .replaceAll(RegExp(r'^https?://'), '')- .replaceAll(RegExp(r'/+$'), '');- return Padding(- padding: const EdgeInsets.only(bottom: 6),- child: Material(- color: cs.surfaceContainerHigh,- borderRadius: BorderRadius.circular(14),- child: InkWell(- borderRadius: BorderRadius.circular(14),- onTap: () => _switchAccount(ctx, account),- onLongPress: () => _removeAccount(ctx, account),- child: Padding(- padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),- child: Row(- children: [- CircleAvatar(- radius: 18,- backgroundColor: cs.primary.withValues(alpha: 0.15),- child: Text(- account.username.isNotEmpty- ? account.username[0].toUpperCase()- : '?',- style: TextStyle(- color: cs.primary,- fontWeight: FontWeight.w600,- ),- ),- ),- const SizedBox(width: 12),- Expanded(- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- Text(account.username,- style: tt.bodyMedium?.copyWith(- fontWeight: FontWeight.w600)),- Text(shortUrl,- style: tt.labelSmall?.copyWith(- color: cs.onSurfaceVariant.withValues(alpha: 0.6)),- maxLines: 1,- overflow: TextOverflow.ellipsis),- ],- ),- ),- Icon(Icons.swap_horiz_rounded,- size: 20, color: cs.primary),- ],- ),- ),- ),- ),- );- }),- const SizedBox(height: 6),- ],- // Add Account button - always visible- Material(- color: cs.surfaceContainerHigh,- borderRadius: BorderRadius.circular(14),- child: InkWell(- borderRadius: BorderRadius.circular(14),- onTap: () => _addAccount(ctx),- child: Padding(- padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),- child: Row(- children: [- CircleAvatar(- radius: 18,- backgroundColor: cs.primary.withValues(alpha: 0.08),- child: Icon(Icons.person_add_rounded,- size: 18, color: cs.primary),- ),- const SizedBox(width: 12),- Text('Add Account',- style: tt.bodyMedium?.copyWith(- fontWeight: FontWeight.w600,- color: cs.primary)),- ],- ),+ // ── Version Info ──+ Center(child: Row(+ mainAxisSize: MainAxisSize.min,+ children: [+ Text(+ 'Absorb v$_appVersion',+ style: tt.bodySmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.4)),+ ),+ const SizedBox(width: 4),+ Icon(+ _isGithubBuild ? Icons.code_rounded : Icons.store_rounded,+ size: 14,+ color: cs.onSurfaceVariant.withValues(alpha: 0.3),+ ),+ if (auth.serverVersion != null)+ Text(+ ' · Server ${auth.serverVersion}',+ style: tt.bodySmall?.copyWith(+ color: cs.onSurfaceVariant.withValues(alpha: 0.4)),+ ),+ ],+ )),++ if (_isGithubBuild) ...[+ const SizedBox(height: 4),+ Center(child: TextButton.icon(+ onPressed: () async {+ final info = await UpdateCheckerService.check(force: true);+ if (!mounted) return;+ if (info == null || !info.hasUpdate) {+ ScaffoldMessenger.of(context).showSnackBar(+ const SnackBar(content: Text('You\'re on the latest version')),+ );+ return;+ }+ showDialog(+ context: context,+ builder: (ctx) => AlertDialog(+ title: const Text('Update available'),+ content: Text('A new version of Absorb is available: ${info.latestVersion}\n\nYou are on ${info.currentVersion}.'),+ actions: [+ TextButton(+ onPressed: () => Navigator.pop(ctx),+ child: const Text('Later'),),- ),+ FilledButton(+ onPressed: () {+ Navigator.pop(ctx);+ launchUrl(Uri.parse(info.downloadUrl), mode: LaunchMode.externalApplication);+ },+ child: const Text('Download'),+ ),+ ],),- const SizedBox(height: 8),- ],- ),- );- }),+ );+ },+ icon: const Icon(Icons.system_update_rounded, size: 16),+ label: const Text('Check for update'),+ )),+ ],- // ── Sign out ──- Padding(- padding: const EdgeInsets.symmetric(horizontal: 16),- child: SizedBox(- width: double.infinity,- child: OutlinedButton.icon(- onPressed: () => _confirmLogout(context),- icon: const Icon(Icons.logout_rounded),- label: const Text('Log out'),- style: OutlinedButton.styleFrom(- foregroundColor: cs.error,- side: BorderSide(color: cs.error.withValues(alpha: 0.5)),- padding: const EdgeInsets.symmetric(vertical: 14),- shape: RoundedRectangleBorder(- borderRadius: BorderRadius.circular(14)),- ),- ),- ),- ),const SizedBox(height: 100),],),@@ -2592,6 +2468,116 @@ class _SettingsScreenState extends State<SettingsScreen> {);}+ void _showAccountSheet(BuildContext context) {+ final cs = Theme.of(context).colorScheme;+ final tt = Theme.of(context).textTheme;+ final auth = context.read<AuthProvider>();+ final accounts = UserAccountService().accounts;+ final otherAccounts = accounts.where((a) =>+ !(a.serverUrl == auth.serverUrl && a.username == auth.username)+ ).toList();++ showModalBottomSheet(+ context: context,+ backgroundColor: Colors.transparent,+ builder: (ctx) => Container(+ decoration: BoxDecoration(+ color: cs.surfaceContainerHigh,+ borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),+ ),+ child: Column(mainAxisSize: MainAxisSize.min, children: [+ Center(child: Container(margin: const EdgeInsets.only(top: 12), width: 36, height: 4,+ decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.12), borderRadius: BorderRadius.circular(2)))),+ const SizedBox(height: 16),+ // Current user+ Padding(+ padding: const EdgeInsets.symmetric(horizontal: 20),+ child: Row(children: [+ CircleAvatar(+ radius: 20,+ backgroundColor: cs.primary.withValues(alpha: 0.15),+ child: Text(+ (auth.username ?? 'U')[0].toUpperCase(),+ style: TextStyle(color: cs.primary, fontWeight: FontWeight.w700, fontSize: 16),+ ),+ ),+ const SizedBox(width: 12),+ Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [+ Text(auth.username ?? 'User', style: tt.bodyLarge?.copyWith(fontWeight: FontWeight.w700)),+ Text(+ auth.serverUrl?.replaceAll(RegExp(r'^https?://'), '').replaceAll(RegExp(r'/+$'), '') ?? '',+ style: tt.labelSmall?.copyWith(color: cs.onSurfaceVariant.withValues(alpha: 0.6)),+ maxLines: 1, overflow: TextOverflow.ellipsis),+ ])),+ Icon(Icons.check_circle_rounded, size: 20, color: cs.primary),+ ]),+ ),+ if (otherAccounts.isNotEmpty) ...[+ const SizedBox(height: 8),+ Divider(height: 1, indent: 20, endIndent: 20, color: cs.onSurface.withValues(alpha: 0.06)),+ const SizedBox(height: 8),+ ...otherAccounts.map((account) {+ final shortUrl = account.serverUrl+ .replaceAll(RegExp(r'^https?://'), '')+ .replaceAll(RegExp(r'/+$'), '');+ return InkWell(+ onTap: () { Navigator.pop(ctx); _switchAccount(context, account); },+ onLongPress: () { Navigator.pop(ctx); _removeAccount(context, account); },+ child: Padding(+ padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),+ child: Row(children: [+ CircleAvatar(+ radius: 20,+ backgroundColor: cs.onSurface.withValues(alpha: 0.06),+ child: Text(+ account.username.isNotEmpty ? account.username[0].toUpperCase() : '?',+ style: TextStyle(color: cs.onSurfaceVariant, fontWeight: FontWeight.w600, fontSize: 16),+ ),+ ),+ const SizedBox(width: 12),+ Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [+ Text(account.username, style: tt.bodyMedium?.copyWith(fontWeight: FontWeight.w600)),+ Text(shortUrl, style: tt.labelSmall?.copyWith(color: cs.onSurfaceVariant.withValues(alpha: 0.6)),+ maxLines: 1, overflow: TextOverflow.ellipsis),+ ])),+ Icon(Icons.swap_horiz_rounded, size: 18, color: cs.onSurface.withValues(alpha: 0.2)),+ ]),+ ),+ );+ }),+ ],+ const SizedBox(height: 8),+ Divider(height: 1, indent: 20, endIndent: 20, color: cs.onSurface.withValues(alpha: 0.06)),+ // Add account+ InkWell(+ onTap: () { Navigator.pop(ctx); _addAccount(context); },+ child: Padding(+ padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),+ child: Row(children: [+ Icon(Icons.person_add_rounded, size: 20, color: cs.primary),+ const SizedBox(width: 14),+ Text('Add Account', style: tt.bodyMedium?.copyWith(fontWeight: FontWeight.w600, color: cs.primary)),+ ]),+ ),+ ),+ // Sign out+ InkWell(+ onTap: () { Navigator.pop(ctx); _confirmLogout(context); },+ child: Padding(+ padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),+ child: Row(children: [+ Icon(Icons.logout_rounded, size: 20, color: cs.error),+ const SizedBox(width: 14),+ Text('Sign Out', style: tt.bodyMedium?.copyWith(fontWeight: FontWeight.w600, color: cs.error)),+ ]),+ ),+ ),+ SizedBox(height: MediaQuery.of(ctx).padding.bottom + 12),+ ]),+ ),+ );+ }+/// Stop any active playback and sync progress to the server before/// switching users, adding an account, or signing out.Future<void> _stopAndSyncPlayback() async {