diff --git a/lib/pages/media_viewer_page.dart b/lib/pages/media_viewer_page.dart index 82ee077..f728d90 100644 --- a/lib/pages/media_viewer_page.dart +++ b/lib/pages/media_viewer_page.dart @@ -83,7 +83,9 @@ class _MediaViewerPageState extends State { return; } setState(() { - _media = media.isEmpty ? [widget.message] : media; + // Never fall back to the first page when the tapped attachment is not in + // the list: that opens a different attachment than the one clicked. + _media = index < 0 ? [widget.message] : media; _index = index < 0 ? 0 : index; _controller = PageController(initialPage: _index); }); diff --git a/lib/utils/message_notifier.dart b/lib/utils/message_notifier.dart index a589018..709a918 100644 --- a/lib/utils/message_notifier.dart +++ b/lib/utils/message_notifier.dart @@ -27,22 +27,23 @@ class MessageNotifier extends ChangeNotifier { /// items are expanded into one entry per attachment. static List viewableMedia(List messages) { final media = []; + // A local file is what makes an attachment viewable, not its status: a + // stale failed/expired flag (an old redelivery) used to hide attachments + // that the bubbles and the viewer can still open. + bool viewable(PrivateMessage message) => + message.localPath != null && + message.mediaType != null && + message.mediaType != MediaType.voice; for (final message in messages) { if (message.isAlbum) { for (final item in albumMedia(message)) { - if (item.localPath != null && - item.mediaType != null && - item.mediaType != MediaType.voice && - item.downloadStatus == MediaDownloadStatus.ready) { + if (viewable(item)) { media.add(item); } } continue; } - if (message.localPath != null && - message.mediaType != null && - message.mediaType != MediaType.voice && - message.downloadStatus == MediaDownloadStatus.ready) { + if (viewable(message)) { media.add(message); } } diff --git a/test/chat_info_media_test.dart b/test/chat_info_media_test.dart index e888c2c..f1b79f3 100644 --- a/test/chat_info_media_test.dart +++ b/test/chat_info_media_test.dart @@ -1,6 +1,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:whisper/models/private_message.dart'; import 'package:whisper/utils/message_notifier.dart'; +import 'package:whisper_websocket_client_dart/models/media_type.dart'; PrivateMessage message(String? type, String? path, DateTime sentAt, {int? downloadStatus}) { @@ -10,8 +11,22 @@ PrivateMessage message(String? type, String? path, DateTime sentAt, downloadStatus: downloadStatus ?? MediaDownloadStatus.ready); } +PrivateMessage album(DateTime sentAt, String path, MediaType type, + {int status = MediaDownloadStatus.ready}) { + final result = PrivateMessage('1', '', sentAt, sentAt, false); + result.setAlbum([ + AlbumItem( + mediaId: 'a', + type: type.name, + size: 1, + localPath: path, + status: status), + ]); + return result; +} + void main() { - test('shared media keeps only ready, viewable media with a local file', () { + test('shared media needs a local file, whatever the status says', () { final now = DateTime(2026, 11, 12, 12); final media = MessageNotifier.viewableMedia([ message('image', '/tmp/a.jpg', now), @@ -21,12 +36,29 @@ void main() { // Not downloaded yet message('image', null, now.subtract(const Duration(minutes: 3))), // Still downloading - message('image', '/tmp/d.jpg', now.subtract(const Duration(minutes: 4)), + message('image', null, now.subtract(const Duration(minutes: 4)), downloadStatus: MediaDownloadStatus.pending), + // A stale failure flag must not hide a file that is right there + message('image', '/tmp/e.jpg', now.subtract(const Duration(minutes: 5)), + downloadStatus: MediaDownloadStatus.expired), // Plain text - message(null, null, now.subtract(const Duration(minutes: 5))), + message(null, null, now.subtract(const Duration(minutes: 6))), + ]); + + expect(media.map((m) => m.localPath), + ['/tmp/a.jpg', '/tmp/b.mp4', '/tmp/e.jpg']); + }); + + test('an album contributes each of its attachments', () { + final now = DateTime(2026, 11, 12, 12); + final media = MessageNotifier.viewableMedia([ + album(now, '/tmp/a.jpg', MediaType.image), + album(now.subtract(const Duration(minutes: 1)), '/tmp/b.mp4', + MediaType.video, + status: MediaDownloadStatus.failed), ]); expect(media.map((m) => m.localPath), ['/tmp/a.jpg', '/tmp/b.mp4']); + expect(media.first.mediaType, MediaType.image); }); }