From 1b19b417dca3bef670f8588f340e097f5e8df332 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 5 Jul 2023 15:45:14 +0200 Subject: [PATCH] deinitialize camera when on Settings screen --- lib/application.dart | 14 ++++-- lib/main_dev.dart | 2 +- lib/main_prod.dart | 3 +- .../widget_container_camera.dart | 44 ++++++++++++++----- .../bloc_container_light_sensor.dart | 5 +++ .../event_container_light_sensor.dart | 4 ++ .../widget_container_light_sensor.dart | 41 ++++++++++++----- 7 files changed, 86 insertions(+), 27 deletions(-) diff --git a/lib/application.dart b/lib/application.dart index 3880a08..f122360 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -12,7 +12,9 @@ import 'package:lightmeter/utils/inherited_generics.dart'; class Application extends StatelessWidget { final Environment env; - const Application(this.env, {super.key}); + Application(this.env, {super.key}); + + final RouteObserver routeObserver = RouteObserver(); @override Widget build(BuildContext context) { @@ -30,15 +32,19 @@ class Application extends StatelessWidget { GlobalCupertinoLocalizations.delegate, ], supportedLocales: S.delegate.supportedLocales, - builder: (context, child) => MediaQuery( - data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), - child: child!, + builder: (context, child) => InheritedWidgetBase>( + data: routeObserver, + 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/main_dev.dart b/lib/main_dev.dart index fd6ed56..c62a531 100644 --- a/lib/main_dev.dart +++ b/lib/main_dev.dart @@ -4,5 +4,5 @@ import 'package:lightmeter/environment.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); - runApp(const Application(Environment.dev())); + runApp(Application(const Environment.dev())); } diff --git a/lib/main_prod.dart b/lib/main_prod.dart index bf02374..a4c9709 100644 --- a/lib/main_prod.dart +++ b/lib/main_prod.dart @@ -6,6 +6,5 @@ import 'package:lightmeter/firebase.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await initializeFirebase(); - - runApp(const Application(Environment.prod())); + runApp(Application(const Environment.prod())); } 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 2f8ef45..945f44c 100644 --- a/lib/screens/metering/components/camera_container/widget_container_camera.dart +++ b/lib/screens/metering/components/camera_container/widget_container_camera.dart @@ -18,9 +18,10 @@ 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 StatelessWidget { +class CameraContainer extends StatefulWidget { final ExposurePair? fastest; final ExposurePair? slowest; final Film film; @@ -44,6 +45,29 @@ class CameraContainer extends StatelessWidget { 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 = @@ -75,14 +99,14 @@ class CameraContainer extends StatelessWidget { children: [ MeteringTopBar( readingsContainer: ReadingsContainer( - fastest: fastest, - slowest: slowest, - film: film, - iso: iso, - nd: nd, - onFilmChanged: onFilmChanged, - onIsoChanged: onIsoChanged, - onNdChanged: onNdChanged, + fastest: widget.fastest, + slowest: widget.slowest, + film: widget.film, + iso: widget.iso, + nd: widget.nd, + onFilmChanged: widget.onFilmChanged, + onIsoChanged: widget.onIsoChanged, + onNdChanged: widget.onNdChanged, ), appendixHeight: topBarOverflow, preview: const _CameraViewBuilder(), @@ -92,7 +116,7 @@ class CameraContainer extends StatelessWidget { padding: const EdgeInsets.symmetric(horizontal: Dimens.paddingM), child: _MiddleContentWrapper( topBarOverflow: topBarOverflow, - leftContent: ExposurePairsList(exposurePairs), + leftContent: ExposurePairsList(widget.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 d0e4031..fd2475e 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 @@ -26,6 +26,7 @@ class LightSensorContainerBloc const LightSensorContainerState(null), ) { on(_onLuxMeteringEvent); + on(_onCancelLuxMeteringEvent); } @override @@ -51,6 +52,10 @@ class LightSensorContainerBloc communicationBloc.add(communication_event.MeteringInProgressEvent(ev100)); } + void _onCancelLuxMeteringEvent(CancelLuxMeteringEvent event, _) { + _cancelMetering(); + } + void _startMetering() { _luxSubscriptions = _meteringInteractor.luxStream().listen((lux) => add(LuxMeteringEvent(lux))); } diff --git a/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart index 8db83b3..5fd69da 100644 --- a/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart @@ -7,3 +7,7 @@ class LuxMeteringEvent extends LightSensorContainerEvent { const LuxMeteringEvent(this.lux); } + +class CancelLuxMeteringEvent extends LightSensorContainerEvent { + const CancelLuxMeteringEvent(); +} 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 4380f02..274de0d 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,13 +1,17 @@ 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 StatelessWidget { +class LightSensorContainer extends StatefulWidget { final ExposurePair? fastest; final ExposurePair? slowest; final Film film; @@ -31,26 +35,43 @@ class LightSensorContainer extends StatelessWidget { 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: fastest, - slowest: slowest, - film: film, - iso: iso, - nd: nd, - onFilmChanged: onFilmChanged, - onIsoChanged: onIsoChanged, - onNdChanged: onNdChanged, + fastest: widget.fastest, + slowest: widget.slowest, + film: widget.film, + iso: widget.iso, + nd: widget.nd, + onFilmChanged: widget.onFilmChanged, + onIsoChanged: widget.onIsoChanged, + onNdChanged: widget.onNdChanged, ), ), Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: Dimens.paddingM), - child: Center(child: ExposurePairsList(exposurePairs)), + child: Center(child: ExposurePairsList(widget.exposurePairs)), ), ), ],