Skip to content

Commit

Permalink
[Adaptive] [Side Sheets] Add SideSheetCallback listener to SideSheetB…
Browse files Browse the repository at this point in the history
…ehavior to track @SheetState state change events.

Includes a fix for a really strange issue where setting the background color from the callback worked, but if setText was called, it would cause the sheet to flash off the screen when STATE_EXPANDED was reached.

PiperOrigin-RevId: 493409073
(cherry picked from commit 2468d6c)
  • Loading branch information
afohrman authored and dsn5ft committed Dec 9, 2022
1 parent 697156a commit 99e09b0
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 1 deletion.
Expand Up @@ -198,4 +198,12 @@ boolean isSettling(View child, int state, boolean isReleasingView) {
<V extends View> int getOutwardEdge(@NonNull V child) {
return child.getLeft();
}

@Override
float calculateSlideOffsetBasedOnOutwardEdge(int left) {
float hiddenOffset = getHiddenOffset();
float sheetWidth = hiddenOffset - getExpandedOffset();

return (hiddenOffset - left) / sheetWidth;
}
}
Expand Up @@ -93,4 +93,13 @@ abstract <V extends View> void setTargetStateOnNestedPreScroll(
* this would return {@code child.getLeft()}.
*/
abstract <V extends View> int getOutwardEdge(@NonNull V child);

/**
* Returns the calculated slide offset based on which edge of the screen the sheet is based on.
* The offset value increases as the sheet moves towards the outward edge.
*
* @return slide offset represented as a float value between 0 and 1. A value of 0 means that the
* sheet is hidden and a value of 1 means that the sheet is fully expanded.
*/
abstract float calculateSlideOffsetBasedOnOutwardEdge(int outwardEdge);
}
Expand Up @@ -52,12 +52,15 @@
import androidx.core.view.accessibility.AccessibilityViewCommand;
import androidx.customview.view.AbsSavedState;
import androidx.customview.widget.ViewDragHelper;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
* An interaction behavior plugin for a child view of {@link CoordinatorLayout} to make it work as a
Expand Down Expand Up @@ -123,6 +126,8 @@ public class SideSheetBehavior<V extends View> extends CoordinatorLayout.Behavio
private int initialX;
private int initialY;

@NonNull private final Set<SideSheetCallback> callbacks = new LinkedHashSet<>();

private boolean touchingScrollingChild;

@Nullable private Map<View, Integer> importantForAccessibilityMap;
Expand Down Expand Up @@ -319,6 +324,11 @@ public boolean onLayoutChild(
ViewCompat.offsetLeftAndRight(child, currentOffset);

nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));

for (SideSheetCallback callback : callbacks) {
callback.onLayout(child);
}

return true;
}

