1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.messaging.ui;
17
18import android.view.Gravity;
19import android.view.View;
20import android.view.ViewPropertyAnimator;
21
22import com.google.common.base.Preconditions;
23
24/**
25 * An interface that defines how a component can be animated with an {@link SnackBar}.
26 */
27public interface SnackBarInteraction {
28    /**
29     * Returns the animator that will be run in reaction to the given SnackBar being shown.
30     *
31     * Implementations may return null here if it determines that the given SnackBar does not need
32     * to animate this component.
33     */
34    ViewPropertyAnimator animateOnSnackBarShow(SnackBar snackBar);
35
36    /**
37     * Returns the animator that will be run in reaction to the given SnackBar being dismissed.
38     *
39     * Implementations may return null here if it determines that the given SnackBar does not need
40     * to animate this component.
41     */
42    ViewPropertyAnimator animateOnSnackBarDismiss(SnackBar snackBar);
43
44    /**
45     * A basic implementation of {@link SnackBarInteraction} that assumes that the
46     * {@link SnackBar} is always shown with {@link Gravity#BOTTOM} and that the provided View will
47     * always need to be translated up to make room for the SnackBar.
48     */
49    public static class BasicSnackBarInteraction implements SnackBarInteraction {
50        private final View mView;
51
52        public BasicSnackBarInteraction(final View view) {
53            mView = Preconditions.checkNotNull(view);
54        }
55
56        @Override
57        public ViewPropertyAnimator animateOnSnackBarShow(final SnackBar snackBar) {
58            final View rootView = snackBar.getRootView();
59            return mView.animate().translationY(-rootView.getMeasuredHeight());
60        }
61
62        @Override
63        public ViewPropertyAnimator animateOnSnackBarDismiss(final SnackBar snackBar) {
64            return mView.animate().translationY(0);
65        }
66    }
67}