1 /*
2  * Copyright (C) 2020 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  */
16 
17 package com.android.deskclock.widget.toast
18 
19 import android.content.Context
20 import android.util.AttributeSet
21 import android.view.View
22 import androidx.annotation.Keep
23 import androidx.coordinatorlayout.widget.CoordinatorLayout
24 
25 import com.google.android.material.snackbar.Snackbar
26 
27 import kotlin.math.min
28 
29 /**
30  * Custom [CoordinatorLayout.Behavior] that slides with the [Snackbar].
31  */
32 @Keep
33 class SnackbarSlidingBehavior(
34     context: Context?,
35     attrs: AttributeSet?
36 ) : CoordinatorLayout.Behavior<View?>() {
layoutDependsOnnull37     override fun layoutDependsOn(
38         parent: CoordinatorLayout,
39         child: View,
40         dependency: View
41     ): Boolean {
42         return dependency is Snackbar.SnackbarLayout
43     }
44 
onDependentViewChangednull45     override fun onDependentViewChanged(
46         parent: CoordinatorLayout,
47         child: View,
48         dependency: View
49     ): Boolean {
50         updateTranslationY(parent, child)
51         return false
52     }
53 
onDependentViewRemovednull54     override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) {
55         updateTranslationY(parent, child)
56     }
57 
onLayoutChildnull58     override fun onLayoutChild(
59         parent: CoordinatorLayout,
60         child: View,
61         layoutDirection: Int
62     ): Boolean {
63         updateTranslationY(parent, child)
64         return false
65     }
66 
updateTranslationYnull67     private fun updateTranslationY(parent: CoordinatorLayout, child: View) {
68         var translationY = 0f
69         for (dependency in parent.getDependencies(child)) {
70             translationY = min(translationY, dependency.y - child.bottom)
71         }
72         child.translationY = translationY
73     }
74 }