PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsSlidingMusicPanelActivit...

416 lines
16 KiB
Kotlin
Raw Normal View History

2020-10-06 08:46:04 +00:00
/*
* Copyright (c) 2020 Hemanth Savarla.
*
* 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.
*
*/
2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.activities.base
2018-11-30 01:06:16 +00:00
2020-10-02 18:05:09 +00:00
import android.annotation.SuppressLint
2019-12-01 11:28:57 +00:00
import android.graphics.Color
2018-11-30 01:06:16 +00:00
import android.os.Bundle
2019-12-01 11:28:57 +00:00
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
2020-05-11 09:51:22 +00:00
import android.widget.FrameLayout
2020-08-13 08:24:36 +00:00
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
2020-10-02 18:05:09 +00:00
import androidx.fragment.app.commit
2019-12-01 11:28:57 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.ColorUtil
2019-02-23 17:39:02 +00:00
import code.name.monkey.retromusic.R
2020-05-09 05:55:42 +00:00
import code.name.monkey.retromusic.RetroBottomSheetBehavior
import code.name.monkey.retromusic.extensions.*
2020-07-28 19:18:34 +00:00
import code.name.monkey.retromusic.fragments.LibraryViewModel
2019-12-01 11:28:57 +00:00
import code.name.monkey.retromusic.fragments.MiniPlayerFragment
import code.name.monkey.retromusic.fragments.NowPlayingScreen
import code.name.monkey.retromusic.fragments.NowPlayingScreen.*
import code.name.monkey.retromusic.fragments.base.AbsPlayerFragment
import code.name.monkey.retromusic.fragments.player.adaptive.AdaptiveFragment
import code.name.monkey.retromusic.fragments.player.blur.BlurPlayerFragment
import code.name.monkey.retromusic.fragments.player.card.CardFragment
import code.name.monkey.retromusic.fragments.player.cardblur.CardBlurFragment
import code.name.monkey.retromusic.fragments.player.circle.CirclePlayerFragment
2020-10-02 12:56:17 +00:00
import code.name.monkey.retromusic.fragments.player.classic.ClassicPlayerFragment
import code.name.monkey.retromusic.fragments.player.color.ColorFragment
import code.name.monkey.retromusic.fragments.player.fit.FitFragment
import code.name.monkey.retromusic.fragments.player.flat.FlatPlayerFragment
import code.name.monkey.retromusic.fragments.player.full.FullPlayerFragment
import code.name.monkey.retromusic.fragments.player.gradient.GradientPlayerFragment
import code.name.monkey.retromusic.fragments.player.material.MaterialFragment
import code.name.monkey.retromusic.fragments.player.normal.PlayerFragment
2020-09-23 21:10:16 +00:00
import code.name.monkey.retromusic.fragments.player.peak.PeakPlayerFragment
import code.name.monkey.retromusic.fragments.player.plain.PlainPlayerFragment
import code.name.monkey.retromusic.fragments.player.simple.SimplePlayerFragment
import code.name.monkey.retromusic.fragments.player.tiny.TinyPlayerFragment
2019-05-19 17:10:21 +00:00
import code.name.monkey.retromusic.helper.MusicPlayerRemote
2019-12-01 15:27:01 +00:00
import code.name.monkey.retromusic.model.CategoryInfo
2020-09-17 17:56:59 +00:00
import code.name.monkey.retromusic.state.NowPlayingPanelState.*
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2019-02-23 17:39:02 +00:00
import code.name.monkey.retromusic.views.BottomNavigationBarTinted
import com.google.android.material.bottomsheet.BottomSheetBehavior.*
import kotlinx.android.synthetic.main.sliding_music_panel_layout.*
2020-07-28 19:18:34 +00:00
import org.koin.androidx.viewmodel.ext.android.viewModel
2019-02-23 17:39:02 +00:00
2020-08-21 14:19:15 +00:00
abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() {
2019-12-01 11:28:57 +00:00
companion object {
val TAG: String = AbsSlidingMusicPanelActivity::class.java.simpleName
}
protected val libraryViewModel by viewModel<LibraryViewModel>()
private lateinit var bottomSheetBehavior: RetroBottomSheetBehavior<FrameLayout>
2020-10-02 12:56:17 +00:00
private var playerFragment: AbsPlayerFragment? = null
2019-12-01 11:28:57 +00:00
private var miniPlayerFragment: MiniPlayerFragment? = null
private var nowPlayingScreen: NowPlayingScreen? = null
2019-12-01 11:28:57 +00:00
private var navigationBarColor: Int = 0
private var taskColor: Int = 0
private var lightStatusBar: Boolean = false
private var lightNavigationBar: Boolean = false
2020-07-28 19:18:34 +00:00
private var paletteColor: Int = Color.WHITE
2019-12-01 11:28:57 +00:00
protected abstract fun createContentView(): View
private val panelState: Int
get() = bottomSheetBehavior.state
2019-12-01 11:28:57 +00:00
private val bottomSheetCallbackList = object : BottomSheetCallback() {
2019-12-01 11:28:57 +00:00
override fun onSlide(bottomSheet: View, slideOffset: Float) {
setMiniPlayerAlphaProgress(slideOffset)
2020-09-23 21:10:16 +00:00
dimBackground.show()
dimBackground.alpha = slideOffset
2019-12-01 11:28:57 +00:00
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
STATE_EXPANDED -> {
2019-12-01 11:28:57 +00:00
onPanelExpanded()
}
STATE_COLLAPSED -> {
2019-12-01 11:28:57 +00:00
onPanelCollapsed()
2020-09-23 21:10:16 +00:00
dimBackground.hide()
2019-12-01 11:28:57 +00:00
}
else -> {
println("Do something")
2019-12-01 11:28:57 +00:00
}
}
}
}
2020-09-23 21:10:16 +00:00
fun getBottomSheetBehavior() = bottomSheetBehavior
2019-12-01 11:28:57 +00:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(createContentView())
chooseFragmentForTheme()
setupSlidingUpPanel()
2020-07-28 19:18:34 +00:00
setupBottomSheet()
2020-09-17 17:56:59 +00:00
updatePanelState()
updateColor()
2020-09-23 21:10:16 +00:00
val themeColor = ATHUtil.resolveColor(this, android.R.attr.windowBackground, Color.GRAY)
dimBackground.setBackgroundColor(ColorUtil.withAlpha(themeColor, 0.5f))
dimBackground.setOnClickListener {
libraryViewModel.setPanelState(COLLAPSED_WITH)
}
}
2020-05-11 09:51:22 +00:00
2020-07-28 19:18:34 +00:00
private fun setupBottomSheet() {
bottomSheetBehavior = from(slidingPanel) as RetroBottomSheetBehavior
bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallbackList)
2019-12-01 11:28:57 +00:00
}
override fun onResume() {
super.onResume()
if (nowPlayingScreen != PreferenceUtil.nowPlayingScreen) {
2019-12-01 11:28:57 +00:00
postRecreate()
}
}
override fun onDestroy() {
super.onDestroy()
bottomSheetBehavior.removeBottomSheetCallback(bottomSheetCallbackList)
2019-12-01 11:28:57 +00:00
}
2020-10-02 18:05:09 +00:00
@SuppressLint("InflateParams")
protected fun wrapSlidingMusicPanel(): View {
val slidingMusicPanelLayout =
layoutInflater.inflate(R.layout.sliding_music_panel_layout, null)
2020-07-28 19:18:34 +00:00
val contentContainer: ViewGroup =
slidingMusicPanelLayout.findViewById(R.id.mainContentFrame)
2020-10-02 18:05:09 +00:00
layoutInflater.inflate(R.layout.activity_main_content, contentContainer)
2019-12-01 11:28:57 +00:00
return slidingMusicPanelLayout
}
2020-08-11 21:31:09 +00:00
fun collapsePanel() {
bottomSheetBehavior.state = STATE_COLLAPSED
2019-12-01 11:28:57 +00:00
}
fun expandPanel() {
bottomSheetBehavior.state = STATE_EXPANDED
2019-12-01 11:28:57 +00:00
}
private fun setMiniPlayerAlphaProgress(progress: Float) {
val alpha = 1 - progress
miniPlayerFragment?.view?.alpha = alpha
miniPlayerFragment?.view?.visibility = if (alpha == 0f) View.GONE else View.VISIBLE
bottomNavigationView.translationY = progress * 500
2020-10-06 08:46:04 +00:00
// bottomNavigationView.alpha = alpha
2019-12-01 11:28:57 +00:00
}
open fun onPanelCollapsed() {
// restore values
super.setLightStatusbar(lightStatusBar)
super.setTaskDescriptionColor(taskColor)
super.setNavigationbarColor(navigationBarColor)
super.setLightNavigationBar(lightNavigationBar)
}
open fun onPanelExpanded() {
onPaletteColorChanged()
}
private fun setupSlidingUpPanel() {
slidingPanel.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
2019-12-01 11:28:57 +00:00
override fun onGlobalLayout() {
slidingPanel.viewTreeObserver.removeOnGlobalLayoutListener(this)
2020-09-23 21:10:16 +00:00
if (nowPlayingScreen != Peak) {
val params = slidingPanel.layoutParams as ViewGroup.LayoutParams
params.height = ViewGroup.LayoutParams.MATCH_PARENT
slidingPanel.layoutParams = params
}
2019-12-01 11:28:57 +00:00
when (panelState) {
STATE_EXPANDED -> onPanelExpanded()
STATE_COLLAPSED -> onPanelCollapsed()
2020-07-28 19:18:34 +00:00
else -> {
2020-10-06 08:46:04 +00:00
// playerFragment!!.onHide()
2020-07-28 19:18:34 +00:00
}
2019-12-01 11:28:57 +00:00
}
}
})
}
fun getBottomNavigationView(): BottomNavigationBarTinted {
return bottomNavigationView
}
override fun onServiceConnected() {
super.onServiceConnected()
if (MusicPlayerRemote.playingQueue.isNotEmpty()) {
slidingPanel.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
2019-12-01 11:28:57 +00:00
override fun onGlobalLayout() {
slidingPanel.viewTreeObserver.removeOnGlobalLayoutListener(this)
2020-09-17 17:56:59 +00:00
if (bottomNavigationView.isVisible) {
libraryViewModel.setPanelState(COLLAPSED_WITH)
} else {
libraryViewModel.setPanelState(COLLAPSED_WITHOUT)
}
2019-12-01 11:28:57 +00:00
}
})
} // don't call hideBottomBar(true) here as it causes a bug with the SlidingUpPanelLayout
}
override fun onQueueChanged() {
super.onQueueChanged()
if (MusicPlayerRemote.playingQueue.isEmpty()) {
2020-09-17 17:56:59 +00:00
libraryViewModel.setPanelState(HIDE)
} else {
if (bottomNavigationView.isVisible) {
libraryViewModel.setPanelState(COLLAPSED_WITH)
2020-09-17 17:56:59 +00:00
} else {
libraryViewModel.setPanelState(COLLAPSED_WITHOUT)
}
}
2019-12-01 11:28:57 +00:00
}
override fun onBackPressed() {
if (!handleBackPress()) super.onBackPressed()
}
private fun handleBackPress(): Boolean {
if (bottomSheetBehavior.peekHeight != 0 && playerFragment!!.onBackPressed()) return true
if (panelState == STATE_EXPANDED) {
2019-12-01 11:28:57 +00:00
collapsePanel()
return true
2020-01-24 17:27:43 +00:00
}
2019-12-01 11:28:57 +00:00
return false
}
2020-07-28 19:18:34 +00:00
private fun onPaletteColorChanged() {
if (panelState == STATE_EXPANDED) {
2019-12-01 11:28:57 +00:00
super.setTaskDescriptionColor(paletteColor)
val isColorLight = ColorUtil.isColorLight(paletteColor)
if (PreferenceUtil.isAdaptiveColor && (nowPlayingScreen == Normal || nowPlayingScreen == Flat)) {
2019-12-01 11:28:57 +00:00
super.setLightNavigationBar(true)
super.setLightStatusbar(isColorLight)
} else if (nowPlayingScreen == Card || nowPlayingScreen == Blur || nowPlayingScreen == BlurCard) {
2019-12-01 11:28:57 +00:00
super.setLightStatusbar(false)
super.setLightNavigationBar(true)
2020-02-02 12:44:16 +00:00
super.setNavigationbarColor(Color.BLACK)
} else if (nowPlayingScreen == Color || nowPlayingScreen == Tiny || nowPlayingScreen == Gradient) {
2019-12-01 11:28:57 +00:00
super.setNavigationbarColor(paletteColor)
super.setLightNavigationBar(isColorLight)
super.setLightStatusbar(isColorLight)
} else if (nowPlayingScreen == Full) {
super.setNavigationbarColor(paletteColor)
super.setLightNavigationBar(isColorLight)
super.setLightStatusbar(false)
} else if (nowPlayingScreen == Classic) {
2020-05-09 05:55:42 +00:00
super.setLightStatusbar(false)
} else if (nowPlayingScreen == Fit) {
super.setLightStatusbar(false)
2019-12-01 11:28:57 +00:00
} else {
2019-12-30 11:01:50 +00:00
super.setLightStatusbar(
ColorUtil.isColorLight(
ATHUtil.resolveColor(
this,
android.R.attr.windowBackground
)
)
)
2019-12-01 11:28:57 +00:00
super.setLightNavigationBar(true)
}
}
}
override fun setLightStatusbar(enabled: Boolean) {
lightStatusBar = enabled
if (panelState == STATE_COLLAPSED) {
2019-12-01 11:28:57 +00:00
super.setLightStatusbar(enabled)
}
}
override fun setLightNavigationBar(enabled: Boolean) {
lightNavigationBar = enabled
if (panelState == STATE_COLLAPSED) {
2019-12-01 11:28:57 +00:00
super.setLightNavigationBar(enabled)
}
}
override fun setNavigationbarColor(color: Int) {
navigationBarColor = color
if (panelState == STATE_COLLAPSED) {
2019-12-01 11:28:57 +00:00
super.setNavigationbarColor(color)
}
}
override fun setTaskDescriptionColor(color: Int) {
taskColor = color
if (panelState == STATE_COLLAPSED) {
2019-12-01 11:28:57 +00:00
super.setTaskDescriptionColor(color)
}
}
2020-05-11 09:51:22 +00:00
fun updateTabs() {
2019-12-01 11:28:57 +00:00
bottomNavigationView.menu.clear()
2020-06-06 18:57:28 +00:00
val currentTabs: List<CategoryInfo> = PreferenceUtil.libraryCategory
2019-12-01 11:28:57 +00:00
for (tab in currentTabs) {
if (tab.visible) {
val menu = tab.category
2019-12-01 15:27:01 +00:00
bottomNavigationView.menu.add(0, menu.id, 0, menu.stringRes).setIcon(menu.icon)
2019-12-01 11:28:57 +00:00
}
}
2020-05-11 09:51:22 +00:00
if (bottomNavigationView.menu.size() == 1) {
bottomNavigationView.hide()
2019-12-01 15:27:01 +00:00
}
2019-12-01 11:28:57 +00:00
}
2020-09-17 17:56:59 +00:00
private fun updateColor() {
libraryViewModel.paletteColor.observe(this, { color ->
this.paletteColor = color
onPaletteColorChanged()
})
}
private fun updatePanelState() {
libraryViewModel.panelState.observe(this, { state ->
val isQueueEmpty = MusicPlayerRemote.playingQueue.isEmpty()
2020-09-17 17:56:59 +00:00
when (state) {
EXPAND -> {
println("EXPAND")
2020-09-17 17:56:59 +00:00
expandPanel()
}
HIDE -> {
println("HIDE")
bottomNavigationView.translateXAnimate(0f)
bottomSheetBehavior.isHideable = true
bottomSheetBehavior.peekHeightAnimate(0)
bottomSheetBehavior.state = STATE_COLLAPSED
ViewCompat.setElevation(slidingPanel, 0f)
ViewCompat.setElevation(bottomNavigationView, 10f)
2020-09-17 17:56:59 +00:00
}
COLLAPSED_WITH -> {
println("COLLAPSED_WITH")
2020-09-17 17:56:59 +00:00
val heightOfBar = bottomNavigationView.height
val height = if (isQueueEmpty) 0 else (heightOfBar * 2) - 24
2020-10-02 18:05:09 +00:00
ViewCompat.setElevation(bottomNavigationView, 20f)
ViewCompat.setElevation(slidingPanel, 20f)
bottomSheetBehavior.isHideable = false
bottomSheetBehavior.peekHeightAnimate(height)
bottomNavigationView.translateXAnimate(0f)
2020-09-17 17:56:59 +00:00
}
COLLAPSED_WITHOUT -> {
println("COLLAPSED_WITHOUT")
2020-09-17 17:56:59 +00:00
val heightOfBar = bottomNavigationView.height
val height = if (isQueueEmpty) 0 else heightOfBar - 24
2020-09-17 17:56:59 +00:00
ViewCompat.setElevation(bottomNavigationView, 10f)
ViewCompat.setElevation(slidingPanel, 10f)
bottomSheetBehavior.isHideable = false
bottomSheetBehavior.peekHeightAnimate(height)
bottomNavigationView.translateXAnimate(150f)
2020-09-17 17:56:59 +00:00
}
else -> {
println("else")
bottomSheetBehavior.isHideable = true
bottomSheetBehavior.peekHeight = 0
2020-09-17 17:56:59 +00:00
collapsePanel()
ViewCompat.setElevation(slidingPanel, 0f)
ViewCompat.setElevation(bottomNavigationView, 10f)
}
}
})
}
2020-09-23 21:10:16 +00:00
private fun chooseFragmentForTheme() {
nowPlayingScreen = PreferenceUtil.nowPlayingScreen
val fragment: Fragment = when (nowPlayingScreen) {
Blur -> BlurPlayerFragment()
Adaptive -> AdaptiveFragment()
Normal -> PlayerFragment()
Card -> CardFragment()
BlurCard -> CardBlurFragment()
Fit -> FitFragment()
Flat -> FlatPlayerFragment()
Full -> FullPlayerFragment()
Plain -> PlainPlayerFragment()
Simple -> SimplePlayerFragment()
Material -> MaterialFragment()
Color -> ColorFragment()
Gradient -> GradientPlayerFragment()
Tiny -> TinyPlayerFragment()
2020-09-23 21:10:16 +00:00
Peak -> PeakPlayerFragment()
Circle -> CirclePlayerFragment()
2020-10-02 12:56:17 +00:00
Classic -> ClassicPlayerFragment()
else -> PlayerFragment()
} // must implement AbsPlayerFragment
2020-10-02 18:05:09 +00:00
supportFragmentManager.commit {
replace(R.id.playerFragmentContainer, fragment)
}
supportFragmentManager.executePendingTransactions()
2020-10-02 18:05:09 +00:00
playerFragment = whichFragment<AbsPlayerFragment>(R.id.playerFragmentContainer)
miniPlayerFragment = whichFragment<MiniPlayerFragment>(R.id.miniPlayerFragment)
miniPlayerFragment?.view?.setOnClickListener { expandPanel() }
}
2020-10-06 08:46:04 +00:00
}