A cross platform Audiobookshelf client for Android and iOS
Dart
Improve playback history tracking and UI
Nathan Poulson445aa7b0e83f
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 9fbcfd9c..ab7c59f4 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -247,9 +247,13 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {}@override- Future<void> seek(Duration position) {+ Future<void> seek(Duration position) async {debugPrint('[Handler] seek(${position.inSeconds}s)');- return _player.seek(position);+ if (_service != null) {+ await _service!.seekTo(position);+ } else {+ await _player.seek(position);+ }}@override@@ -548,12 +552,12 @@ class AudioPlayerService extends ChangeNotifier {final _history = PlaybackHistoryService();/// Log a playback event to history.- void _logEvent(PlaybackEventType type, {String? detail}) {+ void _logEvent(PlaybackEventType type, {String? detail, double? overridePosition}) {if (_currentItemId == null) return;_history.log(itemId: _currentItemId!,type: type,- positionSeconds: position.inMilliseconds / 1000.0,+ positionSeconds: overridePosition ?? position.inMilliseconds / 1000.0,detail: detail,);}@@ -1512,9 +1516,11 @@ class AudioPlayerService extends ChangeNotifier {}Future<void> seekTo(Duration pos) async {+ final from = position;await _seekAbsolute(pos.inMilliseconds / 1000.0);_logEvent(PlaybackEventType.seek,- detail: 'to ${_formatPos(pos)}');+ detail: '${_formatPos(from)} → ${_formatPos(pos)}',+ overridePosition: from.inMilliseconds / 1000.0);notifyListeners();}diff --git a/lib/services/playback_history_service.dart b/lib/services/playback_history_service.dartindex b6f9cd9c..4ef33047 100644--- a/lib/services/playback_history_service.dart+++ b/lib/services/playback_history_service.dart@@ -51,22 +51,29 @@ class PlaybackEvent {String get label {switch (type) {case PlaybackEventType.play:+ if (detail != null && detail!.isNotEmpty) return detail!;return 'Resumed playback';case PlaybackEventType.pause:+ if (detail != null && detail!.isNotEmpty) return detail!;return 'Paused';case PlaybackEventType.seek:+ if (detail != null && detail!.isNotEmpty) return 'Seeked $detail';return 'Seeked';case PlaybackEventType.syncLocal:return 'Saved locally';case PlaybackEventType.syncServer:return 'Synced to server';case PlaybackEventType.autoRewind:+ if (detail != null && detail!.isNotEmpty) return 'Auto-rewound $detail';return 'Auto-rewound';case PlaybackEventType.skipForward:+ if (detail != null && detail!.isNotEmpty) return 'Skipped forward (${detail!})';return 'Skipped forward';case PlaybackEventType.skipBackward:+ if (detail != null && detail!.isNotEmpty) return 'Skipped back (${detail!})';return 'Skipped back';case PlaybackEventType.speedChange:+ if (detail != null && detail!.isNotEmpty) return 'Speed set to ${detail!}';return 'Speed changed';}}diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dartindex 0beb6553..a25caa5e 100644--- a/lib/widgets/absorbing_card.dart+++ b/lib/widgets/absorbing_card.dart@@ -878,33 +878,52 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli),]),),- const SizedBox(height: 8),+ if (_isActive)+ Padding(+ padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),+ child: Text('Tap an event to jump to that position',+ style: tt.bodySmall?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant.withValues(alpha: 0.6), fontStyle: FontStyle.italic)),+ )+ else+ const SizedBox(height: 8),Expanded(child: FutureBuilder<List<PlaybackEvent>>(future: PlaybackHistoryService().getHistory(_itemId),builder: (ctx, snap) {if (!snap.hasData) return const Center(child: CircularProgressIndicator(strokeWidth: 2));final events = snap.data!;if (events.isEmpty) return Center(child: Text('No history yet', style: tt.bodyMedium?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant)));- return ListView.builder(- controller: sc, itemCount: events.length,- itemBuilder: (_, i) {- final e = events[i]; // already newest-first from getHistory- final posLabel = _fmtTime(e.positionSeconds);- final timeAgo = _timeAgo(e.timestamp);- return ListTile(- dense: true,- leading: Icon(_historyIcon(e.type), size: 18, color: accent.withValues(alpha: 0.7)),- title: Text(e.label, style: tt.bodySmall?.copyWith(color: Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.7))),- subtitle: Text('at $posLabel', style: tt.labelSmall?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant)),- trailing: Text(timeAgo, style: tt.labelSmall?.copyWith(color: Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.3))),- onTap: _isActive ? () {- widget.player.seekTo(Duration(seconds: e.positionSeconds.round()));- Navigator.pop(ctx);- ScaffoldMessenger.of(context).showSnackBar(SnackBar(duration: const Duration(seconds: 3), content: Text('Jumped to $posLabel')));- } : null,- );- },- );++ // Build list items with session date headers+ final items = <Widget>[];+ String? lastDateLabel;+ for (int i = 0; i < events.length; i++) {+ final e = events[i];+ final dateLabel = _dateLabel(e.timestamp);+ if (dateLabel != lastDateLabel) {+ lastDateLabel = dateLabel;+ items.add(Padding(+ padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),+ child: Text(dateLabel, style: tt.labelSmall?.copyWith(+ color: accent.withValues(alpha: 0.6), fontWeight: FontWeight.w600, letterSpacing: 0.5)),+ ));+ }+ final posLabel = _fmtTime(e.positionSeconds);+ final timeStr = _timeOfDay(e.timestamp);+ items.add(ListTile(+ dense: true, visualDensity: const VisualDensity(vertical: -2),+ leading: Icon(_historyIcon(e.type), size: 18, color: accent.withValues(alpha: 0.7)),+ title: Text(e.label, style: tt.bodySmall?.copyWith(color: Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.7))),+ subtitle: Text('at $posLabel', style: tt.labelSmall?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant)),+ trailing: Text(timeStr, style: tt.labelSmall?.copyWith(color: Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.3))),+ onTap: _isActive ? () {+ widget.player.seekTo(Duration(seconds: e.positionSeconds.round()));+ Navigator.pop(ctx);+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(duration: const Duration(seconds: 3), content: Text('Jumped to $posLabel')));+ } : null,+ ));+ }++ return ListView(controller: sc, children: items);},)),]),@@ -1051,12 +1070,24 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli}}- String _timeAgo(DateTime dt) {- final diff = DateTime.now().difference(dt);- if (diff.inSeconds < 60) return 'just now';- if (diff.inMinutes < 60) return '${diff.inMinutes}m ago';- if (diff.inHours < 24) return '${diff.inHours}h ago';- return '${diff.inDays}d ago';+ String _dateLabel(DateTime dt) {+ final now = DateTime.now();+ final today = DateTime(now.year, now.month, now.day);+ final date = DateTime(dt.year, dt.month, dt.day);+ if (date == today) return 'Today';+ if (date == today.subtract(const Duration(days: 1))) return 'Yesterday';+ if (now.difference(dt).inDays < 7) {+ const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];+ return days[dt.weekday - 1];+ }+ return '${dt.month}/${dt.day}/${dt.year}';+ }++ String _timeOfDay(DateTime dt) {+ final h = dt.hour % 12 == 0 ? 12 : dt.hour % 12;+ final m = dt.minute.toString().padLeft(2, '0');+ final ampm = dt.hour < 12 ? 'AM' : 'PM';+ return '$h:$m $ampm';}String _fmtDur(double s) {diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dartindex 971c3429..6484cfac 100644--- a/lib/widgets/expanded_card.dart+++ b/lib/widgets/expanded_card.dart@@ -999,7 +999,14 @@ class _ExpandedCardState extends State<ExpandedCard> {),]),),- const SizedBox(height: 8),+ if (_isActive)+ Padding(+ padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),+ child: Text('Tap an event to jump to that position',+ style: tt.bodySmall?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant.withValues(alpha: 0.6), fontStyle: FontStyle.italic)),+ )+ else+ const SizedBox(height: 8),Expanded(child: FutureBuilder<List<PlaybackEvent>>(future: PlaybackHistoryService().getHistory(_itemId),builder: (ctx, snap) {