chore: Follow up crop circle notification avatars

pull/1862/head
krille-chan 2 months ago
parent 200f2c34be
commit e7034184d6
No known key found for this signature in database

@ -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;
}
}

@ -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<void> pushHelper(
PushNotification notification, {
@ -164,11 +164,12 @@ Future<void> _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<void> _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<void> _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,

@ -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<String?> 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());
}
}
Loading…
Cancel
Save