Fix play/pause bug

Possibly fix a bug where the play/pause button would get stuck in the play position when playing from a file intent.
This commit is contained in:
OxygenCobalt 2021-02-25 17:55:20 -07:00
parent e9abee9f64
commit af9fac7de2
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
6 changed files with 35 additions and 21 deletions

View file

@ -38,9 +38,6 @@ class MainActivity : AppCompatActivity() {
// Apply the theme // Apply the theme
setTheme(accent.theme) setTheme(accent.theme)
// onNewIntent doesnt automatically call on startup, so call it here.
onNewIntent(intent)
if (isEdgeOn()) { if (isEdgeOn()) {
doEdgeToEdgeSetup(binding) doEdgeToEdgeSetup(binding)
} }
@ -51,6 +48,9 @@ class MainActivity : AppCompatActivity() {
// Start PlaybackService // Start PlaybackService
startService(Intent(this, PlaybackService::class.java)) startService(Intent(this, PlaybackService::class.java))
// onNewIntent doesnt automatically call on startup, so call it here.
onNewIntent(intent)
} }
override fun onNewIntent(intent: Intent?) { override fun onNewIntent(intent: Intent?) {

View file

@ -88,15 +88,17 @@ class MusicStore private constructor() {
* Get the song for a file [uri]. * Get the song for a file [uri].
* @return The corresponding [Song] for this [uri], null if there isnt one. * @return The corresponding [Song] for this [uri], null if there isnt one.
*/ */
suspend fun getSongForUri(uri: Uri, resolver: ContentResolver): Song? { fun getSongForUri(uri: Uri, resolver: ContentResolver): Song? {
return withContext(Dispatchers.IO) { resolver.query(
resolver.query(uri, null, null, null, null)?.use { cursor -> uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null
)?.use { cursor ->
cursor.moveToFirst() cursor.moveToFirst()
val fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) val fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
return@withContext songs.find { it.fileName == fileName } return songs.find { it.fileName == fileName }
}
} }
return null
} }
/** /**

View file

@ -55,8 +55,6 @@ class CompactPlaybackFragment : Fragment() {
// --- VIEWMODEL SETUP --- // --- VIEWMODEL SETUP ---
playbackModel.disableAnimation()
playbackModel.song.observe(viewLifecycleOwner) { song -> playbackModel.song.observe(viewLifecycleOwner) { song ->
if (song != null) { if (song != null) {
logD("Updating song display to ${song.name}") logD("Updating song display to ${song.name}")
@ -76,4 +74,10 @@ class CompactPlaybackFragment : Fragment() {
return binding.root return binding.root
} }
override fun onStart() {
super.onStart()
playbackModel.disableAnimation()
}
} }

View file

@ -8,6 +8,7 @@ import android.util.AttributeSet
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.widget.AppCompatImageButton import androidx.appcompat.widget.AppCompatImageButton
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.ui.toAnimDrawable import org.oxycblt.auxio.ui.toAnimDrawable
/** /**
@ -45,6 +46,7 @@ class PlayPauseButton @JvmOverloads constructor(
setImageDrawable(iconPauseToPlay) setImageDrawable(iconPauseToPlay)
iconPauseToPlay.start() iconPauseToPlay.start()
} else { } else {
logD("what the FUCK WHY ARENT YOU DOING THIS")
setImageResource(R.drawable.ic_play_large) setImageResource(R.drawable.ic_play_large)
} }
} }

View file

@ -77,9 +77,6 @@ class PlaybackFragment : Fragment(), SeekBar.OnSeekBarChangeListener {
binding.playbackSeekBar.setOnSeekBarChangeListener(this) binding.playbackSeekBar.setOnSeekBarChangeListener(this)
// --- VIEWMODEL SETUP -- // --- VIEWMODEL SETUP --
playbackModel.disableAnimation()
playbackModel.song.observe(viewLifecycleOwner) { song -> playbackModel.song.observe(viewLifecycleOwner) { song ->
if (song != null) { if (song != null) {
logD("Updating song display to ${song.name}.") logD("Updating song display to ${song.name}.")
@ -158,8 +155,13 @@ class PlaybackFragment : Fragment(), SeekBar.OnSeekBarChangeListener {
playbackModel.isPlaying.observe(viewLifecycleOwner) { playbackModel.isPlaying.observe(viewLifecycleOwner) {
binding.playbackPlayPause.apply { binding.playbackPlayPause.apply {
backgroundTintList = if (it) accentColor else controlColor if (it) {
setPlaying(it, playbackModel.canAnimate) backgroundTintList = accentColor
setPlaying(true, playbackModel.canAnimate)
} else {
backgroundTintList = controlColor
setPlaying(false, playbackModel.canAnimate)
}
} }
playbackModel.enableAnimation() playbackModel.enableAnimation()
@ -176,6 +178,12 @@ class PlaybackFragment : Fragment(), SeekBar.OnSeekBarChangeListener {
return binding.root return binding.root
} }
override fun onStart() {
super.onStart()
playbackModel.disableAnimation()
}
// --- SEEK CALLBACKS --- // --- SEEK CALLBACKS ---
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {

View file

@ -181,12 +181,10 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
* This is called after [playWithUri] once its deemed safe to do so. * This is called after [playWithUri] once its deemed safe to do so.
*/ */
private fun playWithUriInternal(uri: Uri, context: Context) { private fun playWithUriInternal(uri: Uri, context: Context) {
viewModelScope.launch {
musicStore.getSongForUri(uri, context.contentResolver)?.let { song -> musicStore.getSongForUri(uri, context.contentResolver)?.let { song ->
playSong(song) playSong(song)
} }
} }
}
/** /**
* Shuffle all songs * Shuffle all songs