Skip to content

Commit

Permalink
[Predictive Back] [Side Sheet] Added handleBackInvoked() predictive b…
Browse files Browse the repository at this point in the history
…ack support for coplanar side sheets.

Added an AnimatorUpdateListener that runs in finishBackProgress() to make the coplanar sibling's margins update as the sheet slides back to its origin edge.

PiperOrigin-RevId: 527749916
  • Loading branch information
afohrman authored and leticiarossi committed Apr 28, 2023
1 parent 824d75e commit b984e64
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 18 deletions.
Expand Up @@ -100,8 +100,7 @@ public View onCreateDemoView(
view,
R.id.standard_side_sheet_container,
R.id.show_standard_side_sheet_button,
R.id.close_icon_button,
/* shouldHandleBack= */ true);
R.id.close_icon_button);

setSideSheetCallback(
standardRightSideSheet, R.id.side_sheet_state_text, R.id.side_sheet_slide_offset_text);
Expand All @@ -112,8 +111,7 @@ public View onCreateDemoView(
view,
R.id.standard_detached_side_sheet_container,
R.id.show_standard_detached_side_sheet_button,
R.id.detached_close_icon_button,
/* shouldHandleBack= */ true);
R.id.detached_close_icon_button);

setSideSheetCallback(
detachedStandardSideSheet,
Expand All @@ -126,8 +124,7 @@ public View onCreateDemoView(
view,
R.id.vertically_scrolling_side_sheet_container,
R.id.show_vertically_scrolling_side_sheet_button,
R.id.vertically_scrolling_side_sheet_close_icon_button,
/* shouldHandleBack= */ true);
R.id.vertically_scrolling_side_sheet_close_icon_button);

setSideSheetCallback(
verticallyScrollingSideSheet,
Expand All @@ -148,8 +145,7 @@ public View onCreateDemoView(
view,
R.id.coplanar_side_sheet_container,
R.id.show_coplanar_side_sheet_button,
R.id.coplanar_side_sheet_close_icon_button,
/* shouldHandleBack= */ false);
R.id.coplanar_side_sheet_close_icon_button);

setSideSheetCallback(
coplanarSideSheet,
Expand All @@ -162,8 +158,7 @@ public View onCreateDemoView(
view,
R.id.coplanar_detached_side_sheet_container,
R.id.show_coplanar_detached_side_sheet_button,
R.id.coplanar_detached_side_sheet_close_icon_button,
/* shouldHandleBack= */ false);
R.id.coplanar_detached_side_sheet_close_icon_button);

