Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(neon): Fix SVG code compilation for web #1041

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/neon/neon/lib/src/utils/push_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions packages/neon/neon/lib/src/utils/universal_svg_file_loader.dart
Original file line number Diff line number Diff line change
@@ -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<void> {
/// 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;
}