Browse Source

neon: Add large icons for push notification

pull/328/head
jld3103 2 years ago
parent
commit
f5ceae87e4
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  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" url: "https://pub.dev"
source: hosted source: hosted
version: "2.11.0" version: "2.11.0"
bitmap:
dependency: transitive
description:
name: bitmap
sha256: d4ec0147d64eff00efbbeead5c04d517ea4fbb528022adaa3551e3586e80f4d4
url: "https://pub.dev"
source: hosted
version: "0.1.3"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:

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

@ -3,7 +3,10 @@ library neon;
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:math';
import 'dart:ui';
import 'package:bitmap/bitmap.dart';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:filesize/filesize.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); final cache = Cache(platform);
await cache.init(); await cache.init();
NextcloudNotificationsNotification? notification;
var accounts = <Account>[]; var accounts = <Account>[];
Account? account; Account? account;
NextcloudNotificationsNotification? notification;
AndroidBitmap<Object>? largeIconBitmap;
try { try {
accounts = loadAccounts(AppStorage('accounts', sharedPreferences)); accounts = loadAccounts(AppStorage('accounts', sharedPreferences));
account = accounts.find(instance); account = accounts.find(instance);
if (account != null) { if (account != null) {
notification = notification =
(await account.client.notifications.getNotification(id: pushNotification.subject.nid!)).ocs.data; (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) { } catch (e, s) {
debugPrint(e.toString()); debugPrint(e.toString());
@ -113,6 +141,7 @@ class PushUtils {
subText: accounts.length > 1 && account != null ? account.client.humanReadableID : null, subText: accounts.length > 1 && account != null ? account.client.humanReadableID : null,
groupKey: 'app_$appID', groupKey: 'app_$appID',
icon: '@mipmap/ic_launcher', icon: '@mipmap/ic_launcher',
largeIcon: largeIconBitmap,
when: when?.millisecondsSinceEpoch, when: when?.millisecondsSinceEpoch,
color: themePrimaryColor, color: themePrimaryColor,
category: pushNotification.type == 'voip' ? AndroidNotificationCategory.call : null, category: pushNotification.type == 'voip' ? AndroidNotificationCategory.call : null,

1
packages/neon/neon/pubspec.yaml

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

Loading…
Cancel
Save