_EnumsModel -> _UserPreferencesModel

This commit is contained in:
Vadim 2023-08-14 12:25:08 +02:00
parent f9f22324db
commit fd7a9d252a

View file

@ -22,15 +22,15 @@ class UserPreferencesProvider extends StatefulWidget {
} }
static DynamicColorState dynamicColorStateOf(BuildContext context) { static DynamicColorState dynamicColorStateOf(BuildContext context) {
return _inheritFromThemeModel(context, _ThemeAspect.dynamicColorState).dynamicColorState; return _inheritFromEnumsModel(context, _Aspect.dynamicColorState).dynamicColorState;
} }
static EvSourceType evSourceTypeOf(BuildContext context) { static EvSourceType evSourceTypeOf(BuildContext context) {
return _inheritFromEnumsModel(context, _ListenableAspect.evSourceType).evSourceType; return _inheritFromEnumsModel(context, _Aspect.evSourceType).evSourceType;
} }
static SupportedLocale localeOf(BuildContext context) { static SupportedLocale localeOf(BuildContext context) {
return _inheritFromEnumsModel(context, _ListenableAspect.locale).locale; return _inheritFromEnumsModel(context, _Aspect.locale).locale;
} }
static MeteringScreenLayoutConfig meteringScreenConfigOf(BuildContext context) { static MeteringScreenLayoutConfig meteringScreenConfigOf(BuildContext context) {
@ -43,23 +43,22 @@ class UserPreferencesProvider extends StatefulWidget {
} }
static StopType stopTypeOf(BuildContext context) { static StopType stopTypeOf(BuildContext context) {
return _inheritFromEnumsModel(context, _ListenableAspect.stopType).stopType; return _inheritFromEnumsModel(context, _Aspect.stopType).stopType;
} }
static ThemeData themeOf(BuildContext context) { static ThemeData themeOf(BuildContext context) {
return _inheritFromThemeModel(context, _ThemeAspect.theme).theme; return _inheritFromEnumsModel(context, _Aspect.theme).theme;
} }
static ThemeType themeTypeOf(BuildContext context) { static ThemeType themeTypeOf(BuildContext context) {
return _inheritFromThemeModel(context, _ThemeAspect.themeType).themeType; return _inheritFromEnumsModel(context, _Aspect.themeType).themeType;
} }
static _EnumsModel _inheritFromEnumsModel(BuildContext context, _ListenableAspect aspect) { static _UserPreferencesModel _inheritFromEnumsModel(
return InheritedModel.inheritFrom<_EnumsModel>(context, aspect: aspect)!; BuildContext context,
} _Aspect aspect,
) {
static _ThemeModel _inheritFromThemeModel(BuildContext context, _ThemeAspect aspect) { return InheritedModel.inheritFrom<_UserPreferencesModel>(context, aspect: aspect)!;
return InheritedModel.inheritFrom<_ThemeModel>(context, aspect: aspect)!;
} }
@override @override
@ -122,19 +121,17 @@ class _UserPreferencesProviderState extends State<UserPreferencesProvider>
dynamicPrimaryColor = null; dynamicPrimaryColor = null;
state = DynamicColorState.unavailable; state = DynamicColorState.unavailable;
} }
return _ThemeModel( return _UserPreferencesModel(
brightness: _themeBrightness, brightness: _themeBrightness,
dynamicColorState: state, dynamicColorState: state,
evSourceType: evSourceType,
locale: locale,
primaryColor: dynamicPrimaryColor ?? primaryColor, primaryColor: dynamicPrimaryColor ?? primaryColor,
stopType: stopType,
themeType: themeType, themeType: themeType,
child: _EnumsModel( child: _MeteringScreenLayoutModel(
evSourceType: evSourceType, data: meteringScreenLayout,
locale: locale, child: widget.child,
stopType: stopType,
child: _MeteringScreenLayoutModel(
data: meteringScreenLayout,
child: widget.child,
),
), ),
); );
}, },
@ -212,60 +209,32 @@ class _UserPreferencesProviderState extends State<UserPreferencesProvider>
} }
} }
enum _ListenableAspect { enum _Aspect {
dynamicColorState,
evSourceType, evSourceType,
locale, locale,
stopType, stopType,
}
class _EnumsModel extends InheritedModel<_ListenableAspect> {
final EvSourceType evSourceType;
final SupportedLocale locale;
final StopType stopType;
const _EnumsModel({
required this.evSourceType,
required this.locale,
required this.stopType,
required super.child,
});
@override
bool updateShouldNotify(_EnumsModel oldWidget) {
return evSourceType != oldWidget.evSourceType ||
locale != oldWidget.locale ||
stopType != oldWidget.stopType;
}
@override
bool updateShouldNotifyDependent(
_EnumsModel oldWidget,
Set<_ListenableAspect> dependencies,
) {
return (evSourceType != oldWidget.evSourceType &&
dependencies.contains(_ListenableAspect.evSourceType)) ||
(locale != oldWidget.locale && dependencies.contains(_ListenableAspect.locale)) ||
(stopType != oldWidget.stopType && dependencies.contains(_ListenableAspect.stopType));
}
}
enum _ThemeAspect {
dynamicColorState,
theme, theme,
themeType, themeType,
} }
class _ThemeModel extends InheritedModel<_ThemeAspect> { class _UserPreferencesModel extends InheritedModel<_Aspect> {
final DynamicColorState dynamicColorState; final DynamicColorState dynamicColorState;
final EvSourceType evSourceType;
final SupportedLocale locale;
final StopType stopType;
final ThemeType themeType; final ThemeType themeType;
final Brightness _brightness; final Brightness _brightness;
final Color _primaryColor; final Color _primaryColor;
const _ThemeModel({ const _UserPreferencesModel({
required Brightness brightness, required Brightness brightness,
required this.dynamicColorState, required this.dynamicColorState,
required this.evSourceType,
required this.locale,
required Color primaryColor, required Color primaryColor,
required this.stopType,
required this.themeType, required this.themeType,
required super.child, required super.child,
}) : _brightness = brightness, }) : _brightness = brightness,
@ -274,23 +243,29 @@ class _ThemeModel extends InheritedModel<_ThemeAspect> {
ThemeData get theme => themeFrom(_primaryColor, _brightness); ThemeData get theme => themeFrom(_primaryColor, _brightness);
@override @override
bool updateShouldNotify(_ThemeModel oldWidget) { bool updateShouldNotify(_UserPreferencesModel oldWidget) {
return _brightness != oldWidget._brightness || return _brightness != oldWidget._brightness ||
dynamicColorState != oldWidget.dynamicColorState || dynamicColorState != oldWidget.dynamicColorState ||
evSourceType != oldWidget.evSourceType ||
locale != oldWidget.locale ||
_primaryColor != oldWidget._primaryColor || _primaryColor != oldWidget._primaryColor ||
stopType != oldWidget.stopType ||
themeType != oldWidget.themeType; themeType != oldWidget.themeType;
} }
@override @override
bool updateShouldNotifyDependent( bool updateShouldNotifyDependent(
_ThemeModel oldWidget, _UserPreferencesModel oldWidget,
Set<_ThemeAspect> dependencies, Set<_Aspect> dependencies,
) { ) {
return (dependencies.contains(_ThemeAspect.theme) && return (dependencies.contains(_Aspect.dynamicColorState) &&
(_brightness != oldWidget._brightness || _primaryColor != oldWidget._primaryColor)) ||
(dependencies.contains(_ThemeAspect.dynamicColorState) &&
dynamicColorState != oldWidget.dynamicColorState) || dynamicColorState != oldWidget.dynamicColorState) ||
(dependencies.contains(_ThemeAspect.themeType) && themeType != oldWidget.themeType); (dependencies.contains(_Aspect.evSourceType) && evSourceType != oldWidget.evSourceType) ||
(dependencies.contains(_Aspect.locale) && locale != oldWidget.locale) ||
(dependencies.contains(_Aspect.stopType) && stopType != oldWidget.stopType) ||
(dependencies.contains(_Aspect.theme) &&
(_brightness != oldWidget._brightness || _primaryColor != oldWidget._primaryColor)) ||
(dependencies.contains(_Aspect.themeType) && themeType != oldWidget.themeType);
} }
} }