From e1a5dbb84378a043a03aecd8cbbe772c0028f508 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Tue, 22 Jun 2021 13:24:37 +0800 Subject: [PATCH] Support gif --- lib/entity/file_util.dart | 1 + lib/widget/album_viewer.dart | 4 ++ lib/widget/archive_viewer.dart | 1 + lib/widget/home_photos.dart | 1 + lib/widget/image_viewer.dart | 8 +++- lib/widget/photo_list_item.dart | 70 +++++++++++++++++++++------------ 6 files changed, 58 insertions(+), 27 deletions(-) diff --git a/lib/entity/file_util.dart b/lib/entity/file_util.dart index 0512524d..69a892a4 100644 --- a/lib/entity/file_util.dart +++ b/lib/entity/file_util.dart @@ -30,6 +30,7 @@ const _supportedFormatMimes = [ "image/png", "image/webp", "image/heic", + "image/gif", // video player currently doesn't work on web if (!platform_k.isWeb) "video/mp4", ]; diff --git a/lib/widget/album_viewer.dart b/lib/widget/album_viewer.dart index 5334b003..8cb402eb 100644 --- a/lib/widget/album_viewer.dart +++ b/lib/widget/album_viewer.dart @@ -300,6 +300,7 @@ class _AlbumViewerState extends State width: _thumbSize, height: _thumbSize); if (file_util.isSupportedImageFormat(f)) { yield _ImageListItem( + file: f, account: widget.account, previewUrl: previewUrl, onTap: () => _onItemTap(i), @@ -368,6 +369,7 @@ class _AlbumViewerState extends State class _ImageListItem extends SelectableItemStreamListItem { _ImageListItem({ + @required this.file, @required this.account, @required this.previewUrl, VoidCallback onTap, @@ -378,9 +380,11 @@ class _ImageListItem extends SelectableItemStreamListItem { return PhotoListImage( account: account, previewUrl: previewUrl, + isGif: file.contentType == "image/gif", ); } + final File file; final Account account; final String previewUrl; } diff --git a/lib/widget/archive_viewer.dart b/lib/widget/archive_viewer.dart index f01d5b7a..fb5f6e30 100644 --- a/lib/widget/archive_viewer.dart +++ b/lib/widget/archive_viewer.dart @@ -341,6 +341,7 @@ class _ImageListItem extends _FileListItem { return PhotoListImage( account: account, previewUrl: previewUrl, + isGif: file.contentType == "image/gif", ); } diff --git a/lib/widget/home_photos.dart b/lib/widget/home_photos.dart index cbd5adea..2a783328 100644 --- a/lib/widget/home_photos.dart +++ b/lib/widget/home_photos.dart @@ -639,6 +639,7 @@ class _ImageListItem extends _FileListItem { return PhotoListImage( account: account, previewUrl: previewUrl, + isGif: file.contentType == "image/gif", ); } diff --git a/lib/widget/image_viewer.dart b/lib/widget/image_viewer.dart index d7b76489..42fcd75c 100644 --- a/lib/widget/image_viewer.dart +++ b/lib/widget/image_viewer.dart @@ -210,10 +210,16 @@ class _ImageViewerState extends State static final _log = Logger("widget.image_viewer._ImageViewerState"); } -String _getImageUrl(Account account, File file) => api_util.getFilePreviewUrl( +String _getImageUrl(Account account, File file) { + if (file.contentType == "image/gif") { + return api_util.getFileUrl(account, file); + } else { + return api_util.getFilePreviewUrl( account, file, width: 1080, height: 1080, a: true, ); + } +} diff --git a/lib/widget/photo_list_item.dart b/lib/widget/photo_list_item.dart index 0ab939c7..5d98f978 100644 --- a/lib/widget/photo_list_item.dart +++ b/lib/widget/photo_list_item.dart @@ -10,6 +10,7 @@ class PhotoListImage extends StatelessWidget { Key key, @required this.account, @required this.previewUrl, + this.isGif = false, }) : super(key: key); @override @@ -17,39 +18,56 @@ class PhotoListImage extends StatelessWidget { return FittedBox( clipBehavior: Clip.hardEdge, fit: BoxFit.cover, - child: Container( - // arbitrary size here - constraints: BoxConstraints.tight(const Size(128, 128)), - color: AppTheme.getListItemBackgroundColor(context), - child: CachedNetworkImage( - imageUrl: previewUrl, - httpHeaders: { - "Authorization": Api.getAuthorizationHeaderValue(account), - }, - fadeInDuration: const Duration(), - filterQuality: FilterQuality.high, - errorWidget: (context, url, error) { - // won't work on web because the image is downloaded by the cache - // manager instead - // where's the preview??? - return Container( - child: Center( - child: Icon( - Icons.image_not_supported, - size: 64, - color: Colors.white.withOpacity(.8), - ), + child: Stack( + children: [ + Container( + // arbitrary size here + constraints: BoxConstraints.tight(const Size(128, 128)), + color: AppTheme.getListItemBackgroundColor(context), + child: CachedNetworkImage( + imageUrl: previewUrl, + httpHeaders: { + "Authorization": Api.getAuthorizationHeaderValue(account), + }, + fadeInDuration: const Duration(), + filterQuality: FilterQuality.high, + errorWidget: (context, url, error) { + // won't work on web because the image is downloaded by the cache + // manager instead + // where's the preview??? + return Container( + child: Center( + child: Icon( + Icons.image_not_supported, + size: 64, + color: Colors.white.withOpacity(.8), + ), + ), + ); + }, + imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet, + ), + ), + if (isGif) + Container( + // arbitrary size here + constraints: BoxConstraints.tight(const Size(128, 128)), + alignment: AlignmentDirectional.topEnd, + padding: const EdgeInsets.symmetric(horizontal: 2), + child: const Icon( + Icons.gif, + size: 36, + color: Colors.white, ), - ); - }, - imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet, - ), + ), + ], ), ); } final Account account; final String previewUrl; + final bool isGif; } class PhotoListVideo extends StatelessWidget {