PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/fragments/other/MiniPlayerFragment.kt

201 lines
7.0 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.
*
*/
package code.name.monkey.retromusic.fragments.other
2018-11-30 01:06:16 +00:00
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
2018-11-30 01:06:16 +00:00
import android.view.animation.DecelerateInterpolator
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.databinding.FragmentMiniPlayerBinding
2020-10-06 08:46:04 +00:00
import code.name.monkey.retromusic.extensions.accentColor
import code.name.monkey.retromusic.extensions.show
import code.name.monkey.retromusic.extensions.textColorPrimary
import code.name.monkey.retromusic.extensions.textColorSecondary
2019-08-01 14:25:53 +00:00
import code.name.monkey.retromusic.fragments.base.AbsMusicServiceFragment
2018-11-30 01:06:16 +00:00
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.helper.MusicProgressViewUpdateHelper
import code.name.monkey.retromusic.helper.PlayPauseButtonOnClickHandler
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2019-11-01 17:41:22 +00:00
import code.name.monkey.retromusic.util.RetroUtil
2019-08-02 18:34:18 +00:00
import kotlin.math.abs
2018-11-30 01:06:16 +00:00
open class MiniPlayerFragment : AbsMusicServiceFragment(R.layout.fragment_mini_player),
MusicProgressViewUpdateHelper.Callback, View.OnClickListener {
2019-08-02 18:34:18 +00:00
private var _binding: FragmentMiniPlayerBinding? = null
private val binding get() = _binding!!
2019-08-02 18:34:18 +00:00
private lateinit var progressViewUpdateHelper: MusicProgressViewUpdateHelper
2018-11-30 01:06:16 +00:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
progressViewUpdateHelper = MusicProgressViewUpdateHelper(this)
}
2018-12-25 14:58:47 +00:00
override fun onClick(view: View) {
2018-11-30 01:06:16 +00:00
when (view.id) {
R.id.actionNext -> MusicPlayerRemote.playNextSong()
R.id.actionPrevious -> MusicPlayerRemote.back()
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentMiniPlayerBinding.bind(view)
2019-07-30 18:30:19 +00:00
view.setOnTouchListener(FlingPlayBackController(requireContext()))
2018-11-30 01:06:16 +00:00
setUpMiniPlayer()
if (RetroUtil.isTablet()) {
binding.actionNext.show()
binding.actionPrevious.show()
2018-11-30 01:06:16 +00:00
} else {
binding.actionNext.visibility =
if (PreferenceUtil.isExtraControls) View.VISIBLE else View.GONE
binding.actionPrevious.visibility =
2020-06-06 18:57:28 +00:00
if (PreferenceUtil.isExtraControls) View.VISIBLE else View.GONE
2018-11-30 01:06:16 +00:00
}
binding.actionNext.setOnClickListener(this)
binding.actionPrevious.setOnClickListener(this)
2018-11-30 01:06:16 +00:00
}
private fun setUpMiniPlayer() {
setUpPlayPauseButton()
binding.progressBar.accentColor()
2018-11-30 01:06:16 +00:00
}
private fun setUpPlayPauseButton() {
binding.miniPlayerPlayPauseButton.setOnClickListener(PlayPauseButtonOnClickHandler())
2018-11-30 01:06:16 +00:00
}
private fun updateSongTitle() {
val song = MusicPlayerRemote.currentSong
val builder = SpannableStringBuilder()
2018-11-30 01:06:16 +00:00
val title = SpannableString(song.title)
2020-05-20 20:28:38 +00:00
title.setSpan(ForegroundColorSpan(textColorPrimary()), 0, title.length, 0)
2018-11-30 01:06:16 +00:00
val text = SpannableString(song.artistName)
2020-05-20 20:28:38 +00:00
text.setSpan(ForegroundColorSpan(textColorSecondary()), 0, text.length, 0)
2018-11-30 01:06:16 +00:00
builder.append(title).append("").append(text)
binding.miniPlayerTitle.isSelected = true
binding.miniPlayerTitle.text = builder
// binding.title.isSelected = true
// binding.title.text = song.title
// binding.text.isSelected = true
// binding.text.text = song.artistName
}
private fun updateSongCover() {
// val song = MusicPlayerRemote.currentSong
// GlideApp.with(requireContext())
// .asBitmap()
// .songCoverOptions(song)
// .transition(RetroGlideExtension.getDefaultTransition())
// .load(RetroGlideExtension.getSongModel(song))
// .into(binding.image)
2018-11-30 01:06:16 +00:00
}
override fun onServiceConnected() {
updateSongTitle()
updateSongCover()
2018-11-30 01:06:16 +00:00
updatePlayPauseDrawableState()
}
override fun onPlayingMetaChanged() {
updateSongTitle()
updateSongCover()
2018-11-30 01:06:16 +00:00
}
override fun onPlayStateChanged() {
updatePlayPauseDrawableState()
2019-11-01 17:41:22 +00:00
}
2018-11-30 01:06:16 +00:00
override fun onUpdateProgressViews(progress: Int, total: Int) {
binding.progressBar.max = total
val animator = ObjectAnimator.ofInt(binding.progressBar, "progress", progress)
2018-11-30 01:06:16 +00:00
animator.duration = 1000
animator.interpolator = DecelerateInterpolator()
animator.start()
}
override fun onResume() {
super.onResume()
2019-08-02 18:34:18 +00:00
progressViewUpdateHelper.start()
2018-11-30 01:06:16 +00:00
}
override fun onPause() {
super.onPause()
2019-08-02 18:34:18 +00:00
progressViewUpdateHelper.stop()
2018-11-30 01:06:16 +00:00
}
protected fun updatePlayPauseDrawableState() {
if (MusicPlayerRemote.isPlaying) {
binding.miniPlayerPlayPauseButton.setImageResource(R.drawable.ic_pause)
2018-11-30 01:06:16 +00:00
} else {
binding.miniPlayerPlayPauseButton.setImageResource(R.drawable.ic_play_arrow)
2018-11-30 01:06:16 +00:00
}
}
class FlingPlayBackController(context: Context) : View.OnTouchListener {
2019-01-28 10:45:51 +00:00
private var flingPlayBackController: GestureDetector
2018-11-30 01:06:16 +00:00
init {
flingPlayBackController = GestureDetector(context,
2020-01-06 06:17:22 +00:00
object : GestureDetector.SimpleOnGestureListener() {
override fun onFling(
2020-10-06 08:46:04 +00:00
e1: MotionEvent,
e2: MotionEvent,
velocityX: Float,
2020-01-06 06:17:22 +00:00
velocityY: Float
): Boolean {
if (abs(velocityX) > abs(velocityY)) {
if (velocityX < 0) {
MusicPlayerRemote.playNextSong()
return true
} else if (velocityX > 0) {
MusicPlayerRemote.playPreviousSong()
return true
2018-11-30 01:06:16 +00:00
}
}
2020-01-06 06:17:22 +00:00
return false
}
})
2018-11-30 01:06:16 +00:00
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View, event: MotionEvent): Boolean {
return flingPlayBackController.onTouchEvent(event)
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
2018-11-30 01:06:16 +00:00
}