embedded assets, provider, and example

This commit is contained in:
julian 2023-04-20 09:32:50 -06:00
parent 7581e4c5f8
commit e3888be1b1
4 changed files with 129 additions and 34 deletions

View file

@ -9,14 +9,12 @@ import 'package:stackwallet/utilities/theme/color_theme.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
@Collection(inheritance: false) @Collection(inheritance: false)
class ColorTheme { class StackTheme {
static String themesDirPath = "/djhfgj/sdfd/themes/";
final String assetBundleUrl; final String assetBundleUrl;
/// should be a uuid /// should be a uuid
@Index(unique: true, replace: true) @Index(unique: true, replace: true)
final String id; final String internalId;
/// the theme name that will be displayed in app /// the theme name that will be displayed in app
final String name; final String name;
@ -39,21 +37,18 @@ class ColorTheme {
} }
@ignore @ignore
Color get background => _background ??= Color( Color get background => _background ??= Color(backgroundInt);
backgroundString.toBigIntFromHex.toInt(),
);
@ignore @ignore
Color? _background; Color? _background;
final String backgroundString; final int backgroundInt;
// ==== backgroundAppBar ===================================================== // ==== backgroundAppBar =====================================================
@ignore @ignore
Color get backgroundAppBar => _backgroundAppBar ??= Color( Color get backgroundAppBar =>
backgroundAppBarString.toBigIntFromHex.toInt(), _backgroundAppBar ??= Color(backgroundAppBarInt);
);
@ignore @ignore
Color? _backgroundAppBar; Color? _backgroundAppBar;
final String backgroundAppBarString; final int backgroundAppBarInt;
// =========================================================================== // ===========================================================================
@ignore @ignore
@ -67,45 +62,82 @@ class ColorTheme {
Gradient? _gradientBackground; Gradient? _gradientBackground;
final String gradientBackgroundString; final String gradientBackgroundString;
// ===========================================================================
@ignore @ignore
Map<Coin, Color> get coinColors => Map<Coin, Color> get coinColors =>
_coinColors ??= parseCoinColors(coinColorsString); _coinColors ??= parseCoinColors(coinColorsJsonString);
@ignore @ignore
Map<Coin, Color>? _coinColors; Map<Coin, Color>? _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.assetBundleUrl,
required this.name, required this.name,
required this.brightnessString, required this.brightnessString,
required this.backgroundString, required this.backgroundInt,
required this.backgroundAppBarString, required this.backgroundAppBarInt,
required this.gradientBackgroundString, required this.gradientBackgroundString,
required this.coinColorsString, required this.coinColorsJsonString,
required this.circleLock, required this.assets,
}); });
factory ColorTheme.fromJson(Map<String, dynamic> json) { factory StackTheme.fromJson({
required Map<String, dynamic> json,
required String applicationThemesDirectoryPath,
}) {
final _id = const Uuid().v1(); final _id = const Uuid().v1();
return ColorTheme( return StackTheme(
id: _id, internalId: _id,
name: json["name"] as String, name: json["name"] as String,
assetBundleUrl: json["assetBundleUrl"] as String, assetBundleUrl: json["assetBundleUrl"] as String,
brightnessString: json["brightness"] as String, brightnessString: json["brightness"] as String,
backgroundString: json["colors"]["background"] as String, backgroundInt: parseColor(json["colors"]["background"] as String),
backgroundAppBarString: json["colors"]["backgroundAppBar"] as String, backgroundAppBarInt:
parseColor(json["colors"]["backgroundAppBar"] as String),
gradientBackgroundString: gradientBackgroundString:
jsonEncode(json["gradients"]["gradientBackground"] as Map), jsonEncode(json["gradients"]["gradientBackground"] as Map),
coinColorsString: jsonEncode(json["coinColors"] as Map), coinColorsJsonString: jsonEncode(json["coinColors"] as Map),
circleLock: assets: ThemeAssets.fromJson(
"$themesDirPath/$_id/${json["assets"]["circleLock"] as String}", 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<Coin, Color> parseCoinColors(String jsonString) { static Map<Coin, Color> parseCoinColors(String jsonString) {
final json = jsonDecode(jsonString) as Map; final json = jsonDecode(jsonString) as Map;
final map = Map<String, dynamic>.from(json); final map = Map<String, dynamic>.from(json);
@ -125,3 +157,25 @@ class ColorTheme {
return result; 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<String, dynamic> json,
required String applicationThemesDirectoryPath,
required String internalThemeUuid,
}) {
return ThemeAssets(
plus:
"$applicationThemesDirectoryPath/$internalThemeUuid/${json["assets"]["svg"]["plus.svg"] as String}",
);
}
}

View file

@ -193,14 +193,14 @@ final Map<String, dynamic> darkJson = {
{ {
"type": "standard", "type": "standard",
"color": "0x0F2D3132", "color": "0x0F2D3132",
"spread_radius": 3, "spread_radius": 3.0,
"blur_radius": 4 "blur_radius": 4.0
}, },
{ {
"type": "home_view_button_bar", "type": "home_view_button_bar",
"color": "0x0F2D3132", "color": "0x0F2D3132",
"spread_radius": 3, "spread_radius": 3.0,
"blur_radius": 4 "blur_radius": 4.0
} }
] ]
}; };

View file

@ -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<StackTheme>((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,
),
),
],
);
}
}