PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/misc/AppBarStateChangeListener.kt

59 lines
1.8 KiB
Kotlin

/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package code.name.monkey.retromusic.misc
import com.google.android.material.appbar.AppBarLayout
import kotlin.math.abs
/**
* @author Hemanth S (h4h13).
* https://stackoverflow.com/a/33891727
*/
abstract class AppBarStateChangeListener : AppBarLayout.OnOffsetChangedListener {
private var mCurrentState = State.IDLE
override fun onOffsetChanged(appBarLayout: AppBarLayout, i: Int) {
when {
i == 0 -> {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED)
}
mCurrentState = State.EXPANDED
}
abs(i) >= appBarLayout.totalScrollRange -> {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED)
}
mCurrentState = State.COLLAPSED
}
else -> {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE)
}
mCurrentState = State.IDLE
}
}
}
abstract fun onStateChanged(appBarLayout: AppBarLayout, state: State)
enum class State {
EXPANDED,
COLLAPSED,
IDLE
}
}