From 86fe9463e5bb9f9042090a7a9c110c14eb6e5b34 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Thu, 3 Aug 2023 20:59:25 +0200 Subject: [PATCH] fix(neon): Fix SVG code compilation for web Signed-off-by: jld3103 --- .../neon/neon/lib/src/utils/push_utils.dart | 5 ++-- .../src/utils/universal_svg_file_loader.dart | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 packages/neon/neon/lib/src/utils/universal_svg_file_loader.dart diff --git a/packages/neon/neon/lib/src/utils/push_utils.dart b/packages/neon/neon/lib/src/utils/push_utils.dart index 13a8a808..2f71828f 100644 --- a/packages/neon/neon/lib/src/utils/push_utils.dart +++ b/packages/neon/neon/lib/src/utils/push_utils.dart @@ -7,7 +7,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; -import 'package:flutter_svg/flutter_svg.dart' show SvgFileLoader, vg; +import 'package:flutter_svg/flutter_svg.dart' show vg; import 'package:image/image.dart' as img; import 'package:meta/meta.dart'; import 'package:neon/src/blocs/accounts.dart'; @@ -17,6 +17,7 @@ import 'package:neon/src/settings/models/storage.dart'; import 'package:neon/src/theme/colors.dart'; import 'package:neon/src/utils/global.dart'; import 'package:neon/src/utils/localizations.dart'; +import 'package:neon/src/utils/universal_svg_file_loader.dart'; import 'package:nextcloud/notifications.dart' as notifications; @internal @@ -106,7 +107,7 @@ class PushUtils { final cacheManager = DefaultCacheManager(); final file = await cacheManager.getSingleFile(notification.icon!); - final pictureInfo = await vg.loadPicture(SvgFileLoader(file), null); + final pictureInfo = await vg.loadPicture(UniversalSvgFileLoader(file), null); const largeIconSize = 256; final scale = largeIconSize / pictureInfo.size.longestSide; diff --git a/packages/neon/neon/lib/src/utils/universal_svg_file_loader.dart b/packages/neon/neon/lib/src/utils/universal_svg_file_loader.dart new file mode 100644 index 00000000..66796fd9 --- /dev/null +++ b/packages/neon/neon/lib/src/utils/universal_svg_file_loader.dart @@ -0,0 +1,30 @@ +import 'dart:convert'; + +import 'package:flutter_svg/flutter_svg.dart'; +import 'package:universal_io/io.dart'; + +/// A [BytesLoader] that decodes SVG data from a file in an isolate and creates +/// a vector_graphics binary representation. +/// +/// It has the same logic as [SvgFileLoader], but uses universal_io to also work on web. +class UniversalSvgFileLoader extends SvgLoader { + /// Creates a new universal SVG file loader. + const UniversalSvgFileLoader( + this.file, { + super.theme, + super.colorMapper, + }); + + /// The file containing the SVG data to decode and render. + final File file; + + @override + String provideSvg(final void message) => utf8.decode(file.readAsBytesSync(), allowMalformed: true); + + @override + int get hashCode => Object.hash(file, theme, colorMapper); + + @override + bool operator ==(final Object other) => + other is UniversalSvgFileLoader && other.file == file && other.theme == theme && other.colorMapper == colorMapper; +}