ML-77 Redundant vibrations (#76)

* wip

* `MeteringScreenLayout = InheritedModelBase<MeteringScreenLayoutFeature, bool>`

* removed `Provider` from providers folder

* wip

* Update pubspec.yaml

* `context.get<MeteringInteractor>()`

* `context.get<Environment>()`

* `context.get<SettingsInteractor>()`

* typo

* fixed `MeteringScreenLayout`

* fixed redundant vibrations
This commit is contained in:
Vadim 2023-06-04 13:04:04 +02:00 committed by GitHub
parent a0eb641cf3
commit cdf7372913
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 505 additions and 327 deletions

View file

@ -1,26 +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:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class Application extends StatelessWidget {
final Environment env;
@ -29,34 +16,13 @@ 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(
builder: (context, _) => _AnnotatedRegionWrapper(
return LightmeterProviders(
env: env,
builder: (context, ready) => ready
? _AnnotatedRegionWrapper(
child: MaterialApp(
theme: context.watch<ThemeData>(),
locale: Locale(context.watch<SupportedLocale>().intlName),
theme: context.listen<ThemeData>(),
locale: Locale(context.listen<SupportedLocale>().intlName),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
@ -74,20 +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(),
);
}
}
@ -100,7 +54,7 @@ class _AnnotatedRegionWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final systemIconsBrightness = ThemeData.estimateBrightnessForColor(
context.watch<ThemeData>().colorScheme.onSurface,
context.listen<ThemeData>().colorScheme.onSurface,
);
return AnnotatedRegion(
value: SystemUiOverlayStyle(

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();

74
lib/providers.dart Normal file
View file

@ -0,0 +1,74 @@
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(
builder: (context) => builder(context, true),
),
),
),
),
),
),
),
),
),
),
),
),
);
} else if (snapshot.error != null) {
return Center(child: Text(snapshot.error!.toString()));
}
return builder(context, false);
},
);
}
}

View file

@ -1,9 +1,12 @@
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>;
typedef EquipmentProfile = EquipmentProfileData;
class EquipmentProfileProvider extends StatefulWidget {
final Widget child;
@ -33,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;
},
);
@ -41,15 +44,15 @@ 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
Widget build(BuildContext context) {
return EquipmentProfiles(
profiles: [_defaultProfile] + _customProfiles,
child: EquipmentProfile(
return InheritedWidgetBase<List<EquipmentProfileData>>(
data: [_defaultProfile] + _customProfiles,
child: InheritedWidgetBase<EquipmentProfileData>(
data: _selectedProfile,
child: widget.child,
),
@ -60,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
@ -92,49 +95,7 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
}
void _refreshSavedProfiles() {
context.read<UserPreferencesService>().equipmentProfiles = _customProfiles;
context.get<UserPreferencesService>().equipmentProfiles = _customProfiles;
setState(() {});
}
}
class EquipmentProfiles extends InheritedWidget {
final List<EquipmentProfileData> profiles;
const EquipmentProfiles({
required this.profiles,
required super.child,
super.key,
});
static List<EquipmentProfileData> of(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<EquipmentProfiles>()!.profiles;
} else {
return context.findAncestorWidgetOfExactType<EquipmentProfiles>()!.profiles;
}
}
@override
bool updateShouldNotify(EquipmentProfiles oldWidget) => true;
}
class EquipmentProfile extends InheritedWidget {
final EquipmentProfileData data;
const EquipmentProfile({
required this.data,
required super.child,
super.key,
});
static EquipmentProfileData of(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<EquipmentProfile>()!.data;
} else {
return context.findAncestorWidgetOfExactType<EquipmentProfile>()!.data;
}
}
@override
bool updateShouldNotify(EquipmentProfile oldWidget) => true;
}

