Skip to content

Commit

Permalink
[Motion] Added default style parameter for MotionUtil when resolving …
Browse files Browse the repository at this point in the history
…theme spring attributes

PiperOrigin-RevId: 714984914
  • Loading branch information
hunterstich authored and pekingme committed Jan 14, 2025
1 parent b31711c commit f4342f1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ private void initializeSizeAnimation() {
}

private SpringForce createSpringForce() {
return MotionUtils.resolveThemeSpringForce(getContext(), R.attr.motionSpringFastSpatial);
return MotionUtils.resolveThemeSpringForce(getContext(), R.attr.motionSpringFastSpatial,
R.style.Motion_Material3_Spring_Standard_Fast_Spatial);
}

@NonNull
Expand Down
16 changes: 12 additions & 4 deletions lib/java/com/google/android/material/motion/MotionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.view.animation.AnimationUtils;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.core.graphics.PathParser;
import androidx.core.view.animation.PathInterpolatorCompat;
import androidx.dynamicanimation.animation.SpringForce;
Expand All @@ -46,15 +47,22 @@ private MotionUtils() {}
* @param context the context from where the theme attribute will be resolved
* @param attrResId the {@code motionSpring*} theme attribute to resolve
* into a {@link SpringForce} object
* @param defStyleRes a {@code MaterialSpring} style to load if attrResId cannot be resolved
* @return a {@link SpringForce} object configured using the stiffness and damping from the
* resolved Material spring attribute
*/
@NonNull
public static SpringForce resolveThemeSpringForce(
@NonNull Context context, @AttrRes int attrResId) {
TypedValue tv = MaterialAttributes.resolveTypedValueOrThrow(
context, attrResId, "MaterialSpring");
TypedArray a = context.obtainStyledAttributes(tv.resourceId, R.styleable.MaterialSpring);
@NonNull Context context, @AttrRes int attrResId, @StyleRes int defStyleRes) {

TypedValue tv = MaterialAttributes.resolve(context, attrResId);
TypedArray a;
if (tv == null) {
a = context.obtainStyledAttributes(null, R.styleable.MaterialSpring, 0, defStyleRes);
} else {
a = context.obtainStyledAttributes(tv.resourceId, R.styleable.MaterialSpring);
}

SpringForce springForce = new SpringForce();
try {
float stiffness = a.getFloat(R.styleable.MaterialSpring_stiffness, Float.MIN_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,34 @@ public void testResolvesThemeSpring() {
context.getResources(), R.dimen.m3_sys_motion_standard_spring_fast_spatial_stiffness);
float expectedDampingRatio = ResourcesCompat.getFloat(
context.getResources(), R.dimen.m3_sys_motion_standard_spring_fast_spatial_damping);
SpringForce spring =
MotionUtils.resolveThemeSpringForce(context, R.attr.motionSpringFastSpatial);
SpringForce spring = MotionUtils.resolveThemeSpringForce(context,
R.attr.motionSpringFastSpatial, R.style.Motion_Material3_Spring_Standard_Fast_Spatial);

assertThat(spring.getStiffness()).isEqualTo(expectedStiffness);
assertThat(spring.getDampingRatio()).isEqualTo(expectedDampingRatio);
}

@Test
public void testAbsentThemeSpring_shouldResolveDefault() {
createActivityAndSetTheme(R.style.Theme_AppCompat_DayNight);
Context context = activityController.get().getApplicationContext();

SpringForce spring = MotionUtils.resolveThemeSpringForce(context,
R.attr.motionSpringFastSpatial, R.style.Motion_MyApp_Spring_Custom_Default);

assertThat(spring.getStiffness()).isEqualTo(1450f);
assertThat(spring.getDampingRatio()).isEqualTo(0.5f);
}

@Test
public void testPartialSpring_shouldThrow() {
createActivityAndSetTheme(R.style.Theme_Material3_DayNight_PartialSpring);
Context context = activityController.get().getApplicationContext();

IllegalArgumentException thrown = assertThrows(
IllegalArgumentException.class,
() -> MotionUtils.resolveThemeSpringForce(context, R.attr.motionSpringFastSpatial)
() -> MotionUtils.resolveThemeSpringForce(context, R.attr.motionSpringFastSpatial,
R.style.Motion_Material3_Spring_Standard_Fast_Spatial)
);
assertThat(thrown).hasMessageThat().contains("must have a damping");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
<resources>
<!-- A theme with an incorrectly configured custom spring. -->
<style name="Theme.Material3.DayNight.PartialSpring" parent="Theme.Material3.DayNight">
<item name="motionSpringFastSpatial">@style/Motion.MyApp.Spring.Custom.Fast.Spatial</item>
<item name="motionSpringFastSpatial">@style/Motion.MyApp.Spring.Custom.Fast.Spatial.Partial</item>
</style>
<style name="Motion.MyApp.Spring.Custom.Fast.Spatial" parent="">
<style name="Motion.MyApp.Spring.Custom.Fast.Spatial.Partial" parent="">
<item name="stiffness">1400</item>
</style>
<style name="Motion.MyApp.Spring.Custom.Default" parent="">
<item name="stiffness">1450</item>
<item name="damping">.5</item>
</style>

<style name="Theme.MaterialComponents.DayNight.IncorrectLegacyEasingAttrType" parent="Theme.MaterialComponents.DayNight">
<item name="motionEasingStandard">@color/color_primary</item>
Expand Down

0 comments on commit f4342f1

Please # to comment.