Skip to content

Commit

Permalink
Merge pull request #1793 from EnsembleUI/fix/camera-onerror
Browse files Browse the repository at this point in the history
fix: camera mobile browser
  • Loading branch information
snehmehta authored Dec 27, 2024
2 parents a9861bb + 07f9758 commit 8730689
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
48 changes: 30 additions & 18 deletions modules/camera/lib/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ class CameraState extends EWidgetState<Camera> with WidgetsBindingObserver {
],
),
),
widget._controller.allowCameraRotate
widget._controller.allowCameraRotate && cameras.length > 1
? buttons(
icon: widget._controller.cameraRotateIcon != null
? iconframework.Icon.fromModel(
Expand All @@ -916,25 +916,37 @@ class CameraState extends EWidgetState<Camera> with WidgetsBindingObserver {
color: iconColor,
),
backgroundColor: Colors.white.withOpacity(0.1),
onPressed: () {
currentModeIndex = 0;

if (isFrontCamera) {
final back = cameras.firstWhere((camera) =>
camera.lensDirection ==
CameraLensDirection.back);
setCamera(cameraDescription: back);
isFrontCamera = false;
} else {
final front = cameras.firstWhere((camera) =>
camera.lensDirection ==
CameraLensDirection.front);
setCamera(cameraDescription: front);
isFrontCamera = true;
onPressed: () async {
try {
if (cameras.length <= 1) return;
currentModeIndex = 0;
setState(() {
isLoading = true;
});
await widget._controller.cameraController
?.dispose();

if (isFrontCamera) {
final back = cameras.firstWhere((camera) =>
camera.lensDirection ==
CameraLensDirection.back);
setCamera(cameraDescription: back);
isFrontCamera = false;
} else {
final front = cameras.firstWhere((camera) =>
camera.lensDirection ==
CameraLensDirection.front);
setCamera(cameraDescription: front);
isFrontCamera = true;
}
setState(() {
isLoading = false;
});
} on Exception catch (_) {
Navigator.pop(context);
}
setState(() {});
})
: const SizedBox.shrink()
: SizedBox(width: iconSize * 2, height: iconSize * 2)
],
),
],
Expand Down
27 changes: 11 additions & 16 deletions modules/camera/lib/camera_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 @@ -55,18 +56,8 @@ class CameraManagerImpl extends CameraManager {
Future<bool?> hasPermission() async {
bool? status;
try {
final cameras = await availableCameras();
if (cameras.isNotEmpty) {
await CameraController(
cameras[0],
ResolutionPreset.max,
enableAudio: false,
).initialize();
status = true;
} else {
status = null;
}
return status;
PermissionStatus permissionStatus = await Permission.camera.status;
status = permissionStatus.isGranted;
} catch (error) {
if (error is CameraException) {
switch (error.code) {
Expand All @@ -84,8 +75,8 @@ class CameraManagerImpl extends CameraManager {
} else {
status = null;
}
return status;
}
return status;
}

Future<File?> convertXFile(XFile element) async {
Expand All @@ -106,13 +97,17 @@ class CameraManagerImpl extends CameraManager {
final isCameraAllowed = await hasPermission();

if (!(isCameraAllowed ?? false)) {
if (cameraAction.onError != null) {
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;
}
debugPrint('ensemble_camera: permission denied');
return;
}

if (isDefault && !kIsWeb) {
Expand Down
3 changes: 2 additions & 1 deletion modules/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
camera: ^0.10.5
camera: ^0.10.6
# embed Ensemble as a sibling project
ensemble:
git:
Expand All @@ -29,6 +29,7 @@ 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 8730689

Please sign in to comment.