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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix refresh issue on switching folders #179

Merged
merged 3 commits into from
Jul 19, 2021
Merged

Conversation

yanivshaked
Copy link
Contributor

@AlexV525
This fixes the following issue on iOS. It is possible this is an overkill, and issue can be solved in a softer manner.
image

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to replace

@override
Widget assetsGridBuilder(BuildContext context) {
// First, we need the count of the assets.
int totalCount = provider.currentPathEntity!.assetCount;

with below snippet (wrap it with a Selector, along with some format changes), instead of changing this flag:

  @override
  Widget assetsGridBuilder(BuildContext context) {
    return Selector<DefaultAssetPickerProvider, AssetPathEntity?>(
      selector: (_, DefaultAssetPickerProvider p) => p.currentPathEntity,
      builder: (_, AssetPathEntity? path, __) {
        // First, we need the count of the assets.
        int totalCount = path!.assetCount;
        // If user chose a special item's position, add 1 count.
        if (specialItemPosition != SpecialItemPosition.none) {
          totalCount += 1;
        }
        // Then use the [totalCount] to calculate how many placeholders we need.
        final int placeholderCount;
        if (isAppleOS && totalCount % gridCount != 0) {
          // When there are left items that not filled into one row,
          // filled the row with placeholders.
          placeholderCount = gridCount - totalCount % gridCount;
        } else {
          // Otherwise, we don't need placeholders.
          placeholderCount = 0;
        }
        // Calculate rows count.
        final int row = (totalCount + placeholderCount) ~/ gridCount;
        // Here we got a magic calculation. [itemSpacing] needs to be divided by
        // [gridCount] since every grid item is squeezed by the [itemSpacing],
        // and it's actual size is reduced with [itemSpacing / gridCount].
        final double dividedSpacing = itemSpacing / gridCount;
        final double topPadding = context.topPadding + kToolbarHeight;

        Widget _sliverGrid(BuildContext ctx, List<AssetEntity> assets) {
          return SliverGrid(
            delegate: SliverChildBuilderDelegate(
              (_, int index) => Builder(
                builder: (BuildContext c) {
                  if (isAppleOS) {
                    if (index < placeholderCount) {
                      return const SizedBox.shrink();
                    }
                    index -= placeholderCount;
                  }
                  return Directionality(
                    textDirection: Directionality.of(context),
                    child: assetGridItemBuilder(c, index, assets),
                  );
                },
              ),
              childCount: assetsGridItemCount(
                context: ctx,
                assets: assets,
                placeholderCount: placeholderCount,
              ),
              findChildIndexCallback: (Key? key) {
                if (key is ValueKey<String>) {
                  return findChildIndexBuilder(
                    id: key.value,
                    assets: assets,
                    placeholderCount: placeholderCount,
                  );
                }
                return null;
              },
            ),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: gridCount,
              mainAxisSpacing: itemSpacing,
              crossAxisSpacing: itemSpacing,
            ),
          );
        }

        return LayoutBuilder(
          builder: (BuildContext c, BoxConstraints constraints) {
            final double itemSize = constraints.maxWidth / gridCount;
            // Check whether all rows can be placed at the same time.
            final bool onlyOneScreen = row * itemSize <=
                constraints.maxHeight -
                    context.bottomPadding -
                    topPadding -
                    permissionLimitedBarHeight;
            final double height;
            if (onlyOneScreen) {
              height = constraints.maxHeight;
            } else {
              // Reduce [permissionLimitedBarHeight] for the final height.
              height = constraints.maxHeight - permissionLimitedBarHeight;
            }
            // Use [ScrollView.anchor] to determine where is the first place of
            // the [SliverGrid]. Each row needs [dividedSpacing] to calculate,
            // then minus one times of [itemSpacing] because spacing's count
            // in the cross axis is always less than the rows.
            final double anchor = math.min(
              (row * (itemSize + dividedSpacing) + topPadding - itemSpacing) /
                  height,
              1,
            );

            return Directionality(
              textDirection: effectiveGridDirection(context),
              child: ColoredBox(
                color: theme.canvasColor,
                child: Selector<DefaultAssetPickerProvider, List<AssetEntity>>(
                  selector: (_, DefaultAssetPickerProvider provider) =>
                      provider.currentAssets,
                  builder: (_, List<AssetEntity> assets, __) =>
                      CustomScrollView(
                    physics: const AlwaysScrollableScrollPhysics(),
                    controller: gridScrollController,
                    anchor: isAppleOS ? anchor : 0,
                    center: isAppleOS ? gridRevertKey : null,
                    slivers: <Widget>[
                      if (isAppleOS)
                        SliverGap.v(context.topPadding + kToolbarHeight),
                      _sliverGrid(_, assets),
                      // Ignore the gap when the [anchor] is not equal to 1.
                      if (isAppleOS && anchor == 1)
                        SliverGap.v(
                          context.bottomPadding + bottomSectionHeight,
                        ),
                      if (isAppleOS)
                        SliverToBoxAdapter(
                          key: gridRevertKey,
                          child: const SizedBox.shrink(),
                        ),
                    ],
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for taking my suggestion. 馃憤

@AlexV525 AlexV525 merged commit e3ba6c7 into master Jul 19, 2021
@AlexV525 AlexV525 deleted the fix-ios-grid branch July 19, 2021 05:22
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.

None yet

2 participants