Browse Source

Merge pull request #328 from provokateurin/feature/large-icons

neon: Add large icons for push notification
pull/330/head
Kate 2 years ago committed by GitHub
parent
commit
b7a5f5e1a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      packages/app/pubspec.lock
  2. 3
      packages/neon/neon/lib/neon.dart
  3. 31
      packages/neon/neon/lib/src/utils/push_utils.dart
  4. 1
      packages/neon/neon/pubspec.yaml

8
packages/app/pubspec.lock

@ -33,6 +33,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.11.0"
bitmap:
dependency: transitive
description:
name: bitmap
sha256: d4ec0147d64eff00efbbeead5c04d517ea4fbb528022adaa3551e3586e80f4d4
url: "https://pub.dev"
source: hosted
version: "0.1.3"
boolean_selector:
dependency: transitive
description:

3
packages/neon/neon/lib/neon.dart

@ -3,7 +3,10 @@ library neon;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:ui';
import 'package:bitmap/bitmap.dart';
import 'package:crypto/crypto.dart';
import 'package:file_picker/file_picker.dart';
import 'package:filesize/filesize.dart';

31
packages/neon/neon/lib/src/utils/push_utils.dart

@ -76,15 +76,43 @@ class PushUtils {
final cache = Cache(platform);
await cache.init();
NextcloudNotificationsNotification? notification;
var accounts = <Account>[];
Account? account;
NextcloudNotificationsNotification? notification;
AndroidBitmap<Object>? largeIconBitmap;
try {
accounts = loadAccounts(AppStorage('accounts', sharedPreferences));
account = accounts.find(instance);
if (account != null) {
notification =
(await account.client.notifications.getNotification(id: pushNotification.subject.nid!)).ocs.data;
if (notification.icon?.endsWith('.svg') ?? false) {
// Only SVG icons are supported right now (should be most of them)
final cacheManager = DefaultCacheManager();
final file = await cacheManager.getSingleFile(notification.icon!);
final pictureInfo = await vg.loadPicture(SvgFileLoader(file), null);
const largeIconSize = 256;
final scale = min(largeIconSize / pictureInfo.size.width, largeIconSize / pictureInfo.size.height);
final scaledWidth = (pictureInfo.size.width * scale).toInt();
final scaledHeight = (pictureInfo.size.height * scale).toInt();
final recorder = PictureRecorder();
Canvas(recorder)
..scale(scale)
..drawPicture(pictureInfo.picture)
..drawColor(themePrimaryColor, BlendMode.srcIn);
pictureInfo.picture.dispose();
final image = recorder.endRecording().toImageSync(scaledWidth, scaledHeight);
final bytes = await image.toByteData(format: ImageByteFormat.png);
final bitmap = await Bitmap.fromProvider(MemoryImage(bytes!.buffer.asUint8List()));
largeIconBitmap = ByteArrayAndroidBitmap(bitmap.buildHeaded());
}
}
} catch (e, s) {
debugPrint(e.toString());
@ -113,6 +141,7 @@ class PushUtils {
subText: accounts.length > 1 && account != null ? account.client.humanReadableID : null,
groupKey: 'app_$appID',
icon: '@mipmap/ic_launcher',
largeIcon: largeIconBitmap,
when: when?.millisecondsSinceEpoch,
color: themePrimaryColor,
category: pushNotification.type == 'voip' ? AndroidNotificationCategory.call : null,

1
packages/neon/neon/pubspec.yaml

@ -7,6 +7,7 @@ environment:
flutter: '>=3.10.0'
dependencies:
bitmap: ^0.1.3
collection: ^1.17.1
crypto: ^3.0.3
file_picker: ^5.3.0

Loading…
Cancel
Save