mirror of
https://github.com/vodemn/m3_lightmeter.git
synced 2024-11-25 00:40:39 +00:00
ML-72 buildPreview() was called on a disposed CameraController (#73)
* added `DeinitializeEvent` * typo
This commit is contained in:
parent
00b2728453
commit
8b74c4e1a4
2 changed files with 24 additions and 21 deletions
|
@ -23,8 +23,7 @@ import 'package:lightmeter/utils/log_2.dart';
|
||||||
class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraContainerState> {
|
class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraContainerState> {
|
||||||
final MeteringInteractor _meteringInteractor;
|
final MeteringInteractor _meteringInteractor;
|
||||||
late final _WidgetsBindingObserver _observer;
|
late final _WidgetsBindingObserver _observer;
|
||||||
CameraController? _cameraController;
|
late CameraController _cameraController;
|
||||||
CameraController? get cameraController => _cameraController;
|
|
||||||
|
|
||||||
static const _maxZoom = 7.0;
|
static const _maxZoom = 7.0;
|
||||||
RangeValues? _zoomRange;
|
RangeValues? _zoomRange;
|
||||||
|
@ -50,6 +49,7 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
|
||||||
on<RequestPermissionEvent>(_onRequestPermission);
|
on<RequestPermissionEvent>(_onRequestPermission);
|
||||||
on<OpenAppSettingsEvent>(_onOpenAppSettings);
|
on<OpenAppSettingsEvent>(_onOpenAppSettings);
|
||||||
on<InitializeEvent>(_onInitialize);
|
on<InitializeEvent>(_onInitialize);
|
||||||
|
on<DeinitializeEvent>(_onDeinitialize);
|
||||||
on<ZoomChangedEvent>(_onZoomChanged);
|
on<ZoomChangedEvent>(_onZoomChanged);
|
||||||
on<ExposureOffsetChangedEvent>(_onExposureOffsetChanged);
|
on<ExposureOffsetChangedEvent>(_onExposureOffsetChanged);
|
||||||
on<ExposureOffsetResetEvent>(_onExposureOffsetResetEvent);
|
on<ExposureOffsetResetEvent>(_onExposureOffsetResetEvent);
|
||||||
|
@ -60,7 +60,7 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
|
||||||
@override
|
@override
|
||||||
Future<void> close() async {
|
Future<void> close() async {
|
||||||
WidgetsBinding.instance.removeObserver(_observer);
|
WidgetsBinding.instance.removeObserver(_observer);
|
||||||
unawaited(_cameraController?.dispose());
|
unawaited(_cameraController.dispose());
|
||||||
communicationBloc.add(communication_event.MeteringEndedEvent(_ev100));
|
communicationBloc.add(communication_event.MeteringEndedEvent(_ev100));
|
||||||
return super.close();
|
return super.close();
|
||||||
}
|
}
|
||||||
|
@ -118,30 +118,30 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
|
||||||
enableAudio: false,
|
enableAudio: false,
|
||||||
);
|
);
|
||||||
|
|
||||||
await _cameraController!.initialize();
|
await _cameraController.initialize();
|
||||||
await _cameraController!.setFlashMode(FlashMode.off);
|
await _cameraController.setFlashMode(FlashMode.off);
|
||||||
|
|
||||||
_zoomRange = await Future.wait<double>([
|
_zoomRange = await Future.wait<double>([
|
||||||
_cameraController!.getMinZoomLevel(),
|
_cameraController.getMinZoomLevel(),
|
||||||
_cameraController!.getMaxZoomLevel(),
|
_cameraController.getMaxZoomLevel(),
|
||||||
]).then((levels) => RangeValues(levels[0], math.min(_maxZoom, levels[1])));
|
]).then((levels) => RangeValues(levels[0], math.min(_maxZoom, levels[1])));
|
||||||
_currentZoom = _zoomRange!.start;
|
_currentZoom = _zoomRange!.start;
|
||||||
|
|
||||||
_exposureOffsetRange = await Future.wait<double>([
|
_exposureOffsetRange = await Future.wait<double>([
|
||||||
_cameraController!.getMinExposureOffset(),
|
_cameraController.getMinExposureOffset(),
|
||||||
_cameraController!.getMaxExposureOffset(),
|
_cameraController.getMaxExposureOffset(),
|
||||||
]).then(
|
]).then(
|
||||||
(levels) => RangeValues(
|
(levels) => RangeValues(
|
||||||
math.max(_exposureMaxRange.start, levels[0]),
|
math.max(_exposureMaxRange.start, levels[0]),
|
||||||
math.min(_exposureMaxRange.end, levels[1]),
|
math.min(_exposureMaxRange.end, levels[1]),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
await _cameraController!.getExposureOffsetStepSize().then((value) {
|
await _cameraController.getExposureOffsetStepSize().then((value) {
|
||||||
_exposureStep = value == 0 ? 0.1 : value;
|
_exposureStep = value == 0 ? 0.1 : value;
|
||||||
});
|
});
|
||||||
_currentExposureOffset = 0.0;
|
_currentExposureOffset = 0.0;
|
||||||
|
|
||||||
emit(CameraInitializedState(_cameraController!));
|
emit(CameraInitializedState(_cameraController));
|
||||||
|
|
||||||
_emitActiveState(emit);
|
_emitActiveState(emit);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -149,14 +149,19 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onDeinitialize(DeinitializeEvent _, Emitter emit) async {
|
||||||
|
emit(const CameraLoadingState());
|
||||||
|
unawaited(_cameraController.dispose());
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _onZoomChanged(ZoomChangedEvent event, Emitter emit) async {
|
Future<void> _onZoomChanged(ZoomChangedEvent event, Emitter emit) async {
|
||||||
_cameraController!.setZoomLevel(event.value);
|
_cameraController.setZoomLevel(event.value);
|
||||||
_currentZoom = event.value;
|
_currentZoom = event.value;
|
||||||
_emitActiveState(emit);
|
_emitActiveState(emit);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onExposureOffsetChanged(ExposureOffsetChangedEvent event, Emitter emit) async {
|
Future<void> _onExposureOffsetChanged(ExposureOffsetChangedEvent event, Emitter emit) async {
|
||||||
_cameraController!.setExposureOffset(event.value);
|
_cameraController.setExposureOffset(event.value);
|
||||||
_currentExposureOffset = event.value;
|
_currentExposureOffset = event.value;
|
||||||
_emitActiveState(emit);
|
_emitActiveState(emit);
|
||||||
}
|
}
|
||||||
|
@ -178,13 +183,12 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool get _canTakePhoto => !(_cameraController == null ||
|
bool get _canTakePhoto =>
|
||||||
!_cameraController!.value.isInitialized ||
|
!(!_cameraController.value.isInitialized || _cameraController.value.isTakingPicture);
|
||||||
_cameraController!.value.isTakingPicture);
|
|
||||||
|
|
||||||
Future<double?> _takePhoto() async {
|
Future<double?> _takePhoto() async {
|
||||||
try {
|
try {
|
||||||
final file = await _cameraController!.takePicture();
|
final file = await _cameraController.takePicture();
|
||||||
final Uint8List bytes = await file.readAsBytes();
|
final Uint8List bytes = await file.readAsBytes();
|
||||||
Directory(file.path).deleteSync(recursive: true);
|
Directory(file.path).deleteSync(recursive: true);
|
||||||
|
|
||||||
|
@ -213,8 +217,7 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
|
||||||
add(const InitializeEvent());
|
add(const InitializeEvent());
|
||||||
case AppLifecycleState.paused:
|
case AppLifecycleState.paused:
|
||||||
case AppLifecycleState.detached:
|
case AppLifecycleState.detached:
|
||||||
_cameraController?.dispose();
|
add(const DeinitializeEvent());
|
||||||
_cameraController = null;
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@ class InitializeEvent extends CameraContainerEvent {
|
||||||
const InitializeEvent();
|
const InitializeEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReinitializeEvent extends CameraContainerEvent {
|
class DeinitializeEvent extends CameraContainerEvent {
|
||||||
const ReinitializeEvent();
|
const DeinitializeEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
class ZoomChangedEvent extends CameraContainerEvent {
|
class ZoomChangedEvent extends CameraContainerEvent {
|
||||||
|
|
Loading…
Reference in a new issue