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

⚡️ Use viewAsset in the preview button #508

Merged
merged 3 commits into from
Nov 16, 2023

Conversation

AlexV525
Copy link
Member

@AlexV525
Copy link
Member Author

@rbarzel Could you verify if this works as intended?

@rbarzel
Copy link

rbarzel commented Nov 15, 2023

@AlexV525 This change does invoke viewAsset when preview is clicked. But the actual display looks strange with the edge-to-edge semi transparent background. (See screenshot)

Also, even if this is now working as intended, I still don't understand how I can use viewAsset to customize the preview screen. Perhaps what's needed is a new method like viewAssetWithDelegate that allows me to pass in my own AssetPickerViewerBuilderDelegate.

Thanks for your help!

Screenshot_20231115-070825

@AlexV525
Copy link
Member Author

Override viewAsset and use push to viewer with delegate within.

@rbarzel
Copy link

rbarzel commented Nov 15, 2023

That's what I was doing, but most of my overridden DefaultAssetPickerViewerBuilderDelegate methods do not get called. The only overriden method getting called is assetPageBuilder. Here's my code.

  @override
  Future<void> viewAsset(
    BuildContext context,
    int index,
    AssetEntity currentAsset,
  ) async {
    // - When we reached the maximum select count and the asset is not selected,
    //   do nothing.
    // - When the special type is WeChat Moment, pictures and videos cannot
    //   be selected at the same time. Video select should be banned if any
    //   pictures are selected.

    logger.w('_MyAssetPickerBuilderDelegate: viewAsset');

    if ((!provider.selectedAssets.contains(currentAsset) &&
            provider.selectedMaximumAssets) ||
        (isWeChatMoment &&
            currentAsset.type == AssetType.video &&
            provider.selectedAssets.isNotEmpty)) {
      return;
    }
    final List<AssetEntity> current;
    final List<AssetEntity>? selected;
    final int effectiveIndex;
    if (isWeChatMoment) {
      if (currentAsset.type == AssetType.video) {
        current = <AssetEntity>[currentAsset];
        selected = null;
        effectiveIndex = 0;
      } else {
        current = provider.currentAssets
            .where((AssetEntity e) => e.type == AssetType.image)
            .toList();
        selected = provider.selectedAssets;
        effectiveIndex = current.indexOf(currentAsset);
      }
    } else {
      current = provider.currentAssets;
      selected = provider.selectedAssets;
      effectiveIndex = index;
    }
    final List<AssetEntity>? result =
        await AssetPickerViewer.pushToViewerWithDelegate(
      context,
      delegate: _MyAssetPickerViewerBuilderDelegate(
        currentIndex: effectiveIndex,
        previewAssets: current,
        themeData: theme,
      ),
    );

    if (result != null && context.mounted) {
      Navigator.of(context).maybePop(result);
    }
  }
class _MyAssetPickerViewerBuilderDelegate
    extends DefaultAssetPickerViewerBuilderDelegate {
  _MyAssetPickerViewerBuilderDelegate({
    required super.currentIndex,
    required super.previewAssets,
    required super.themeData,
  });

  @override
  int get selectedCount {
    logger.w('_MyAssetPickerViewerBuilderDelegate: selectedCount');
    return super.selectedCount;
  }

  @override
  double get bottomBarHeight {
    logger.w('_MyAssetPickerViewerBuilderDelegate: bottomBarHeight');
    return super.bottomBarHeight;
  }

  @override
  AssetEntity get currentAsset {
    logger.w('_MyAssetPickerViewerBuilderDelegate: currentAsset');
    return super.currentAsset;
  }

  @override
  Widget bottomDetailBuilder(BuildContext context) {
    logger.w('_MyAssetPickerViewerBuilderDelegate: bottomDetailBuilder');
    return super.bottomDetailBuilder(context);
  }

  @override
  Widget build(BuildContext context) {
    logger.w('_MyAssetPickerViewerBuilderDelegate: build');
    return super.build(context);
  }

  @override
  Widget assetPageBuilder(BuildContext context, int index) {
    logger.w('_MyAssetPickerViewerBuilderDelegate: assetPageBuilder');
    return super.assetPageBuilder(context, index);
  }

  @override
  Widget confirmButton(BuildContext context) {
    logger.w('_MyAssetPickerViewerBuilderDelegate: confirmButton');
    return super.confirmButton(context);
  }

  @override
  AssetPickerTextDelegate get textDelegate {
    logger.w('_MyAssetPickerViewerBuilderDelegate: textDelegate');
    return const EnglishAssetPickerTextDelegate();
  }

  @override
  Widget selectButton(BuildContext context) {
    logger.w('_MyAssetPickerViewerBuilderDelegate: selectButton');
    return super.selectButton(context);
  }

  @override
  Widget appBar(BuildContext context) {
    logger.w('_MyAssetPickerViewerBuilderDelegate: appBar');
    return super.appBar(context);
  }
}

@AlexV525
Copy link
Member Author

image

@AlexV525 AlexV525 marked this pull request as ready for review November 16, 2023 01:56
@AlexV525 AlexV525 merged commit 6f7fe7e into main Nov 16, 2023
4 of 5 checks passed
@AlexV525 AlexV525 deleted the fix/use-view-asset-in-preview-button branch November 16, 2023 01:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants