From 28c72d492f22259d37b3719494f31e35e1acb53a Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 13 Aug 2023 21:59:37 +0200 Subject: [PATCH] Removed Inherited Generics --- lib/providers/equipment_profile_provider.dart | 52 ++++++++++--- .../provider_container_camera.dart | 5 +- .../provider_container_light_sensor.dart | 5 +- .../widget_container_readings.dart | 13 ++-- lib/screens/metering/flow_metering.dart | 45 ++++++++---- lib/screens/metering/screen_metering.dart | 7 +- .../utils/listsner_equipment_profiles.dart | 30 ++++++++ .../caffeine/provider_list_tile_caffeine.dart | 5 +- .../haptics/provider_list_tile_haptics.dart | 5 +- .../provider_list_tile_volume_actions.dart | 5 +- .../provider_dialog_calibration.dart | 5 +- .../widget_list_tile_calibration.dart | 7 +- .../screen_equipment_profile.dart | 19 ++--- lib/screens/settings/flow_settings.dart | 20 ++++- lib/screens/settings/screen_settings.dart | 7 +- lib/utils/inherited_generics.dart | 73 ------------------- 16 files changed, 156 insertions(+), 147 deletions(-) create mode 100644 lib/screens/metering/utils/listsner_equipment_profiles.dart delete mode 100644 lib/utils/inherited_generics.dart diff --git a/lib/providers/equipment_profile_provider.dart b/lib/providers/equipment_profile_provider.dart index 3a533ae..6c36faf 100644 --- a/lib/providers/equipment_profile_provider.dart +++ b/lib/providers/equipment_profile_provider.dart @@ -1,12 +1,9 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/providers/services_provider.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:uuid/uuid.dart'; // TODO(@vodemn): This will be removed in #89 -typedef EquipmentProfiles = List; - class EquipmentProfileProvider extends StatefulWidget { final Widget child; @@ -51,12 +48,10 @@ class EquipmentProfileProviderState extends State { @override Widget build(BuildContext context) { - return InheritedWidgetBase>( - data: [_defaultProfile] + _customProfiles, - child: InheritedWidgetBase( - data: _selectedProfile, - child: widget.child, - ), + return EquipmentProfiles( + profiles: [_defaultProfile] + _customProfiles, + selected: _selectedProfile, + child: widget.child, ); } @@ -101,3 +96,42 @@ class EquipmentProfileProviderState extends State { setState(() {}); } } + +// Copied from #89 +enum EquipmentProfilesAspect { list, selected } + +class EquipmentProfiles extends InheritedModel { + const EquipmentProfiles({ + super.key, + required this.profiles, + required this.selected, + required super.child, + }); + + final List profiles; + final EquipmentProfile selected; + + static List of(BuildContext context) { + return InheritedModel.inheritFrom( + context, + aspect: EquipmentProfilesAspect.list, + )! + .profiles; + } + + static EquipmentProfile selectedOf(BuildContext context) { + return InheritedModel.inheritFrom( + context, + aspect: EquipmentProfilesAspect.selected, + )! + .selected; + } + + @override + bool updateShouldNotify(EquipmentProfiles oldWidget) => false; + + @override + bool updateShouldNotifyDependent( + EquipmentProfiles oldWidget, Set dependencies,) => + false; +} diff --git a/lib/screens/metering/components/camera_container/provider_container_camera.dart b/lib/screens/metering/components/camera_container/provider_container_camera.dart index 344f573..0e27700 100644 --- a/lib/screens/metering/components/camera_container/provider_container_camera.dart +++ b/lib/screens/metering/components/camera_container/provider_container_camera.dart @@ -2,12 +2,11 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:lightmeter/data/models/exposure_pair.dart'; import 'package:lightmeter/data/models/film.dart'; -import 'package:lightmeter/interactors/metering_interactor.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; import 'package:lightmeter/screens/metering/components/camera_container/bloc_container_camera.dart'; import 'package:lightmeter/screens/metering/components/camera_container/event_container_camera.dart'; import 'package:lightmeter/screens/metering/components/camera_container/widget_container_camera.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/metering/flow_metering.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; class CameraContainerProvider extends StatelessWidget { @@ -39,7 +38,7 @@ class CameraContainerProvider extends StatelessWidget { return BlocProvider( lazy: false, create: (context) => CameraContainerBloc( - context.get(), + MeteringInteractorProvider.of(context), context.read(), )..add(const RequestPermissionEvent()), child: CameraContainer( diff --git a/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart index c7423fc..aa27504 100644 --- a/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart @@ -2,11 +2,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:lightmeter/data/models/exposure_pair.dart'; import 'package:lightmeter/data/models/film.dart'; -import 'package:lightmeter/interactors/metering_interactor.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; import 'package:lightmeter/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart'; import 'package:lightmeter/screens/metering/components/light_sensor_container/widget_container_light_sensor.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/metering/flow_metering.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; class LightSensorContainerProvider extends StatelessWidget { @@ -38,7 +37,7 @@ class LightSensorContainerProvider extends StatelessWidget { return BlocProvider( lazy: false, create: (context) => LightSensorContainerBloc( - context.get(), + MeteringInteractorProvider.of(context), context.read(), ), child: LightSensorContainer( diff --git a/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart b/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart index efa6c30..d31380c 100644 --- a/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart +++ b/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart @@ -9,7 +9,6 @@ import 'package:lightmeter/providers/user_preferences_provider.dart'; import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/components/animated_dialog_picker/widget_picker_dialog_animated.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/components/reading_value_container/widget_container_reading_value.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; class ReadingsContainer extends StatelessWidget { @@ -77,7 +76,7 @@ class ReadingsContainer extends StatelessWidget { Expanded( child: _IsoValuePicker( selectedValue: iso, - values: context.listen().isoValues, + values: EquipmentProfiles.selectedOf(context).isoValues, onChanged: onIsoChanged, ), ), @@ -85,7 +84,7 @@ class ReadingsContainer extends StatelessWidget { Expanded( child: _NdValuePicker( selectedValue: nd, - values: context.listen().ndValues, + values: EquipmentProfiles.selectedOf(context).ndValues, onChanged: onNdChanged, ), ), @@ -108,16 +107,16 @@ class _EquipmentProfilePicker extends StatelessWidget { return AnimatedDialogPicker( icon: Icons.camera, title: S.of(context).equipmentProfile, - selectedValue: context.listen(), - values: context.listen(), + selectedValue: EquipmentProfiles.selectedOf(context), + values: EquipmentProfiles.of(context), itemTitleBuilder: (_, value) => Text(value.id.isEmpty ? S.of(context).none : value.name), onChanged: EquipmentProfileProvider.of(context).setProfile, closedChild: ReadingValueContainer.singleValue( value: ReadingValue( label: S.of(context).equipmentProfile, - value: context.listen().id.isEmpty + value: EquipmentProfiles.selectedOf(context).id.isEmpty ? S.of(context).none - : context.listen().name, + : EquipmentProfiles.selectedOf(context).name, ), ), ); diff --git a/lib/screens/metering/flow_metering.dart b/lib/screens/metering/flow_metering.dart index ca5874e..b72b6db 100644 --- a/lib/screens/metering/flow_metering.dart +++ b/lib/screens/metering/flow_metering.dart @@ -6,7 +6,6 @@ import 'package:lightmeter/screens/metering/bloc_metering.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; import 'package:lightmeter/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart'; import 'package:lightmeter/screens/metering/screen_metering.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; class MeteringFlow extends StatefulWidget { const MeteringFlow({super.key}); @@ -18,7 +17,7 @@ class MeteringFlow extends StatefulWidget { class _MeteringFlowState extends State { @override Widget build(BuildContext context) { - return InheritedWidgetBase( + return MeteringInteractorProvider( data: MeteringInteractor( ServicesProvider.userPreferencesServiceOf(context), ServicesProvider.caffeineServiceOf(context), @@ -27,22 +26,36 @@ class _MeteringFlowState extends State { ServicesProvider.lightSensorServiceOf(context), ServicesProvider.volumeEventsServiceOf(context), )..initialize(), - child: InheritedWidgetBase( - data: VolumeKeysNotifier(ServicesProvider.volumeEventsServiceOf(context)), - child: MultiBlocProvider( - providers: [ - BlocProvider(create: (_) => MeteringCommunicationBloc()), - BlocProvider( - create: (context) => MeteringBloc( - context.get(), - context.get(), - context.read(), - ), + child: MultiBlocProvider( + providers: [ + BlocProvider(create: (_) => MeteringCommunicationBloc()), + BlocProvider( + create: (context) => MeteringBloc( + MeteringInteractorProvider.of(context), + VolumeKeysNotifier(ServicesProvider.volumeEventsServiceOf(context)), + context.read(), ), - ], - child: const MeteringScreen(), - ), + ), + ], + child: const MeteringScreen(), ), ); } } + +class MeteringInteractorProvider extends InheritedWidget { + final MeteringInteractor data; + + const MeteringInteractorProvider({ + required this.data, + required super.child, + super.key, + }); + + static MeteringInteractor of(BuildContext context) { + return context.findAncestorWidgetOfExactType()!.data; + } + + @override + bool updateShouldNotify(MeteringInteractorProvider oldWidget) => false; +} diff --git a/lib/screens/metering/screen_metering.dart b/lib/screens/metering/screen_metering.dart index 7e30351..aefc2b4 100644 --- a/lib/screens/metering/screen_metering.dart +++ b/lib/screens/metering/screen_metering.dart @@ -6,6 +6,7 @@ import 'package:lightmeter/data/models/ev_source_type.dart'; import 'package:lightmeter/data/models/exposure_pair.dart'; import 'package:lightmeter/data/models/film.dart'; import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; +import 'package:lightmeter/providers/equipment_profile_provider.dart'; import 'package:lightmeter/providers/services_provider.dart'; import 'package:lightmeter/providers/user_preferences_provider.dart'; import 'package:lightmeter/screens/metering/bloc_metering.dart'; @@ -15,7 +16,7 @@ import 'package:lightmeter/screens/metering/components/light_sensor_container/pr import 'package:lightmeter/screens/metering/event_metering.dart'; import 'package:lightmeter/screens/metering/state_metering.dart'; import 'package:lightmeter/screens/metering/utils/listener_metering_layout_feature.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/metering/utils/listsner_equipment_profiles.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; class MeteringScreen extends StatelessWidget { @@ -72,7 +73,7 @@ class _InheritedListeners extends StatelessWidget { @override Widget build(BuildContext context) { - return InheritedWidgetListener( + return EquipmentProfileListener( onDidChangeDependencies: (value) { context.read().add(EquipmentProfileChangedEvent(value)); }, @@ -146,7 +147,7 @@ class _MeteringContainerBuidler extends StatelessWidget { final StopType stopType = UserPreferencesProvider.stopTypeOf(context); final int evSteps = (ev * (stopType.index + 1)).round(); - final EquipmentProfile equipmentProfile = context.listen(); + final EquipmentProfile equipmentProfile = EquipmentProfiles.selectedOf(context); final List apertureValues = equipmentProfile.apertureValues.whereStopType(stopType); final List shutterSpeedValues = diff --git a/lib/screens/metering/utils/listsner_equipment_profiles.dart b/lib/screens/metering/utils/listsner_equipment_profiles.dart new file mode 100644 index 0000000..ec604ce --- /dev/null +++ b/lib/screens/metering/utils/listsner_equipment_profiles.dart @@ -0,0 +1,30 @@ +import 'package:flutter/material.dart'; +import 'package:lightmeter/providers/equipment_profile_provider.dart'; +import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; + +class EquipmentProfileListener extends StatefulWidget { + final ValueChanged onDidChangeDependencies; + final Widget child; + + const EquipmentProfileListener({ + required this.onDidChangeDependencies, + required this.child, + super.key, + }); + + @override + State createState() => _EquipmentProfileListenerState(); +} + +class _EquipmentProfileListenerState extends State { + @override + void didChangeDependencies() { + super.didChangeDependencies(); + widget.onDidChangeDependencies(EquipmentProfiles.selectedOf(context)); + } + + @override + Widget build(BuildContext context) { + return widget.child; + } +} diff --git a/lib/screens/settings/components/general/components/caffeine/provider_list_tile_caffeine.dart b/lib/screens/settings/components/general/components/caffeine/provider_list_tile_caffeine.dart index 4509d20..8d9bcfb 100644 --- a/lib/screens/settings/components/general/components/caffeine/provider_list_tile_caffeine.dart +++ b/lib/screens/settings/components/general/components/caffeine/provider_list_tile_caffeine.dart @@ -1,10 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/components/general/components/caffeine/bloc_list_tile_caffeine.dart'; import 'package:lightmeter/screens/settings/components/general/components/caffeine/widget_list_tile_caffeine.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/settings/flow_settings.dart'; class CaffeineListTileProvider extends StatelessWidget { const CaffeineListTileProvider({super.key}); @@ -12,7 +11,7 @@ class CaffeineListTileProvider extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( - create: (context) => CaffeineListTileBloc(context.get()), + create: (context) => CaffeineListTileBloc(SettingsInteractorProvider.of(context)), child: const CaffeineListTile(), ); } diff --git a/lib/screens/settings/components/general/components/haptics/provider_list_tile_haptics.dart b/lib/screens/settings/components/general/components/haptics/provider_list_tile_haptics.dart index beb0462..34cdeab 100644 --- a/lib/screens/settings/components/general/components/haptics/provider_list_tile_haptics.dart +++ b/lib/screens/settings/components/general/components/haptics/provider_list_tile_haptics.dart @@ -1,10 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/components/general/components/haptics/bloc_list_tile_haptics.dart'; import 'package:lightmeter/screens/settings/components/general/components/haptics/widget_list_tile_haptics.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/settings/flow_settings.dart'; class HapticsListTileProvider extends StatelessWidget { const HapticsListTileProvider({super.key}); @@ -12,7 +11,7 @@ class HapticsListTileProvider extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( - create: (context) => HapticsListTileBloc(context.get()), + create: (context) => HapticsListTileBloc(SettingsInteractorProvider.of(context)), child: const HapticsListTile(), ); } diff --git a/lib/screens/settings/components/general/components/volume_actions/provider_list_tile_volume_actions.dart b/lib/screens/settings/components/general/components/volume_actions/provider_list_tile_volume_actions.dart index 790ad4f..f99505f 100644 --- a/lib/screens/settings/components/general/components/volume_actions/provider_list_tile_volume_actions.dart +++ b/lib/screens/settings/components/general/components/volume_actions/provider_list_tile_volume_actions.dart @@ -1,10 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/components/general/components/volume_actions/bloc_list_tile_volume_actions.dart'; import 'package:lightmeter/screens/settings/components/general/components/volume_actions/widget_list_tile_volume_actions.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/settings/flow_settings.dart'; class VolumeActionsListTileProvider extends StatelessWidget { const VolumeActionsListTileProvider({super.key}); @@ -12,7 +11,7 @@ class VolumeActionsListTileProvider extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( - create: (context) => VolumeActionsListTileBloc(context.get()), + create: (context) => VolumeActionsListTileBloc(SettingsInteractorProvider.of(context)), child: const VolumeActionsListTile(), ); } diff --git a/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/provider_dialog_calibration.dart b/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/provider_dialog_calibration.dart index a95ec1b..a26a6cf 100644 --- a/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/provider_dialog_calibration.dart +++ b/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/provider_dialog_calibration.dart @@ -1,10 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/components/metering/components/calibration/components/calibration_dialog/bloc_dialog_calibration.dart'; import 'package:lightmeter/screens/settings/components/metering/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/settings/flow_settings.dart'; class CalibrationDialogProvider extends StatelessWidget { const CalibrationDialogProvider({super.key}); @@ -12,7 +11,7 @@ class CalibrationDialogProvider extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( - create: (context) => CalibrationDialogBloc(context.get()), + create: (context) => CalibrationDialogBloc(SettingsInteractorProvider.of(context)), child: const CalibrationDialog(), ); } diff --git a/lib/screens/settings/components/metering/components/calibration/widget_list_tile_calibration.dart b/lib/screens/settings/components/metering/components/calibration/widget_list_tile_calibration.dart index e591e4b..61690c4 100644 --- a/lib/screens/settings/components/metering/components/calibration/widget_list_tile_calibration.dart +++ b/lib/screens/settings/components/metering/components/calibration/widget_list_tile_calibration.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/components/metering/components/calibration/components/calibration_dialog/provider_dialog_calibration.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:lightmeter/screens/settings/flow_settings.dart'; class CalibrationListTile extends StatelessWidget { const CalibrationListTile({super.key}); @@ -15,8 +14,8 @@ class CalibrationListTile extends StatelessWidget { onTap: () { showDialog( context: context, - builder: (_) => InheritedWidgetBase( - data: context.get(), + builder: (_) => SettingsInteractorProvider( + data: SettingsInteractorProvider.of(context), child: const CalibrationDialogProvider(), ), ); diff --git a/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart b/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart index 4872d79..3120b38 100644 --- a/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart +++ b/lib/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/screen_equipment_profile.dart @@ -1,11 +1,11 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/providers/equipment_profile_provider.dart'; + import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_container/widget_container_equipment_profile.dart'; import 'package:lightmeter/screens/settings/components/metering/components/equipment_profiles/components/equipment_profile_screen/components/equipment_profile_name_dialog/widget_dialog_equipment_profile_name.dart'; import 'package:lightmeter/screens/shared/sliver_screen/screen_sliver.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; class EquipmentProfilesScreen extends StatefulWidget { @@ -19,13 +19,12 @@ class _EquipmentProfilesScreenState extends State { static const maxProfiles = 5 + 1; // replace with a constant from iap late List> profileContainersKeys = []; - int get profilesCount => context.listen().length; + int get profilesCount => EquipmentProfiles.of(context).length; @override - void initState() { - super.initState(); - profileContainersKeys = context - .get() + void didChangeDependencies() { + super.didChangeDependencies(); + profileContainersKeys = EquipmentProfiles.of(context) .map((e) => GlobalKey(debugLabel: e.id)) .toList(); } @@ -58,14 +57,14 @@ class _EquipmentProfilesScreenState extends State { ), child: EquipmentProfileContainer( key: profileContainersKeys[index], - data: context.listen()[index], + data: EquipmentProfiles.of(context)[index], onExpand: () => _keepExpandedAt(index), onUpdate: (profileData) => _updateProfileAt(profileData, index), onDelete: () => _removeProfileAt(index), ), ) : const SizedBox.shrink(), - childCount: profileContainersKeys.length, + childCount: profilesCount, ), ), ], @@ -79,7 +78,6 @@ class _EquipmentProfilesScreenState extends State { ).then((value) { if (value != null) { EquipmentProfileProvider.of(context).addProfile(value); - profileContainersKeys.add(GlobalKey()); } }); } @@ -89,8 +87,7 @@ class _EquipmentProfilesScreenState extends State { } void _removeProfileAt(int index) { - EquipmentProfileProvider.of(context).deleteProfile(context.listen()[index]); - profileContainersKeys.removeAt(index); + EquipmentProfileProvider.of(context).deleteProfile(EquipmentProfiles.of(context)[index]); } void _keepExpandedAt(int index) { diff --git a/lib/screens/settings/flow_settings.dart b/lib/screens/settings/flow_settings.dart index eeeed10..04ec09d 100644 --- a/lib/screens/settings/flow_settings.dart +++ b/lib/screens/settings/flow_settings.dart @@ -2,14 +2,13 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/providers/services_provider.dart'; import 'package:lightmeter/screens/settings/screen_settings.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; class SettingsFlow extends StatelessWidget { const SettingsFlow({super.key}); @override Widget build(BuildContext context) { - return InheritedWidgetBase( + return SettingsInteractorProvider( data: SettingsInteractor( ServicesProvider.userPreferencesServiceOf(context), ServicesProvider.caffeineServiceOf(context), @@ -20,3 +19,20 @@ class SettingsFlow extends StatelessWidget { ); } } + +class SettingsInteractorProvider extends InheritedWidget { + final SettingsInteractor data; + + const SettingsInteractorProvider({ + required this.data, + required super.child, + super.key, + }); + + static SettingsInteractor of(BuildContext context) { + return context.findAncestorWidgetOfExactType()!.data; + } + + @override + bool updateShouldNotify(SettingsInteractorProvider oldWidget) => false; +} diff --git a/lib/screens/settings/screen_settings.dart b/lib/screens/settings/screen_settings.dart index 3c745bd..9332d3f 100644 --- a/lib/screens/settings/screen_settings.dart +++ b/lib/screens/settings/screen_settings.dart @@ -1,12 +1,11 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/components/about/widget_settings_section_about.dart'; import 'package:lightmeter/screens/settings/components/general/widget_settings_section_general.dart'; import 'package:lightmeter/screens/settings/components/metering/widget_settings_section_metering.dart'; import 'package:lightmeter/screens/settings/components/theme/widget_settings_section_theme.dart'; +import 'package:lightmeter/screens/settings/flow_settings.dart'; import 'package:lightmeter/screens/shared/sliver_screen/screen_sliver.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @@ -19,12 +18,12 @@ class _SettingsScreenState extends State { @override void didChangeDependencies() { super.didChangeDependencies(); - context.get().disableVolumeHandling(); + SettingsInteractorProvider.of(context).disableVolumeHandling(); } @override void deactivate() { - context.get().restoreVolumeHandling(); + SettingsInteractorProvider.of(context).restoreVolumeHandling(); super.deactivate(); } diff --git a/lib/utils/inherited_generics.dart b/lib/utils/inherited_generics.dart deleted file mode 100644 index 6378d2b..0000000 --- a/lib/utils/inherited_generics.dart +++ /dev/null @@ -1,73 +0,0 @@ -import 'package:flutter/widgets.dart'; - -/// Listening to multiple dependencies at the same time causes firing an event for all dependencies -/// even though some of them didn't change: -/// ```dart -/// @override -/// void didChangeDependencies() { -/// super.didChangeDependencies(); -/// _bloc.add(EquipmentProfileChangedEvent(EquipmentProfile.of(context))); -/// if (!MeteringScreenLayout.featureStatusOf(context, MeteringScreenLayoutFeature.filmPicker)) { -/// _bloc.add(const FilmChangedEvent(Film.other())); -/// } -/// } -/// ``` -/// To overcome this issue I've decided to create a generic listener, -/// that will listen to each dependency separately. -class InheritedWidgetListener extends StatefulWidget { - final ValueChanged onDidChangeDependencies; - final Widget child; - - const InheritedWidgetListener({ - required this.onDidChangeDependencies, - required this.child, - super.key, - }); - - @override - State> createState() => _InheritedWidgetListenerState(); -} - -class _InheritedWidgetListenerState extends State> { - @override - void didChangeDependencies() { - super.didChangeDependencies(); - widget.onDidChangeDependencies(context.listen()); - } - - @override - Widget build(BuildContext context) { - return widget.child; - } -} - -class InheritedWidgetBase extends InheritedWidget { - final T data; - - const InheritedWidgetBase({ - required this.data, - required super.child, - super.key, - }); - - static T of(BuildContext context, {bool listen = true}) { - if (listen) { - return context.dependOnInheritedWidgetOfExactType>()!.data; - } else { - return context.findAncestorWidgetOfExactType>()!.data; - } - } - - @override - bool updateShouldNotify(InheritedWidgetBase oldWidget) => true; -} - -extension InheritedWidgetBaseContext on BuildContext { - T get() { - return InheritedWidgetBase.of(this, listen: false); - } - - T listen() { - return InheritedWidgetBase.of(this); - } -}