diff --git a/flutter/lib/src/uvccamera_controller.dart b/flutter/lib/src/uvccamera_controller.dart index ea02973a..6f2430e6 100644 --- a/flutter/lib/src/uvccamera_controller.dart +++ b/flutter/lib/src/uvccamera_controller.dart @@ -4,6 +4,10 @@ import 'package:cross_file/cross_file.dart'; import 'package:flutter/widgets.dart'; import 'uvccamera_button_event.dart'; +import 'uvccamera_controller_disposed_exception.dart'; +import 'uvccamera_controller_illegal_state_exception.dart'; +import 'uvccamera_controller_initialized_exception.dart'; +import 'uvccamera_controller_not_initialized_exception.dart'; import 'uvccamera_controller_state.dart'; import 'uvccamera_device.dart'; import 'uvccamera_mode.dart'; @@ -45,8 +49,11 @@ class UvcCameraController extends ValueNotifier { /// Initializes the controller on the specified device. Future _initialize(UvcCameraDevice device) async { + if (_initializeFuture != null) { + throw UvcCameraControllerInitializedException(); + } if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); + throw UvcCameraControllerDisposedException(); } final Completer initializeCompleter = Completer(); @@ -58,15 +65,11 @@ 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, @@ -94,16 +97,14 @@ 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; } @@ -145,11 +146,10 @@ class UvcCameraController extends ValueNotifier { _ensureInitializedNotDisposed(); if (value.isRecordingVideo) { - throw Exception('UvcCameraController is already recording video'); + throw UvcCameraControllerIllegalStateException('UvcCameraController is already recording video'); } - final XFile videoRecordingFile = - await UvcCameraPlatformInterface.instance.startVideoRecording( + final XFile videoRecordingFile = await UvcCameraPlatformInterface.instance.startVideoRecording( _cameraId!, videoRecordingMode, ); @@ -166,7 +166,7 @@ class UvcCameraController extends ValueNotifier { _ensureInitializedNotDisposed(); if (!value.isRecordingVideo) { - throw Exception('UvcCameraController is not recording video'); + throw UvcCameraControllerIllegalStateException('UvcCameraController is not recording video'); } await UvcCameraPlatformInterface.instance.stopVideoRecording(_cameraId!); @@ -192,10 +192,10 @@ class UvcCameraController extends ValueNotifier { /// Ensures that the controller is initialized and not disposed. void _ensureInitializedNotDisposed() { if (_isDisposed) { - throw Exception('UvcCameraController is disposed'); + throw UvcCameraControllerDisposedException(); } if (_initializeFuture == null) { - throw Exception('UvcCameraController is not initialized'); + throw UvcCameraControllerNotInitializedException(); } } } diff --git a/flutter/lib/src/uvccamera_controller_disposed_exception.dart b/flutter/lib/src/uvccamera_controller_disposed_exception.dart new file mode 100644 index 00000000..7636960a --- /dev/null +++ b/flutter/lib/src/uvccamera_controller_disposed_exception.dart @@ -0,0 +1,6 @@ +import 'uvccamera_exception.dart'; + +/// Exception thrown when the [UvcCameraController] is disposed yet an operation is attempted. +class UvcCameraControllerDisposedException extends UvcCameraException { + const UvcCameraControllerDisposedException() : super('UvcCameraController is disposed'); +} diff --git a/flutter/lib/src/uvccamera_controller_illegal_state_exception.dart b/flutter/lib/src/uvccamera_controller_illegal_state_exception.dart new file mode 100644 index 00000000..bc6039eb --- /dev/null +++ b/flutter/lib/src/uvccamera_controller_illegal_state_exception.dart @@ -0,0 +1,8 @@ +import 'uvccamera_exception.dart'; + +/// Exception thrown when the [UvcCameraController] is in an illegal state. +class UvcCameraControllerIllegalStateException extends UvcCameraException { + const UvcCameraControllerIllegalStateException([ + super.message, + ]); +} diff --git a/flutter/lib/src/uvccamera_controller_initialized_exception.dart b/flutter/lib/src/uvccamera_controller_initialized_exception.dart new file mode 100644 index 00000000..5f4eb1d7 --- /dev/null +++ b/flutter/lib/src/uvccamera_controller_initialized_exception.dart @@ -0,0 +1,6 @@ +import 'uvccamera_exception.dart'; + +/// Exception thrown when the [UvcCameraController] is already initialized. +class UvcCameraControllerInitializedException extends UvcCameraException { + const UvcCameraControllerInitializedException() : super('UvcCameraController is already initialized'); +} diff --git a/flutter/lib/src/uvccamera_controller_not_initialized_exception.dart b/flutter/lib/src/uvccamera_controller_not_initialized_exception.dart new file mode 100644 index 00000000..43dce05f --- /dev/null +++ b/flutter/lib/src/uvccamera_controller_not_initialized_exception.dart @@ -0,0 +1,6 @@ +import 'uvccamera_exception.dart'; + +/// Exception thrown when the [UvcCameraController] is not initialized yet an operation is attempted. +class UvcCameraControllerNotInitializedException extends UvcCameraException { + const UvcCameraControllerNotInitializedException() : super('UvcCameraController is not initialized'); +} diff --git a/flutter/lib/src/uvccamera_exception.dart b/flutter/lib/src/uvccamera_exception.dart new file mode 100644 index 00000000..b1ed6ada --- /dev/null +++ b/flutter/lib/src/uvccamera_exception.dart @@ -0,0 +1,12 @@ +/// UVC Camera Exception +class UvcCameraException implements Exception { + final dynamic message; + + const UvcCameraException([this.message]); + + @override + String toString() { + if (message == null) return runtimeType.toString(); + return "$runtimeType: $message"; + } +} diff --git a/flutter/lib/uvccamera.dart b/flutter/lib/uvccamera.dart index 673a6eaf..0945e686 100644 --- a/flutter/lib/uvccamera.dart +++ b/flutter/lib/uvccamera.dart @@ -3,10 +3,15 @@ library; export 'src/uvccamera.dart' show UvcCamera; export 'src/uvccamera_button_event.dart' show UvcCameraButtonEvent; export 'src/uvccamera_controller.dart' show UvcCameraController; +export 'src/uvccamera_controller_disposed_exception.dart' show UvcCameraControllerDisposedException; +export 'src/uvccamera_controller_illegal_state_exception.dart' show UvcCameraControllerIllegalStateException; +export 'src/uvccamera_controller_initialized_exception.dart' show UvcCameraControllerInitializedException; +export 'src/uvccamera_controller_not_initialized_exception.dart' show UvcCameraControllerNotInitializedException; export 'src/uvccamera_controller_state.dart' show UvcCameraControllerState; export 'src/uvccamera_device.dart' show UvcCameraDevice; export 'src/uvccamera_device_event.dart' show UvcCameraDeviceEvent; export 'src/uvccamera_device_event_type.dart' show UvcCameraDeviceEventType; +export 'src/uvccamera_exception.dart' show UvcCameraException; export 'src/uvccamera_frame_format.dart' show UvcCameraFrameFormat; export 'src/uvccamera_mode.dart' show UvcCameraMode; export 'src/uvccamera_preview.dart' show UvcCameraPreview;