Skip to content

Commit

Permalink
🚚 rename builderOptions into pickerConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
LeGoffMael committed Jul 10, 2024
1 parent 5da1987 commit f570631
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 69 deletions.
3 changes: 1 addition & 2 deletions example/lib/pages/camera/camera_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ class _CameraPickerState extends State<CameraPicker> {
context,
onPressed: () => InstaAssetPicker.pickAssets(
context,
builderOptions: InstaAssetPickerBuilderOptions(
context,
pickerConfig: InstaAssetPickerConfig(
title: widget.description.fullLabel,
pickerTheme: widget.getPickerTheme(context),
actionsBuilder: (
Expand Down
3 changes: 1 addition & 2 deletions example/lib/pages/camera/wechat_camera_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class WeChatCameraPicker extends StatelessWidget with InstaPickerInterface {
context,
onPressed: () => InstaAssetPicker.pickAssets(
context,
builderOptions: InstaAssetPickerBuilderOptions(
context,
pickerConfig: InstaAssetPickerConfig(
title: description.fullLabel,
pickerTheme: getPickerTheme(context),
actionsBuilder: (
Expand Down
3 changes: 1 addition & 2 deletions example/lib/pages/restorable_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class _PickerScreenState extends State<RestorablePicker> {
final List<AssetEntity>? result =
await _instaAssetsPicker.restorableAssetsPicker(
context,
builderOptions: InstaAssetPickerBuilderOptions(
context,
pickerConfig: InstaAssetPickerConfig(
title: widget.description.fullLabel,
closeOnComplete: true,
pickerTheme: _pickerTheme,
Expand Down
3 changes: 1 addition & 2 deletions example/lib/widgets/insta_picker_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ mixin InstaPickerInterface on Widget {
void pickAssets(BuildContext context, {required int maxAssets}) =>
InstaAssetPicker.pickAssets(
context,
builderOptions: InstaAssetPickerBuilderOptions(
context,
pickerConfig: InstaAssetPickerConfig(
title: description.fullLabel,
closeOnComplete: true,
pickerTheme: getPickerTheme(context),
Expand Down
131 changes: 83 additions & 48 deletions lib/src/assets_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,54 @@ class InstaAssetCropDelegate {
final List<double> cropRatios;
}

class InstaAssetPickerBuilderOptions {
InstaAssetPickerBuilderOptions(
BuildContext context, {
ThemeData? pickerTheme,
AssetPickerTextDelegate? textDelegate,
Locale? locale,
/// Configurations for the [InstaAssetPickerBuilder].
class InstaAssetPickerConfig {
const InstaAssetPickerConfig({
/// [DefaultAssetPickerBuilderDelegate] config
this.gridCount = _kGridCount,
this.pickerTheme,
this.specialItemPosition,
this.specialItemBuilder,
this.loadingIndicatorBuilder,
this.selectPredicate,
this.limitedPermissionOverlayPredicate,
this.themeColor,
this.textDelegate,
this.locale,
this.gridThumbnailSize = defaultAssetGridPreviewSize,

/// [InstaAssetPickerBuilder] config
this.title,
this.closeOnComplete = false,
this.actionsBuilder,
});

InstaAssetPickerConfig.withContext(
BuildContext context, {
/// [DefaultAssetPickerBuilderDelegate] config
this.gridCount = _kGridCount,
this.specialItemPosition,
this.specialItemBuilder,
this.loadingIndicatorBuilder,
this.selectPredicate,
this.limitedPermissionOverlayPredicate,
this.specialItemBuilder,
this.specialItemPosition,
this.themeColor,
this.gridThumbnailSize = defaultAssetGridPreviewSize,

/// [InstaAssetPickerBuilder] config
this.title,
this.closeOnComplete = false,
this.actionsBuilder,
}) : pickerTheme = pickerTheme ??
}) : pickerTheme =
InstaAssetPicker.themeData(Theme.of(context).primaryColor),
textDelegate =
textDelegate ?? InstaAssetPicker.defaultTextDelegate(context),
locale = locale ?? Localizations.maybeLocaleOf(context);
textDelegate = InstaAssetPicker.defaultTextDelegate(context),
locale = Localizations.maybeLocaleOf(context);

/* [DefaultAssetPickerBuilderDelegate] config */

/// Specifies the number of assets in the cross axis.
/// Defaults to [_kGridCount], like instagram.
Expand All @@ -61,38 +90,48 @@ class InstaAssetPickerBuilderOptions {
/// It is by default initialized with the `primaryColor` of the context theme.
final ThemeData? pickerTheme;

/// Set a special item in the picker with several positions.
/// Since the grid view is reversed, [SpecialItemPosition.prepend]
/// will be at the top and [SpecialItemPosition.append] at the bottom.
/// Defaults to [SpecialItemPosition.none].
final SpecialItemPosition? specialItemPosition;

/// Specifies [Widget] for the the special item.
final SpecialItemBuilder<AssetPathEntity>? specialItemBuilder;

/// The loader indicator to display in the picker.
final LoadingIndicatorBuilder? loadingIndicatorBuilder;

final AssetSelectPredicate<AssetEntity>? selectPredicate;

/// Specifies if the limited permission overlay should be displayed.
final LimitedPermissionOverlayPredicate? limitedPermissionOverlayPredicate;

/// Main color for the picker.
final Color? themeColor;

/// Specifies the language to apply to the picker.
/// Default is the locale language from the context.
final AssetPickerTextDelegate? textDelegate;

/// Identifier used to select picker language
final Locale? locale;

/// Thumbnail size in the grid.
final ThumbnailSize gridThumbnailSize;

/* [InstaAssetPickerBuilder] config */

/// Specifies the text title in the picker [AppBar].
final String? title;

/// Specifies if the picker should be closed after assets selection confirmation.
/// Defaults to `false`.
final bool closeOnComplete;

/// The loader indicator to display in the picker.
final LoadingIndicatorBuilder? loadingIndicatorBuilder;

/// Specifies if the limited permission overlay should be displayed.
final LimitedPermissionOverlayPredicate? limitedPermissionOverlayPredicate;

/// Specifies [Widget] for the the special item.
final SpecialItemBuilder<AssetPathEntity>? specialItemBuilder;

/// Set a special item in the picker with several positions.
/// Since the grid view is reversed, [SpecialItemPosition.prepend]
/// will be at the top and [SpecialItemPosition.append] at the bottom.
/// Defaults to [SpecialItemPosition.none].
final SpecialItemPosition? specialItemPosition;

/// The [Widget] to display on top of the assets grid view.
/// Default is unselect all assets button.
final InstaPickerActionsBuilder? actionsBuilder;

/// Identifier used to select picker language
final Locale? locale;
}

class InstaAssetPicker {
Expand Down Expand Up @@ -183,7 +222,7 @@ class InstaAssetPicker {
/// Set [onPermissionDenied] to manually handle the denied permission error.
/// The default behavior is to open a [ScaffoldMessenger].
///
/// Crop options
/// Crop parameters
/// - Set [cropDelegate] to customize the display and export of crops.
///
/// Those arguments are used by [InstaAssetPickerBuilder]
Expand All @@ -195,7 +234,7 @@ class InstaAssetPicker {
/// - The [onCompleted] callback is called when the assets selection is confirmed.
/// It will as argument a [Stream] with exportation details [InstaAssetsExportDetails].
///
/// - Set [builderOptions] to specifies more optional parameters for the picker.
/// - Set [pickerConfig] to specifies more optional parameters for the picker.
Future<List<AssetEntity>?> restorableAssetsPicker(
BuildContext context, {
Key? key,
Expand All @@ -204,24 +243,22 @@ class InstaAssetPicker {
Function(BuildContext context, String delegateDescription)?
onPermissionDenied,

/// Crop options
/// Crop parameters
InstaAssetCropDelegate cropDelegate = const InstaAssetCropDelegate(),

/// InstaAssetPickerBuilder options
/// InstaAssetPickerBuilder parameters
required DefaultAssetPickerProvider Function() provider,
required Function(Stream<InstaAssetsExportDetails> exportDetails)
onCompleted,
InstaAssetPickerBuilderOptions? builderOptions,
InstaAssetPickerConfig pickerConfig = const InstaAssetPickerConfig(),
}) async {
builderOptions ??= InstaAssetPickerBuilderOptions(context);

PermissionState? ps;
try {
ps = await _permissionCheck();
} catch (e) {
_openErrorPermission(
context,
builderOptions.textDelegate,
pickerConfig.textDelegate,
onPermissionDenied,
);
return [];
Expand All @@ -238,7 +275,7 @@ class InstaAssetPicker {
keepScrollOffset: true,
cropDelegate: cropDelegate,
onCompleted: onCompleted,
options: builderOptions,
config: pickerConfig,
);

return AssetPicker.pickAssetsWithDelegate(
Expand Down Expand Up @@ -268,7 +305,7 @@ class InstaAssetPicker {
/// - The [onCompleted] callback is called when the assets selection is confirmed.
/// It will as argument a [Stream] with exportation details [InstaAssetsExportDetails].
///
/// - Set [builderOptions] to specifies more optional parameters for the picker.
/// - Set [pickerConfig] to specifies more optional parameters for the picker.
///
/// Those arguments are used by [DefaultAssetPickerProvider]
///
Expand Down Expand Up @@ -303,15 +340,15 @@ class InstaAssetPicker {
Function(BuildContext context, String delegateDescription)?
onPermissionDenied,

/// Crop options
/// Crop parameters
InstaAssetCropDelegate cropDelegate = const InstaAssetCropDelegate(),

/// InstaAssetPickerBuilder options
/// InstaAssetPickerBuilder parameters
required Function(Stream<InstaAssetsExportDetails> exportDetails)
onCompleted,
InstaAssetPickerBuilderOptions? builderOptions,
InstaAssetPickerConfig pickerConfig = const InstaAssetPickerConfig(),

/// DefaultAssetPickerProvider options
/// DefaultAssetPickerProvider parameters
List<AssetEntity>? selectedAssets,
int maxAssets = defaultMaxAssetsCount,
int pageSize = defaultAssetsPerPage,
Expand All @@ -322,16 +359,14 @@ class InstaAssetPicker {
PMFilter? filterOptions,
Duration initializeDelayDuration = _kInitializeDelayDuration,
}) async {
builderOptions ??= InstaAssetPickerBuilderOptions(context);

// must be called before initializing any picker provider to avoid `PlatformException(PERMISSION_REQUESTING)` type exception
PermissionState? ps;
try {
ps = await _permissionCheck();
} catch (e) {
_openErrorPermission(
context,
builderOptions.textDelegate,
pickerConfig.textDelegate,
onPermissionDenied,
);
return [];
Expand All @@ -355,7 +390,7 @@ class InstaAssetPicker {
keepScrollOffset: false,
cropDelegate: cropDelegate,
onCompleted: onCompleted,
options: builderOptions,
config: pickerConfig,
);

return AssetPicker.pickAssetsWithDelegate(
Expand Down
29 changes: 16 additions & 13 deletions lib/src/widget/insta_asset_picker_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,28 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
required super.initialPermission,
required super.provider,
required this.onCompleted,
required InstaAssetPickerBuilderOptions options,
required InstaAssetPickerConfig config,
InstaAssetCropDelegate cropDelegate = const InstaAssetCropDelegate(),
super.keepScrollOffset,
}) : _cropController =
InstaAssetsCropController(keepScrollOffset, cropDelegate),
title = options.title,
closeOnComplete = options.closeOnComplete,
actionsBuilder = options.actionsBuilder,
title = config.title,
closeOnComplete = config.closeOnComplete,
actionsBuilder = config.actionsBuilder,
super(
gridCount: options.gridCount,
pickerTheme: options.pickerTheme,
textDelegate: options.textDelegate,
loadingIndicatorBuilder: options.loadingIndicatorBuilder,
limitedPermissionOverlayPredicate:
options.limitedPermissionOverlayPredicate,
specialItemBuilder: options.specialItemBuilder,
gridCount: config.gridCount,
pickerTheme: config.pickerTheme,
specialItemPosition:
options.specialItemPosition ?? SpecialItemPosition.none,
locale: options.locale,
config.specialItemPosition ?? SpecialItemPosition.none,
specialItemBuilder: config.specialItemBuilder,
loadingIndicatorBuilder: config.loadingIndicatorBuilder,
selectPredicate: config.selectPredicate,
limitedPermissionOverlayPredicate:
config.limitedPermissionOverlayPredicate,
themeColor: config.themeColor,
textDelegate: config.textDelegate,
locale: config.locale,
gridThumbnailSize: config.gridThumbnailSize,
shouldRevertGrid: false,
);

Expand Down

0 comments on commit f570631

Please sign in to comment.