Skip to content

Commit

Permalink
Merge pull request #1797 from EnsembleUI/fix/camera-onerror
Browse files Browse the repository at this point in the history
remove: permission handler
  • Loading branch information
snehmehta authored Dec 31, 2024
2 parents fc1177a + 24a2817 commit 2a908d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
5 changes: 5 additions & 0 deletions modules/camera/lib/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class Camera extends StatefulWidget
Key? key,
this.onCapture,
this.onComplete,
this.onError,
}) : super(key: key);

final Function? onCapture;
final Function? onComplete;
final Function(dynamic error)? onError;

final MyCameraController _controller = MyCameraController();

Expand Down Expand Up @@ -244,6 +246,9 @@ class CameraState extends EWidgetState<Camera> with WidgetsBindingObserver {
if (e is CameraException && e.code == 'CameraAccessDenied') {
hasPermission = false;
}
debugPrint(e.toString());
widget.onError?.call(e);
Navigator.pop(context);
}
setState(() {
isLoading = false;
Expand Down
41 changes: 21 additions & 20 deletions modules/camera/lib/camera_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:ensemble/util/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

import './camera.dart';

Expand Down Expand Up @@ -56,8 +55,14 @@ class CameraManagerImpl extends CameraManager {
Future<bool?> hasPermission() async {
bool? status;
try {
PermissionStatus permissionStatus = await Permission.camera.status;
status = permissionStatus.isGranted;
final cameras = await availableCameras();
if (cameras.isNotEmpty) {
await CameraController(cameras[0], ResolutionPreset.max).initialize();
status = true;
} else {
status = null;
}
return status;
} catch (error) {
if (error is CameraException) {
switch (error.code) {
Expand All @@ -75,8 +80,8 @@ class CameraManagerImpl extends CameraManager {
} else {
status = null;
}
return status;
}
return status;
}

Future<File?> convertXFile(XFile element) async {
Expand All @@ -94,22 +99,6 @@ class CameraManagerImpl extends CameraManager {
scopeManager?.dataContext.eval(cameraAction.options?['default']),
fallback: false);

final isCameraAllowed = await hasPermission();

if (!(isCameraAllowed ?? false)) {
final requestedPermissionStatus = await Permission.camera.request();

if ((!requestedPermissionStatus.isGranted) &&
cameraAction.onError != null) {
ScreenController().executeAction(context, cameraAction.onError!,
event: EnsembleEvent(null,
error: 'ensemble_camera: permission denied'));

debugPrint('ensemble_camera: permission denied');
return;
}
}

if (isDefault && !kIsWeb) {
await defaultCamera(context, cameraAction, scopeManager);
} else {
Expand Down Expand Up @@ -172,6 +161,18 @@ class CameraManagerImpl extends CameraManager {
ScreenController()
.executeAction(context, cameraAction.onComplete!);
},
onError: cameraAction.onError == null
? null
: (error) {
ScreenController().executeAction(
context,
cameraAction.onError!,
event: EnsembleEvent(
null,
error: error.toString(),
),
);
},
);

if (cameraAction.id != null) {
Expand Down
1 change: 0 additions & 1 deletion modules/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ dependencies:
sensors_plus: ^3.0.0
video_player: ^2.6.1
qr_code_scanner: ^1.0.1
permission_handler: ^11.3.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 2a908d3

Please sign in to comment.