From 21db99d34d8555ebf36c294b921274d69da3a81e Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Thu, 23 Feb 2023 18:27:17 +0100 Subject: [PATCH] refactor(ui): replace ThemeResolver by ThemeHelper in Loader / RichTextBuilder (closes #148) --- .../lib/src/domain/entities/entities.dart | 3 +- .../src/domain/entities/loader/loader.dart | 17 ++++ .../{ => loader}/loader_component.dart | 0 .../{ => loader}/loader_component.g.dart | 0 .../lib/src/domain/entities/loader_style.dart | 82 ------------------- .../rich_text_builder/rich_text_builder.dart | 1 - .../rich_text_builder_component.dart | 1 - .../rich_text_builder_component.g.dart | 3 - .../rich_text_builder_style.dart | 79 ------------------ .../lib/src/components/loader/loader.dart | 49 ++++++----- .../lib/src/components/loader/loader.g.dart | 1 - .../loader/loader_theme_resolver.dart | 58 ------------- .../rich_text_builder/rich_text_builder.dart | 45 +++++----- .../rich_text_builder.g.dart | 6 -- .../rich_text_builder_theme_resolver.dart | 53 ------------ 15 files changed, 68 insertions(+), 330 deletions(-) create mode 100644 packages/wyatt_ui_components/lib/src/domain/entities/loader/loader.dart rename packages/wyatt_ui_components/lib/src/domain/entities/{ => loader}/loader_component.dart (100%) rename packages/wyatt_ui_components/lib/src/domain/entities/{ => loader}/loader_component.g.dart (100%) delete mode 100644 packages/wyatt_ui_components/lib/src/domain/entities/loader_style.dart delete mode 100644 packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_style.dart delete mode 100644 packages/wyatt_ui_kit/lib/src/components/loader/loader_theme_resolver.dart delete mode 100644 packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder_theme_resolver.dart diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/entities.dart b/packages/wyatt_ui_components/lib/src/domain/entities/entities.dart index 1c7756e8..6fc5b795 100644 --- a/packages/wyatt_ui_components/lib/src/domain/entities/entities.dart +++ b/packages/wyatt_ui_components/lib/src/domain/entities/entities.dart @@ -19,8 +19,7 @@ export './buttons/buttons.dart'; export './cards/cards.dart'; export './component.dart'; export './error_widget_component.dart'; -export './loader_component.dart'; -export './loader_style.dart'; +export './loader/loader.dart'; export './loading_widget_component.dart'; export './rich_text_builder/rich_text_builder.dart'; export './text_inputs/text_inputs.dart'; diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/loader/loader.dart b/packages/wyatt_ui_components/lib/src/domain/entities/loader/loader.dart new file mode 100644 index 00000000..67446c69 --- /dev/null +++ b/packages/wyatt_ui_components/lib/src/domain/entities/loader/loader.dart @@ -0,0 +1,17 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +export './loader_component.dart'; diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/loader_component.dart b/packages/wyatt_ui_components/lib/src/domain/entities/loader/loader_component.dart similarity index 100% rename from packages/wyatt_ui_components/lib/src/domain/entities/loader_component.dart rename to packages/wyatt_ui_components/lib/src/domain/entities/loader/loader_component.dart diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/loader_component.g.dart b/packages/wyatt_ui_components/lib/src/domain/entities/loader/loader_component.g.dart similarity index 100% rename from packages/wyatt_ui_components/lib/src/domain/entities/loader_component.g.dart rename to packages/wyatt_ui_components/lib/src/domain/entities/loader/loader_component.g.dart diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/loader_style.dart b/packages/wyatt_ui_components/lib/src/domain/entities/loader_style.dart deleted file mode 100644 index b32c3a35..00000000 --- a/packages/wyatt_ui_components/lib/src/domain/entities/loader_style.dart +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (C) 2023 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'dart:ui'; - -import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart'; - -class LoaderStyle extends ThemeStyle { - const LoaderStyle({ - this.colors, - this.stroke, - }); - - /// Merges non-null `b` attributes in `a` - static LoaderStyle? merge( - LoaderStyle? a, - LoaderStyle? b, - ) { - if (b == null) { - return a?.copyWith(); - } - if (a == null) { - return b.copyWith(); - } - - return a.copyWith( - colors: b.colors, - stroke: b.stroke, - ); - } - - /// Used for interpolation. - static LoaderStyle? lerp( - LoaderStyle? a, - LoaderStyle? b, - double t, - ) { - if (a == null || b == null) { - return null; - } - // b.copyWith to return b attributes even if they are not lerped - return b.copyWith( - colors: MultiColor.lerp(a.colors, b.colors, t), - stroke: lerpDouble(a.stroke, b.stroke, t), - ); - } - - /// Gradient colors from start to end. - final MultiColor? colors; - - /// Loader stroke width - final double? stroke; - - @override - LoaderStyle? mergeWith(LoaderStyle? other) => LoaderStyle.merge(this, other); - - @override - LoaderStyle copyWith({ - MultiColor? colors, - double? stroke, - }) => - LoaderStyle( - colors: colors ?? this.colors, - stroke: stroke ?? this.stroke, - ); - - @override - String toString() => 'LoaderStyle($colors, $stroke)'; -} diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder.dart b/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder.dart index 1eae2365..61d315f5 100644 --- a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder.dart +++ b/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder.dart @@ -16,4 +16,3 @@ export 'parser.dart'; export 'rich_text_builder_component.dart'; -export 'rich_text_builder_style.dart'; diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.dart b/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.dart index 603aaa32..31fed0cf 100644 --- a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.dart +++ b/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.dart @@ -28,7 +28,6 @@ abstract class RichTextBuilderComponent extends Component this.parser, this.defaultStyle, this.styles, - super.themeResolver, super.key, }); diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.g.dart b/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.g.dart index 9c5194e6..52532f5c 100644 --- a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.g.dart +++ b/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_component.g.dart @@ -11,15 +11,12 @@ abstract class $RichTextBuilderComponentCWProxy { RichTextBuilderComponent parser(RichTextParser? parser); RichTextBuilderComponent defaultStyle(TextStyle? defaultStyle); RichTextBuilderComponent styles(Map? styles); - RichTextBuilderComponent themeResolver( - ThemeResolver? themeResolver); RichTextBuilderComponent key(Key? key); RichTextBuilderComponent call({ String? text, RichTextParser? parser, TextStyle? defaultStyle, Map? styles, - ThemeResolver? themeResolver, Key? key, }); } diff --git a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_style.dart b/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_style.dart deleted file mode 100644 index 814bc237..00000000 --- a/packages/wyatt_ui_components/lib/src/domain/entities/rich_text_builder/rich_text_builder_style.dart +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (C) 2023 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'package:flutter/widgets.dart'; -import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart'; - -class RichTextBuilderStyle extends ThemeStyle { - const RichTextBuilderStyle({ - this.defaultStyle, - this.styles, - }); - - /// Merges non-null `b` attributes in `a` - static RichTextBuilderStyle? merge( - RichTextBuilderStyle? a, - RichTextBuilderStyle? b, - ) { - if (b == null) { - return a?.copyWith(); - } - if (a == null) { - return b.copyWith(); - } - - return a.copyWith( - defaultStyle: b.defaultStyle, - styles: b.styles, - ); - } - - /// Used for interpolation. - static RichTextBuilderStyle? lerp( - RichTextBuilderStyle? a, - RichTextBuilderStyle? b, - double t, - ) { - if (a == null || b == null) { - return null; - } - // b.copyWith to return b attributes even if they are not lerped - return b.copyWith( - defaultStyle: TextStyle.lerp(a.defaultStyle, b.defaultStyle, t), - styles: b.styles, // TODO(wyatt): compute lerp value of each styles - ); - } - - /// Default TextStyle used in this rich text component. - final TextStyle? defaultStyle; - - /// Used styles in this rich text component. - final Map? styles; - - @override - RichTextBuilderStyle? mergeWith(RichTextBuilderStyle? other) => - RichTextBuilderStyle.merge(this, other); - - @override - RichTextBuilderStyle? copyWith({ - TextStyle? defaultStyle, - Map? styles, - }) => - RichTextBuilderStyle( - defaultStyle: defaultStyle ?? this.defaultStyle, - styles: styles ?? this.styles, - ); -} diff --git a/packages/wyatt_ui_kit/lib/src/components/loader/loader.dart b/packages/wyatt_ui_kit/lib/src/components/loader/loader.dart index dd28e602..807274c4 100644 --- a/packages/wyatt_ui_kit/lib/src/components/loader/loader.dart +++ b/packages/wyatt_ui_kit/lib/src/components/loader/loader.dart @@ -20,7 +20,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_animate/flutter_animate.dart'; import 'package:wyatt_component_copy_with_extension/component_copy_with_extension.dart'; import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart'; -import 'package:wyatt_ui_kit/src/components/loader/loader_theme_resolver.dart'; import 'package:wyatt_ui_kit/wyatt_ui_kit.dart'; part 'loader.g.dart'; @@ -33,30 +32,11 @@ class Loader extends LoaderComponent with $LoaderCWMixin { super.stroke, super.duration, super.flip, - super.themeResolver, super.key, }); - @override - LoaderThemeResolver? get themeResolver => - super.themeResolver as LoaderThemeResolver?; - - /// Negotiate the theme to get a complete style. - LoaderStyle _resolve(BuildContext context) { - final LoaderThemeResolver resolver = themeResolver ?? - LoaderThemeResolver( - customStyleFn: (context, {extra}) => LoaderStyle( - colors: colors, - stroke: stroke, - ), - ); - - return resolver.negotiate(context); - } - @override Widget build(BuildContext context) { - final style = _resolve(context); final dimension = (radius != null) ? radius! * 2 : context.buttonTheme.height; return SizedBox.square( @@ -64,9 +44,34 @@ class Loader extends LoaderComponent with $LoaderCWMixin { child: RepaintBoundary( child: CustomPaint( painter: _LoaderPainter( - style.colors ?? const MultiColor([]), + ThemeHelper.getThemeElement( + [ + colors, + Theme.of(context).extension()?.colors, + MultiColor([ + Theme.of(context).progressIndicatorTheme.color ?? + context.colorScheme.primary, + context.colorScheme.onPrimary, + ]), + + /// This is the default value. So the final + /// value cannot be null. + const MultiColor([]) + ], + valueValidator: (multiColor) => + multiColor != null && multiColor.isColor, + transform: (multiColor) => multiColor, + )!, dimension / 2, - style.stroke ?? 4, + ThemeHelper.getThemeElement( + [ + stroke, + Theme.of(context).extension()?.stroke, + 4, + ], + valueValidator: (stroke) => stroke != null, + transform: (stroke) => stroke, + )!, flip: flip ?? false, ), ) diff --git a/packages/wyatt_ui_kit/lib/src/components/loader/loader.g.dart b/packages/wyatt_ui_kit/lib/src/components/loader/loader.g.dart index 4f1243de..7576c3b4 100644 --- a/packages/wyatt_ui_kit/lib/src/components/loader/loader.g.dart +++ b/packages/wyatt_ui_kit/lib/src/components/loader/loader.g.dart @@ -41,7 +41,6 @@ class $LoaderCWProxyImpl implements $LoaderComponentCWProxy { stroke: stroke ?? _value.stroke, duration: duration ?? _value.duration, flip: flip ?? _value.flip, - themeResolver: themeResolver ?? _value.themeResolver, key: key ?? _value.key, ); } diff --git a/packages/wyatt_ui_kit/lib/src/components/loader/loader_theme_resolver.dart b/packages/wyatt_ui_kit/lib/src/components/loader/loader_theme_resolver.dart deleted file mode 100644 index 72e98477..00000000 --- a/packages/wyatt_ui_kit/lib/src/components/loader/loader_theme_resolver.dart +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (C) 2023 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'package:flutter/material.dart'; -import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart'; -import 'package:wyatt_ui_kit/wyatt_ui_kit.dart'; - -class LoaderThemeResolver - extends ThemeResolver { - const LoaderThemeResolver({ - required this.customStyleFn, - }); - - @override - final LoaderStyle? Function( - BuildContext context, { - void extra, - }) customStyleFn; - - /// Values taken from - @override - LoaderStyle computeDefaultValue( - BuildContext context, { - void extra, - }) => - LoaderStyle( - colors: MultiColor([ - Theme.of(context).progressIndicatorTheme.color ?? - context.colorScheme.primary, - context.colorScheme.onPrimary, - ]), - stroke: 4, - ); - - @override - LoaderStyle? computeExtensionValueFn( - BuildContext context, - LoaderThemeExtension? themeExtension, { - void extra, - }) => - LoaderStyle( - colors: themeExtension?.colors, - stroke: themeExtension?.stroke, - ); -} diff --git a/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.dart b/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.dart index c16df96c..a0f80177 100644 --- a/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.dart +++ b/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.dart @@ -17,7 +17,6 @@ import 'package:flutter/material.dart'; import 'package:wyatt_component_copy_with_extension/component_copy_with_extension.dart'; import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart'; -import 'package:wyatt_ui_kit/src/components/rich_text_builder/rich_text_builder_theme_resolver.dart'; import 'package:wyatt_ui_kit/wyatt_ui_kit.dart'; part 'rich_text_builder.g.dart'; @@ -30,37 +29,39 @@ class RichTextBuilder extends RichTextBuilderComponent super.parser, super.defaultStyle, super.styles, - super.themeResolver, super.key, }); - @override - RichTextBuilderThemeResolver? get themeResolver => - super.themeResolver as RichTextBuilderThemeResolver?; - - /// Negotiate the theme to get a complete style. - RichTextBuilderStyle _resolve(BuildContext context) { - final RichTextBuilderThemeResolver resolver = themeResolver ?? - RichTextBuilderThemeResolver( - customStyleFn: (context, {extra}) => RichTextBuilderStyle( - defaultStyle: defaultStyle, - styles: styles, - ), - ); - - return resolver.negotiate(context); - } - @override Widget build(BuildContext context) { - final style = _resolve(context); final RegExp regex = RegExp(r'<(.*?)>(.*?)<\/\1>'); final root = RichTextNode.from( text ?? '', regex, RichTextStyleParameter( - style.defaultStyle, - style.styles ?? {}, + ThemeHelper.getThemeElement( + [ + defaultStyle, + Theme.of(context) + .extension() + ?.defaultStyle, + context.textTheme.bodyMedium, + ], + valueValidator: (style) => style != null, + transform: (style) => style, + ), + ThemeHelper.getThemeElement, + Map>( + [ + styles, + Theme.of(context) + .extension() + ?.styles, + const {}, + ], + valueValidator: (styles) => styles != null, + transform: (styles) => styles, + )!, null, ), ); diff --git a/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.g.dart b/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.g.dart index 235c5f0f..87ca0548 100644 --- a/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.g.dart +++ b/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder.g.dart @@ -20,10 +20,6 @@ class $RichTextBuilderCWProxyImpl implements $RichTextBuilderComponentCWProxy { RichTextBuilder styles(Map? styles) => this(styles: styles); @override - RichTextBuilder themeResolver( - ThemeResolver? themeResolver) => - this(themeResolver: themeResolver); - @override RichTextBuilder key(Key? key) => this(key: key); @override RichTextBuilder call({ @@ -31,7 +27,6 @@ class $RichTextBuilderCWProxyImpl implements $RichTextBuilderComponentCWProxy { RichTextParser? parser, TextStyle? defaultStyle, Map? styles, - ThemeResolver? themeResolver, Key? key, }) => RichTextBuilder( @@ -39,7 +34,6 @@ class $RichTextBuilderCWProxyImpl implements $RichTextBuilderComponentCWProxy { parser: parser ?? _value.parser, defaultStyle: defaultStyle ?? _value.defaultStyle, styles: styles ?? _value.styles, - themeResolver: themeResolver ?? _value.themeResolver, key: key ?? _value.key, ); } diff --git a/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder_theme_resolver.dart b/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder_theme_resolver.dart deleted file mode 100644 index 74202562..00000000 --- a/packages/wyatt_ui_kit/lib/src/components/rich_text_builder/rich_text_builder_theme_resolver.dart +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (C) 2023 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'package:flutter/material.dart'; -import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart'; -import 'package:wyatt_ui_kit/wyatt_ui_kit.dart'; - -class RichTextBuilderThemeResolver extends ThemeResolver { - const RichTextBuilderThemeResolver({ - required this.customStyleFn, - }); - - /// Values taken from - @override - RichTextBuilderStyle computeDefaultValue( - BuildContext context, { - void extra, - }) => - RichTextBuilderStyle( - defaultStyle: context.textTheme.bodyMedium, - ); - - @override - final RichTextBuilderStyle? Function( - BuildContext context, { - void extra, - }) customStyleFn; - - @override - RichTextBuilderStyle? computeExtensionValueFn( - BuildContext context, - RichTextBuilderThemeExtension? themeExtension, { - void extra, - }) => - RichTextBuilderStyle( - defaultStyle: themeExtension?.defaultStyle, - styles: themeExtension?.styles, - ); -}