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

Check permission before opening picker #6

Merged
merged 3 commits into from
Feb 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 67 additions & 4 deletions lib/src/assets_picker.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:insta_assets_picker/insta_assets_picker.dart';
import 'package:insta_assets_picker/src/widget/insta_asset_picker_delegate.dart';
Expand All @@ -13,6 +15,33 @@ class InstaAssetPicker {
builder?.dispose();
}

/// Request the current [PermissionState] of required permissions.
///
/// Throw an error if permissions are unauthorized.
/// Since the exception is thrown from the MethodChannel it cannot be caught by a try/catch
///
/// check `AssetPickerDelegate.permissionCheck()` from flutter_wechat_assets_picker package for more information.
static Future<PermissionState> _permissionCheck() =>
AssetPicker.permissionCheck();

/// Open a [ScaffoldMessenger] describing the reason why the picker cannot be opened.
static void _openErrorPermission(
BuildContext context,
AssetPickerTextDelegate textDelegate,
Function(BuildContext, String)? customHandler,
) {
final defaultDescription =
'${textDelegate.unableToAccessAll}\n${textDelegate.goToSystemSettings}';

if (customHandler != null) {
customHandler(context, defaultDescription);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(defaultDescription)),
);
}
}

/// Build a [ThemeData] with the given [themeColor] for the picker.
///
/// check `AssetPickerDelegate.themeData()` from flutter_wechat_assets_picker package for more information.
Expand All @@ -29,6 +58,9 @@ class InstaAssetPicker {
/// By extending the [AssetPickerPageRoute], users can customize the route
/// and use it with the [pageRouteBuilder].
///
/// Set [onPermissionDenied] to manually handle the denied permission error.
/// The default behavior is to open a [ScaffoldMessenger].
///
/// Those arguments are used by [InstaAssetPickerBuilder]
///
/// - Set [provider] of type [DefaultAssetPickerProvider] to specifies picker options.
Expand Down Expand Up @@ -62,6 +94,8 @@ class InstaAssetPicker {
Key? key,
bool useRootNavigator = true,
AssetPickerPageRouteBuilder<List<AssetEntity>>? pageRouteBuilder,
Function(BuildContext context, String delegateDescription)?
onPermissionDenied,

/// InstaAssetPickerBuilder options
int gridCount = _kGridCount,
Expand All @@ -78,14 +112,27 @@ class InstaAssetPicker {
assert(provider.requestType == RequestType.image,
'Only images can be shown in the picker for now');

final locale = Localizations.maybeLocaleOf(context);
final text = textDelegate ?? assetPickerTextDelegateFromLocale(locale);

PermissionState? ps;
if (builder == null) {
try {
ps = await _permissionCheck();
} catch (e) {
_openErrorPermission(context, text, onPermissionDenied);
}
}

builder ??= InstaAssetPickerBuilder(
initialPermission: ps ?? PermissionState.denied,
provider: provider,
title: title,
gridCount: gridCount,
pickerTheme: pickerTheme ?? themeData(Theme.of(context).primaryColor),
locale: Localizations.maybeLocaleOf(context),
locale: locale,
keepScrollOffset: true,
textDelegate: textDelegate,
textDelegate: text,
loadingIndicatorBuilder: loadingIndicatorBuilder,
closeOnComplete: closeOnComplete,
isSquareDefaultCrop: isSquareDefaultCrop,
Expand All @@ -108,6 +155,9 @@ class InstaAssetPicker {
/// By extending the [AssetPickerPageRoute], users can customize the route
/// and use it with the [pageRouteBuilder].
///
/// Set [onPermissionDenied] to manually handle the denied permission error.
/// The default behavior is to open a [ScaffoldMessenger].
///
/// Those arguments are used by [InstaAssetPickerBuilder]
///
/// - Set [gridCount] to specifies the number of assets in the cross axis.
Expand Down Expand Up @@ -164,6 +214,8 @@ class InstaAssetPicker {
Key? key,
bool useRootNavigator = true,
AssetPickerPageRouteBuilder<List<AssetEntity>>? pageRouteBuilder,
Function(BuildContext context, String delegateDescription)?
onPermissionDenied,

/// InstaAssetPickerBuilder options
int gridCount = _kGridCount,
Expand Down Expand Up @@ -199,14 +251,25 @@ class InstaAssetPicker {
initializeDelayDuration: initializeDelayDuration,
);

final locale = Localizations.maybeLocaleOf(context);
final text = textDelegate ?? assetPickerTextDelegateFromLocale(locale);

PermissionState? ps;
try {
ps = await _permissionCheck();
} catch (e) {
_openErrorPermission(context, text, onPermissionDenied);
}

final InstaAssetPickerBuilder builder = InstaAssetPickerBuilder(
initialPermission: ps ?? PermissionState.denied,
provider: provider,
title: title,
gridCount: gridCount,
pickerTheme: pickerTheme ?? themeData(Theme.of(context).primaryColor),
locale: Localizations.maybeLocaleOf(context),
locale: locale,
keepScrollOffset: false,
textDelegate: textDelegate,
textDelegate: text,
loadingIndicatorBuilder: loadingIndicatorBuilder,
closeOnComplete: closeOnComplete,
isSquareDefaultCrop: isSquareDefaultCrop,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/widget/insta_asset_picker_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const _kPathSelectorRowHeight = 50.0;

class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
InstaAssetPickerBuilder({
required super.initialPermission,
required super.provider,
required this.onCompleted,
super.gridCount = 4,
Expand All @@ -39,7 +40,6 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
this.isSquareDefaultCrop = true,
}) : super(
shouldRevertGrid: false,
initialPermission: PermissionState.authorized,
specialItemPosition: SpecialItemPosition.none,
);

Expand Down