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

♻️ Make delegate respect generic types as much as possible #639

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
7 changes: 3 additions & 4 deletions example/lib/constants/picker_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,9 @@ class PickMethod {
return;
}
final picker = context.findAncestorWidgetOfExactType<
AssetPicker<AssetEntity, AssetPathEntity>>()!;
final builder =
picker.builder as DefaultAssetPickerBuilderDelegate;
final p = builder.provider;
AssetPicker<AssetEntity, AssetPathEntity,
DefaultAssetPickerBuilderDelegate>>()!;
final p = picker.builder.provider;
await p.switchPath(
PathWrapper<AssetPathEntity>(
path:
Expand Down
26 changes: 18 additions & 8 deletions example/lib/customs/pickers/directory_file_asset_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ class _DirectoryFileAssetPickerState extends State<DirectoryFileAssetPicker> {
return GestureDetector(
onTap: isDisplayingDetail
? () async {
final Widget viewer = AssetPickerViewer<File, Directory>(
final viewer = AssetPickerViewer<
File,
Directory,
FileAssetPickerViewerProvider,
FileAssetPickerViewerBuilderDelegate>(
builder: FileAssetPickerViewerBuilderDelegate(
currentIndex: index,
previewAssets: fileList,
Expand Down Expand Up @@ -392,7 +396,11 @@ class FileAssetPickerBuilder
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return AssetPickerViewer<File, Directory>(
return AssetPickerViewer<
File,
Directory,
FileAssetPickerViewerProvider,
FileAssetPickerViewerBuilderDelegate>(
builder: FileAssetPickerViewerBuilderDelegate(
currentIndex:
index ?? provider.selectedAssets.indexOf(currentAsset),
Expand All @@ -418,7 +426,8 @@ class FileAssetPickerBuilder
List<File>? selectedAssets,
FileAssetPickerProvider? selectorProvider,
}) async {
final Widget viewer = AssetPickerViewer<File, Directory>(
final viewer = AssetPickerViewer<File, Directory,
FileAssetPickerViewerProvider, FileAssetPickerViewerBuilderDelegate>(
builder: FileAssetPickerViewerBuilderDelegate(
currentIndex: index,
previewAssets: previewAssets,
Expand Down Expand Up @@ -1210,17 +1219,18 @@ class FileAssetPickerViewerProvider extends AssetPickerViewerProvider<File> {
}

class FileAssetPickerViewerBuilderDelegate
extends AssetPickerViewerBuilderDelegate<File, Directory> {
extends AssetPickerViewerBuilderDelegate<File, Directory,
FileAssetPickerViewerProvider> {
FileAssetPickerViewerBuilderDelegate({
required super.previewAssets,
required super.themeData,
required super.currentIndex,
super.selectedAssets,
super.selectorProvider,
this.selectorProvider,
super.provider,
}) : super(
maxAssets: selectorProvider?.maxAssets,
);
}) : super(maxAssets: selectorProvider?.maxAssets);

final FileAssetPickerProvider? selectorProvider;

late final PageController _pageController = PageController(
initialPage: currentIndex,
Expand Down
25 changes: 15 additions & 10 deletions example/lib/customs/pickers/insta_asset_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ class _InstaAssetPickerState extends State<InstaAssetPicker> {
}
}

class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
class InstaAssetPickerBuilder<T extends DefaultAssetPickerProvider>
extends DefaultAssetPickerBuilderDelegate<T> {
InstaAssetPickerBuilder({
required super.provider,
required super.initialPermission,
Expand Down Expand Up @@ -349,7 +350,7 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
/// Initialize [_previewAsset] with [p.selectedAssets] if not empty
/// otherwise if the first item of the album
Future<void> _initializePreviewAsset(
DefaultAssetPickerProvider p,
T p,
bool shouldDisplayAssets,
) async {
if (_previewAsset.value != null) {
Expand Down Expand Up @@ -487,8 +488,8 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
SizedBox(
width: MediaQuery.sizeOf(context).width,
height: previewHeight(context),
child: Selector<DefaultAssetPickerProvider, List<AssetEntity>>(
selector: (_, DefaultAssetPickerProvider p) => p.selectedAssets,
child: Selector<T, List<AssetEntity>>(
selector: (_, T p) => p.selectedAssets,
builder: (_, List<AssetEntity> selected, __) {
if (previewAsset == null && selected.isEmpty) {
return loadingIndicator(context);
Expand All @@ -502,7 +503,11 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
final List<AssetEntity> assets =
selected.isEmpty ? <AssetEntity>[previewAsset!] : selected;

return AssetPickerViewer<AssetEntity, AssetPathEntity>(
return AssetPickerViewer<
AssetEntity,
AssetPathEntity,
AssetPickerViewerProvider<AssetEntity>,
InstaAssetPickerViewerBuilder>(
builder: InstaAssetPickerViewerBuilder(
currentIndex: effectiveIndex == -1 ? 0 : effectiveIndex,
previewAssets: assets,
Expand Down Expand Up @@ -541,7 +546,7 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
_kPathSelectorRowHeight +
MediaQuery.paddingOf(context).top;

return ChangeNotifierProvider<DefaultAssetPickerProvider>.value(
return ChangeNotifierProvider<T>.value(
value: provider,
builder: (BuildContext context, _) => ValueListenableBuilder<double>(
valueListenable: _viewerPosition,
Expand Down Expand Up @@ -644,8 +649,8 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {

Widget _buildListAlbums(BuildContext context) {
appBarPreferredSize ??= appBar(context).preferredSize;
return Consumer<DefaultAssetPickerProvider>(
builder: (BuildContext context, DefaultAssetPickerProvider provider, __) {
return Consumer<T>(
builder: (BuildContext context, T provider, __) {
if (isAppleOS(context)) {
return pathEntityListWidget(context);
}
Expand All @@ -672,8 +677,8 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {

Widget _buildGrid(BuildContext context) {
appBarPreferredSize ??= appBar(context).preferredSize;
return Consumer<DefaultAssetPickerProvider>(
builder: (BuildContext context, DefaultAssetPickerProvider p, __) {
return Consumer<T>(
builder: (BuildContext context, T p, __) {
final bool shouldDisplayAssets =
p.hasAssetsToDisplay || shouldBuildSpecialItem;
_initializePreviewAsset(p, shouldDisplayAssets);
Expand Down
5 changes: 4 additions & 1 deletion example/lib/customs/pickers/multi_tabs_assets_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ class MultiTabAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate {
late final TabController _tabController;

@override
void initState(AssetPickerState<AssetEntity, AssetPathEntity> state) {
void initState(
AssetPickerState<AssetEntity, AssetPathEntity, MultiTabAssetPickerBuilder>
state,
) {
super.initState(state);
_tabController = TabController(length: 3, vsync: state);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2019 The FlutterCandies author. All rights reserved.
// Use of this source code is governed by an Apache license that can be found
// in the LICENSE file.

export 'src/widget/builder/asset_entity_grid_item_builder.dart';
export 'src/widget/builder/audio_page_builder.dart';
export 'src/widget/builder/image_page_builder.dart';
export 'src/widget/builder/video_page_builder.dart';
Loading