From 785ac2973c5b080a9d9119c173ad5e60875eb62a Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 18 May 2023 20:20:15 +0200 Subject: [PATCH] wip --- lib/application.dart | 99 +++++-------------- lib/data/permissions_service.dart | 2 + lib/providers.dart | 72 ++++++++++++++ lib/providers/equipment_profile_provider.dart | 11 +-- lib/providers/ev_source_type_provider.dart | 9 +- .../metering_screen_layout_provider.dart | 5 +- lib/providers/supported_locale_provider.dart | 5 +- lib/providers/theme_provider.dart | 4 +- lib/screens/metering/flow_metering.dart | 9 +- lib/screens/metering/screen_metering.dart | 2 +- .../widget_list_tile_report_issue.dart | 3 +- .../widget_list_tile_source_code.dart | 3 +- .../widget_list_tile_write_email.dart | 3 +- .../widget_dialog_calibration.dart | 2 +- .../widget_list_tile_calibration.dart | 1 - lib/screens/settings/flow_settings.dart | 8 +- 16 files changed, 126 insertions(+), 112 deletions(-) create mode 100644 lib/providers.dart diff --git a/lib/application.dart b/lib/application.dart index 82f4a8f..3880a08 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -1,27 +1,13 @@ -import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; -import 'package:lightmeter/data/caffeine_service.dart'; -import 'package:lightmeter/data/haptics_service.dart'; -import 'package:lightmeter/data/light_sensor_service.dart'; import 'package:lightmeter/data/models/supported_locale.dart'; -import 'package:lightmeter/data/permissions_service.dart'; -import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/environment.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/providers/equipment_profile_provider.dart'; -import 'package:lightmeter/providers/ev_source_type_provider.dart'; -import 'package:lightmeter/providers/metering_screen_layout_provider.dart'; -import 'package:lightmeter/providers/stop_type_provider.dart'; -import 'package:lightmeter/providers/supported_locale_provider.dart'; -import 'package:lightmeter/providers/theme_provider.dart'; +import 'package:lightmeter/providers.dart'; import 'package:lightmeter/screens/metering/flow_metering.dart'; import 'package:lightmeter/screens/settings/flow_settings.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; -import 'package:provider/provider.dart'; -import 'package:shared_preferences/shared_preferences.dart'; class Application extends StatelessWidget { final Environment env; @@ -30,67 +16,32 @@ class Application extends StatelessWidget { @override Widget build(BuildContext context) { - return FutureBuilder( - future: Future.wait([ - SharedPreferences.getInstance(), - if (Platform.isAndroid) const LightSensorService().hasSensor() else Future.value(false), - ]), - builder: (_, snapshot) { - if (snapshot.data != null) { - return MultiProvider( - providers: [ - Provider.value(value: env.copyWith(hasLightSensor: snapshot.data![1] as bool)), - Provider( - create: (_) => UserPreferencesService(snapshot.data![0] as SharedPreferences), - ), - Provider(create: (_) => const CaffeineService()), - Provider(create: (_) => const HapticsService()), - Provider(create: (_) => PermissionsService()), - Provider(create: (_) => const LightSensorService()), - ], - child: MeteringScreenLayoutProvider( - child: StopTypeProvider( - child: EquipmentProfileProvider( - child: EvSourceTypeProvider( - child: SupportedLocaleProvider( - child: ThemeProvider( - child: Builder( - builder: (context) => _AnnotatedRegionWrapper( - child: MaterialApp( - theme: context.listen(), - locale: Locale(context.listen().intlName), - localizationsDelegates: const [ - S.delegate, - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ], - supportedLocales: S.delegate.supportedLocales, - builder: (context, child) => MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), - child: child!, - ), - initialRoute: "metering", - routes: { - "metering": (context) => const MeteringFlow(), - "settings": (context) => const SettingsFlow(), - }, - ), - ), - ), - ), - ), - ), + return LightmeterProviders( + env: env, + builder: (context, ready) => ready + ? _AnnotatedRegionWrapper( + child: MaterialApp( + theme: context.listen(), + locale: Locale(context.listen().intlName), + localizationsDelegates: const [ + S.delegate, + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: S.delegate.supportedLocales, + builder: (context, child) => MediaQuery( + data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), + child: child!, ), + initialRoute: "metering", + routes: { + "metering": (context) => const MeteringFlow(), + "settings": (context) => const SettingsFlow(), + }, ), - ), - ); - } else if (snapshot.error != null) { - return Center(child: Text(snapshot.error!.toString())); - } else { - return const SizedBox.shrink(); - } - }, + ) + : const SizedBox(), ); } } diff --git a/lib/data/permissions_service.dart b/lib/data/permissions_service.dart index fe731b4..d01e022 100644 --- a/lib/data/permissions_service.dart +++ b/lib/data/permissions_service.dart @@ -1,6 +1,8 @@ import 'package:permission_handler/permission_handler.dart'; class PermissionsService { + + const PermissionsService(); Future checkCameraPermission() async => Permission.camera.status; Future requestCameraPermission() async => Permission.camera.request(); diff --git a/lib/providers.dart b/lib/providers.dart new file mode 100644 index 0000000..0ba7c39 --- /dev/null +++ b/lib/providers.dart @@ -0,0 +1,72 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:lightmeter/data/caffeine_service.dart'; +import 'package:lightmeter/data/haptics_service.dart'; +import 'package:lightmeter/data/light_sensor_service.dart'; +import 'package:lightmeter/data/permissions_service.dart'; +import 'package:lightmeter/data/shared_prefs_service.dart'; +import 'package:lightmeter/environment.dart'; +import 'package:lightmeter/providers/equipment_profile_provider.dart'; +import 'package:lightmeter/providers/ev_source_type_provider.dart'; +import 'package:lightmeter/providers/metering_screen_layout_provider.dart'; +import 'package:lightmeter/providers/stop_type_provider.dart'; +import 'package:lightmeter/providers/supported_locale_provider.dart'; +import 'package:lightmeter/providers/theme_provider.dart'; +import 'package:lightmeter/utils/inherited_generics.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class LightmeterProviders extends StatelessWidget { + final Environment env; + final Widget Function(BuildContext context, bool ready) builder; + + const LightmeterProviders({required this.env, required this.builder, super.key}); + + @override + Widget build(BuildContext context) { + return FutureBuilder( + future: Future.wait([ + SharedPreferences.getInstance(), + if (Platform.isAndroid) const LightSensorService().hasSensor() else Future.value(false), + ]), + builder: (_, snapshot) { + if (snapshot.data != null) { + return InheritedWidgetBase( + data: env.copyWith(hasLightSensor: snapshot.data![1] as bool), + child: InheritedWidgetBase( + data: UserPreferencesService(snapshot.data![0] as SharedPreferences), + child: InheritedWidgetBase( + data: const LightSensorService(), + child: InheritedWidgetBase( + data: const CaffeineService(), + child: InheritedWidgetBase( + data: const HapticsService(), + child: InheritedWidgetBase( + data: const PermissionsService(), + child: MeteringScreenLayoutProvider( + child: StopTypeProvider( + child: EquipmentProfileProvider( + child: EvSourceTypeProvider( + child: SupportedLocaleProvider( + child: ThemeProvider( + child: builder(context, true), + ), + ), + ), + ), + ), + ), + ), + ), + ), + ), + ), + ); + } else if (snapshot.error != null) { + return Center(child: Text(snapshot.error!.toString())); + } + return builder(context, false); + }, + ); + } +} diff --git a/lib/providers/equipment_profile_provider.dart b/lib/providers/equipment_profile_provider.dart index 1c986fa..c5ef82d 100644 --- a/lib/providers/equipment_profile_provider.dart +++ b/lib/providers/equipment_profile_provider.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; -import 'package:provider/provider.dart'; import 'package:uuid/uuid.dart'; typedef EquipmentProfiles = List; @@ -37,7 +36,7 @@ class EquipmentProfileProviderState extends State { EquipmentProfileData get _selectedProfile => _customProfiles.firstWhere( (e) => e.id == _selectedId, orElse: () { - context.read().selectedEquipmentProfileId = _defaultProfile.id; + context.get().selectedEquipmentProfileId = _defaultProfile.id; return _defaultProfile; }, ); @@ -45,8 +44,8 @@ class EquipmentProfileProviderState extends State { @override void initState() { super.initState(); - _selectedId = context.read().selectedEquipmentProfileId; - _customProfiles = context.read().equipmentProfiles; + _selectedId = context.get().selectedEquipmentProfileId; + _customProfiles = context.get().equipmentProfiles; } @override @@ -64,7 +63,7 @@ class EquipmentProfileProviderState extends State { setState(() { _selectedId = data.id; }); - context.read().selectedEquipmentProfileId = _selectedProfile.id; + context.get().selectedEquipmentProfileId = _selectedProfile.id; } /// Creates a default equipment profile @@ -96,7 +95,7 @@ class EquipmentProfileProviderState extends State { } void _refreshSavedProfiles() { - context.read().equipmentProfiles = _customProfiles; + context.get().equipmentProfiles = _customProfiles; setState(() {}); } } diff --git a/lib/providers/ev_source_type_provider.dart b/lib/providers/ev_source_type_provider.dart index afbc759..aed873b 100644 --- a/lib/providers/ev_source_type_provider.dart +++ b/lib/providers/ev_source_type_provider.dart @@ -3,7 +3,6 @@ import 'package:lightmeter/data/models/ev_source_type.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/environment.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; -import 'package:provider/provider.dart'; class EvSourceTypeProvider extends StatefulWidget { final Widget child; @@ -24,9 +23,9 @@ class EvSourceTypeProviderState extends State { @override void initState() { super.initState(); - final evSourceType = context.read().evSourceType; + final evSourceType = context.get().evSourceType; valueListenable = ValueNotifier( - evSourceType == EvSourceType.sensor && !context.read().hasLightSensor + evSourceType == EvSourceType.sensor && !context.get().hasLightSensor ? EvSourceType.camera : evSourceType, ); @@ -53,12 +52,12 @@ class EvSourceTypeProviderState extends State { void toggleType() { switch (valueListenable.value) { case EvSourceType.camera: - if (context.read().hasLightSensor) { + if (context.get().hasLightSensor) { valueListenable.value = EvSourceType.sensor; } case EvSourceType.sensor: valueListenable.value = EvSourceType.camera; } - context.read().evSourceType = valueListenable.value; + context.get().evSourceType = valueListenable.value; } } diff --git a/lib/providers/metering_screen_layout_provider.dart b/lib/providers/metering_screen_layout_provider.dart index 3c0cc14..fe3d80c 100644 --- a/lib/providers/metering_screen_layout_provider.dart +++ b/lib/providers/metering_screen_layout_provider.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; -import 'package:provider/provider.dart'; typedef MeteringScreenLayout = InheritedModelBase; @@ -21,7 +20,7 @@ class MeteringScreenLayoutProvider extends StatefulWidget { class MeteringScreenLayoutProviderState extends State { late final MeteringScreenLayoutConfig _config = - context.read().meteringScreenLayout; + context.get().meteringScreenLayout; @override Widget build(BuildContext context) { @@ -41,6 +40,6 @@ class MeteringScreenLayoutProviderState extends State().meteringScreenLayout = _config; + context.get().meteringScreenLayout = _config; } } diff --git a/lib/providers/supported_locale_provider.dart b/lib/providers/supported_locale_provider.dart index 3a6aca9..caa7ced 100644 --- a/lib/providers/supported_locale_provider.dart +++ b/lib/providers/supported_locale_provider.dart @@ -3,7 +3,6 @@ import 'package:lightmeter/data/models/supported_locale.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; -import 'package:provider/provider.dart'; class SupportedLocaleProvider extends StatefulWidget { final Widget child; @@ -24,7 +23,7 @@ class SupportedLocaleProviderState extends State { @override void initState() { super.initState(); - valueListenable = ValueNotifier(context.read().locale); + valueListenable = ValueNotifier(context.get().locale); } @override @@ -48,7 +47,7 @@ class SupportedLocaleProviderState extends State { void setLocale(SupportedLocale locale) { S.load(Locale(locale.intlName)).then((value) { valueListenable.value = locale; - context.read().locale = locale; + context.get().locale = locale; }); } } diff --git a/lib/providers/theme_provider.dart b/lib/providers/theme_provider.dart index f4a8829..9773df1 100644 --- a/lib/providers/theme_provider.dart +++ b/lib/providers/theme_provider.dart @@ -8,8 +8,6 @@ import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:material_color_utilities/material_color_utilities.dart'; -import 'package:provider/provider.dart'; - class ThemeProvider extends StatefulWidget { final Widget child; @@ -46,7 +44,7 @@ class ThemeProvider extends StatefulWidget { } class ThemeProviderState extends State with WidgetsBindingObserver { - UserPreferencesService get _prefs => context.read(); + UserPreferencesService get _prefs => context.get(); late final _themeTypeNotifier = ValueNotifier(_prefs.themeType); late final _dynamicColorNotifier = ValueNotifier(_prefs.dynamicColor); diff --git a/lib/screens/metering/flow_metering.dart b/lib/screens/metering/flow_metering.dart index b6364c1..ae35f2c 100644 --- a/lib/screens/metering/flow_metering.dart +++ b/lib/screens/metering/flow_metering.dart @@ -12,7 +12,6 @@ import 'package:lightmeter/screens/metering/communication/bloc_communication_met import 'package:lightmeter/screens/metering/screen_metering.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; -import 'package:provider/provider.dart'; class MeteringFlow extends StatefulWidget { const MeteringFlow({super.key}); @@ -26,10 +25,10 @@ class _MeteringFlowState extends State { Widget build(BuildContext context) { return Provider( create: (context) => MeteringInteractor( - context.read(), - context.read(), - context.read(), - context.read(), + context.get(), + context.get(), + context.get(), + context.get(), context.read(), ), child: MultiBlocProvider( diff --git a/lib/screens/metering/screen_metering.dart b/lib/screens/metering/screen_metering.dart index 5058897..29c7813 100644 --- a/lib/screens/metering/screen_metering.dart +++ b/lib/screens/metering/screen_metering.dart @@ -48,7 +48,7 @@ class MeteringScreen extends StatelessWidget { isMetering: state is LoadingState || state is MeteringDataState && state.continuousMetering, hasError: state is MeteringDataState && state.hasError, - onSwitchEvSourceType: context.read().hasLightSensor + onSwitchEvSourceType: context.get().hasLightSensor ? EvSourceTypeProvider.of(context).toggleType : 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 f7e4e4b..966c415 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 @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/environment.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; class ReportIssueListTile extends StatelessWidget { @@ -14,7 +13,7 @@ class ReportIssueListTile extends StatelessWidget { title: Text(S.of(context).reportIssue), onTap: () { launchUrl( - Uri.parse(context.read().issuesReportUrl), + Uri.parse(context.get().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 39199a9..b213b97 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 @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/environment.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; class SourceCodeListTile extends StatelessWidget { @@ -14,7 +13,7 @@ class SourceCodeListTile extends StatelessWidget { title: Text(S.of(context).sourceCode), onTap: () { launchUrl( - Uri.parse(context.read().sourceCodeUrl), + Uri.parse(context.get().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 1641e9c..ca02fcc 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 @@ -2,7 +2,6 @@ import 'package:clipboard/clipboard.dart'; import 'package:flutter/material.dart'; import 'package:lightmeter/environment.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; class WriteEmailListTile extends StatelessWidget { @@ -14,7 +13,7 @@ class WriteEmailListTile extends StatelessWidget { leading: const Icon(Icons.email), title: Text(S.of(context).writeEmail), onTap: () { - final email = context.read().contactEmail; + final email = context.get().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 7a4b5fa..f914a3f 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 = context.read().hasLightSensor; + final bool hasLightSensor = context.get().hasLightSensor; return AlertDialog( icon: const Icon(Icons.settings_brightness), titlePadding: Dimens.dialogIconTitlePadding, 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 7781371..64ebd37 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 @@ -2,7 +2,6 @@ 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:provider/provider.dart'; class CalibrationListTile extends StatelessWidget { const CalibrationListTile({super.key}); diff --git a/lib/screens/settings/flow_settings.dart b/lib/screens/settings/flow_settings.dart index 9e757b4..61a4835 100644 --- a/lib/screens/settings/flow_settings.dart +++ b/lib/screens/settings/flow_settings.dart @@ -4,7 +4,7 @@ import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/screen_settings.dart'; -import 'package:provider/provider.dart'; +import 'package:lightmeter/utils/inherited_generics.dart'; class SettingsFlow extends StatelessWidget { const SettingsFlow({super.key}); @@ -13,9 +13,9 @@ class SettingsFlow extends StatelessWidget { Widget build(BuildContext context) { return Provider( create: (context) => SettingsInteractor( - context.read(), - context.read(), - context.read(), + context.get(), + context.get(), + context.get(), ), child: const SettingsScreen(), );