This commit is contained in:
Vadim 2023-05-18 20:20:15 +02:00
parent 3bb2b7845b
commit 785ac2973c
16 changed files with 126 additions and 112 deletions

View file

@ -1,27 +1,13 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.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/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/environment.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/equipment_profile_provider.dart'; import 'package:lightmeter/providers.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/screens/metering/flow_metering.dart'; import 'package:lightmeter/screens/metering/flow_metering.dart';
import 'package:lightmeter/screens/settings/flow_settings.dart'; import 'package:lightmeter/screens/settings/flow_settings.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Application extends StatelessWidget { class Application extends StatelessWidget {
final Environment env; final Environment env;
@ -30,32 +16,10 @@ class Application extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder( return LightmeterProviders(
future: Future.wait([ env: env,
SharedPreferences.getInstance(), builder: (context, ready) => ready
if (Platform.isAndroid) const LightSensorService().hasSensor() else Future.value(false), ? _AnnotatedRegionWrapper(
]),
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( child: MaterialApp(
theme: context.listen<ThemeData>(), theme: context.listen<ThemeData>(),
locale: Locale(context.listen<SupportedLocale>().intlName), locale: Locale(context.listen<SupportedLocale>().intlName),
@ -76,21 +40,8 @@ class Application extends StatelessWidget {
"settings": (context) => const SettingsFlow(), "settings": (context) => const SettingsFlow(),
}, },
), ),
), )
), : const SizedBox(),
),
),
),
),
),
),
);
} else if (snapshot.error != null) {
return Center(child: Text(snapshot.error!.toString()));
} else {
return const SizedBox.shrink();
}
},
); );
} }
} }

View file

@ -1,6 +1,8 @@
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
class PermissionsService { class PermissionsService {
const PermissionsService();
Future<PermissionStatus> checkCameraPermission() async => Permission.camera.status; Future<PermissionStatus> checkCameraPermission() async => Permission.camera.status;
Future<PermissionStatus> requestCameraPermission() async => Permission.camera.request(); Future<PermissionStatus> requestCameraPermission() async => Permission.camera.request();

72
lib/providers.dart Normal file
View file

@ -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<Environment>(
data: env.copyWith(hasLightSensor: snapshot.data![1] as bool),
child: InheritedWidgetBase<UserPreferencesService>(
data: UserPreferencesService(snapshot.data![0] as SharedPreferences),
child: InheritedWidgetBase<LightSensorService>(
data: const LightSensorService(),
child: InheritedWidgetBase<CaffeineService>(
data: const CaffeineService(),
child: InheritedWidgetBase<HapticsService>(
data: const HapticsService(),
child: InheritedWidgetBase<PermissionsService>(
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);
},
);
}
}

View file

@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:provider/provider.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
typedef EquipmentProfiles = List<EquipmentProfileData>; typedef EquipmentProfiles = List<EquipmentProfileData>;
@ -37,7 +36,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
EquipmentProfileData get _selectedProfile => _customProfiles.firstWhere( EquipmentProfileData get _selectedProfile => _customProfiles.firstWhere(
(e) => e.id == _selectedId, (e) => e.id == _selectedId,
orElse: () { orElse: () {
context.read<UserPreferencesService>().selectedEquipmentProfileId = _defaultProfile.id; context.get<UserPreferencesService>().selectedEquipmentProfileId = _defaultProfile.id;
return _defaultProfile; return _defaultProfile;
}, },
); );
@ -45,8 +44,8 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_selectedId = context.read<UserPreferencesService>().selectedEquipmentProfileId; _selectedId = context.get<UserPreferencesService>().selectedEquipmentProfileId;
_customProfiles = context.read<UserPreferencesService>().equipmentProfiles; _customProfiles = context.get<UserPreferencesService>().equipmentProfiles;
} }
@override @override
@ -64,7 +63,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
setState(() { setState(() {
_selectedId = data.id; _selectedId = data.id;
}); });
context.read<UserPreferencesService>().selectedEquipmentProfileId = _selectedProfile.id; context.get<UserPreferencesService>().selectedEquipmentProfileId = _selectedProfile.id;
} }
/// Creates a default equipment profile /// Creates a default equipment profile
@ -96,7 +95,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
} }
void _refreshSavedProfiles() { void _refreshSavedProfiles() {
context.read<UserPreferencesService>().equipmentProfiles = _customProfiles; context.get<UserPreferencesService>().equipmentProfiles = _customProfiles;
setState(() {}); setState(() {});
} }
} }

View file

