From dc51d5eda58a7d084122816eef242d52a573b1cd Mon Sep 17 00:00:00 2001 From: Jhionan Date: Thu, 12 Dec 2024 12:15:33 -0300 Subject: [PATCH] (imp) flutter: unify UvcCameraController state checks --- flutter/lib/src/uvccamera_controller.dart | 74 +++++++++-------------- 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/flutter/lib/src/uvccamera_controller.dart b/flutter/lib/src/uvccamera_controller.dart index ad7f2ed5..74ad66b6 100644 --- a/flutter/lib/src/uvccamera_controller.dart +++ b/flutter/lib/src/uvccamera_controller.dart @@ -58,11 +58,15 @@ class UvcCameraController extends ValueNotifier { resolutionPreset, ); - _textureId = await UvcCameraPlatformInterface.instance.getCameraTextureId(_cameraId!); - final previewMode = await UvcCameraPlatformInterface.instance.getPreviewMode(_cameraId!); + _textureId = await UvcCameraPlatformInterface.instance + .getCameraTextureId(_cameraId!); + final previewMode = + await UvcCameraPlatformInterface.instance.getPreviewMode(_cameraId!); - _cameraStatusEventStream = await UvcCameraPlatformInterface.instance.attachToCameraStatusCallback(_cameraId!); - _cameraButtonEventStream = await UvcCameraPlatformInterface.instance.attachToCameraButtonCallback(_cameraId!); + _cameraStatusEventStream = await UvcCameraPlatformInterface.instance + .attachToCameraStatusCallback(_cameraId!); + _cameraButtonEventStream = await UvcCameraPlatformInterface.instance + .attachToCameraButtonCallback(_cameraId!); value = value.copyWith( isInitialized: true, @@ -90,14 +94,16 @@ class UvcCameraController extends ValueNotifier { if (_cameraButtonEventStream != null) { if (_cameraId != null) { - await UvcCameraPlatformInterface.instance.detachFromCameraButtonCallback(_cameraId!); + await UvcCameraPlatformInterface.instance + .detachFromCameraButtonCallback(_cameraId!); } _cameraButtonEventStream = null; } if (_cameraStatusEventStream != null) { if (_cameraId != null) { - await UvcCameraPlatformInterface.instance.detachFromCameraStatusCallback(_cameraId!); + await UvcCameraPlatformInterface.instance + .detachFromCameraStatusCallback(_cameraId!); } _cameraStatusEventStream = null; } @@ -112,62 +118,38 @@ class UvcCameraController extends ValueNotifier { /// Returns the camera ID. int get cameraId { - if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); - } - if (_cameraId == null) { - throw Exception('UvcCameraController is not initialized'); - } + _ensureInitializedNotDisposed(); return _cameraId!; } /// Returns the texture ID. int get textureId { - if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); - } - if (_textureId == null) { - throw Exception('UvcCameraController is not initialized'); - } + _ensureInitializedNotDisposed(); return _textureId!; } /// Returns a stream of camera status events. Stream get cameraStatusEvents { - if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); - } - if (_cameraStatusEventStream == null) { - throw Exception('UvcCameraController is not initialized'); - } + _ensureInitializedNotDisposed(); return _cameraStatusEventStream!; } /// Returns a stream of camera button events. Stream get cameraButtonEvents { - if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); - } - if (_cameraButtonEventStream == null) { - throw Exception('UvcCameraController is not initialized'); - } + _ensureInitializedNotDisposed(); return _cameraButtonEventStream!; } /// Starts video recording. Future startVideoRecording(UvcCameraMode videoRecordingMode) async { - if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); - } - if (_cameraId == null) { - throw Exception('UvcCameraController is not initialized'); - } + _ensureInitializedNotDisposed(); if (value.isRecordingVideo) { throw Exception('UvcCameraController is already recording video'); } - final XFile videoRecordingFile = await UvcCameraPlatformInterface.instance.startVideoRecording( + final XFile videoRecordingFile = + await UvcCameraPlatformInterface.instance.startVideoRecording( _cameraId!, videoRecordingMode, ); @@ -181,12 +163,7 @@ class UvcCameraController extends ValueNotifier { /// Stops video recording. Future stopVideoRecording() async { - if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); - } - if (_cameraId == null) { - throw Exception('UvcCameraController is not initialized'); - } + _ensureInitializedNotDisposed(); if (!value.isRecordingVideo) { throw Exception('UvcCameraController is not recording video'); @@ -207,13 +184,18 @@ class UvcCameraController extends ValueNotifier { /// Returns a widget showing a live camera preview. Widget buildPreview() { + _ensureInitializedNotDisposed(); + + return Texture(textureId: _textureId!); + } + + /// Ensures that the controller is initialized and not disposed. + void _ensureInitializedNotDisposed() { if (_isDisposed) { throw Exception('UvcCameraController is disposed'); } - if (_textureId == null) { + if (_initializeFuture == null) { throw Exception('UvcCameraController is not initialized'); } - - return Texture(textureId: _textureId!); } }