# BottomInfoToggler.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2020 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.mingraviditet.ui.widgets

    import android.view.ViewGroup
    import android.view.animation.AnticipateInterpolator
    import android.view.animation.OvershootInterpolator
    import android.widget.TextView
    import androidx.annotation.UiThread
    import androidx.recyclerview.widget.RecyclerView

    class BottomInfoToggler(val infoTextView: TextView, recyclerview: RecyclerView) {
        enum class LastAnimationState {
            StartedHideAnimation,
            StartedShowAnimation,
            Default
        }

        private var animationState: LastAnimationState = LastAnimationState.Default

        init {
            val lastScrollUpdates = MutableList(10) { 0 }
            recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    if (dy > 10 || dy < -10) {
                        lastScrollUpdates.removeFirst()
                        lastScrollUpdates.add(dy)

                        if (lastScrollUpdates.average() > 10) {
                            hide()
                        } else {
                            show()
                        }
                    }

                    super.onScrolled(recyclerView, dx, dy)
                }
            })
        }

        @UiThread
        fun hide() {
            if (animationState == LastAnimationState.StartedHideAnimation) {
                return
            }
            animationState = LastAnimationState.StartedHideAnimation

            val bottomMargin = (infoTextView.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin.toFloat()
            infoTextView.animate().apply {
                translationY(infoTextView.height.toFloat() + bottomMargin)
                duration = 300
                interpolator = AnticipateInterpolator()
                start()
            }
        }

        @UiThread
        fun show() {
            if (animationState == LastAnimationState.StartedShowAnimation) {
                return
            }
            animationState = LastAnimationState.StartedShowAnimation

            infoTextView.animate().apply {
                translationY(0f)
                duration = 300
                interpolator = OvershootInterpolator()
                start()
            }
        }
    }