@ -3,7 +3,6 @@ import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/environment.dart'; import 'package:lightmeter/environment.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
class EvSourceTypeProvider extends StatefulWidget { class EvSourceTypeProvider extends StatefulWidget {
final Widget child; final Widget child;
@ -24,9 +23,9 @@ class EvSourceTypeProviderState extends State<EvSourceTypeProvider> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
final evSourceType = context.read<UserPreferencesService>().evSourceType; final evSourceType = context.get<UserPreferencesService>().evSourceType;
valueListenable = ValueNotifier( valueListenable = ValueNotifier(
evSourceType == EvSourceType.sensor && !context.read<Environment>().hasLightSensor evSourceType == EvSourceType.sensor && !context.get<Environment>().hasLightSensor
? EvSourceType.camera ? EvSourceType.camera
: evSourceType, : evSourceType,
); );
@ -53,12 +52,12 @@ class EvSourceTypeProviderState extends State<EvSourceTypeProvider> {
void toggleType() { void toggleType() {
switch (valueListenable.value) { switch (valueListenable.value) {
case EvSourceType.camera: case EvSourceType.camera:
if (context.read<Environment>().hasLightSensor) { if (context.get<Environment>().hasLightSensor) {
valueListenable.value = EvSourceType.sensor; valueListenable.value = EvSourceType.sensor;
} }
case EvSourceType.sensor: case EvSourceType.sensor:
valueListenable.value = EvSourceType.camera; valueListenable.value = EvSourceType.camera;
} }
context.read<UserPreferencesService>().evSourceType = valueListenable.value; context.get<UserPreferencesService>().evSourceType = valueListenable.value;
} }
} }

View file

@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; import 'package:lightmeter/data/models/metering_screen_layout_config.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
typedef MeteringScreenLayout = InheritedModelBase<MeteringScreenLayoutFeature, bool>; typedef MeteringScreenLayout = InheritedModelBase<MeteringScreenLayoutFeature, bool>;
@ -21,7 +20,7 @@ class MeteringScreenLayoutProvider extends StatefulWidget {
class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvider> { class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvider> {
late final MeteringScreenLayoutConfig _config = late final MeteringScreenLayoutConfig _config =
context.read<UserPreferencesService>().meteringScreenLayout; context.get<UserPreferencesService>().meteringScreenLayout;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -41,6 +40,6 @@ class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvid
); );
}); });
}); });
context.read<UserPreferencesService>().meteringScreenLayout = _config; context.get<UserPreferencesService>().meteringScreenLayout = _config;
} }
} }

View file

@ -3,7 +3,6 @@ import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
class SupportedLocaleProvider extends StatefulWidget { class SupportedLocaleProvider extends StatefulWidget {
final Widget child; final Widget child;
@ -24,7 +23,7 @@ class SupportedLocaleProviderState extends State<SupportedLocaleProvider> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
valueListenable = ValueNotifier(context.read<UserPreferencesService>().locale); valueListenable = ValueNotifier(context.get<UserPreferencesService>().locale);
} }
@override @override
@ -48,7 +47,7 @@ class SupportedLocaleProviderState extends State<SupportedLocaleProvider> {
void setLocale(SupportedLocale locale) { void setLocale(SupportedLocale locale) {
S.load(Locale(locale.intlName)).then((value) { S.load(Locale(locale.intlName)).then((value) {
valueListenable.value = locale; valueListenable.value = locale;
context.read<UserPreferencesService>().locale = locale; context.get<UserPreferencesService>().locale = locale;
}); });
} }
} }

View file

