From f17e4edb2d461cca3860f61d442fefd9d5b219c7 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 13 Aug 2023 22:19:14 +0200 Subject: [PATCH] Removed redundant methods from `ServicesProvider` --- lib/application.dart | 2 ++ lib/providers/equipment_profile_provider.dart | 14 +++++---- lib/providers/services_provider.dart | 30 +------------------ lib/providers/user_preferences_provider.dart | 24 +++++++-------- lib/screens/metering/flow_metering.dart | 14 ++++----- lib/screens/metering/screen_metering.dart | 2 +- .../widget_list_tile_report_issue.dart | 2 +- .../widget_list_tile_source_code.dart | 2 +- .../widget_list_tile_write_email.dart | 2 +- .../widget_dialog_calibration.dart | 2 +- lib/screens/settings/flow_settings.dart | 8 ++--- 11 files changed, 39 insertions(+), 63 deletions(-) diff --git a/lib/application.dart b/lib/application.dart index 6ec0dbb..5c801b1 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -90,6 +90,8 @@ class Application extends StatelessWidget { } else if (snapshot.error != null) { return Center(child: Text(snapshot.error!.toString())); } + + // TODO(@vodemn): maybe user splashscreen instead return const SizedBox(); }, ); diff --git a/lib/providers/equipment_profile_provider.dart b/lib/providers/equipment_profile_provider.dart index 6c36faf..c0294fa 100644 --- a/lib/providers/equipment_profile_provider.dart +++ b/lib/providers/equipment_profile_provider.dart @@ -33,7 +33,7 @@ class EquipmentProfileProviderState extends State { EquipmentProfile get _selectedProfile => _customProfiles.firstWhere( (e) => e.id == _selectedId, orElse: () { - ServicesProvider.userPreferencesServiceOf(context).selectedEquipmentProfileId = + ServicesProvider.of(context).userPreferencesService.selectedEquipmentProfileId = _defaultProfile.id; return _defaultProfile; }, @@ -42,8 +42,8 @@ class EquipmentProfileProviderState extends State { @override void initState() { super.initState(); - _selectedId = ServicesProvider.userPreferencesServiceOf(context).selectedEquipmentProfileId; - _customProfiles = ServicesProvider.userPreferencesServiceOf(context).equipmentProfiles; + _selectedId = ServicesProvider.of(context).userPreferencesService.selectedEquipmentProfileId; + _customProfiles = ServicesProvider.of(context).userPreferencesService.equipmentProfiles; } @override @@ -59,7 +59,7 @@ class EquipmentProfileProviderState extends State { setState(() { _selectedId = data.id; }); - ServicesProvider.userPreferencesServiceOf(context).selectedEquipmentProfileId = + ServicesProvider.of(context).userPreferencesService.selectedEquipmentProfileId = _selectedProfile.id; } @@ -92,7 +92,7 @@ class EquipmentProfileProviderState extends State { } void _refreshSavedProfiles() { - ServicesProvider.userPreferencesServiceOf(context).equipmentProfiles = _customProfiles; + ServicesProvider.of(context).userPreferencesService.equipmentProfiles = _customProfiles; setState(() {}); } } @@ -132,6 +132,8 @@ class EquipmentProfiles extends InheritedModel { @override bool updateShouldNotifyDependent( - EquipmentProfiles oldWidget, Set dependencies,) => + EquipmentProfiles oldWidget, + Set dependencies, + ) => false; } diff --git a/lib/providers/services_provider.dart b/lib/providers/services_provider.dart index c394482..c2c548f 100644 --- a/lib/providers/services_provider.dart +++ b/lib/providers/services_provider.dart @@ -27,35 +27,7 @@ class ServicesProvider extends InheritedWidget { required super.child, }); - static CaffeineService caffeineServiceOf(BuildContext context) { - return ServicesProvider._of(context).caffeineService; - } - - static Environment environmentOf(BuildContext context) { - return ServicesProvider._of(context).environment; - } - - static HapticsService hapticsServiceOf(BuildContext context) { - return ServicesProvider._of(context).hapticsService; - } - - static LightSensorService lightSensorServiceOf(BuildContext context) { - return ServicesProvider._of(context).lightSensorService; - } - - static PermissionsService permissionsServiceOf(BuildContext context) { - return ServicesProvider._of(context).permissionsService; - } - - static UserPreferencesService userPreferencesServiceOf(BuildContext context) { - return ServicesProvider._of(context).userPreferencesService; - } - - static VolumeEventsService volumeEventsServiceOf(BuildContext context) { - return ServicesProvider._of(context).volumeEventsService; - } - - static ServicesProvider _of(BuildContext context) { + static ServicesProvider of(BuildContext context) { return context.findAncestorWidgetOfExactType()!; } diff --git a/lib/providers/user_preferences_provider.dart b/lib/providers/user_preferences_provider.dart index 41fd4bf..be8771d 100644 --- a/lib/providers/user_preferences_provider.dart +++ b/lib/providers/user_preferences_provider.dart @@ -71,12 +71,12 @@ class UserPreferencesProvider extends StatefulWidget { class _UserPreferencesProviderState extends State with WidgetsBindingObserver { UserPreferencesService get userPreferencesService => - ServicesProvider.userPreferencesServiceOf(context); + ServicesProvider.of(context).userPreferencesService; late bool dynamicColor = userPreferencesService.dynamicColor; late EvSourceType evSourceType; late final MeteringScreenLayoutConfig meteringScreenLayout = - ServicesProvider.userPreferencesServiceOf(context).meteringScreenLayout; + ServicesProvider.of(context).userPreferencesService.meteringScreenLayout; late Color primaryColor = userPreferencesService.primaryColor; late StopType stopType = userPreferencesService.stopType; late SupportedLocale locale = userPreferencesService.locale; @@ -85,9 +85,9 @@ class _UserPreferencesProviderState extends State @override void initState() { super.initState(); - evSourceType = ServicesProvider.userPreferencesServiceOf(context).evSourceType; + evSourceType = ServicesProvider.of(context).userPreferencesService.evSourceType; evSourceType = evSourceType == EvSourceType.sensor && - !ServicesProvider.environmentOf(context).hasLightSensor + !ServicesProvider.of(context).environment.hasLightSensor ? EvSourceType.camera : evSourceType; WidgetsBinding.instance.addObserver(this); @@ -145,11 +145,11 @@ class _UserPreferencesProviderState extends State setState(() { dynamicColor = enable; }); - ServicesProvider.userPreferencesServiceOf(context).dynamicColor = enable; + ServicesProvider.of(context).userPreferencesService.dynamicColor = enable; } void toggleEvSourceType() { - if (!ServicesProvider.environmentOf(context).hasLightSensor) { + if (!ServicesProvider.of(context).environment.hasLightSensor) { return; } setState(() { @@ -160,7 +160,7 @@ class _UserPreferencesProviderState extends State evSourceType = EvSourceType.camera; } }); - ServicesProvider.userPreferencesServiceOf(context).evSourceType = evSourceType; + ServicesProvider.of(context).userPreferencesService.evSourceType = evSourceType; } void setLocale(SupportedLocale locale) { @@ -168,7 +168,7 @@ class _UserPreferencesProviderState extends State setState(() { this.locale = locale; }); - ServicesProvider.userPreferencesServiceOf(context).locale = locale; + ServicesProvider.of(context).userPreferencesService.locale = locale; }); } @@ -182,28 +182,28 @@ class _UserPreferencesProviderState extends State ); }); }); - ServicesProvider.userPreferencesServiceOf(context).meteringScreenLayout = meteringScreenLayout; + ServicesProvider.of(context).userPreferencesService.meteringScreenLayout = meteringScreenLayout; } void setPrimaryColor(Color primaryColor) { setState(() { this.primaryColor = primaryColor; }); - ServicesProvider.userPreferencesServiceOf(context).primaryColor = primaryColor; + ServicesProvider.of(context).userPreferencesService.primaryColor = primaryColor; } void setStopType(StopType stopType) { setState(() { this.stopType = stopType; }); - ServicesProvider.userPreferencesServiceOf(context).stopType = stopType; + ServicesProvider.of(context).userPreferencesService.stopType = stopType; } void setThemeType(ThemeType themeType) { setState(() { this.themeType = themeType; }); - ServicesProvider.userPreferencesServiceOf(context).themeType = themeType; + ServicesProvider.of(context).userPreferencesService.themeType = themeType; } Brightness get _themeBrightness { diff --git a/lib/screens/metering/flow_metering.dart b/lib/screens/metering/flow_metering.dart index b72b6db..cca5675 100644 --- a/lib/screens/metering/flow_metering.dart +++ b/lib/screens/metering/flow_metering.dart @@ -19,12 +19,12 @@ class _MeteringFlowState extends State { Widget build(BuildContext context) { return MeteringInteractorProvider( data: MeteringInteractor( - ServicesProvider.userPreferencesServiceOf(context), - ServicesProvider.caffeineServiceOf(context), - ServicesProvider.hapticsServiceOf(context), - ServicesProvider.permissionsServiceOf(context), - ServicesProvider.lightSensorServiceOf(context), - ServicesProvider.volumeEventsServiceOf(context), + ServicesProvider.of(context).userPreferencesService, + ServicesProvider.of(context).caffeineService, + ServicesProvider.of(context).hapticsService, + ServicesProvider.of(context).permissionsService, + ServicesProvider.of(context).lightSensorService, + ServicesProvider.of(context).volumeEventsService, )..initialize(), child: MultiBlocProvider( providers: [ @@ -32,7 +32,7 @@ class _MeteringFlowState extends State { BlocProvider( create: (context) => MeteringBloc( MeteringInteractorProvider.of(context), - VolumeKeysNotifier(ServicesProvider.volumeEventsServiceOf(context)), + VolumeKeysNotifier(ServicesProvider.of(context).volumeEventsService), context.read(), ), ), diff --git a/lib/screens/metering/screen_metering.dart b/lib/screens/metering/screen_metering.dart index aefc2b4..ba4dbea 100644 --- a/lib/screens/metering/screen_metering.dart +++ b/lib/screens/metering/screen_metering.dart @@ -47,7 +47,7 @@ class MeteringScreen extends StatelessWidget { builder: (context, state) => MeteringBottomControlsProvider( ev: state is MeteringDataState ? state.ev : null, isMetering: state.isMetering, - onSwitchEvSourceType: ServicesProvider.environmentOf(context).hasLightSensor + onSwitchEvSourceType: ServicesProvider.of(context).environment.hasLightSensor ? UserPreferencesProvider.of(context).toggleEvSourceType : null, onMeasure: () => context.read().add(const MeasureEvent()), diff --git a/lib/screens/settings/components/about/components/report_issue/widget_list_tile_report_issue.dart b/lib/screens/settings/components/about/components/report_issue/widget_list_tile_report_issue.dart index 8500f7d..72bc1b5 100644 --- a/lib/screens/settings/components/about/components/report_issue/widget_list_tile_report_issue.dart +++ b/lib/screens/settings/components/about/components/report_issue/widget_list_tile_report_issue.dart @@ -13,7 +13,7 @@ class ReportIssueListTile extends StatelessWidget { title: Text(S.of(context).reportIssue), onTap: () { launchUrl( - Uri.parse(ServicesProvider.environmentOf(context).issuesReportUrl), + Uri.parse(ServicesProvider.of(context).environment.issuesReportUrl), mode: LaunchMode.externalApplication, ); }, diff --git a/lib/screens/settings/components/about/components/source_code/widget_list_tile_source_code.dart b/lib/screens/settings/components/about/components/source_code/widget_list_tile_source_code.dart index 6d3c546..4327332 100644 --- a/lib/screens/settings/components/about/components/source_code/widget_list_tile_source_code.dart +++ b/lib/screens/settings/components/about/components/source_code/widget_list_tile_source_code.dart @@ -13,7 +13,7 @@ class SourceCodeListTile extends StatelessWidget { title: Text(S.of(context).sourceCode), onTap: () { launchUrl( - Uri.parse(ServicesProvider.environmentOf(context).sourceCodeUrl), + Uri.parse(ServicesProvider.of(context).environment.sourceCodeUrl), mode: LaunchMode.externalApplication, ); }, diff --git a/lib/screens/settings/components/about/components/write_email/widget_list_tile_write_email.dart b/lib/screens/settings/components/about/components/write_email/widget_list_tile_write_email.dart index a5cc9eb..b0a4391 100644 --- a/lib/screens/settings/components/about/components/write_email/widget_list_tile_write_email.dart +++ b/lib/screens/settings/components/about/components/write_email/widget_list_tile_write_email.dart @@ -13,7 +13,7 @@ class WriteEmailListTile extends StatelessWidget { leading: const Icon(Icons.email), title: Text(S.of(context).writeEmail), onTap: () { - final email = ServicesProvider.environmentOf(context).contactEmail; + final email = ServicesProvider.of(context).environment.contactEmail; final mailToUrl = Uri.parse('mailto:$email?subject=M3 Lightmeter'); canLaunchUrl(mailToUrl).then((canLaunch) { if (canLaunch) { diff --git a/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart b/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart index be29f79..69a4f00 100644 --- a/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart +++ b/lib/screens/settings/components/metering/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart @@ -14,7 +14,7 @@ class CalibrationDialog extends StatelessWidget { @override Widget build(BuildContext context) { - final bool hasLightSensor = ServicesProvider.environmentOf(context).hasLightSensor; + final bool hasLightSensor = ServicesProvider.of(context).environment.hasLightSensor; return AlertDialog( icon: const Icon(Icons.settings_brightness), titlePadding: Dimens.dialogIconTitlePadding, diff --git a/lib/screens/settings/flow_settings.dart b/lib/screens/settings/flow_settings.dart index 04ec09d..f3c8156 100644 --- a/lib/screens/settings/flow_settings.dart +++ b/lib/screens/settings/flow_settings.dart @@ -10,10 +10,10 @@ class SettingsFlow extends StatelessWidget { Widget build(BuildContext context) { return SettingsInteractorProvider( data: SettingsInteractor( - ServicesProvider.userPreferencesServiceOf(context), - ServicesProvider.caffeineServiceOf(context), - ServicesProvider.hapticsServiceOf(context), - ServicesProvider.volumeEventsServiceOf(context), + ServicesProvider.of(context).userPreferencesService, + ServicesProvider.of(context).caffeineService, + ServicesProvider.of(context).hapticsService, + ServicesProvider.of(context).volumeEventsService, ), child: const SettingsScreen(), );