Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-160: Handle permission for Camera #203

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2640,5 +2640,9 @@
"addACaption": "Add a caption...",
"noImagesFound": "No Images found",
"captionForImagesIsNotSupportYet": "Caption for images is not support yet.",
"tapToAllowAccessToYourGallery": "Tap to allow access to your Gallery"
"tapToAllowAccessToYourGallery": "Tap to allow access to your Gallery",
"tapToAllowAccessToYourCamera": "You can enable camera access in the Settings app to make video calls in",
"twake": "Twake",
"dismiss": "Dismiss",
"permissionAccess": "Permission access"
}
2 changes: 1 addition & 1 deletion lib/domain/usecase/send_image_interactor.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import 'package:fluffychat/presentation/extensions/asset_entity_extension.dart';
import 'package:fluffychat/presentation/extensions/room_extension.dart';
import 'package:matrix/matrix.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:fluffychat/presentation/extensions/asset_entity_extension.dart';

class SendImageInteractor {
Future<void> execute({
Expand Down
34 changes: 31 additions & 3 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui';

import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/usecase/send_image_interactor.dart';
import 'package:fluffychat/domain/usecase/send_images_interactor.dart';
import 'package:fluffychat/pages/chat/dialog_permission_camera_widget.dart';
import 'package:fluffychat/pages/forward/forward.dart';
import 'package:fluffychat/utils/network_connection_service.dart';
import 'package:fluffychat/utils/voip/permission_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
Expand All @@ -22,13 +25,10 @@ import 'package:image_picker/image_picker.dart';
import 'package:linagora_design_flutter/images_picker/images_picker.dart' hide ImagePicker;
import 'package:matrix/matrix.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:record/record.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/presentation/extensions/room_extension.dart';

import 'package:fluffychat/pages/chat/chat_view.dart';
import 'package:fluffychat/pages/chat/event_info_dialog.dart';
import 'package:fluffychat/pages/chat/recording_dialog.dart';
Expand All @@ -38,6 +38,7 @@ import 'package:fluffychat/utils/matrix_sdk_extensions/ios_badge_client_extensio
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
import '../../utils/account_bundles.dart';
import '../../utils/localized_exception_extension.dart';
import '../../utils/matrix_sdk_extensions/matrix_file_extension.dart';
Expand Down Expand Up @@ -1168,6 +1169,33 @@ class ChatController extends State<Chat> {
return PermissionHandlerService().requestPermissionForPhotoActions();
}

Future<PermissionStatus>? getCurrentCameraPermission() {
return PermissionHandlerService().requestPermissionForCameraActions();
}

Future<void> goToSettings() async {
final result = await showDialog<bool?>(
context: context,
useRootNavigator: false,
builder: (c) => const CupertinoDialogPermissionCamera(),
);

if (result == true) {
Navigator.pop(context);
}
}
void imagePickAction() async {
Navigator.pop(context);
final assetEntity = await CameraPicker.pickFromCamera(
context,
locale: window.locale,
);
if (assetEntity != null && assetEntity.type == AssetType.image) {
final sendImageInteractor = getIt.get<SendImageInteractor>();
sendImageInteractor.execute(room: room!, entity: assetEntity);
}
}

void removeAllImageSelected() {
imagePickerController.clearAssetCounter();
numberSelectedImagesNotifier.value = 0;
Expand Down
19 changes: 14 additions & 5 deletions lib/pages/chat/chat_input_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ class ChatInputRow extends StatelessWidget {
margin: const EdgeInsets.only(right: 4.0),
icon: Icons.add_circle_outline,
onPressed: () async {
final currentPermission = await controller.getCurrentPhotoPermission();
if (currentPermission != null) {
final currentPermissionPhotos = await controller.getCurrentPhotoPermission();
final currentPermissionCamera = await controller.getCurrentCameraPermission();
if (currentPermissionPhotos != null && currentPermissionCamera != null) {
showImagesPickerBottomSheet(
controller: controller,
context: context,
permissionStatus: currentPermission
permissionStatusPhotos: currentPermissionPhotos,
permissionStatusCamera: currentPermissionCamera,
).whenComplete(() => controller.removeAllImageSelected());
}
},
Expand Down Expand Up @@ -271,13 +273,14 @@ class _ChatAccountPicker extends StatelessWidget {
Future<void> showImagesPickerBottomSheet({
required BuildContext context,
required ChatController controller,
required PermissionStatus permissionStatus,
required PermissionStatus permissionStatusPhotos,
required PermissionStatus permissionStatusCamera,
}) async {
return await ImagePicker.showImagesGridBottomSheet(
context: context,
controller: controller.imagePickerController,
backgroundImageCamera: const AssetImage("assets/verification.png"),
permissionStatus: permissionStatus,
permissionStatus: permissionStatusPhotos,
assetBackgroundColor: LinagoraSysColors.material().background,
counterImageBuilder: (counterImage) {
if (counterImage == 0) {
Expand Down Expand Up @@ -408,5 +411,11 @@ Future<void> showImagesPickerBottomSheet({
),
],
),
cameraWidget: UseCameraWidget(
onPressed: permissionStatusCamera == PermissionStatus.granted
? () => controller.imagePickAction()
: () => controller.goToSettings(),
backgroundImage: const AssetImage("assets/verification.png"),
),
);
}
46 changes: 46 additions & 0 deletions lib/pages/chat/dialog_permission_camera_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:fluffychat/utils/voip/permission_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

class CupertinoDialogPermissionCamera extends StatelessWidget {
const CupertinoDialogPermissionCamera({super.key});

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(
L10n.of(context)!.permissionAccess,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
content: RichText(
text: TextSpan(
text: '${L10n.of(context)!.tapToAllowAccessToYourCamera} ',
style: Theme.of(context).textTheme.titleSmall,
children: <TextSpan>[
TextSpan(text: '${L10n.of(context)!.twake}.', style: Theme.of(context).textTheme.titleSmall!.copyWith(
fontWeight: FontWeight.bold,
)),
],
),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: Text(L10n.of(context)!.dismiss),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
PermissionHandlerService().goToSettingsForPermissionActions();
},
child: Text(L10n.of(context)!.settings),
),
],
);
}
}
8 changes: 5 additions & 3 deletions lib/presentation/extensions/room_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:collection';
import 'package:dartz/dartz.dart' hide id;
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/presentation/extensions/asset_entity_extension.dart';
import 'package:flutter/widgets.dart';
import 'package:matrix/matrix.dart';
import 'package:matrix/src/utils/file_send_request_credentials.dart';
import 'package:photo_manager/photo_manager.dart';
Expand Down Expand Up @@ -38,6 +37,7 @@ extension SendImage on Room {
try {
final mediaConfig = await client.getConfig();
final maxMediaSize = mediaConfig.mUploadSize;
Logs().d('SendImage::sendImageFileEvent(): FileSized ${file.bytes.lengthInBytes} || maxMediaSize $maxMediaSize');
if (maxMediaSize != null && maxMediaSize < file.bytes.lengthInBytes) {
throw FileTooBigMatrixException(file.bytes.lengthInBytes, maxMediaSize);
}
Expand Down Expand Up @@ -102,18 +102,20 @@ extension SendImage on Room {
contentType: uploadThumbnail.mimeType,
)
: null;
} on MatrixException catch (_) {
} on MatrixException catch (e) {
fakeImageEvent.rooms!.join!.values.first.timeline!.events!.first
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
await handleImageFakeSync(fakeImageEvent);
Logs().v('Error: $e');
rethrow;
} catch (_) {
} catch (e) {
if (DateTime.now().isAfter(timeoutDate)) {
fakeImageEvent.rooms!.join!.values.first.timeline!.events!.first
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
await handleImageFakeSync(fakeImageEvent);
rethrow;
}
Logs().v('Error: $e');
Logs().v('Send File into room failed. Try again...');
await Future.delayed(const Duration(seconds: 1));
}
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/voip/permission_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class PermissionHandlerService {
}
}

Future<PermissionStatus> requestPermissionForCameraActions() async {
final currentStatus = await Permission.camera.status;
if (currentStatus == PermissionStatus.denied || currentStatus == PermissionStatus.permanentlyDenied) {
final newStatus = await Permission.camera.request();
return newStatus.isGranted ? PermissionStatus.granted : newStatus;
} else {
return currentStatus;
}
}

Future<PermissionStatus> _handlePhotosPermissionIOSAction() async {
final currentStatus = await Permission.photos.status;
return _handlePhotoPermission(currentStatus);
Expand Down
48 changes: 48 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,46 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.3.3"
camera:
dependency: transitive
description:
name: camera
sha256: ebebead3d5ec3d148249331d751d462d7e8c98102b8830a9b45ec96a2bd4333f
url: "https://pub.dev"
source: hosted
version: "0.10.5+2"
camera_android:
dependency: transitive
description:
name: camera_android
sha256: f83e406d34f5faa80bf0f5c3beee4b4c11da94a94e9621c1bb8e312988621b4b
url: "https://pub.dev"
source: hosted
version: "0.10.8+2"
camera_avfoundation:
dependency: transitive
description:
name: camera_avfoundation
sha256: "1a416e452b30955b392f4efbf23291d3f2ba3660a85e1628859eb62d2a2bab26"
url: "https://pub.dev"
source: hosted
version: "0.9.13+2"
camera_platform_interface:
dependency: transitive
description:
name: camera_platform_interface
sha256: "60fa0bb62a4f3bf3a7c413e31e4cd01b69c779ccc8e4668904a24581b86c316b"
url: "https://pub.dev"
source: hosted
version: "2.5.1"
camera_web:
dependency: transitive
description:
name: camera_web
sha256: bcbd775fb3a9d51cc3ece899d54ad66f6306410556bac5759f78e13f9228841f
url: "https://pub.dev"
source: hosted
version: "0.3.1+4"
canonical_json:
dependency: transitive
description:
Expand Down Expand Up @@ -2640,6 +2680,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.12"
wechat_camera_picker:
dependency: "direct main"
description:
name: wechat_camera_picker
sha256: d8108ea33b1ed25933770199d08d64c210a2854bc6a326fd058a2f34dca8bf46
url: "https://pub.dev"
source: hosted
version: "3.8.0"
win32:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ dependencies:
flutter_inappwebview: ^5.7.2+3
tuple: ^2.0.2
lottie: ^2.3.2
wechat_camera_picker: ^3.8.0
dev_dependencies:
build_runner: ^2.3.3
dart_code_metrics: ^5.7.3
Expand Down