chore: Follow up crop circle notification avatars
This commit is contained in:
parent
200f2c34be
commit
e7034184d6
3 changed files with 18 additions and 46 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:image/image.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
|
||||||
extension ClientDownloadContentExtension on Client {
|
extension ClientDownloadContentExtension on Client {
|
||||||
|
|
@ -10,6 +11,7 @@ extension ClientDownloadContentExtension on Client {
|
||||||
bool isThumbnail = false,
|
bool isThumbnail = false,
|
||||||
bool? animated,
|
bool? animated,
|
||||||
ThumbnailMethod? thumbnailMethod,
|
ThumbnailMethod? thumbnailMethod,
|
||||||
|
bool rounded = false,
|
||||||
}) async {
|
}) async {
|
||||||
// To stay compatible with previous storeKeys:
|
// To stay compatible with previous storeKeys:
|
||||||
final cacheKey = isThumbnail
|
final cacheKey = isThumbnail
|
||||||
|
|
@ -44,10 +46,17 @@ extension ClientDownloadContentExtension on Client {
|
||||||
if (response.statusCode != 200) {
|
if (response.statusCode != 200) {
|
||||||
throw Exception();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return remoteData;
|
await database?.storeFile(cacheKey, imageData, 0);
|
||||||
|
|
||||||
|
return imageData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
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/client_manager.dart';
|
||||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||||
import 'package:fluffychat/utils/platform_infos.dart';
|
import 'package:fluffychat/utils/platform_infos.dart';
|
||||||
import 'package:fluffychat/utils/shortcut_memory_icon.dart';
|
|
||||||
|
|
||||||
Future<void> pushHelper(
|
Future<void> pushHelper(
|
||||||
PushNotification notification, {
|
PushNotification notification, {
|
||||||
|
|
@ -164,11 +164,12 @@ Future<void> _tryPushHelper(
|
||||||
: await client
|
: await client
|
||||||
.downloadMxcCached(
|
.downloadMxcCached(
|
||||||
avatar,
|
avatar,
|
||||||
thumbnailMethod: ThumbnailMethod.scale,
|
thumbnailMethod: ThumbnailMethod.crop,
|
||||||
width: 256,
|
width: 256,
|
||||||
height: 256,
|
height: 256,
|
||||||
animated: false,
|
animated: false,
|
||||||
isThumbnail: true,
|
isThumbnail: true,
|
||||||
|
rounded: true,
|
||||||
)
|
)
|
||||||
.timeout(const Duration(seconds: 3));
|
.timeout(const Duration(seconds: 3));
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
|
|
@ -182,11 +183,12 @@ Future<void> _tryPushHelper(
|
||||||
: await client
|
: await client
|
||||||
.downloadMxcCached(
|
.downloadMxcCached(
|
||||||
senderAvatar,
|
senderAvatar,
|
||||||
thumbnailMethod: ThumbnailMethod.scale,
|
thumbnailMethod: ThumbnailMethod.crop,
|
||||||
width: 256,
|
width: 256,
|
||||||
height: 256,
|
height: 256,
|
||||||
animated: false,
|
animated: false,
|
||||||
isThumbnail: true,
|
isThumbnail: true,
|
||||||
|
rounded: true,
|
||||||
)
|
)
|
||||||
.timeout(const Duration(seconds: 3));
|
.timeout(const Duration(seconds: 3));
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
|
|
@ -313,10 +315,7 @@ Future<void> _setShortcut(
|
||||||
action: AppConfig.inviteLinkPrefix + event.room.id,
|
action: AppConfig.inviteLinkPrefix + event.room.id,
|
||||||
shortLabel: title,
|
shortLabel: title,
|
||||||
conversationShortcut: true,
|
conversationShortcut: true,
|
||||||
icon: await avatarFile?.toShortcutMemoryIcon(
|
icon: avatarFile == null ? null : base64Encode(avatarFile),
|
||||||
event.room.id,
|
|
||||||
event.room.client.database,
|
|
||||||
),
|
|
||||||
shortcutIconAsset: avatarFile == null
|
shortcutIconAsset: avatarFile == null
|
||||||
? ShortcutIconAsset.androidAsset
|
? ShortcutIconAsset.androidAsset
|
||||||
: ShortcutIconAsset.memoryAsset,
|
: 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…
Add table
Add a link
Reference in a new issue