From b25f95c706e0d70bdc470e3c5b83c6ab23bdec7b Mon Sep 17 00:00:00 2001 From: Frank Merkel <138444693+frankmer@users.noreply.github.com> Date: Thu, 21 Nov 2024 16:15:25 +0100 Subject: [PATCH] customizer edits --- lib/mains/main_customizer.dart | 6 +- lib/mains/main_netknights.dart | 6 +- .../enums/image_file_type_extension.dart | 31 ++++++- lib/model/widget_image.dart | 4 +- .../application_customization.dart | 83 ++++++++++++++----- lib/views/splash_screen/splash_screen.dart | 2 +- 6 files changed, 98 insertions(+), 34 deletions(-) diff --git a/lib/mains/main_customizer.dart b/lib/mains/main_customizer.dart index 4efc8134..dcbec33c 100644 --- a/lib/mains/main_customizer.dart +++ b/lib/mains/main_customizer.dart @@ -82,13 +82,13 @@ class CustomizationAuthenticator extends ConsumerWidget { FeedbackView.routeName: (context) => const FeedbackView(), ImportTokensView.routeName: (context) => const ImportTokensView(), LicenseView.routeName: (context) => LicenseView( - appImage: applicationCustomizer.appImage.getWidget, + appImage: applicationCustomizer.licensesViewImage.getWidget, appName: applicationCustomizer.appName, websiteLink: applicationCustomizer.websiteLink, ), MainView.routeName: (context) => MainView( - appIcon: applicationCustomizer.appIcon.getWidget, - appImage: applicationCustomizer.appImage.getWidget, + appIcon: applicationCustomizer.appbarIcon.getWidget, + appImage: applicationCustomizer.splashScreenImage.getWidget, appName: applicationCustomizer.appName, disablePatchNotes: applicationCustomizer.disabledFeatures.contains(AppFeature.patchNotes), ), diff --git a/lib/mains/main_netknights.dart b/lib/mains/main_netknights.dart index 94709bb8..46db0960 100644 --- a/lib/mains/main_netknights.dart +++ b/lib/mains/main_netknights.dart @@ -104,13 +104,13 @@ class PrivacyIDEAAuthenticator extends ConsumerWidget { FeedbackView.routeName: (context) => const FeedbackView(), ImportTokensView.routeName: (context) => const ImportTokensView(), LicenseView.routeName: (context) => LicenseView( - appImage: _customization.appImage.getWidget, + appImage: _customization.licensesViewImage.getWidget, appName: _customization.appName, websiteLink: _customization.websiteLink, ), MainView.routeName: (context) => MainView( - appIcon: _customization.appIcon.getWidget, - appImage: _customization.appImage.getWidget, + appIcon: _customization.appbarIcon.getWidget, + appImage: _customization.splashScreenImage.getWidget, appName: _customization.appName, disablePatchNotes: _customization.disabledFeatures.contains(AppFeature.patchNotes), ), diff --git a/lib/model/extensions/enums/image_file_type_extension.dart b/lib/model/extensions/enums/image_file_type_extension.dart index 07486c40..27e3a46f 100644 --- a/lib/model/extensions/enums/image_file_type_extension.dart +++ b/lib/model/extensions/enums/image_file_type_extension.dart @@ -26,6 +26,18 @@ import 'package:image_picker/image_picker.dart'; import '../../enums/image_file_type.dart'; extension ImageFileTypeX on ImageFileType { + static ImageFileType fromExtensionString(String ex) => switch (ex) { + 'svg' => ImageFileType.svg, + 'svgz' => ImageFileType.svgz, + 'png' => ImageFileType.png, + 'jpg' => ImageFileType.jpg, + 'jpeg' => ImageFileType.jpeg, + 'gif' => ImageFileType.gif, + 'bmp' => ImageFileType.bmp, + 'webp' => ImageFileType.webp, + _ => throw Exception('Unknown extension: $ex'), + }; + Widget buildImageWidget(Uint8List imageData) => switch (this) { ImageFileType.svg => SvgPicture.memory( imageData, @@ -67,9 +79,9 @@ extension ImageFileTypeX on ImageFileType { ), }; - String get fileExtension => toString().split('.').last; + String get extension => toString().split('.').last; - String get fileType => switch (this) { + String get typeName => switch (this) { ImageFileType.svg => 'Scalable Vector Graphic', ImageFileType.svgz => 'Scalable Vector Graphic (compressed)', ImageFileType.png => 'PNG', @@ -80,8 +92,19 @@ extension ImageFileTypeX on ImageFileType { ImageFileType.webp => 'WebP', }; + String get mimeType => switch (this) { + ImageFileType.svg => 'image/svg+xml', + ImageFileType.svgz => 'image/svg+xml', + ImageFileType.png => 'image/png', + ImageFileType.jpg => 'image/jpeg', + ImageFileType.jpeg => 'image/jpeg', + ImageFileType.gif => 'image/gif', + ImageFileType.bmp => 'image/bmp', + ImageFileType.webp => 'image/webp', + }; + /// Builds an [XFile] from the given [imageData] and [fileName]. - /// The [fileType] is used as the name of the file. + /// The [fileName] is used as the name of the file. /// The file extension is determined by the [ImageFileType]. - XFile buildXFile(Uint8List imageData, String fileName) => XFile.fromData(imageData, name: "$fileName.$fileExtension"); + XFile buildXFile(Uint8List imageData, String fileName) => XFile.fromData(imageData, name: "$fileName.$extension"); } diff --git a/lib/model/widget_image.dart b/lib/model/widget_image.dart index 57b7772c..2bbc2bc7 100644 --- a/lib/model/widget_image.dart +++ b/lib/model/widget_image.dart @@ -48,7 +48,7 @@ class WidgetImage { final Uint8List imageData; final String fileName; - String get fullFileName => '$fileName.${fileType.fileType}'; + String get fullFileName => '$fileName.${fileType.extension}'; WidgetImage({ required this.fileType, @@ -77,7 +77,7 @@ class WidgetImage { try { return fileType.buildImageWidget(imageData); } catch (e) { - Logger.error('Image is not an ${fileType.fileType}, or the image data is corrupted.', error: e); + Logger.error('Image is not an ${fileType.typeName}, or the image data is corrupted.', error: e); rethrow; } } diff --git a/lib/utils/customization/application_customization.dart b/lib/utils/customization/application_customization.dart index c0235c2f..2c7e66a8 100644 --- a/lib/utils/customization/application_customization.dart +++ b/lib/utils/customization/application_customization.dart @@ -62,9 +62,14 @@ class ApplicationCustomization { final String appName; final String websiteLink; - final WidgetImage appIcon; - - final WidgetImage appImage; + final WidgetImage appbarIcon; + static const String appbarIconFileName = 'appbar_icon'; + final WidgetImage splashScreenImage; + static const String splashScreenImageFileName = 'splash_screen_image'; + final WidgetImage backgroundImage; + static const String backgroundImageFileName = 'background_image'; + final WidgetImage licensesViewImage; + static const String licensesViewImageFileName = 'licenses_view_image'; final ThemeCustomization lightTheme; final ThemeCustomization darkTheme; @@ -77,19 +82,27 @@ class ApplicationCustomization { this.websiteLink = 'https://netknights.it/', this.fontFamilyName = _defaultFontName, this.customFontBytes, - WidgetImage? appIcon, - WidgetImage? appImage, + WidgetImage? appbarIcon, + WidgetImage? splashScreenImage, + WidgetImage? backgroundImage, + WidgetImage? licensesViewImage, this.lightTheme = ThemeCustomization.defaultLightTheme, this.darkTheme = ThemeCustomization.defaultDarkTheme, this.disabledFeatures = const {}, - }) : appIcon = appIcon ?? WidgetImage(fileType: ImageFileType.png, imageData: defaultIconUint8List, fileName: 'appIcon'), - appImage = appImage ?? WidgetImage(fileType: ImageFileType.png, imageData: defaultImageUint8List, fileName: 'appImage'); + }) : appbarIcon = appbarIcon ?? WidgetImage(fileType: ImageFileType.png, imageData: defaultIconUint8List, fileName: appbarIconFileName), + splashScreenImage = + splashScreenImage ?? WidgetImage(fileType: ImageFileType.png, imageData: defaultImageUint8List, fileName: splashScreenImageFileName), + backgroundImage = backgroundImage ?? WidgetImage(fileType: ImageFileType.png, imageData: defaultImageUint8List, fileName: backgroundImageFileName), + licensesViewImage = + licensesViewImage ?? WidgetImage(fileType: ImageFileType.png, imageData: defaultImageUint8List, fileName: licensesViewImageFileName); ApplicationCustomization copyWith({ String? appName, String? websiteLink, - WidgetImage? appIcon, - WidgetImage? appImage, + WidgetImage? appbarIcon, + WidgetImage? splashScreenImage, + WidgetImage? backgroundImage, + WidgetImage? licensesViewImage, ThemeCustomization? lightTheme, ThemeCustomization? darkTheme, Set? disabledFeatures, @@ -97,8 +110,10 @@ class ApplicationCustomization { ApplicationCustomization( appName: appName ?? this.appName, websiteLink: websiteLink ?? this.websiteLink, - appIcon: appIcon ?? this.appIcon, - appImage: appImage ?? this.appImage, + appbarIcon: appbarIcon ?? this.appbarIcon, + splashScreenImage: splashScreenImage ?? this.splashScreenImage, + backgroundImage: backgroundImage ?? this.backgroundImage, + licensesViewImage: licensesViewImage ?? this.licensesViewImage, lightTheme: lightTheme ?? this.lightTheme, darkTheme: darkTheme ?? this.darkTheme, disabledFeatures: disabledFeatures ?? this.disabledFeatures, @@ -112,8 +127,10 @@ class ApplicationCustomization { websiteLink == other.websiteLink && customFontBytes == other.customFontBytes && fontFamilyName == other.fontFamilyName && - appIcon == other.appIcon && - appImage == other.appImage && + appbarIcon == other.appbarIcon && + splashScreenImage == other.splashScreenImage && + backgroundImage == other.backgroundImage && + licensesViewImage == other.licensesViewImage && lightTheme == other.lightTheme && darkTheme == other.darkTheme && disabledFeatures == other.disabledFeatures; @@ -124,8 +141,10 @@ class ApplicationCustomization { websiteLink, customFontBytes, fontFamilyName, - appIcon, - appImage, + appbarIcon, + splashScreenImage, + backgroundImage, + licensesViewImage, lightTheme, darkTheme, disabledFeatures, @@ -137,8 +156,10 @@ class ApplicationCustomization { websiteLink: websiteLink, customFontBytes: fontBytes, fontFamilyName: fontName, - appIcon: appIcon, - appImage: appImage, + appbarIcon: appbarIcon, + splashScreenImage: splashScreenImage, + backgroundImage: backgroundImage, + licensesViewImage: licensesViewImage, lightTheme: lightTheme, darkTheme: darkTheme, disabledFeatures: disabledFeatures, @@ -163,8 +184,26 @@ class ApplicationCustomization { websiteLink: json['websiteLink'] as String? ?? 'https://netknights.it/', customFontBytes: json['customFontBytes'] != null ? base64Decode(json['customFontBytes'] as String) : null, fontFamilyName: json['fontFamilyName'] as String? ?? _defaultFontName, - appIcon: json['appIcon'] == null ? null : WidgetImage.fromJson(json['appIcon'] as Map), - appImage: json['appImage'] == null ? null : WidgetImage.fromJson(json['appImage'] as Map), + appbarIcon: json['appbarIcon'] != null + ? WidgetImage.fromJson(json['appbarIcon'] as Map) + : json['appIcon'] != null + ? WidgetImage.fromJson(json['appIcon'] as Map) + : null, + splashScreenImage: json['splashScreenImage'] != null + ? WidgetImage.fromJson(json['splashScreenImage'] as Map) + : json['appImage'] != null + ? WidgetImage.fromJson(json['appImage'] as Map) + : null, + backgroundImage: json['backgroundImage'] != null + ? WidgetImage.fromJson(json['backgroundImage'] as Map) + : json['appImage'] != null + ? WidgetImage.fromJson(json['appImage'] as Map) + : null, + licensesViewImage: json['licensesViewImage'] != null + ? WidgetImage.fromJson(json['licensesViewImage'] as Map) + : json['appImage'] != null + ? WidgetImage.fromJson(json['appImage'] as Map) + : null, lightTheme: json['lightTheme'] != null ? ThemeCustomization.fromJson(json['lightTheme'] as Map) : ThemeCustomization.defaultLightTheme, darkTheme: json['darkTheme'] != null ? ThemeCustomization.fromJson(json['darkTheme'] as Map) : ThemeCustomization.defaultDarkTheme, disabledFeatures: @@ -176,8 +215,10 @@ class ApplicationCustomization { 'websiteLink': websiteLink, 'customFontBytes': customFontBytes != null ? base64Encode(customFontBytes!) : null, 'fontFamilyName': fontFamilyName, - 'appIcon': appIcon.toJson(), - 'appImage': appImage.toJson(), + 'appbarIcon': appbarIcon.toJson(), + 'splashScreenImage': splashScreenImage.toJson(), + 'backgroundImage': backgroundImage.toJson(), + 'licensesViewImage': licensesViewImage.toJson(), 'lightTheme': lightTheme.toJson(), 'darkTheme': darkTheme.toJson(), 'disabledFeatures': disabledFeatures.map((e) => e.name).toList(), diff --git a/lib/views/splash_screen/splash_screen.dart b/lib/views/splash_screen/splash_screen.dart index 7237f383..21da741b 100644 --- a/lib/views/splash_screen/splash_screen.dart +++ b/lib/views/splash_screen/splash_screen.dart @@ -129,7 +129,7 @@ class _SplashScreenState extends ConsumerState { child: SizedBox( height: 99999, width: 99999, - child: _customization.appImage.getWidget, + child: _customization.splashScreenImage.getWidget, ), ), ),