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/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,32 +16,10 @@ 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(
return LightmeterProviders(
env: env,
builder: (context, ready) => ready
? _AnnotatedRegionWrapper(
child: MaterialApp(
theme: context.listen<ThemeData>(),
locale: Locale(context.listen<SupportedLocale>().intlName),
@ -76,21 +40,8 @@ class Application extends StatelessWidget {
"settings": (context) => const SettingsFlow(),
},
),
),
),
),
),
),
),
),
),
);
} else if (snapshot.error != null) {
return Center(child: Text(snapshot.error!.toString()));
} else {
return const SizedBox.shrink();
}
},
)
: const SizedBox(),
);
}
}

View file

@ -1,6 +1,8 @@
import 'package:permission_handler/permission_handler.dart';
class PermissionsService {
const PermissionsService();
Future<PermissionStatus> checkCameraPermission() async => Permission.camera.status;
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/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<EquipmentProfileData>;
@ -37,7 +36,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
EquipmentProfileData get _selectedProfile => _customProfiles.firstWhere(
(e) => e.id == _selectedId,
orElse: () {
context.read<UserPreferencesService>().selectedEquipmentProfileId = _defaultProfile.id;
context.get<UserPreferencesService>().selectedEquipmentProfileId = _defaultProfile.id;
return _defaultProfile;
},
);
@ -45,8 +44,8 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
@override
void initState() {
super.initState();
_selectedId = context.read<UserPreferencesService>().selectedEquipmentProfileId;
_customProfiles = context.read<UserPreferencesService>().equipmentProfiles;
_selectedId = context.get<UserPreferencesService>().selectedEquipmentProfileId;
_customProfiles = context.get<UserPreferencesService>().equipmentProfiles;
}
@override
@ -64,7 +63,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
setState(() {
_selectedId = data.id;
});
context.read<UserPreferencesService>().selectedEquipmentProfileId = _selectedProfile.id;
context.get<UserPreferencesService>().selectedEquipmentProfileId = _selectedProfile.id;
}
/// Creates a default equipment profile
@ -96,7 +95,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
}
void _refreshSavedProfiles() {
context.read<UserPreferencesService>().equipmentProfiles = _customProfiles;
context.get<UserPreferencesService>().equipmentProfiles = _customProfiles;
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/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<EvSourceTypeProvider> {
@override
void initState() {
super.initState();
final evSourceType = context.read<UserPreferencesService>().evSourceType;
final evSourceType = context.get<UserPreferencesService>().evSourceType;
valueListenable = ValueNotifier(
evSourceType == EvSourceType.sensor && !context.read<Environment>().hasLightSensor
evSourceType == EvSourceType.sensor && !context.get<Environment>().hasLightSensor
? EvSourceType.camera
: evSourceType,
);
@ -53,12 +52,12 @@ class EvSourceTypeProviderState extends State<EvSourceTypeProvider> {
void toggleType() {
switch (valueListenable.value) {
case EvSourceType.camera:
if (context.read<Environment>().hasLightSensor) {
if (context.get<Environment>().hasLightSensor) {
valueListenable.value = EvSourceType.sensor;
}
case EvSourceType.sensor:
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/shared_prefs_service.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:provider/provider.dart';
typedef MeteringScreenLayout = InheritedModelBase<MeteringScreenLayoutFeature, bool>;
@ -21,7 +20,7 @@ class MeteringScreenLayoutProvider extends StatefulWidget {
class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvider> {
late final MeteringScreenLayoutConfig _config =
context.read<UserPreferencesService>().meteringScreenLayout;
context.get<UserPreferencesService>().meteringScreenLayout;
@override
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/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<SupportedLocaleProvider> {
@override
void initState() {
super.initState();
valueListenable = ValueNotifier(context.read<UserPreferencesService>().locale);
valueListenable = ValueNotifier(context.get<UserPreferencesService>().locale);
}
@override
@ -48,7 +47,7 @@ class SupportedLocaleProviderState extends State<SupportedLocaleProvider> {
void setLocale(SupportedLocale locale) {
S.load(Locale(locale.intlName)).then((value) {
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: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<ThemeProvider> with WidgetsBindingObserver {
UserPreferencesService get _prefs => context.read<UserPreferencesService>();
UserPreferencesService get _prefs => context.get<UserPreferencesService>();
late final _themeTypeNotifier = ValueNotifier<ThemeType>(_prefs.themeType);
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/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<MeteringFlow> {
Widget build(BuildContext context) {
return Provider(
create: (context) => MeteringInteractor(
context.read<UserPreferencesService>(),
context.read<CaffeineService>(),
context.read<HapticsService>(),
context.read<PermissionsService>(),
context.get<UserPreferencesService>(),
context.get<CaffeineService>(),
context.get<HapticsService>(),
context.get<PermissionsService>(),
context.read<LightSensorService>(),
),
child: MultiBlocProvider(

View file

@ -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<Environment>().hasLightSensor
onSwitchEvSourceType: context.get<Environment>().hasLightSensor
? EvSourceTypeProvider.of(context).toggleType
: null,
onMeasure: () => context.read<MeteringBloc>().add(const MeasureEvent()),

View file

@ -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<Environment>().issuesReportUrl),
Uri.parse(context.get<Environment>().issuesReportUrl),
mode: LaunchMode.externalApplication,
);
},

View file

@ -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<Environment>().sourceCodeUrl),
Uri.parse(context.get<Environment>().sourceCodeUrl),
mode: LaunchMode.externalApplication,
);
},

View file

@ -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<Environment>().contactEmail;
final email = context.get<Environment>().contactEmail;
final mailToUrl = Uri.parse('mailto:$email?subject=M3 Lightmeter');
canLaunchUrl(mailToUrl).then((canLaunch) {
if (canLaunch) {

View file

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

View file

@ -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});

View file

@ -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<UserPreferencesService>(),
context.read<CaffeineService>(),
context.read<HapticsService>(),
context.get<UserPreferencesService>(),
context.get<CaffeineService>(),
context.get<HapticsService>(),
),
child: const SettingsScreen(),
);