@ -8,8 +8,6 @@ import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:material_color_utilities/material_color_utilities.dart'; import 'package:material_color_utilities/material_color_utilities.dart';
import 'package:provider/provider.dart';
class ThemeProvider extends StatefulWidget { class ThemeProvider extends StatefulWidget {
final Widget child; final Widget child;
@ -46,7 +44,7 @@ class ThemeProvider extends StatefulWidget {
} }
class ThemeProviderState extends State<ThemeProvider> with WidgetsBindingObserver { class ThemeProviderState extends State<ThemeProvider> with WidgetsBindingObserver {
UserPreferencesService get _prefs => context.read<UserPreferencesService>(); UserPreferencesService get _prefs => context.get<UserPreferencesService>();
late final _themeTypeNotifier = ValueNotifier<ThemeType>(_prefs.themeType); late final _themeTypeNotifier = ValueNotifier<ThemeType>(_prefs.themeType);
late final _dynamicColorNotifier = ValueNotifier<bool>(_prefs.dynamicColor); late final _dynamicColorNotifier = ValueNotifier<bool>(_prefs.dynamicColor);

View file

@ -12,7 +12,6 @@ import 'package:lightmeter/screens/metering/communication/bloc_communication_met
import 'package:lightmeter/screens/metering/screen_metering.dart'; import 'package:lightmeter/screens/metering/screen_metering.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:provider/provider.dart';
class MeteringFlow extends StatefulWidget { class MeteringFlow extends StatefulWidget {
const MeteringFlow({super.key}); const MeteringFlow({super.key});
@ -26,10 +25,10 @@ class _MeteringFlowState extends State<MeteringFlow> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Provider( return Provider(
create: (context) => MeteringInteractor( create: (context) => MeteringInteractor(
context.read<UserPreferencesService>(), context.get<UserPreferencesService>(),
context.read<CaffeineService>(), context.get<CaffeineService>(),
context.read<HapticsService>(), context.get<HapticsService>(),
context.read<PermissionsService>(), context.get<PermissionsService>(),
context.read<LightSensorService>(), context.read<LightSensorService>(),
), ),
child: MultiBlocProvider( child: MultiBlocProvider(

View file

@ -48,7 +48,7 @@ class MeteringScreen extends StatelessWidget {
isMetering: isMetering:
state is LoadingState || state is MeteringDataState && state.continuousMetering, state is LoadingState || state is MeteringDataState && state.continuousMetering,
hasError: state is MeteringDataState && state.hasError, hasError: state is MeteringDataState && state.hasError,
onSwitchEvSourceType: context.read<Environment>().hasLightSensor onSwitchEvSourceType: context.get<Environment>().hasLightSensor
? EvSourceTypeProvider.of(context).toggleType ? EvSourceTypeProvider.of(context).toggleType
: null, : null,
onMeasure: () => context.read<MeteringBloc>().add(const MeasureEvent()), onMeasure: () => context.read<MeteringBloc>().add(const MeasureEvent()),

View file

@ -1,7 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/environment.dart'; import 'package:lightmeter/environment.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
class ReportIssueListTile extends StatelessWidget { class ReportIssueListTile extends StatelessWidget {
@ -14,7 +13,7 @@ class ReportIssueListTile extends StatelessWidget {
title: Text(S.of(context).reportIssue), title: Text(S.of(context).reportIssue),
onTap: () { onTap: () {
launchUrl( launchUrl(
Uri.parse(context.read<Environment>().issuesReportUrl), Uri.parse(context.get<Environment>().issuesReportUrl),
mode: LaunchMode.externalApplication, mode: LaunchMode.externalApplication,
); );
}, },

View file

@ -1,7 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/environment.dart'; import 'package:lightmeter/environment.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
class SourceCodeListTile extends StatelessWidget { class SourceCodeListTile extends StatelessWidget {
@ -14,7 +13,7 @@ class SourceCodeListTile extends StatelessWidget {
title: Text(S.of(context).sourceCode), title: Text(S.of(context).sourceCode),
onTap: () { onTap: () {
launchUrl( launchUrl(
Uri.parse(context.read<Environment>().sourceCodeUrl), Uri.parse(context.get<Environment>().sourceCodeUrl),
mode: LaunchMode.externalApplication, mode: LaunchMode.externalApplication,
); );
}, },

View file

@ -2,7 +2,6 @@ import 'package:clipboard/clipboard.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/environment.dart'; import 'package:lightmeter/environment.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
class WriteEmailListTile extends StatelessWidget { class WriteEmailListTile extends StatelessWidget {
@ -14,7 +13,7 @@ class WriteEmailListTile extends StatelessWidget {
leading: const Icon(Icons.email), leading: const Icon(Icons.email),
title: Text(S.of(context).writeEmail), title: Text(S.of(context).writeEmail),
onTap: () { onTap: () {
final email = context.read<Environment>().contactEmail; final email = context.get<Environment>().contactEmail;
final mailToUrl = Uri.parse('mailto:$email?subject=M3 Lightmeter'); final mailToUrl = Uri.parse('mailto:$email?subject=M3 Lightmeter');
canLaunchUrl(mailToUrl).then((canLaunch) { canLaunchUrl(mailToUrl).then((canLaunch) {
if (canLaunch) { if (canLaunch) {

View file

@ -14,7 +14,7 @@ class CalibrationDialog extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final bool hasLightSensor = context.read<Environment>().hasLightSensor; final bool hasLightSensor = context.get<Environment>().hasLightSensor;
return AlertDialog( return AlertDialog(
icon: const Icon(Icons.settings_brightness), icon: const Icon(Icons.settings_brightness),
titlePadding: Dimens.dialogIconTitlePadding, titlePadding: Dimens.dialogIconTitlePadding,

View file

@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/interactors/settings_interactor.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/screens/settings/components/metering/components/calibration/components/calibration_dialog/provider_dialog_calibration.dart';
import 'package:provider/provider.dart';
class CalibrationListTile extends StatelessWidget { class CalibrationListTile extends StatelessWidget {
const CalibrationListTile({super.key}); const CalibrationListTile({super.key});

View file

@ -4,7 +4,7 @@ import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/interactors/settings_interactor.dart';
import 'package:lightmeter/screens/settings/screen_settings.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 { class SettingsFlow extends StatelessWidget {
const SettingsFlow({super.key}); const SettingsFlow({super.key});
@ -13,9 +13,9 @@ class SettingsFlow extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Provider( return Provider(
create: (context) => SettingsInteractor( create: (context) => SettingsInteractor(
context.read<UserPreferencesService>(), context.get<UserPreferencesService>(),
context.read<CaffeineService>(), context.get<CaffeineService>(),
context.read<HapticsService>(), context.get<HapticsService>(),
), ),
child: const SettingsScreen(), child: const SettingsScreen(),
); );