View file

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/environment.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class EvSourceTypeProvider extends StatefulWidget {
final Widget child;
@ -23,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,
);
@ -41,9 +41,9 @@ class EvSourceTypeProviderState extends State<EvSourceTypeProvider> {
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: valueListenable,
builder: (_, value, child) => Provider.value(
value: value,
child: child,
builder: (_, value, child) => InheritedWidgetBase<EvSourceType>(
data: value,
child: child!,
),
child: widget.child,
);
@ -52,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

@ -1,7 +1,7 @@
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:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class MeteringScreenLayoutProvider extends StatefulWidget {
final Widget child;
@ -18,12 +18,12 @@ 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) {
return MeteringScreenLayout(
config: MeteringScreenLayoutConfig.from(_config),
return InheritedModelBase<MeteringScreenLayoutFeature, bool>(
data: MeteringScreenLayoutConfig.from(_config),
child: widget.child,
);
}
@ -38,45 +38,23 @@ class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvid
);
});
});
context.read<UserPreferencesService>().meteringScreenLayout = _config;
context.get<UserPreferencesService>().meteringScreenLayout = _config;
}
}
class MeteringScreenLayout extends InheritedModel<MeteringScreenLayoutFeature> {
final MeteringScreenLayoutConfig config;
const MeteringScreenLayout({
required this.config,
required super.child,
super.key,
});
typedef _MeteringScreenLayoutModel = InheritedModelBase<MeteringScreenLayoutFeature, bool>;
extension MeteringScreenLayout on InheritedModelBase<MeteringScreenLayoutFeature, bool> {
static MeteringScreenLayoutConfig of(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<MeteringScreenLayout>()!.config;
return context.dependOnInheritedWidgetOfExactType<_MeteringScreenLayoutModel>()!.data;
} else {
return context.findAncestorWidgetOfExactType<MeteringScreenLayout>()!.config;
return context.findAncestorWidgetOfExactType<_MeteringScreenLayoutModel>()!.data;
}
}
static bool featureStatusOf(BuildContext context, MeteringScreenLayoutFeature feature) {
return InheritedModel.inheritFrom<MeteringScreenLayout>(context, aspect: feature)!
.config[feature]!;
}
@override
bool updateShouldNotify(MeteringScreenLayout oldWidget) => true;
@override
bool updateShouldNotifyDependent(
MeteringScreenLayout oldWidget,
Set<MeteringScreenLayoutFeature> dependencies,
) {
for (final dependecy in dependencies) {
if (oldWidget.config[dependecy] != config[dependecy]) {
return true;
}
}
return false;
static bool featureOf(BuildContext context, MeteringScreenLayoutFeature aspect) {
return InheritedModel.inheritFrom<_MeteringScreenLayoutModel>(context, aspect: aspect)!
.data[aspect]!;
}
}

View file

@ -1,11 +1,11 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:provider/provider.dart';
class StopTypeProvider extends StatefulWidget {
final Widget? child;
final Widget child;
const StopTypeProvider({this.child, super.key});
const StopTypeProvider({required this.child, super.key});
static StopTypeProviderState of(BuildContext context) {
return context.findAncestorStateOfType<StopTypeProviderState>()!;
@ -28,8 +28,8 @@ class StopTypeProviderState extends State<StopTypeProvider> {
@override
Widget build(BuildContext context) {
return Provider.value(
value: _stopType,
return InheritedWidgetBase<StopType>(
data: _stopType,
child: widget.child,
);
}

View file

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class SupportedLocaleProvider extends StatefulWidget {
final Widget child;
@ -23,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
@ -36,9 +36,9 @@ class SupportedLocaleProviderState extends State<SupportedLocaleProvider> {
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: valueListenable,
builder: (_, value, child) => Provider.value(
value: value,
child: child,
builder: (_, value, child) => InheritedWidgetBase<SupportedLocale>(
data: value,
child: child!,
),
child: widget.child,
);
@ -47,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

@ -5,15 +5,14 @@ import 'package:lightmeter/data/models/dynamic_colors_state.dart';
import 'package:lightmeter/data/models/theme_type.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
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 TransitionBuilder? builder;
final Widget child;
const ThemeProvider({
this.builder,
required this.child,
super.key,
});
@ -45,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);
@ -76,8 +75,8 @@ class ThemeProviderState extends State<ThemeProvider> with WidgetsBindingObserve
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: _themeTypeNotifier,
builder: (_, themeType, __) => Provider.value(
value: themeType,
builder: (_, themeType, __) => InheritedWidgetBase<ThemeType>(
data: themeType,
child: ValueListenableBuilder(
valueListenable: _dynamicColorNotifier,
builder: (_, useDynamicColor, __) => _DynamicColorProvider(
@ -88,7 +87,7 @@ class ThemeProviderState extends State<ThemeProvider> with WidgetsBindingObserve
builder: (_, primaryColor, __) => _ThemeDataProvider(
primaryColor: dynamicPrimaryColor ?? primaryColor,
brightness: _themeBrightness,
builder: widget.builder,
child: widget.child,
),
),
),
@ -154,8 +153,8 @@ class _DynamicColorProvider extends StatelessWidget {
dynamicPrimaryColor = null;
state = DynamicColorState.unavailable;
}
return Provider.value(
value: state,
return InheritedWidgetBase<DynamicColorState>(
data: state,
child: builder(context, dynamicPrimaryColor),
);
},
@ -166,19 +165,19 @@ class _DynamicColorProvider extends StatelessWidget {
class _ThemeDataProvider extends StatelessWidget {
final Color primaryColor;
final Brightness brightness;
final TransitionBuilder? builder;
final Widget child;
const _ThemeDataProvider({
required this.primaryColor,
required this.brightness,
required this.builder,
required this.child,
});
@override
Widget build(BuildContext context) {
return Provider.value(
value: _themeFromColorScheme(_colorSchemeFromColor()),
builder: builder,
return InheritedWidgetBase<ThemeData>(
data: _themeFromColorScheme(_colorSchemeFromColor()),
child: child,
);
}

View file

@ -40,7 +40,7 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
this.stopType,
) : super(
MeteringDataState(
ev: 0.0,
ev: null,
film: _meteringInteractor.film,
iso: _meteringInteractor.iso,
nd: _meteringInteractor.ndFilter,
@ -77,56 +77,76 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
}
void _onStopTypeChanged(StopTypeChangedEvent event, Emitter emit) {
if (stopType != event.stopType) {
stopType = event.stopType;
_updateMeasurements();
}
}
void _onEquipmentProfileChanged(EquipmentProfileChangedEvent event, Emitter emit) {
_equipmentProfileData = event.equipmentProfileData;
bool willUpdateMeasurements = false;
/// Update selected ISO value, if selected equipment profile
/// doesn't contain currently selected value
if (!event.equipmentProfileData.isoValues.any((v) => _iso.value == v.value)) {
_meteringInteractor.iso = event.equipmentProfileData.isoValues.first;
_iso = event.equipmentProfileData.isoValues.first;
willUpdateMeasurements &= true;
}
/// The same for ND filter
if (!event.equipmentProfileData.ndValues.any((v) => _nd.value == v.value)) {
_meteringInteractor.ndFilter = event.equipmentProfileData.ndValues.first;
_nd = event.equipmentProfileData.ndValues.first;
willUpdateMeasurements &= true;
}
if (willUpdateMeasurements) {
_updateMeasurements();
}
}
void _onFilmChanged(FilmChangedEvent event, Emitter emit) {
if (_iso.value != event.data.iso) {
if (_film.name != event.data.name) {
_film = event.data;
_meteringInteractor.film = event.data;
/// If user selects 'Other' film we preserve currently selected ISO
/// and therefore only discard reciprocity formula
if (_iso.value != event.data.iso && event.data != const Film.other()) {
final newIso = IsoValue.values.firstWhere(
(e) => e.value == event.data.iso,
orElse: () => _iso,
);
add(IsoChangedEvent(newIso));
_meteringInteractor.iso = newIso;
_iso = newIso;
}
_film = event.data;
_meteringInteractor.film = event.data;
_updateMeasurements();
}
}
void _onIsoChanged(IsoChangedEvent event, Emitter emit) {
if (event.isoValue.value != _film.iso) {
/// Discard currently selected film even if ISO is the same,
/// because, for example, Fomapan 400 and any Ilford 400
/// have different reciprocity formulas
_film = Film.values.first;
}
if (_iso != event.isoValue) {
_meteringInteractor.iso = event.isoValue;
_iso = event.isoValue;
_updateMeasurements();
}
}
void _onNdChanged(NdChangedEvent event, Emitter emit) {
if (_nd != event.ndValue) {
_meteringInteractor.ndFilter = event.ndValue;
_nd = event.ndValue;
_updateMeasurements();
}
}
void _onMeasure(MeasureEvent _, Emitter emit) {
_meteringInteractor.quickVibration();

View file

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class MeteringBottomControls extends StatelessWidget {
final double? ev;
@ -44,7 +44,7 @@ class MeteringBottomControls extends StatelessWidget {
child: IconButton(
onPressed: onSwitchEvSourceType,
icon: Icon(
context.watch<EvSourceType>() != EvSourceType.camera
context.listen<EvSourceType>() != EvSourceType.camera
? Icons.camera_rear
: Icons.wb_incandescent,
),

View file

@ -6,6 +6,7 @@ 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/widget_container_camera.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class CameraContainerProvider extends StatelessWidget {
@ -37,7 +38,7 @@ class CameraContainerProvider extends StatelessWidget {
return BlocProvider(
lazy: false,
create: (context) => CameraContainerBloc(
context.read<MeteringInteractor>(),
context.get<MeteringInteractor>(),
context.read<MeteringCommunicationBloc>(),
),
child: CameraContainer(

View file

@ -56,14 +56,14 @@ class CameraContainer extends StatelessWidget {
topBarOverflow += Dimens.readingContainerSingleValueHeight;
topBarOverflow += Dimens.paddingS;
}
if (MeteringScreenLayout.featureStatusOf(
if (MeteringScreenLayout.featureOf(
context,
MeteringScreenLayoutFeature.extremeExposurePairs,
)) {
topBarOverflow += Dimens.readingContainerDoubleValueHeight;
topBarOverflow += Dimens.paddingS;
}
if (MeteringScreenLayout.featureStatusOf(
if (MeteringScreenLayout.featureOf(
context,
MeteringScreenLayoutFeature.filmPicker,
)) {

View file

@ -6,6 +6,7 @@ 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:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class LightSensorContainerProvider extends StatelessWidget {
@ -37,7 +38,7 @@ class LightSensorContainerProvider extends StatelessWidget {
return BlocProvider(
lazy: false,
create: (context) => LightSensorContainerBloc(
context.read<MeteringInteractor>(),
context.get<MeteringInteractor>(),
context.read<MeteringCommunicationBloc>(),
),
child: LightSensorContainer(

View file

@ -9,6 +9,7 @@ import 'package:lightmeter/providers/metering_screen_layout_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 {
@ -42,7 +43,7 @@ class ReadingsContainer extends StatelessWidget {
const _EquipmentProfilePicker(),
const _InnerPadding(),
],
if (MeteringScreenLayout.featureStatusOf(
if (MeteringScreenLayout.featureOf(
context,
MeteringScreenLayoutFeature.extremeExposurePairs,
)) ...[
@ -60,7 +61,7 @@ class ReadingsContainer extends StatelessWidget {
),
const _InnerPadding(),
],
if (MeteringScreenLayout.featureStatusOf(
if (MeteringScreenLayout.featureOf(
context,
MeteringScreenLayoutFeature.filmPicker,
)) ...[
@ -76,7 +77,7 @@ class ReadingsContainer extends StatelessWidget {
Expanded(
child: _IsoValuePicker(
selectedValue: iso,
values: EquipmentProfile.of(context).isoValues,
values: context.listen<EquipmentProfile>().isoValues,
onChanged: onIsoChanged,
),
),
@ -84,7 +85,7 @@ class ReadingsContainer extends StatelessWidget {
Expanded(
child: _NdValuePicker(
selectedValue: nd,
values: EquipmentProfile.of(context).ndValues,
values: context.listen<EquipmentProfile>().ndValues,
onChanged: onNdChanged,
),
),
@ -107,16 +108,16 @@ class _EquipmentProfilePicker extends StatelessWidget {
return AnimatedDialogPicker<EquipmentProfileData>(
icon: Icons.camera,
title: S.of(context).equipmentProfile,
selectedValue: EquipmentProfile.of(context),
values: EquipmentProfiles.of(context),
selectedValue: context.listen<EquipmentProfile>(),
values: context.listen<EquipmentProfiles>(),
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: EquipmentProfile.of(context).id.isEmpty
value: context.listen<EquipmentProfile>().id.isEmpty
? S.of(context).none
: EquipmentProfile.of(context).name,
: context.listen<EquipmentProfile>().name,
),
),
);

View file

@ -10,8 +10,8 @@ import 'package:lightmeter/providers/equipment_profile_provider.dart';
import 'package:lightmeter/screens/metering/bloc_metering.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
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});
@ -23,13 +23,13 @@ class MeteringFlow extends StatefulWidget {
class _MeteringFlowState extends State<MeteringFlow> {
@override
Widget build(BuildContext context) {
return Provider(
create: (context) => MeteringInteractor(
context.read<UserPreferencesService>(),
context.read<CaffeineService>(),
context.read<HapticsService>(),
context.read<PermissionsService>(),
context.read<LightSensorService>(),
return InheritedWidgetBase<MeteringInteractor>(
data: MeteringInteractor(
context.get<UserPreferencesService>(),
context.get<CaffeineService>(),
context.get<HapticsService>(),
context.get<PermissionsService>(),
context.get<LightSensorService>(),
),
child: MultiBlocProvider(
providers: [
@ -37,9 +37,9 @@ class _MeteringFlowState extends State<MeteringFlow> {
BlocProvider(
create: (context) => MeteringBloc(
context.read<MeteringCommunicationBloc>(),
context.read<MeteringInteractor>(),
EquipmentProfile.of(context, listen: false),
context.read<StopType>(),
context.get<MeteringInteractor>(),
context.get<EquipmentProfile>(),
context.get<StopType>(),
),
),
],

View file

@ -7,38 +7,22 @@ import 'package:lightmeter/data/models/metering_screen_layout_config.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/screens/metering/bloc_metering.dart';
import 'package:lightmeter/screens/metering/components/bottom_controls/provider_bottom_controls.dart';
import 'package:lightmeter/screens/metering/components/camera_container/provider_container_camera.dart';
import 'package:lightmeter/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart';
import 'package:lightmeter/screens/metering/event_metering.dart';
import 'package:lightmeter/screens/metering/state_metering.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class MeteringScreen extends StatefulWidget {
class MeteringScreen extends StatelessWidget {
const MeteringScreen({super.key});
@override
State<MeteringScreen> createState() => _MeteringScreenState();
}
class _MeteringScreenState extends State<MeteringScreen> {
MeteringBloc get _bloc => context.read<MeteringBloc>();
@override
void didChangeDependencies() {
super.didChangeDependencies();
_bloc.add(EquipmentProfileChangedEvent(EquipmentProfile.of(context)));
_bloc.add(StopTypeChangedEvent(context.watch<StopType>()));
if (!MeteringScreenLayout.featureStatusOf(context, MeteringScreenLayoutFeature.filmPicker)) {
_bloc.add(const FilmChangedEvent(Film.other()));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
return _InheritedListeners(
child: Scaffold(
backgroundColor: Theme.of(context).colorScheme.background,
body: Column(
children: [
@ -51,9 +35,10 @@ class _MeteringScreenState extends State<MeteringScreen> {
film: state.film,
iso: state.iso,
nd: state.nd,
onFilmChanged: (value) => _bloc.add(FilmChangedEvent(value)),
onIsoChanged: (value) => _bloc.add(IsoChangedEvent(value)),
onNdChanged: (value) => _bloc.add(NdChangedEvent(value)),
onFilmChanged: (value) =>
context.read<MeteringBloc>().add(FilmChangedEvent(value)),
onIsoChanged: (value) => context.read<MeteringBloc>().add(IsoChangedEvent(value)),
onNdChanged: (value) => context.read<MeteringBloc>().add(NdChangedEvent(value)),
),
),
),
@ -63,15 +48,43 @@ class _MeteringScreenState extends State<MeteringScreen> {
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: () => _bloc.add(const MeasureEvent()),
onMeasure: () => context.read<MeteringBloc>().add(const MeasureEvent()),
onSettings: () => Navigator.pushNamed(context, 'settings'),
),
),
],
),
),
);
}
}
class _InheritedListeners extends StatelessWidget {
final Widget child;
const _InheritedListeners({required this.child});
@override
Widget build(BuildContext context) {
return InheritedWidgetListener<EquipmentProfile>(
onDidChangeDependencies: (value) {
context.read<MeteringBloc>().add(EquipmentProfileChangedEvent(value));
},
child: InheritedWidgetListener<StopType>(
onDidChangeDependencies: (value) {
context.read<MeteringBloc>().add(StopTypeChangedEvent(value));
},
child: InheritedModelAspectListener<MeteringScreenLayoutFeature, bool>(
aspect: MeteringScreenLayoutFeature.filmPicker,
onDidChangeDependencies: (value) {
if (!value) context.read<MeteringBloc>().add(const FilmChangedEvent(Film.other()));
},
child: child,
),
),
);
}
}
@ -101,7 +114,7 @@ class _MeteringContainerBuidler extends StatelessWidget {
@override
Widget build(BuildContext context) {
return context.watch<EvSourceType>() == EvSourceType.camera
return context.listen<EvSourceType>() == EvSourceType.camera
? CameraContainerProvider(
fastest: fastest,
slowest: slowest,

View file

@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:url_launcher/url_launcher.dart';
class ReportIssueListTile extends StatelessWidget {
@ -14,7 +14,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,7 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:url_launcher/url_launcher.dart';
class SourceCodeListTile extends StatelessWidget {
@ -14,7 +14,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,7 @@ 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:lightmeter/utils/inherited_generics.dart';
import 'package:url_launcher/url_launcher.dart';
class WriteEmailListTile extends StatelessWidget {
@ -14,7 +14,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

@ -4,6 +4,7 @@ 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';
class CaffeineListTileProvider extends StatelessWidget {
const CaffeineListTileProvider({super.key});
@ -11,7 +12,7 @@ class CaffeineListTileProvider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CaffeineListTileBloc(context.read<SettingsInteractor>()),
create: (context) => CaffeineListTileBloc(context.get<SettingsInteractor>()),
child: const CaffeineListTile(),
);
}

View file

@ -4,6 +4,7 @@ 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';
class HapticsListTileProvider extends StatelessWidget {
const HapticsListTileProvider({super.key});
@ -11,7 +12,7 @@ class HapticsListTileProvider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => HapticsListTileBloc(context.read<SettingsInteractor>()),
create: (context) => HapticsListTileBloc(context.get<SettingsInteractor>()),
child: const HapticsListTile(),
);
}

View file

@ -3,7 +3,7 @@ import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/supported_locale_provider.dart';
import 'package:lightmeter/screens/settings/components/shared/dialog_picker.dart/widget_dialog_picker.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class LanguageListTile extends StatelessWidget {
const LanguageListTile({super.key});
@ -13,14 +13,14 @@ class LanguageListTile extends StatelessWidget {
return ListTile(
leading: const Icon(Icons.language),
title: Text(S.of(context).language),
trailing: Text(context.watch<SupportedLocale>().localizedName),
trailing: Text(context.listen<SupportedLocale>().localizedName),
onTap: () {
showDialog<SupportedLocale>(
context: context,
builder: (_) => DialogPicker<SupportedLocale>(
icon: Icons.language,
title: S.of(context).chooseLanguage,
selectedValue: context.read<SupportedLocale>(),
selectedValue: context.get<SupportedLocale>(),
values: SupportedLocale.values,
titleAdapter: (context, value) => value.localizedName,
),

View file

@ -4,6 +4,7 @@ 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';
class CalibrationDialogProvider extends StatelessWidget {
const CalibrationDialogProvider({super.key});
@ -11,7 +12,7 @@ class CalibrationDialogProvider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CalibrationDialogBloc(context.read<SettingsInteractor>()),
create: (context) => CalibrationDialogBloc(context.get<SettingsInteractor>()),
child: const CalibrationDialog(),
);
}

View file

@ -7,6 +7,7 @@ import 'package:lightmeter/screens/settings/components/metering/components/calib
import 'package:lightmeter/screens/settings/components/metering/components/calibration/components/calibration_dialog/event_dialog_calibration.dart';
import 'package:lightmeter/screens/settings/components/metering/components/calibration/components/calibration_dialog/state_dialog_calibration.dart';
import 'package:lightmeter/screens/shared/centered_slider/widget_slider_centered.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:lightmeter/utils/to_string_signed.dart';
class CalibrationDialog extends StatelessWidget {
@ -14,7 +15,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,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:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class CalibrationListTile extends StatelessWidget {
const CalibrationListTile({super.key});
@ -15,8 +15,8 @@ class CalibrationListTile extends StatelessWidget {
onTap: () {
showDialog<double>(
context: context,
builder: (_) => Provider.value(
value: context.read<SettingsInteractor>(),
builder: (_) => InheritedWidgetBase(
data: context.get<SettingsInteractor>(),
child: const CalibrationDialogProvider(),
),
);

View file

@ -5,6 +5,7 @@ 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 {
@ -18,12 +19,13 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
static const maxProfiles = 5 + 1; // replace with a constant from iap
late List<GlobalKey<EquipmentProfileContainerState>> profileContainersKeys = [];
int get profilesCount => EquipmentProfiles.of(context).length;
int get profilesCount => context.listen<EquipmentProfiles>().length;
@override
void initState() {
super.initState();
profileContainersKeys = EquipmentProfiles.of(context, listen: false)
profileContainersKeys = context
.get<EquipmentProfiles>()
.map((e) => GlobalKey<EquipmentProfileContainerState>(debugLabel: e.id))
.toList();
}
@ -56,7 +58,7 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
),
child: EquipmentProfileContainer(
key: profileContainersKeys[index],
data: EquipmentProfiles.of(context)[index],
data: context.listen<EquipmentProfiles>()[index],
onExpand: () => _keepExpandedAt(index),
onUpdate: (profileData) => _updateProfileAt(profileData, index),
onDelete: () => _removeProfileAt(index),
@ -87,7 +89,7 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
}
void _removeProfileAt(int index) {
EquipmentProfileProvider.of(context).deleteProfile(EquipmentProfiles.of(context)[index]);
EquipmentProfileProvider.of(context).deleteProfile(context.listen<EquipmentProfiles>()[index]);
profileContainersKeys.removeAt(index);
}

View file

@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/stop_type_provider.dart';
import 'package:lightmeter/screens/settings/components/shared/dialog_picker.dart/widget_dialog_picker.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:provider/provider.dart';
class StopTypeListTile extends StatelessWidget {
const StopTypeListTile({super.key});
@ -13,14 +13,14 @@ class StopTypeListTile extends StatelessWidget {
return ListTile(
leading: const Icon(Icons.straighten),
title: Text(S.of(context).fractionalStops),
trailing: Text(_typeToString(context, context.watch<StopType>())),
trailing: Text(_typeToString(context, context.listen<StopType>())),
onTap: () {
showDialog<StopType>(
context: context,
builder: (_) => DialogPicker<StopType>(
icon: Icons.straighten,
title: S.of(context).showFractionalStops,
selectedValue: context.read<StopType>(),
selectedValue: context.get<StopType>(),
values: StopType.values,
titleAdapter: _typeToString,
),

View file

@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/dynamic_colors_state.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/theme_provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class DynamicColorListTile extends StatelessWidget {
const DynamicColorListTile({super.key});
@ -12,7 +12,7 @@ class DynamicColorListTile extends StatelessWidget {
return SwitchListTile(
secondary: const Icon(Icons.colorize),
title: Text(S.of(context).dynamicColor),
value: context.watch<DynamicColorState>() == DynamicColorState.enabled,
value: context.listen<DynamicColorState>() == DynamicColorState.enabled,
onChanged: ThemeProvider.of(context).enableDynamicColor,
);
}

View file

@ -1,18 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/dynamic_colors_state.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/theme_provider.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/settings/components/theme/components/primary_color/components/primary_color_picker_dialog/widget_dialog_picker_primary_color.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class PrimaryColorListTile extends StatelessWidget {
const PrimaryColorListTile({super.key});
@override
Widget build(BuildContext context) {
if (context.watch<DynamicColorState>() == DynamicColorState.enabled) {
if (context.listen<DynamicColorState>() == DynamicColorState.enabled) {
return Opacity(
opacity: Dimens.disabledOpacity,
child: IgnorePointer(

View file

@ -3,7 +3,7 @@ import 'package:lightmeter/data/models/theme_type.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/theme_provider.dart';
import 'package:lightmeter/screens/settings/components/shared/dialog_picker.dart/widget_dialog_picker.dart';
import 'package:provider/provider.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class ThemeTypeListTile extends StatelessWidget {
const ThemeTypeListTile({super.key});
@ -13,14 +13,14 @@ class ThemeTypeListTile extends StatelessWidget {
return ListTile(
leading: const Icon(Icons.brightness_6),
title: Text(S.of(context).theme),
trailing: Text(_typeToString(context, context.watch<ThemeType>())),
trailing: Text(_typeToString(context, context.listen<ThemeType>())),
onTap: () {
showDialog<ThemeType>(
context: context,
builder: (_) => DialogPicker<ThemeType>(
icon: Icons.brightness_6,
title: S.of(context).chooseTheme,
selectedValue: context.read<ThemeType>(),
selectedValue: context.get<ThemeType>(),
values: ThemeType.values,
titleAdapter: _typeToString,
),

View file

@ -1,12 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/dynamic_colors_state.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/screens/settings/components/shared/settings_section/widget_settings_section.dart';
import 'package:lightmeter/screens/settings/components/theme/components/dynamic_color/widget_list_tile_dynamic_color.dart';
import 'package:lightmeter/screens/settings/components/theme/components/primary_color/widget_list_tile_primary_color.dart';
import 'package:lightmeter/screens/settings/components/theme/components/theme_type/widget_list_tile_theme_type.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class ThemeSettingsSection extends StatelessWidget {
const ThemeSettingsSection({super.key});
@ -18,7 +17,7 @@ class ThemeSettingsSection extends StatelessWidget {
children: [
const ThemeTypeListTile(),
const PrimaryColorListTile(),
if (context.read<DynamicColorState>() != DynamicColorState.unavailable)
if (context.get<DynamicColorState>() != DynamicColorState.unavailable)
const DynamicColorListTile(),
],
);

View file

@ -4,18 +4,18 @@ 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});
@override
Widget build(BuildContext context) {
return Provider(
create: (context) => SettingsInteractor(
context.read<UserPreferencesService>(),
context.read<CaffeineService>(),
context.read<HapticsService>(),
return InheritedWidgetBase<SettingsInteractor>(
data: SettingsInteractor(
context.get<UserPreferencesService>(),
context.get<CaffeineService>(),
context.get<HapticsService>(),
),
child: const SettingsScreen(),
);

View file

@ -0,0 +1,171 @@
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<T> extends StatefulWidget {
final ValueChanged<T> onDidChangeDependencies;
final Widget child;
const InheritedWidgetListener({
required this.onDidChangeDependencies,
required this.child,
super.key,
});
@override
State<InheritedWidgetListener<T>> createState() => _InheritedWidgetListenerState<T>();
}
class _InheritedWidgetListenerState<T> extends State<InheritedWidgetListener<T>> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
widget.onDidChangeDependencies(context.listen<T>());
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}
class InheritedWidgetBase<T> extends InheritedWidget {
final T data;
const InheritedWidgetBase({
required this.data,
required super.child,
super.key,
});
static T of<T>(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<InheritedWidgetBase<T>>()!.data;
} else {
return context.findAncestorWidgetOfExactType<InheritedWidgetBase<T>>()!.data;
}
}
@override
bool updateShouldNotify(InheritedWidgetBase<T> oldWidget) => true;
}
extension InheritedWidgetBaseContext on BuildContext {
T get<T>() {
return InheritedWidgetBase.of<T>(this, listen: false);
}
T listen<T>() {
return InheritedWidgetBase.of<T>(this);
}
}
/// 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 InheritedModelAspectListener<A extends Object, T> extends StatefulWidget {
final A aspect;
final ValueChanged<T> onDidChangeDependencies;
final Widget child;
const InheritedModelAspectListener({
required this.aspect,
required this.onDidChangeDependencies,
required this.child,
super.key,
});
@override
State<InheritedModelAspectListener<A, T>> createState() =>
_InheritedModelAspectListenerState<A, T>();
}
class _InheritedModelAspectListenerState<A extends Object, T>
extends State<InheritedModelAspectListener<A, T>> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
widget.onDidChangeDependencies(context.listenModelFeature<A, T>(widget.aspect));
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}
class InheritedModelBase<A, T> extends InheritedModel<A> {
final Map<A, T> data;
const InheritedModelBase({
required this.data,
required super.child,
super.key,
});
static Map<A, T> of<A, T>(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<InheritedModelBase<A, T>>()!.data;
} else {
return context.findAncestorWidgetOfExactType<InheritedModelBase<A, T>>()!.data;
}
}
static T featureOf<A extends Object, T>(BuildContext context, A aspect) {
return InheritedModel.inheritFrom<InheritedModelBase<A, T>>(context, aspect: aspect)!
.data[aspect]!;
}
@override
bool updateShouldNotify(InheritedModelBase oldWidget) => true;
@override
bool updateShouldNotifyDependent(
InheritedModelBase<A, T> oldWidget,
Set<A> dependencies,
) {
for (final dependecy in dependencies) {
if (oldWidget.data[dependecy] != data[dependecy]) {
return true;
}
}
return false;
}
}
extension InheritedModelBaseContext on BuildContext {
Map<A, T> getModel<A, T>() {
return InheritedModelBase.of<A, T>(this, listen: false);
}
Map<A, T> listenModel<A, T>() {
return InheritedModelBase.of<A, T>(this);
}
T listenModelFeature<A extends Object, T>(A aspect) {
return InheritedModelBase.featureOf<A, T>(this, aspect);
}
}

View file

@ -29,7 +29,6 @@ dependencies:
material_color_utilities: 0.2.0
package_info_plus: 4.0.1
permission_handler: 10.2.0
provider: 6.0.4
shared_preferences: 2.1.1
url_launcher: 6.1.11
uuid: 3.0.7