Update local config when shuffle, repeat mode is changed

This commit is contained in:
Naveen 2023-08-28 12:41:14 +05:30
parent f14972eb97
commit 197e143403
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
4 changed files with 40 additions and 15 deletions

View file

@ -264,8 +264,7 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener {
}
}
private fun setupShuffleButton() {
val isShuffleEnabled = config.isShuffleEnabled
private fun setupShuffleButton(isShuffleEnabled: Boolean = config.isShuffleEnabled) {
activity_track_toggle_shuffle.apply {
applyColorFilter(if (isShuffleEnabled) getProperPrimaryColor() else getProperTextColor())
alpha = if (isShuffleEnabled) 1f else MEDIUM_ALPHA
@ -293,8 +292,7 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener {
}
}
private fun setupPlaybackSettingButton() {
val playbackSetting = config.playbackSetting
private fun setupPlaybackSettingButton(playbackSetting: PlaybackSetting = config.playbackSetting) {
activity_track_playback_setting.apply {
contentDescription = getString(playbackSetting.contentDescriptionStringRes)
setImageResource(playbackSetting.iconRes)
@ -352,15 +350,13 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener {
return BitmapDrawable(resources, bitmapResized)
}
override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)
updatePlayerState()
}
override fun onPlaybackStateChanged(playbackState: Int) = updatePlayerState()
override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying)
updatePlayerState()
}
override fun onIsPlayingChanged(isPlaying: Boolean) = updatePlayerState()
override fun onRepeatModeChanged(repeatMode: Int) = setupPlaybackSettingButton(getPlaybackSetting(repeatMode))
override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) = setupShuffleButton(shuffleModeEnabled)
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
super.onMediaItemTransition(mediaItem, reason)
@ -382,13 +378,16 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener {
private fun updatePlayerState() {
withPlayer {
val isPlaying = isReallyPlaying
activity_track_play_pause.updatePlayPauseIcon(isReallyPlaying, getProperTextColor())
updateProgress(currentPosition)
if (isPlaying) {
scheduleProgressUpdate()
} else {
cancelProgressUpdate()
}
updateProgress(currentPosition)
updatePlayPause(isPlaying)
setupShuffleButton(shuffleModeEnabled)
setupPlaybackSettingButton(getPlaybackSetting(repeatMode))
}
}
@ -410,4 +409,8 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener {
private fun updateProgress(currentPosition: Long) {
activity_track_progressbar.progress = currentPosition.milliseconds.inWholeSeconds.toInt()
}
private fun updatePlayPause(isPlaying: Boolean) {
activity_track_play_pause.updatePlayPauseIcon(isPlaying, getProperTextColor())
}
}

View file

@ -13,6 +13,7 @@ import android.provider.MediaStore
import android.provider.MediaStore.Audio
import android.util.Size
import androidx.core.net.toUri
import androidx.media3.common.Player
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
@ -279,3 +280,12 @@ fun Context.getTrackFromUri(uri: Uri?, callback: (track: Track) -> Unit) {
}
fun Context.isTabVisible(flag: Int) = config.showTabs and flag != 0
fun Context.getPlaybackSetting(repeatMode: @Player.RepeatMode Int): PlaybackSetting {
return when (repeatMode) {
Player.REPEAT_MODE_OFF -> PlaybackSetting.REPEAT_OFF
Player.REPEAT_MODE_ONE -> PlaybackSetting.REPEAT_TRACK
Player.REPEAT_MODE_ALL -> PlaybackSetting.REPEAT_PLAYLIST
else -> config.playbackSetting
}
}

View file

@ -118,7 +118,10 @@ fun Player.maybeForcePrevious(): Boolean {
}
}
/**
* This function takes a list of media items and the current index in the playlist. It then
* adds the media items that come before and after the current index to the player's playlist.
*/
fun Player.addRemainingMediaItems(mediaItems: List<MediaItem>, currentIndex: Int) {
val itemsAtStart = mediaItems.take(currentIndex)
val itemsAtEnd = mediaItems.takeLast(mediaItems.lastIndex - currentIndex)

View file

@ -8,6 +8,7 @@ import androidx.media3.common.util.UnstableApi
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.getPlaybackSetting
import com.simplemobiletools.musicplayer.helpers.PlaybackSetting
import com.simplemobiletools.musicplayer.playback.PlaybackService
@ -43,4 +44,12 @@ internal fun PlaybackService.getPlayerListener() = object : Player.Listener {
}
}
}
override fun onRepeatModeChanged(repeatMode: Int) {
config.playbackSetting = getPlaybackSetting(repeatMode)
}
override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) {
config.isShuffleEnabled = shuffleModeEnabled
}
}