refactor: Make notification avatars rounded
This commit is contained in:
parent
f798421dd2
commit
9643242cc8
2 changed files with 49 additions and 6 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
|
import 'dart:math' show min;
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'package:image/image.dart';
|
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
|
||||||
extension ClientDownloadContentExtension on Client {
|
extension ClientDownloadContentExtension on Client {
|
||||||
|
|
@ -49,10 +50,10 @@ extension ClientDownloadContentExtension on Client {
|
||||||
var imageData = response.bodyBytes;
|
var imageData = response.bodyBytes;
|
||||||
|
|
||||||
if (rounded) {
|
if (rounded) {
|
||||||
final image = decodeImage(imageData);
|
imageData = await _convertToCircularImage(
|
||||||
if (image != null) {
|
imageData,
|
||||||
imageData = encodePng(copyCropCircle(image));
|
min(width ?? 64, height ?? 64).round(),
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await database.storeFile(cacheKey, imageData, 0);
|
await database.storeFile(cacheKey, imageData, 0);
|
||||||
|
|
@ -60,3 +61,43 @@ extension ClientDownloadContentExtension on Client {
|
||||||
return imageData;
|
return imageData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Uint8List> _convertToCircularImage(
|
||||||
|
Uint8List imageBytes,
|
||||||
|
int size,
|
||||||
|
) async {
|
||||||
|
final codec = await instantiateImageCodec(imageBytes);
|
||||||
|
final frame = await codec.getNextFrame();
|
||||||
|
final originalImage = frame.image;
|
||||||
|
|
||||||
|
final recorder = PictureRecorder();
|
||||||
|
final canvas = Canvas(recorder);
|
||||||
|
|
||||||
|
final paint = Paint();
|
||||||
|
final rect = Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble());
|
||||||
|
|
||||||
|
final clipPath = Path()
|
||||||
|
..addOval(
|
||||||
|
Rect.fromCircle(center: Offset(size / 2, size / 2), radius: size / 2),
|
||||||
|
);
|
||||||
|
|
||||||
|
canvas.clipPath(clipPath);
|
||||||
|
|
||||||
|
canvas.drawImageRect(
|
||||||
|
originalImage,
|
||||||
|
Rect.fromLTWH(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
originalImage.width.toDouble(),
|
||||||
|
originalImage.height.toDouble(),
|
||||||
|
),
|
||||||
|
rect,
|
||||||
|
paint,
|
||||||
|
);
|
||||||
|
|
||||||
|
final picture = recorder.endRecording();
|
||||||
|
final circularImage = await picture.toImage(size, size);
|
||||||
|
|
||||||
|
final byteData = await circularImage.toByteData(format: ImageByteFormat.png);
|
||||||
|
return byteData!.buffer.asUint8List();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ extension LocalNotificationsExtension on MatrixState {
|
||||||
Uri? thumbnailUri;
|
Uri? thumbnailUri;
|
||||||
|
|
||||||
if (avatarUrl != null) {
|
if (avatarUrl != null) {
|
||||||
const size = 64;
|
const size = 128;
|
||||||
const thumbnailMethod = ThumbnailMethod.crop;
|
const thumbnailMethod = ThumbnailMethod.crop;
|
||||||
// Pre-cache so that we can later just set the thumbnail uri as icon:
|
// Pre-cache so that we can later just set the thumbnail uri as icon:
|
||||||
await client.downloadMxcCached(
|
await client.downloadMxcCached(
|
||||||
|
|
@ -59,6 +59,7 @@ extension LocalNotificationsExtension on MatrixState {
|
||||||
height: size,
|
height: size,
|
||||||
thumbnailMethod: thumbnailMethod,
|
thumbnailMethod: thumbnailMethod,
|
||||||
isThumbnail: true,
|
isThumbnail: true,
|
||||||
|
rounded: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
thumbnailUri =
|
thumbnailUri =
|
||||||
|
|
@ -92,6 +93,7 @@ extension LocalNotificationsExtension on MatrixState {
|
||||||
height: size,
|
height: size,
|
||||||
thumbnailMethod: thumbnailMethod,
|
thumbnailMethod: thumbnailMethod,
|
||||||
isThumbnail: true,
|
isThumbnail: true,
|
||||||
|
rounded: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
final image = decodeImage(data);
|
final image = decodeImage(data);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue