Skip to content

Commit

Permalink
(imp) flutter: exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pelykh committed Dec 17, 2024
1 parent 06d5153 commit 53abc58
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 deletions.
38 changes: 19 additions & 19 deletions flutter/lib/src/uvccamera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,8 +49,11 @@ class UvcCameraController extends ValueNotifier<UvcCameraControllerState> {

/// Initializes the controller on the specified device.
Future<void> _initialize(UvcCameraDevice device) async {
if (_initializeFuture != null) {
throw UvcCameraControllerInitializedException();
}
if (_isDisposed) {
throw Exception('UvcCameraController is disposed');
throw UvcCameraControllerDisposedException();
}

final Completer<void> initializeCompleter = Completer<void>();
Expand All @@ -58,15 +65,11 @@ class UvcCameraController extends ValueNotifier<UvcCameraControllerState> {
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,
Expand Down Expand Up @@ -94,16 +97,14 @@ class UvcCameraController extends ValueNotifier<UvcCameraControllerState> {

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;
}
Expand Down Expand Up @@ -145,11 +146,10 @@ class UvcCameraController extends ValueNotifier<UvcCameraControllerState> {
_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,
);
Expand All @@ -166,7 +166,7 @@ class UvcCameraController extends ValueNotifier<UvcCameraControllerState> {
_ensureInitializedNotDisposed();

if (!value.isRecordingVideo) {
throw Exception('UvcCameraController is not recording video');
throw UvcCameraControllerIllegalStateException('UvcCameraController is not recording video');
}

await UvcCameraPlatformInterface.instance.stopVideoRecording(_cameraId!);
Expand All @@ -192,10 +192,10 @@ class UvcCameraController extends ValueNotifier<UvcCameraControllerState> {
/// 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();
}
}
}
6 changes: 6 additions & 0 deletions flutter/lib/src/uvccamera_controller_disposed_exception.dart
Original file line number Diff line number Diff line change
@@ -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');
}
Original file line number Diff line number Diff line change
@@ -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,
]);
}
Original file line number Diff line number Diff line change
@@ -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');
}
Original file line number Diff line number Diff line change
@@ -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');
}
12 changes: 12 additions & 0 deletions flutter/lib/src/uvccamera_exception.dart
Original file line number Diff line number Diff line change
@@ -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";
}
}
5 changes: 5 additions & 0 deletions flutter/lib/uvccamera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 53abc58

Please sign in to comment.