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

fix: The permission workflow for the camera should now be OK #2763

Merged
merged 1 commit into from
Aug 11, 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
23 changes: 17 additions & 6 deletions packages/smooth_app/lib/helpers/permission_helper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:smooth_app/helpers/camera_helper.dart';
Expand Down Expand Up @@ -35,8 +37,11 @@ class PermissionListener extends ValueNotifier<DevicePermission> {
DevicePermissionStatus.checking,
);

_onNewPermissionStatus(await permission.request());
_status = _DevicePermissionStatus.asked;
await _requestPermission();
}

_status = _DevicePermissionStatus.answered;
}

Future<void> askPermission(
Expand All @@ -49,23 +54,29 @@ class PermissionListener extends ValueNotifier<DevicePermission> {

_status = _DevicePermissionStatus.asked;

// On non-Android platforms, this call will always return false
final bool showRationale = await permission.shouldShowRequestRationale;

if (showRationale) {
_onNewPermissionStatus(await permission.request());
} else {
// Directly ask for the permission on Android (first time) and iOS
if (showRationale || !Platform.isAndroid) {
await _requestPermission();
}

if (!value.isGranted) {
final bool? shouldOpenSettings = await onRationaleNotAvailable.call();

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

_status = _DevicePermissionStatus.answered;
}

void _onNewPermissionStatus(PermissionStatus status) {
Future<void> _requestPermission() async {
final PermissionStatus status = await permission.request();

value = DevicePermission._fromPermissionStatus(
permission,
status,
Expand Down
14 changes: 4 additions & 10 deletions packages/smooth_app/lib/pages/scan/scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/continuous_scan_model.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
Expand Down Expand Up @@ -49,15 +48,10 @@ class _ScanPageState extends State<ScanPage> {

return SmoothScaffold(
brightness: Brightness.light,
body: ChangeNotifierProvider<PermissionListener>(
create: (_) => PermissionListener(
permission: Permission.camera,
),
child: ScannerOverlay(
backgroundChild: const _ScanPageBackgroundWidget(),
foregroundChild: const _ScanPageForegroundWidget(),
topChild: const _ScanPageTopWidget(),
),
body: ScannerOverlay(
backgroundChild: const _ScanPageBackgroundWidget(),
foregroundChild: const _ScanPageForegroundWidget(),
topChild: const _ScanPageTopWidget(),
),
);
}
Expand Down