diff --git a/lib/utils/client_download_content_extension.dart b/lib/utils/client_download_content_extension.dart index 9fff1eccd..f93d522f0 100644 --- a/lib/utils/client_download_content_extension.dart +++ b/lib/utils/client_download_content_extension.dart @@ -1,5 +1,6 @@ import 'dart:typed_data'; +import 'package:image/image.dart'; import 'package:matrix/matrix.dart'; extension ClientDownloadContentExtension on Client { @@ -10,6 +11,7 @@ extension ClientDownloadContentExtension on Client { bool isThumbnail = false, bool? animated, ThumbnailMethod? thumbnailMethod, + bool rounded = false, }) async { // To stay compatible with previous storeKeys: final cacheKey = isThumbnail @@ -44,10 +46,17 @@ extension ClientDownloadContentExtension on Client { if (response.statusCode != 200) { throw Exception(); } - final remoteData = response.bodyBytes; + var imageData = response.bodyBytes; - await database?.storeFile(cacheKey, remoteData, 0); + if (rounded) { + final image = decodeImage(imageData); + if (image != null) { + imageData = copyCropCircle(image).toUint8List(); + } + } + + await database?.storeFile(cacheKey, imageData, 0); - return remoteData; + return imageData; } } diff --git a/lib/utils/push_helper.dart b/lib/utils/push_helper.dart index 41684a3cc..3035b50fa 100644 --- a/lib/utils/push_helper.dart +++ b/lib/utils/push_helper.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:ui'; import 'package:flutter/foundation.dart'; @@ -15,7 +16,6 @@ import 'package:fluffychat/utils/client_download_content_extension.dart'; import 'package:fluffychat/utils/client_manager.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart'; import 'package:fluffychat/utils/platform_infos.dart'; -import 'package:fluffychat/utils/shortcut_memory_icon.dart'; Future pushHelper( PushNotification notification, { @@ -164,11 +164,12 @@ Future _tryPushHelper( : await client .downloadMxcCached( avatar, - thumbnailMethod: ThumbnailMethod.scale, + thumbnailMethod: ThumbnailMethod.crop, width: 256, height: 256, animated: false, isThumbnail: true, + rounded: true, ) .timeout(const Duration(seconds: 3)); } catch (e, s) { @@ -182,11 +183,12 @@ Future _tryPushHelper( : await client .downloadMxcCached( senderAvatar, - thumbnailMethod: ThumbnailMethod.scale, + thumbnailMethod: ThumbnailMethod.crop, width: 256, height: 256, animated: false, isThumbnail: true, + rounded: true, ) .timeout(const Duration(seconds: 3)); } catch (e, s) { @@ -313,10 +315,7 @@ Future _setShortcut( action: AppConfig.inviteLinkPrefix + event.room.id, shortLabel: title, conversationShortcut: true, - icon: await avatarFile?.toShortcutMemoryIcon( - event.room.id, - event.room.client.database, - ), + icon: avatarFile == null ? null : base64Encode(avatarFile), shortcutIconAsset: avatarFile == null ? ShortcutIconAsset.androidAsset : ShortcutIconAsset.memoryAsset, diff --git a/lib/utils/shortcut_memory_icon.dart b/lib/utils/shortcut_memory_icon.dart deleted file mode 100644 index c1557e44a..000000000 --- a/lib/utils/shortcut_memory_icon.dart +++ /dev/null @@ -1,36 +0,0 @@ -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:image/image.dart'; -import 'package:matrix/matrix.dart'; - -extension ShortcutMemoryIcon on Uint8List { - Future toShortcutMemoryIcon( - String roomId, - DatabaseApi? database, - ) async { - final cacheKey = Uri.parse('im.fluffychat://shortcuts/$roomId'); - final cachedFile = await database?.getFile(cacheKey); - if (cachedFile != null) return base64Encode(cachedFile); - - final image = decodeImage(this); - if (image == null) return null; - - final size = image.width < image.height ? image.width : image.height; - final x = (image.width - size) ~/ 2; - final y = (image.height - size) ~/ 2; - - final croppedImage = copyCrop( - image, - x: x, - y: y, - width: size, - height: size, - ); - - final bytes = croppedImage.toUint8List(); - await database?.storeFile(cacheKey, bytes, 0); - - return base64Encode(croppedImage.toUint8List()); - } -}