setSideSheetCallback(
detachedCoplanarSideSheet,
Expand Down Expand Up @@ -224,8 +219,7 @@ private View setUpSideSheet(
@NonNull View view,
@IdRes int sideSheetContainerId,
@IdRes int showSideSheetButtonId,
@IdRes int closeIconButtonId,
boolean shouldHandleBack) {
@IdRes int closeIconButtonId) {
View sideSheet = view.findViewById(sideSheetContainerId);
SideSheetBehavior<View> sideSheetBehavior = SideSheetBehavior.from(sideSheet);

Expand All @@ -235,9 +229,7 @@ private View setUpSideSheet(
View standardSideSheetCloseIconButton = sideSheet.findViewById(closeIconButtonId);
standardSideSheetCloseIconButton.setOnClickListener(v -> hideSideSheet(sideSheetBehavior));

if (shouldHandleBack) {
setupBackHandling(sideSheetBehavior);
}
setupBackHandling(sideSheetBehavior);

sideSheetViews.add(sideSheet);

Expand Down
Expand Up @@ -102,6 +102,17 @@ void updateCoplanarSiblingLayoutParams(
}
}

@Override
void updateCoplanarSiblingAdjacentMargin(
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int coplanarSiblingAdjacentMargin) {
coplanarSiblingLayoutParams.leftMargin = coplanarSiblingAdjacentMargin;
}

@Override
int getCoplanarSiblingAdjacentMargin(@NonNull MarginLayoutParams coplanarSiblingLayoutParams) {
return coplanarSiblingLayoutParams.leftMargin;
}

@Override
public int getParentInnerEdge(@NonNull CoordinatorLayout parent) {
return parent.getLeft();
Expand Down
Expand Up @@ -103,6 +103,17 @@ void updateCoplanarSiblingLayoutParams(
}
}

@Override
void updateCoplanarSiblingAdjacentMargin(
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int coplanarSiblingAdjacentMargin) {
coplanarSiblingLayoutParams.rightMargin = coplanarSiblingAdjacentMargin;
}

@Override
int getCoplanarSiblingAdjacentMargin(@NonNull MarginLayoutParams coplanarSiblingLayoutParams) {
return coplanarSiblingLayoutParams.rightMargin;
}

@Override
public int getParentInnerEdge(@NonNull CoordinatorLayout parent) {
return parent.getRight();
Expand Down
11 changes: 11 additions & 0 deletions lib/java/com/google/android/material/sidesheet/SheetDelegate.java
Expand Up @@ -71,6 +71,17 @@ abstract class SheetDelegate {
abstract void updateCoplanarSiblingLayoutParams(
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int sheetLeft, int sheetRight);

/** Sets the coplanar sheet's margin that's adjacent to the side sheet to the provided value. */
abstract void updateCoplanarSiblingAdjacentMargin(
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int coplanarSiblingAdjacentMargin);

/**
* Returns the coplanar sibling's margin that's adjacent to the sheet's left or right edge,
* depending on the sheet edge.
*/
abstract int getCoplanarSiblingAdjacentMargin(
@NonNull MarginLayoutParams coplanarSiblingLayoutParams);

/**
* Calculates the margin on the inner side of the sheet based on the {@link MarginLayoutParams}.
* For right based sheets, the inner margin would be the right margin.
Expand Down
Expand Up @@ -23,6 +23,7 @@

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
Expand Down Expand Up @@ -59,6 +60,7 @@
import androidx.core.view.accessibility.AccessibilityViewCommand;
import androidx.customview.view.AbsSavedState;
import androidx.customview.widget.ViewDragHelper;
import com.google.android.material.animation.AnimationUtils;
import com.google.android.material.motion.MaterialSideContainerBackHelper;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.MaterialShapeDrawable;
Expand Down Expand Up @@ -988,8 +990,7 @@ public void updateBackProgress(@NonNull BackEvent backEvent) {
if (sideContainerBackHelper == null) {
return;
}
sideContainerBackHelper.updateBackProgress(
backEvent, getGravityFromSheetEdge());
sideContainerBackHelper.updateBackProgress(backEvent, getGravityFromSheetEdge());
}

@Override
Expand All @@ -1015,7 +1016,31 @@ public void onAnimationEnd(Animator animation) {
}
}
},
/* finishAnimatorUpdateListener= */ null);
getCoplanarFinishAnimatorUpdateListener());
}

@Nullable
private AnimatorUpdateListener getCoplanarFinishAnimatorUpdateListener() {
View coplanarSiblingView = getCoplanarSiblingView();
if (coplanarSiblingView == null) {
return null;
}

MarginLayoutParams coplanarSiblingLayoutParams =
(MarginLayoutParams) coplanarSiblingView.getLayoutParams();
if (coplanarSiblingLayoutParams == null) {
return null;
}

int coplanarSiblingAdjacentMargin =
sheetDelegate.getCoplanarSiblingAdjacentMargin(coplanarSiblingLayoutParams);

return animation -> {
sheetDelegate.updateCoplanarSiblingAdjacentMargin(
coplanarSiblingLayoutParams,
AnimationUtils.lerp(coplanarSiblingAdjacentMargin, 0, animation.getAnimatedFraction()));
coplanarSiblingView.requestLayout();
};
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
Expand Down

0 comments on commit b984e64

Please sign in to comment.