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

feat: If camera permission is denied, allow the user to approve it again #1583

Merged
merged 8 commits into from
Apr 17, 2022
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
2 changes: 0 additions & 2 deletions .github/workflows/crowdin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Crowdin Action
on:
push:
branches: [ crowdin-trigger ]
schedule:
- cron: "0 0 * * *"

jobs:
synchronize-with-crowdin:
Expand Down
8 changes: 0 additions & 8 deletions packages/smooth_app/lib/data_models/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class UserPreferences extends ChangeNotifier {
'lastVisitedOnboardingPage';
static const String _TAG_PREFIX_FLAG = 'FLAG_PREFIX_';
static const String _TAG_DEV_MODE = 'devMode';
static const String _TAG_CAMERA_DECLINE = 'declined_camera_use_once';
static const String _TAG_CRASH_REPORTS = 'crash_reports';
static const String _TAG_ANALYTICS_REPORTS = 'analytics_reports';
static const String _TAG_EXCLUDED_ATTRIBUTE_IDS = 'excluded_attributes';
Expand Down Expand Up @@ -90,13 +89,6 @@ class UserPreferences extends ChangeNotifier {
: OnboardingPage.values[pageIndex];
}

Future<void> setCameraDecline(final bool declined) async {
_sharedPreferences.setBool(_TAG_CAMERA_DECLINE, declined);
}

bool get cameraDeclinedOnce =>
_sharedPreferences.getBool(_TAG_CAMERA_DECLINE) ?? false;

String _getFlagTag(final String key) => _TAG_PREFIX_FLAG + key;

Future<void> setFlag(
Expand Down
4 changes: 2 additions & 2 deletions packages/smooth_app/lib/generic_lib/widgets/smooth_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class SmoothCard extends StatelessWidget {

final Widget child;
final Color? color;
final EdgeInsets? margin;
final EdgeInsets? padding;
final EdgeInsetsGeometry? margin;
final EdgeInsetsGeometry? padding;
final double elevation;

@override
Expand Down
93 changes: 93 additions & 0 deletions packages/smooth_app/lib/helpers/permission_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionListener extends ValueNotifier<DevicePermission> {
PermissionListener({
required this.permission,
}) : super(DevicePermission._initial(permission)) {
checkPermission();
}

final Permission permission;

Future<void> checkPermission() async {
value = DevicePermission._(
permission,
DevicePermissionStatus.checking,
);

_onNewPermissionStatus(await permission.request());
}

Future<void> askPermission(
Future<bool?> Function() onRationaleNotAvailable,
) async {
final bool showRationale = await permission.shouldShowRequestRationale;

if (showRationale) {
_onNewPermissionStatus(await permission.request());
} else {
final bool? shouldOpenSettings = await onRationaleNotAvailable.call();

if (shouldOpenSettings == true) {
await openAppSettings();
return checkPermission();
}
}
}

void _onNewPermissionStatus(PermissionStatus status) {
value = DevicePermission._fromPermissionStatus(
permission,
status,
);
}
}

class DevicePermission {
const DevicePermission._(this.permission, this.status);

const DevicePermission._initial(this.permission)
: status = DevicePermissionStatus.checking;

DevicePermission._fromPermissionStatus(
this.permission, PermissionStatus status)
: status = _extractFromPermissionStatus(status);

final Permission permission;
final DevicePermissionStatus status;

static DevicePermissionStatus _extractFromPermissionStatus(
PermissionStatus status,
) {
switch (status) {
case PermissionStatus.denied:
return DevicePermissionStatus.denied;
case PermissionStatus.granted:
return DevicePermissionStatus.granted;
case PermissionStatus.restricted:
return DevicePermissionStatus.restricted;
case PermissionStatus.limited:
return DevicePermissionStatus.limited;
case PermissionStatus.permanentlyDenied:
return DevicePermissionStatus.permanentlyDenied;
}
}

bool get isGranted => status == DevicePermissionStatus.granted;

@override
String toString() {
return 'DevicePermission{permission: $permission, status: $status}';
}
}

enum DevicePermissionStatus {
checking,
denied,
granted,
restricted,
limited,
permanentlyDenied,
}
25 changes: 1 addition & 24 deletions packages/smooth_app/lib/helpers/picture_capture_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ Future<bool> uploadCapturedPicture(
ProductQuery.getUser(),
image,
),
title: _imageFieldLabel(
appLocalizations,
imageField,
),
title: appLocalizations.uploading_image,
);
if (result == null || result.error != null || result.status != 'status ok') {
await LoadingDialog.error(
Expand All @@ -42,26 +39,6 @@ Future<bool> uploadCapturedPicture(
return true;
}

String _imageFieldLabel(
AppLocalizations appLocalizations,
ImageField field,
) {
switch (field) {
case ImageField.FRONT:
return appLocalizations.uploading_image_type_front;
case ImageField.INGREDIENTS:
return appLocalizations.uploading_image_type_ingredients;
case ImageField.NUTRITION:
return appLocalizations.uploading_image_type_nutrition;
case ImageField.PACKAGING:
return appLocalizations.uploading_image_type_packaging;
case ImageField.OTHER:
return appLocalizations.uploading_image_type_other;
default:
return appLocalizations.uploading_image_type_generic;
}
}

Future<void> _updateContinuousScanModel(
BuildContext context, String barcode) async {
final ContinuousScanModel? model =
Expand Down
23 changes: 23 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"app_name": "Smoothie",
"@Utils": {},
"yes": "Yes",
"@yes": {},
Expand Down Expand Up @@ -438,6 +439,10 @@
"@selecting_photo": {
"description": "Progress indicator when the users takes a photo"
},
"uploading_image": "Uploading photo to the server",
"@uploading_image": {
"description": "Message when a new picture is uploading to the server"
},
"uploading_image_type_front": "Uploading front image to Open Food Facts",
"@uploading_image_type_front": {
"description": "Message when a new front picture is being uploaded to the server"
Expand Down Expand Up @@ -747,6 +752,24 @@
"@permission_photo_error": {
"description": "When the camera/photo permission failed to be acquired (!= denied)"
},
"permission_photo_denied_title": "Allow camera use to scan barcodes",
"permission_photo_denied_message": "For an enhanced experience, please allow {appName} to access your camera. You will be able to directly scan barcodes.",
"@permission_photo_denied_message": {
"description": "When the camera/photo permission is denied by user",
"placeholders": {
"appName": {
"type": "String"
}
}
},
"permission_photo_denied_button": "Allow",
"@permission_photo_denied_button": {
"description": "When the camera/photo permission is denied by user"
},
"permission_photo_denied_dialog_settings_title": "Permission denied",
"permission_photo_denied_dialog_settings_message": "As you've previously denied the camera permission, you must allow it manually from the Settings.",
"permission_photo_denied_dialog_settings_button_open": "Open settings",
"permission_photo_denied_dialog_settings_button_cancel": "Cancel",
"permission_photo_denied": "No camera access granted",
"@permission_photo_denied": {
"description": "When the camera/photo permission is denied by user"
Expand Down
Loading