Add durations to PlaybackFragment

Add the song duration & the current duration to PlaybackFragment, update the SeekBar in PlaybackFragment to reflect those durations.
This commit is contained in:
OxygenCobalt
2020-10-12 16:13:15 -06:00
parent 09d4e107e0
commit 3bafc17d0c
9 changed files with 103 additions and 27 deletions
@@ -14,6 +14,7 @@ sealed class BaseModel {
}
// Song
// TODO: Maybe move durations to a solely-millis system
data class Song(
override val id: Long = -1,
override var name: String,
@@ -3,9 +3,11 @@ package org.oxycblt.auxio.playback
import android.content.res.ColorStateList
import android.graphics.drawable.AnimatedVectorDrawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
@@ -15,11 +17,11 @@ import org.oxycblt.auxio.databinding.FragmentPlaybackBinding
import org.oxycblt.auxio.theme.accent
import org.oxycblt.auxio.theme.toColor
class PlaybackFragment : Fragment() {
class PlaybackFragment : Fragment(), SeekBar.OnSeekBarChangeListener {
private val playbackModel: PlaybackViewModel by activityViewModels()
// TODO: Implement media controls
// TODO: Implement nav to artists/albums
// TODO: Add a full playback fragment
// TODO: Possibly implement a trackbar with a spectrum shown as well.
override fun onCreateView(
inflater: LayoutInflater,
@@ -31,6 +33,7 @@ class PlaybackFragment : Fragment() {
// Create accents & icons to use
val accentColor = ColorStateList.valueOf(accent.first.toColor(requireContext()))
val inactiveColor = ColorStateList.valueOf(R.color.control_color.toColor(requireContext()))
val normalTextColor = binding.playbackDurationCurrent.currentTextColor
val iconPauseToPlay = ContextCompat.getDrawable(
requireContext(), R.drawable.ic_pause_to_play
@@ -50,6 +53,7 @@ class PlaybackFragment : Fragment() {
// Make marquee scroll work
binding.playbackSong.isSelected = true
binding.playbackSeekBar.setOnSeekBarChangeListener(this)
// --- VIEWMODEL SETUP --
@@ -73,6 +77,39 @@ class PlaybackFragment : Fragment() {
}
}
playbackModel.isSeeking.observe(viewLifecycleOwner) {
// Highlight the current duration if the user is seeking, and revert it if not.
if (it) {
binding.playbackDurationCurrent.setTextColor(accentColor)
} else {
binding.playbackDurationCurrent.setTextColor(normalTextColor)
}
}
// Updates for the current duration TextView/Seekbar
playbackModel.formattedCurrentDuration.observe(viewLifecycleOwner) {
binding.playbackDurationCurrent.text = it
}
playbackModel.formattedSeekBarProgress.observe(viewLifecycleOwner) {
binding.playbackSeekBar.progress = it
}
Log.d(this::class.simpleName, "Fragment Created.")
return binding.root
}
// Seeking callbacks
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
playbackModel.updateCurrentDurationWithProgress(progress)
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
playbackModel.setSeekingStatus(true)
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
playbackModel.setSeekingStatus(false)
}
}
@@ -2,8 +2,10 @@ package org.oxycblt.auxio.playback
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.music.toDuration
// TODO: Implement media controls
// TODO: Add the playback service itself
@@ -13,12 +15,23 @@ class PlaybackViewModel : ViewModel() {
private val mCurrentSong = MutableLiveData<Song>()
val currentSong: LiveData<Song> get() = mCurrentSong
private val mShouldOpenPlayback = MutableLiveData<Boolean>()
val shouldOpenPlayback: LiveData<Boolean> get() = mShouldOpenPlayback
private val mCurrentDuration = MutableLiveData(0L)
val currentDuration: LiveData<Long> get() = mCurrentDuration
private val mIsPlaying = MutableLiveData(false)
val isPlaying: LiveData<Boolean> get() = mIsPlaying
private val mIsSeeking = MutableLiveData(false)
val isSeeking: LiveData<Boolean> get() = mIsSeeking
val formattedCurrentDuration = Transformations.map(currentDuration) {
it.toDuration()
}
val formattedSeekBarProgress = Transformations.map(currentDuration) {
((it.toDouble() / mCurrentSong.value!!.seconds) * 100).toInt()
}
fun updateSong(song: Song) {
mCurrentSong.value = song
@@ -27,16 +40,17 @@ class PlaybackViewModel : ViewModel() {
}
}
fun openPlayback() {
mShouldOpenPlayback.value = true
}
fun doneWithOpenPlayback() {
mShouldOpenPlayback.value = false
}
// Invert, not directly set the p
// Invert, not directly set the playing status
fun invertPlayingStatus() {
mIsPlaying.value = !mIsPlaying.value!!
}
fun setSeekingStatus(status: Boolean) {
mIsSeeking.value = status
}
fun updateCurrentDurationWithProgress(progress: Int) {
mCurrentDuration.value =
((progress.toDouble() / 100) * mCurrentSong.value!!.seconds).toLong()
}
}