Skip to content

Commit

Permalink
✨ Integrate PermissionRequestOption for callers (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Dec 6, 2023
1 parent 540f678 commit b702fd6
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ that can be found in the LICENSE file. -->

See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.

## 9.0.0-dev.1

### Breaking changes

- Integrate `PermissionRequestOption` for callers.

## 8.9.0-dev.1

### Breaking changes
Expand Down
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ apply plugin: 'kotlin-kapt'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 33
compileSdkVersion 34

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -40,7 +40,7 @@ android {
defaultConfig {
applicationId "com.fluttercandies.wechatAssetsPickerExample"
minSdkVersion 21
targetSdkVersion 33
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
10 changes: 8 additions & 2 deletions example/lib/customs/pickers/insta_asset_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ class _InstaAssetPickerState extends State<InstaAssetPicker> {
}

Future<void> callPicker(BuildContext context) async {
final PermissionState ps = await AssetPicker.permissionCheck();

final PermissionState ps = await AssetPicker.permissionCheck(
requestOption: PermissionRequestOption(
androidPermission: AndroidPermission(
type: provider.requestType,
mediaLocation: false,
),
),
);
final InstaAssetPickerBuilder builder = InstaAssetPickerBuilder(
provider: provider,
initialPermission: ps,
Expand Down
9 changes: 8 additions & 1 deletion example/lib/customs/pickers/multi_tabs_assets_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ class _MultiTabAssetPickerState extends State<MultiTabAssetPicker> {
bool isDisplayingDetail = true;

Future<void> callPicker(BuildContext context) async {
final PermissionState ps = await AssetPicker.permissionCheck();
final PermissionState ps = await AssetPicker.permissionCheck(
requestOption: const PermissionRequestOption(
androidPermission: AndroidPermission(
type: RequestType.all,
mediaLocation: false,
),
),
);

final DefaultAssetPickerProvider provider = DefaultAssetPickerProvider(
selectedAssets: entities,
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: wechat_assets_picker_demo
description: The demo project for the wechat_assets_picker package.
version: 8.8.0+47
version: 9.0.0+48
publish_to: none

environment:
Expand Down
60 changes: 60 additions & 0 deletions guides/migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This document gathered all breaking changes and migrations requirement between m

## Major versions

- [9.0.0](#900)
- [8.6.0](#860)
- [8.3.0](#830)
- [8.2.0](#820)
Expand All @@ -16,6 +17,65 @@ This document gathered all breaking changes and migrations requirement between m
- [6.0.0](#600)
- [5.0.0](#500)

## 9.0.0

### Summary

`PermissionRequestOption` has been added to
`AssetPickerDelegate.permissionCheck` and
`AssetPickerDelegate.pickAssetsWithDelegate` as an argument.
Classes that extend `AssetPickerDelegate` and override these methods must migrate,
Delegates that use `AssetPicker.permissionCheck`
should choose whether to pass the request option.

### Details

Before:

1. ```dart
AssetPicker.permissionCheck();
```

2. ```dart
Future<PermissionState> permissionCheck();
```

3. ```dart
Future<List<Asset>?> pickAssetsWithDelegate<Asset, Path,
PickerProvider extends AssetPickerProvider<Asset, Path>>(
BuildContext context, {
required AssetPickerBuilderDelegate<Asset, Path> delegate,
Key? key,
bool useRootNavigator = true,
AssetPickerPageRouteBuilder<List<Asset>>? pageRouteBuilder,
})
```

After:

1. ```dart
AssetPicker.permissionCheck(requestOption: ...);
```

2. ```dart
Future<PermissionState> permissionCheck({
PermissionRequestOption requestOption = const PermissionRequestOption,
});
```

3. ```dart
Future<List<Asset>?> pickAssetsWithDelegate<Asset, Path,
PickerProvider extends AssetPickerProvider<Asset, Path>>(
BuildContext context, {
required AssetPickerBuilderDelegate<Asset, Path> delegate,
PermissionRequestOption requestOption =
const PermissionRequestOption,
Key? key,
bool useRootNavigator = true,
AssetPickerPageRouteBuilder<List<Asset>>? pageRouteBuilder,
})
```

## 8.6.0

### Summary
Expand Down
21 changes: 17 additions & 4 deletions lib/src/delegates/asset_picker_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ class AssetPickerDelegate {
/// See also:
/// * [PermissionState] which defined all states of required permissions.
/// {@endtemplate}
Future<PermissionState> permissionCheck() async {
final PermissionState ps = await PhotoManager.requestPermissionExtend();
Future<PermissionState> permissionCheck({
PermissionRequestOption requestOption = const PermissionRequestOption(),
}) async {
final PermissionState ps = await PhotoManager.requestPermissionExtend(
requestOption: requestOption,
);
if (ps != PermissionState.authorized && ps != PermissionState.limited) {
throw StateError('Permission state error with $ps.');
}
Expand Down Expand Up @@ -64,7 +68,14 @@ class AssetPickerDelegate {
bool useRootNavigator = true,
AssetPickerPageRouteBuilder<List<AssetEntity>>? pageRouteBuilder,
}) async {
final PermissionState ps = await permissionCheck();
final PermissionState ps = await permissionCheck(
requestOption: PermissionRequestOption(
androidPermission: AndroidPermission(
type: pickerConfig.requestType,
mediaLocation: false,
),
),
);
final AssetPickerPageRoute<List<AssetEntity>> route =
pageRouteBuilder?.call(const SizedBox.shrink()) ??
AssetPickerPageRoute<List<AssetEntity>>(
Expand Down Expand Up @@ -134,11 +145,13 @@ class AssetPickerDelegate {
PickerProvider extends AssetPickerProvider<Asset, Path>>(
BuildContext context, {
required AssetPickerBuilderDelegate<Asset, Path> delegate,
PermissionRequestOption permissionRequestOption =
const PermissionRequestOption(),
Key? key,
bool useRootNavigator = true,
AssetPickerPageRouteBuilder<List<Asset>>? pageRouteBuilder,
}) async {
await permissionCheck();
await permissionCheck(requestOption: permissionRequestOption);
final Widget picker = AssetPicker<Asset, Path>(
key: key,
builder: delegate,
Expand Down
6 changes: 4 additions & 2 deletions lib/src/widget/asset_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ class AssetPicker<Asset, Path> extends StatefulWidget {
}

/// {@macro wechat_assets_picker.delegates.AssetPickerDelegate.permissionCheck}
static Future<PermissionState> permissionCheck() {
return _pickerDelegate.permissionCheck();
static Future<PermissionState> permissionCheck({
PermissionRequestOption requestOption = const PermissionRequestOption(),
}) {
return _pickerDelegate.permissionCheck(requestOption: requestOption);
}

/// {@macro wechat_assets_picker.delegates.AssetPickerDelegate.pickAssets}
Expand Down
8 changes: 6 additions & 2 deletions lib/src/widget/asset_picker_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ class AssetPickerViewer<Asset, Path> extends StatefulWidget {
int? maxAssets,
bool shouldReversePreview = false,
AssetSelectPredicate<AssetEntity>? selectPredicate,
PermissionRequestOption permissionRequestOption =
const PermissionRequestOption(),
}) async {
await AssetPicker.permissionCheck();
await AssetPicker.permissionCheck(requestOption: permissionRequestOption);
final Widget viewer = AssetPickerViewer<AssetEntity, AssetPathEntity>(
builder: DefaultAssetPickerViewerBuilderDelegate(
currentIndex: currentIndex,
Expand Down Expand Up @@ -82,8 +84,10 @@ class AssetPickerViewer<Asset, Path> extends StatefulWidget {
static Future<List<A>?> pushToViewerWithDelegate<A, P>(
BuildContext context, {
required AssetPickerViewerBuilderDelegate<A, P> delegate,
PermissionRequestOption permissionRequestOption =
const PermissionRequestOption(),
}) async {
await AssetPicker.permissionCheck();
await AssetPicker.permissionCheck(requestOption: permissionRequestOption);
final Widget viewer = AssetPickerViewer<A, P>(builder: delegate);
final PageRouteBuilder<List<A>> pageRoute = PageRouteBuilder<List<A>>(
pageBuilder: (_, __, ___) => viewer,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: wechat_assets_picker
version: 8.9.0-dev.1
version: 9.0.0-dev.1
description: |
An image picker (also with videos and audio)
for Flutter projects based on WeChat's UI,
Expand Down
13 changes: 11 additions & 2 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class TestPhotoManagerPlugin extends PhotoManagerPlugin {

class TestAssetPickerDelegate extends AssetPickerDelegate {
@override
Future<PermissionState> permissionCheck() async {
Future<PermissionState> permissionCheck({
PermissionRequestOption requestOption = const PermissionRequestOption(),
}) async {
return SynchronousFuture<PermissionState>(PermissionState.authorized);
}

Expand All @@ -85,7 +87,14 @@ class TestAssetPickerDelegate extends AssetPickerDelegate {
bool useRootNavigator = true,
AssetPickerPageRouteBuilder<List<AssetEntity>>? pageRouteBuilder,
}) async {
final PermissionState ps = await permissionCheck();
final PermissionState ps = await permissionCheck(
requestOption: PermissionRequestOption(
androidPermission: AndroidPermission(
type: pickerConfig.requestType,
mediaLocation: false,
),
),
);
final AssetPathEntity pathEntity = AssetPathEntity(
id: 'test',
name: 'pathEntity',
Expand Down

0 comments on commit b702fd6

Please sign in to comment.