diff --git a/lib/models/isar/sw_theme.dart b/lib/models/isar/sw_theme.dart index 02f11174f..8e8d6a29d 100644 --- a/lib/models/isar/sw_theme.dart +++ b/lib/models/isar/sw_theme.dart @@ -9,14 +9,12 @@ import 'package:stackwallet/utilities/theme/color_theme.dart'; import 'package:uuid/uuid.dart'; @Collection(inheritance: false) -class ColorTheme { - static String themesDirPath = "/djhfgj/sdfd/themes/"; - +class StackTheme { final String assetBundleUrl; /// should be a uuid @Index(unique: true, replace: true) - final String id; + final String internalId; /// the theme name that will be displayed in app final String name; @@ -39,21 +37,18 @@ class ColorTheme { } @ignore - Color get background => _background ??= Color( - backgroundString.toBigIntFromHex.toInt(), - ); + Color get background => _background ??= Color(backgroundInt); @ignore Color? _background; - final String backgroundString; + final int backgroundInt; // ==== backgroundAppBar ===================================================== @ignore - Color get backgroundAppBar => _backgroundAppBar ??= Color( - backgroundAppBarString.toBigIntFromHex.toInt(), - ); + Color get backgroundAppBar => + _backgroundAppBar ??= Color(backgroundAppBarInt); @ignore Color? _backgroundAppBar; - final String backgroundAppBarString; + final int backgroundAppBarInt; // =========================================================================== @ignore @@ -67,45 +62,82 @@ class ColorTheme { Gradient? _gradientBackground; final String gradientBackgroundString; + // =========================================================================== + @ignore Map get coinColors => - _coinColors ??= parseCoinColors(coinColorsString); + _coinColors ??= parseCoinColors(coinColorsJsonString); @ignore Map? _coinColors; - final String coinColorsString; + final String coinColorsJsonString; - // ==== assets ===================================================== - final String circleLock; + // =========================================================================== - ColorTheme({ - required this.id, + // =========================================================================== + // =========================================================================== + + final ThemeAssets assets; + + // =========================================================================== + // =========================================================================== + + StackTheme({ + required this.internalId, required this.assetBundleUrl, required this.name, required this.brightnessString, - required this.backgroundString, - required this.backgroundAppBarString, + required this.backgroundInt, + required this.backgroundAppBarInt, required this.gradientBackgroundString, - required this.coinColorsString, - required this.circleLock, + required this.coinColorsJsonString, + required this.assets, }); - factory ColorTheme.fromJson(Map json) { + factory StackTheme.fromJson({ + required Map json, + required String applicationThemesDirectoryPath, + }) { final _id = const Uuid().v1(); - return ColorTheme( - id: _id, + return StackTheme( + internalId: _id, name: json["name"] as String, assetBundleUrl: json["assetBundleUrl"] as String, brightnessString: json["brightness"] as String, - backgroundString: json["colors"]["background"] as String, - backgroundAppBarString: json["colors"]["backgroundAppBar"] as String, + backgroundInt: parseColor(json["colors"]["background"] as String), + backgroundAppBarInt: + parseColor(json["colors"]["backgroundAppBar"] as String), gradientBackgroundString: jsonEncode(json["gradients"]["gradientBackground"] as Map), - coinColorsString: jsonEncode(json["coinColors"] as Map), - circleLock: - "$themesDirPath/$_id/${json["assets"]["circleLock"] as String}", + coinColorsJsonString: jsonEncode(json["coinColors"] as Map), + assets: ThemeAssets.fromJson( + json: json, + applicationThemesDirectoryPath: applicationThemesDirectoryPath, + internalThemeUuid: _id, + ), ); } + /// Grab the int value of the hex color string. + /// 8 char string value expected where the first 2 are opacity + static int parseColor(String colorHex) { + try { + final int colorValue = colorHex.toBigIntFromHex.toInt(); + if (colorValue >= 0 && colorValue <= 0xFFFFFFFF) { + return colorValue; + } else { + throw ArgumentError( + '"$colorHex" and corresponding int ' + 'value "$colorValue" is not a valid color.', + ); + } + } catch (_) { + throw ArgumentError( + '"$colorHex" is not a valid hex number', + ); + } + } + + /// parse coin colors json and fetch color or use default static Map parseCoinColors(String jsonString) { final json = jsonDecode(jsonString) as Map; final map = Map.from(json); @@ -125,3 +157,25 @@ class ColorTheme { return result; } } + +@Embedded(inheritance: false) +class ThemeAssets { + final String plus; + + // todo: add all assets expected in json + + ThemeAssets({ + required this.plus, + }); + + factory ThemeAssets.fromJson({ + required Map json, + required String applicationThemesDirectoryPath, + required String internalThemeUuid, + }) { + return ThemeAssets( + plus: + "$applicationThemesDirectoryPath/$internalThemeUuid/${json["assets"]["svg"]["plus.svg"] as String}", + ); + } +} diff --git a/lib/temp_themes/dark.dart b/lib/themes/defaults/dark.dart similarity index 98% rename from lib/temp_themes/dark.dart rename to lib/themes/defaults/dark.dart index 8f784195d..daa00cb6a 100644 --- a/lib/temp_themes/dark.dart +++ b/lib/themes/defaults/dark.dart @@ -193,14 +193,14 @@ final Map darkJson = { { "type": "standard", "color": "0x0F2D3132", - "spread_radius": 3, - "blur_radius": 4 + "spread_radius": 3.0, + "blur_radius": 4.0 }, { "type": "home_view_button_bar", "color": "0x0F2D3132", - "spread_radius": 3, - "blur_radius": 4 + "spread_radius": 3.0, + "blur_radius": 4.0 } ] }; diff --git a/lib/themes/theme_providers.dart b/lib/themes/theme_providers.dart new file mode 100644 index 000000000..e975d6983 --- /dev/null +++ b/lib/themes/theme_providers.dart @@ -0,0 +1,41 @@ +import 'dart:io'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_svg/flutter_svg.dart'; +import 'package:stackwallet/models/isar/sw_theme.dart'; +import 'package:stackwallet/themes/defaults/dark.dart'; + +final applicationThemesDirectoryPathProvider = StateProvider((ref) => ""); + +final themeProvider = StateProvider((ref) { + // Return default if no theme was properly loaded on startup. This should + // technically never actually be read but we don't want an optional. + // Ideally Riverpod would would give us some kind of 'late' provider option + return StackTheme.fromJson( + json: darkJson, + // Explicitly use ref.read here as we do not want any rebuild on this + // value change. + applicationThemesDirectoryPath: + ref.read(applicationThemesDirectoryPathProvider), + ); +}); + +/// example +class ExampleWidget extends ConsumerWidget { + const ExampleWidget({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context, WidgetRef ref) { + return Column( + children: [ + const Text("Hello, world!"), + SvgPicture.file( + File( + ref.watch(themeProvider).assets.plus, + ), + ), + ], + ); + } +} diff --git a/lib/temp_themes/theme_template.dart b/lib/themes/theme_template.dart similarity index 100% rename from lib/temp_themes/theme_template.dart rename to lib/themes/theme_template.dart