Expand All @@ -335,7 +345,11 @@ private int calculateCurrentOffset(int savedOutwardEdge, V child) {

switch (state) {
case STATE_EXPANDED:
currentOffset = savedOutwardEdge;
// TODO (b/261619910): This is a workaround for a bug where the expanded offset was getting
// recalculated if onLayoutChild() was called while the sheet was in the process of
// expanding/offsetting. Revisit this and refactor if necessary when adding left based
// sheets.
currentOffset = ViewUtils.isLayoutRtl(child) ? getExpandedOffset() : 0;
break;
case STATE_DRAGGING:
case STATE_SETTLING:
Expand Down Expand Up @@ -494,6 +508,7 @@ public void onNestedPreScroll(
}
sheetDelegate.setTargetStateOnNestedPreScroll(
coordinatorLayout, child, target, dx, dy, consumed, type);
dispatchOnSlide(child, sheetDelegate.getOutwardEdge(child));
lastNestedScrollDx = dx;
nestedScrolled = true;
}
Expand Down Expand Up @@ -601,6 +616,24 @@ float getHideThreshold() {
return HIDE_THRESHOLD;
}

/**
* Adds a callback to be notified of side sheet events.
*
* @param callback The callback to notify when side sheet events occur.
*/
public void addCallback(@NonNull SideSheetCallback callback) {
callbacks.add(callback);
}

/**
* Removes a previously added callback.
*
* @param callback The callback to remove.
*/
public void removeCallback(@NonNull SideSheetCallback callback) {
callbacks.remove(callback);
}

/**
* Sets the state of the sheet. The sheet will transition to that state with animation.
*
Expand Down Expand Up @@ -679,6 +712,10 @@ void setStateInternal(@SheetState int state) {
updateImportantForAccessibility(false);
}

for (SideSheetCallback callback : callbacks) {
callback.onStateChanged(sheet, state);
}

updateAccessibilityActions();
}

Expand Down Expand Up @@ -794,6 +831,12 @@ public boolean tryCaptureView(@NonNull View child, int pointerId) {
return viewRef != null && viewRef.get() == child;
}

@Override
public void onViewPositionChanged(
@NonNull View changedView, int left, int top, int dx, int dy) {
dispatchOnSlide(changedView, left);
}

@Override
public void onViewDragStateChanged(@SheetState int state) {
if (state == ViewDragHelper.STATE_DRAGGING && draggable) {
Expand Down Expand Up @@ -825,6 +868,15 @@ public int getViewHorizontalDragRange(@NonNull View child) {
}
};

private void dispatchOnSlide(@NonNull View child, int outwardEdge) {
if (!callbacks.isEmpty()) {
float slideOffset = sheetDelegate.calculateSlideOffsetBasedOnOutwardEdge(outwardEdge);
for (SideSheetCallback callback : callbacks) {
callback.onSlide(child, slideOffset);
}
}
}

/**
* Checks whether a nested scroll should be enabled. If {@code false} all nested scrolls will be
* consumed by the side sheet.
Expand Down
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.material.sidesheet;

import android.view.View;
import androidx.annotation.NonNull;
import com.google.android.material.sidesheet.Sheet.SheetState;

/** Callback that monitors side sheet events. */
public abstract class SideSheetCallback {

/**
* Called when the sheet changes its state.
*
* @param sheet The sheet view.
* @param newState The new state. This should be one of {@link SideSheetBehavior#STATE_DRAGGING},
* {@link SideSheetBehavior#STATE_SETTLING}, {@link SideSheetBehavior#STATE_EXPANDED} or
* {@link SideSheetBehavior#STATE_HIDDEN}.
*/
public abstract void onStateChanged(@NonNull View sheet, @SheetState int newState);

/**
* Called when the sheet is being dragged.
*
* @param sheet The sheet view.
* @param slideOffset The new offset of this sheet within [0,1] range. Offset increases as this
* sheet is moving towards the outward edge. A value of 0 means that the sheet is hidden, and
* a value of 1 means that the sheet is fully expanded.
*/
public abstract void onSlide(@NonNull View sheet, float slideOffset);

void onLayout(@NonNull View sheet) {}
}
@@ -0,0 +1,121 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.material.sidesheet;

import com.google.android.material.test.R;

import static android.graphics.Color.RED;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Looper;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

/** Tests for {@link com.google.android.material.sidesheet.SideSheetBehavior}. */
@RunWith(RobolectricTestRunner.class)
public class SideSheetCallbackTest {

private View sideSheet;
private SideSheetBehavior<View> sideSheetBehavior;

@Before
public void setUp() throws Exception {
AppCompatActivity activity = Robolectric.buildActivity(TestActivity.class).setup().get();
CoordinatorLayout coordinatorLayout =
(CoordinatorLayout) activity.getLayoutInflater().inflate(R.layout.test_side_sheet, null);
sideSheet = coordinatorLayout.findViewById(R.id.test_side_sheet_container);
sideSheetBehavior = SideSheetBehavior.from(sideSheet);

activity.setContentView(coordinatorLayout);

// Wait until the layout is measured.
shadowOf(Looper.getMainLooper()).idle();
}

@Test
public void test_setSheetRedOnExpandWithCallback_sheetIsRedOnExpand() {
// Create a callback and add it to the side sheet behavior.
sideSheetBehavior.addCallback(
new SideSheetCallback() {
@Override
public void onStateChanged(@NonNull View sheet, int newState) {
if (newState == SideSheetBehavior.STATE_EXPANDED) {
ViewCompat.setBackground(sideSheet, new ColorDrawable(RED));
}
}

@Override
public void onSlide(@NonNull View sheet, float slideOffset) {}
});

sideSheetBehavior.expand();
shadowOf(Looper.getMainLooper()).idle();

assertThat(sideSheetBehavior.getState()).isEqualTo(SideSheetBehavior.STATE_EXPANDED);
assertThat(sideSheet.getBackground()).isInstanceOf(ColorDrawable.class);
assertThat(((ColorDrawable) sideSheet.getBackground()).getColor()).isEqualTo(RED);
}

@Test
public void test_removeCallback_callbackIsRemoved() {
SideSheetCallback sideSheetCallback = createExpandedRedSideSheetCallback();
// Ensure that side sheet doesn't already have a background.
ViewCompat.setBackground(sideSheet, null);

sideSheetBehavior.addCallback(sideSheetCallback);
sideSheetBehavior.removeCallback(sideSheetCallback);

sideSheetBehavior.expand();
shadowOf(Looper.getMainLooper()).idle();

assertThat(sideSheetBehavior.getState()).isEqualTo(SideSheetBehavior.STATE_EXPANDED);
assertThat(sideSheet.getBackground()).isNull();
}

private SideSheetCallback createExpandedRedSideSheetCallback() {
return new SideSheetCallback() {
@Override
public void onStateChanged(@NonNull View sheet, int newState) {
if (newState == SideSheetBehavior.STATE_EXPANDED) {
ViewCompat.setBackground(sideSheet, new ColorDrawable(RED));
}
}

@Override
public void onSlide(@NonNull View sheet, float slideOffset) {}
};
}

private static class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setTheme(R.style.Theme_Material3_Light_NoActionBar);
}
}
}
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2022 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/test_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
android:id="@+id/test_side_sheet_container"
style="@style/Widget.Material3.SideSheet"
android:layout_width="256dp"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/side_sheet_behavior"
tools:targetApi="lollipop">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Test Side Sheet Content" />
</LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 comments on commit 99e09b0

Please sign in to comment.