From cdaf0905da3910bfc9d3617bf85dc60c70363fe2 Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 6 Jul 2023 17:43:57 +0200 Subject: [PATCH] init/deinit camera on settings open --- lib/application.dart | 14 ++---- lib/screens/metering/bloc_metering.dart | 10 ++++ .../bloc_communication_metering.dart | 2 + .../event_communication_metering.dart | 8 +++ .../state_communication_metering.dart | 8 +++ .../bloc_container_camera.dart | 50 ++++++++++++------- .../widget_container_camera.dart | 44 ++++------------ .../bloc_container_light_sensor.dart | 14 ++++-- .../widget_container_light_sensor.dart | 41 ++++----------- lib/screens/metering/event_metering.dart | 8 +++ lib/screens/metering/screen_metering.dart | 7 ++- 11 files changed, 106 insertions(+), 100 deletions(-) diff --git a/lib/application.dart b/lib/application.dart index f122360..3880a08 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -12,9 +12,7 @@ import 'package:lightmeter/utils/inherited_generics.dart'; class Application extends StatelessWidget { final Environment env; - Application(this.env, {super.key}); - - final RouteObserver routeObserver = RouteObserver(); + const Application(this.env, {super.key}); @override Widget build(BuildContext context) { @@ -32,19 +30,15 @@ class Application extends StatelessWidget { GlobalCupertinoLocalizations.delegate, ], supportedLocales: S.delegate.supportedLocales, - builder: (context, child) => InheritedWidgetBase>( - data: routeObserver, - child: MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), - child: child!, - ), + builder: (context, child) => MediaQuery( + data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), + child: child!, ), initialRoute: "metering", routes: { "metering": (context) => const MeteringFlow(), "settings": (context) => const SettingsFlow(), }, - navigatorObservers: [routeObserver], ), ) : const SizedBox(), diff --git a/lib/screens/metering/bloc_metering.dart b/lib/screens/metering/bloc_metering.dart index 827a317..8e4a2f6 100644 --- a/lib/screens/metering/bloc_metering.dart +++ b/lib/screens/metering/bloc_metering.dart @@ -48,6 +48,8 @@ class MeteringBloc extends Bloc { on(_onMeasure, transformer: droppable()); on(_onMeasured); on(_onMeasureError); + on(_onSettingsOpened); + on(_onSettingsClosed); } @override @@ -233,4 +235,12 @@ class MeteringBloc extends Bloc { add(const MeasureEvent()); } } + + void _onSettingsOpened(SettingsOpenedEvent _, Emitter __) { + _communicationBloc.add(const communication_events.SettingsOpenedEvent()); + } + + void _onSettingsClosed(SettingsClosedEvent _, Emitter __) { + _communicationBloc.add(const communication_events.SettingsClosedEvent()); + } } diff --git a/lib/screens/metering/communication/bloc_communication_metering.dart b/lib/screens/metering/communication/bloc_communication_metering.dart index 1c54dc3..11ebe37 100644 --- a/lib/screens/metering/communication/bloc_communication_metering.dart +++ b/lib/screens/metering/communication/bloc_communication_metering.dart @@ -11,5 +11,7 @@ class MeteringCommunicationBloc on((_, emit) => emit(MeasureState())); on((event, emit) => emit(MeteringInProgressState(event.ev100))); on((event, emit) => emit(MeteringEndedState(event.ev100))); + on((_, emit) => emit(const SettingsOpenedState())); + on((_, emit) => emit(const SettingsClosedState())); } } diff --git a/lib/screens/metering/communication/event_communication_metering.dart b/lib/screens/metering/communication/event_communication_metering.dart index ac63b57..c7e0fd8 100644 --- a/lib/screens/metering/communication/event_communication_metering.dart +++ b/lib/screens/metering/communication/event_communication_metering.dart @@ -47,3 +47,11 @@ class MeteringEndedEvent extends MeasuredEvent { @override int get hashCode => Object.hash(ev100, runtimeType); } + +class SettingsOpenedEvent extends ScreenEvent { + const SettingsOpenedEvent(); +} + +class SettingsClosedEvent extends ScreenEvent { + const SettingsClosedEvent(); +} diff --git a/lib/screens/metering/communication/state_communication_metering.dart b/lib/screens/metering/communication/state_communication_metering.dart index 2923cf1..2d3991e 100644 --- a/lib/screens/metering/communication/state_communication_metering.dart +++ b/lib/screens/metering/communication/state_communication_metering.dart @@ -51,3 +51,11 @@ class MeteringEndedState extends MeasuredState { @override int get hashCode => Object.hash(ev100, runtimeType); } + +class SettingsOpenedState extends SourceState { + const SettingsOpenedState(); +} + +class SettingsClosedState extends SourceState { + const SettingsClosedState(); +} diff --git a/lib/screens/metering/components/camera_container/bloc_container_camera.dart b/lib/screens/metering/components/camera_container/bloc_container_camera.dart index 0e409cc..86a7999 100644 --- a/lib/screens/metering/components/camera_container/bloc_container_camera.dart +++ b/lib/screens/metering/components/camera_container/bloc_container_camera.dart @@ -40,6 +40,8 @@ class CameraContainerBloc extends EvSourceBlocBase _appLifecycleStateObserver(AppLifecycleState state) async { - switch (state) { - case AppLifecycleState.resumed: - add(const InitializeEvent()); - case AppLifecycleState.paused: - case AppLifecycleState.detached: - add(const DeinitializeEvent()); - default: + if (!_settingsOpened) { + switch (state) { + case AppLifecycleState.resumed: + add(const InitializeEvent()); + case AppLifecycleState.paused: + case AppLifecycleState.detached: + add(const DeinitializeEvent()); + default: + } } } diff --git a/lib/screens/metering/components/camera_container/widget_container_camera.dart b/lib/screens/metering/components/camera_container/widget_container_camera.dart index 945f44c..2f8ef45 100644 --- a/lib/screens/metering/components/camera_container/widget_container_camera.dart +++ b/lib/screens/metering/components/camera_container/widget_container_camera.dart @@ -18,10 +18,9 @@ import 'package:lightmeter/screens/metering/components/camera_container/state_co import 'package:lightmeter/screens/metering/components/shared/exposure_pairs_list/widget_list_exposure_pairs.dart'; import 'package:lightmeter/screens/metering/components/shared/metering_top_bar/widget_top_bar_metering.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/widget_container_readings.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; -class CameraContainer extends StatefulWidget { +class CameraContainer extends StatelessWidget { final ExposurePair? fastest; final ExposurePair? slowest; final Film film; @@ -45,29 +44,6 @@ class CameraContainer extends StatefulWidget { super.key, }); - @override - State createState() => _CameraContainerState(); -} - -class _CameraContainerState extends State with RouteAware { - @override - void didChangeDependencies() { - super.didChangeDependencies(); - context.get>().subscribe(this, ModalRoute.of(context)!); - } - - @override - void didPushNext() { - super.didPushNext(); - context.read().add(const DeinitializeEvent()); - } - - @override - void didPopNext() { - super.didPopNext(); - context.read().add(const InitializeEvent()); - } - @override Widget build(BuildContext context) { final double cameraViewHeight = @@ -99,14 +75,14 @@ class _CameraContainerState extends State with RouteAware { children: [ MeteringTopBar( readingsContainer: ReadingsContainer( - fastest: widget.fastest, - slowest: widget.slowest, - film: widget.film, - iso: widget.iso, - nd: widget.nd, - onFilmChanged: widget.onFilmChanged, - onIsoChanged: widget.onIsoChanged, - onNdChanged: widget.onNdChanged, + fastest: fastest, + slowest: slowest, + film: film, + iso: iso, + nd: nd, + onFilmChanged: onFilmChanged, + onIsoChanged: onIsoChanged, + onNdChanged: onNdChanged, ), appendixHeight: topBarOverflow, preview: const _CameraViewBuilder(), @@ -116,7 +92,7 @@ class _CameraContainerState extends State with RouteAware { padding: const EdgeInsets.symmetric(horizontal: Dimens.paddingM), child: _MiddleContentWrapper( topBarOverflow: topBarOverflow, - leftContent: ExposurePairsList(widget.exposurePairs), + leftContent: ExposurePairsList(exposurePairs), rightContent: const _CameraControlsBuilder(), ), ), diff --git a/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart index fd2475e..9441fe2 100644 --- a/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart @@ -31,12 +31,16 @@ class LightSensorContainerBloc @override void onCommunicationState(communication_states.SourceState communicationState) { - if (communicationState is communication_states.MeasureState) { - if (_luxSubscriptions == null) { - _startMetering(); - } else { + switch (communicationState) { + case communication_states.MeasureState(): + if (_luxSubscriptions == null) { + _startMetering(); + } else { + _cancelMetering(); + } + case communication_states.SettingsOpenedState(): _cancelMetering(); - } + default: } } diff --git a/lib/screens/metering/components/light_sensor_container/widget_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/widget_container_light_sensor.dart index 274de0d..4380f02 100644 --- a/lib/screens/metering/components/light_sensor_container/widget_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/widget_container_light_sensor.dart @@ -1,17 +1,13 @@ import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:lightmeter/data/models/exposure_pair.dart'; import 'package:lightmeter/data/models/film.dart'; import 'package:lightmeter/res/dimens.dart'; -import 'package:lightmeter/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart'; -import 'package:lightmeter/screens/metering/components/light_sensor_container/event_container_light_sensor.dart'; import 'package:lightmeter/screens/metering/components/shared/exposure_pairs_list/widget_list_exposure_pairs.dart'; import 'package:lightmeter/screens/metering/components/shared/metering_top_bar/widget_top_bar_metering.dart'; import 'package:lightmeter/screens/metering/components/shared/readings_container/widget_container_readings.dart'; -import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; -class LightSensorContainer extends StatefulWidget { +class LightSensorContainer extends StatelessWidget { final ExposurePair? fastest; final ExposurePair? slowest; final Film film; @@ -35,43 +31,26 @@ class LightSensorContainer extends StatefulWidget { super.key, }); - @override - State createState() => _LightSensorContainerState(); -} - -class _LightSensorContainerState extends State with RouteAware { - @override - void didChangeDependencies() { - super.didChangeDependencies(); - context.get>().subscribe(this, ModalRoute.of(context)!); - } - - @override - void didPushNext() { - super.didPushNext(); - context.read().add(const CancelLuxMeteringEvent()); - } - @override Widget build(BuildContext context) { return Column( children: [ MeteringTopBar( readingsContainer: ReadingsContainer( - fastest: widget.fastest, - slowest: widget.slowest, - film: widget.film, - iso: widget.iso, - nd: widget.nd, - onFilmChanged: widget.onFilmChanged, - onIsoChanged: widget.onIsoChanged, - onNdChanged: widget.onNdChanged, + fastest: fastest, + slowest: slowest, + film: film, + iso: iso, + nd: nd, + onFilmChanged: onFilmChanged, + onIsoChanged: onIsoChanged, + onNdChanged: onNdChanged, ), ), Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: Dimens.paddingM), - child: Center(child: ExposurePairsList(widget.exposurePairs)), + child: Center(child: ExposurePairsList(exposurePairs)), ), ), ], diff --git a/lib/screens/metering/event_metering.dart b/lib/screens/metering/event_metering.dart index e12ba35..852b5e4 100644 --- a/lib/screens/metering/event_metering.dart +++ b/lib/screens/metering/event_metering.dart @@ -45,3 +45,11 @@ class MeasureErrorEvent extends MeteringEvent { const MeasureErrorEvent({required this.isMetering}); } + +class SettingsOpenedEvent extends MeteringEvent { + const SettingsOpenedEvent(); +} + +class SettingsClosedEvent extends MeteringEvent { + const SettingsClosedEvent(); +} diff --git a/lib/screens/metering/screen_metering.dart b/lib/screens/metering/screen_metering.dart index 134d789..4fb1e85 100644 --- a/lib/screens/metering/screen_metering.dart +++ b/lib/screens/metering/screen_metering.dart @@ -50,7 +50,12 @@ class MeteringScreen extends StatelessWidget { ? EvSourceTypeProvider.of(context).toggleType : null, onMeasure: () => context.read().add(const MeasureEvent()), - onSettings: () => Navigator.pushNamed(context, 'settings'), + onSettings: () { + context.read().add(const SettingsOpenedEvent()); + Navigator.pushNamed(context, 'settings').then((value) { + context.read().add(const SettingsClosedEvent()); + }); + }, ), ), ],