Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4eeb2798 authored by Mady Mellor's avatar Mady Mellor
Browse files

Add a listener for when the illustration animation view is bound

This allows users of the IllustrationPreference to update the
colors in the animation view more easily.

Bug: 243902048
Test: IllustrationPreferenceTest
Change-Id: Iaeef38005c081f13a8517e18ef53460a164509a2
parent db096dc1
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -59,6 +59,18 @@ public class IllustrationPreference extends Preference {
    private Uri mImageUri;
    private Drawable mImageDrawable;
    private View mMiddleGroundView;
    private OnBindListener mOnBindListener;

    /**
     * Interface to listen in on when {@link #onBindViewHolder(PreferenceViewHolder)} occurs.
     */
    public interface OnBindListener {
        /**
         * Called when when {@link #onBindViewHolder(PreferenceViewHolder)} occurs.
         * @param animationView the animation view for this preference.
         */
        void onBind(LottieAnimationView animationView);
    }

    private final Animatable2.AnimationCallback mAnimationCallback =
            new Animatable2.AnimationCallback() {
@@ -133,6 +145,17 @@ public class IllustrationPreference extends Preference {
        if (IS_ENABLED_LOTTIE_ADAPTIVE_COLOR) {
            ColorUtils.applyDynamicColors(getContext(), illustrationView);
        }

        if (mOnBindListener != null) {
            mOnBindListener.onBind(illustrationView);
        }
    }

    /**
     * Sets a listener to be notified when the views are binded.
     */
    public void setOnBindListener(OnBindListener listener) {
        mOnBindListener = listener;
    }

    /**
+29 −0
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ public class IllustrationPreferenceTest {
    private PreferenceViewHolder mViewHolder;
    private FrameLayout mMiddleGroundLayout;
    private final Context mContext = ApplicationProvider.getApplicationContext();
    private IllustrationPreference.OnBindListener mOnBindListener;
    private LottieAnimationView mOnBindListenerAnimationView;

    @Before
    public void setUp() {
@@ -82,6 +84,12 @@ public class IllustrationPreferenceTest {

        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
        mPreference = new IllustrationPreference(mContext, attributeSet);
        mOnBindListener = new IllustrationPreference.OnBindListener() {
            @Override
            public void onBind(LottieAnimationView animationView) {
                mOnBindListenerAnimationView = animationView;
            }
        };
    }

    @Test
@@ -186,4 +194,25 @@ public class IllustrationPreferenceTest {
        assertThat(mBackgroundView.getMaxHeight()).isEqualTo(restrictedHeight);
        assertThat(mAnimationView.getMaxHeight()).isEqualTo(restrictedHeight);
    }

    @Test
    public void setOnBindListener_isNotified() {
        mOnBindListenerAnimationView = null;
        mPreference.setOnBindListener(mOnBindListener);

        mPreference.onBindViewHolder(mViewHolder);

        assertThat(mOnBindListenerAnimationView).isNotNull();
        assertThat(mOnBindListenerAnimationView).isEqualTo(mAnimationView);
    }

    @Test
    public void setOnBindListener_notNotified() {
        mOnBindListenerAnimationView = null;
        mPreference.setOnBindListener(null);

        mPreference.onBindViewHolder(mViewHolder);

        assertThat(mOnBindListenerAnimationView).isNull();
    }
}