playback: use playsong settings

Use PlaySong modeling within settings too. This largely completes the
PlaySong refactor and fully adds the ability to play songs by
themselves as an option.

Resolves #424.
This commit is contained in:
Alexander Capehart
2023-07-11 11:58:05 -06:00
parent 97816e349a
commit 3908400418
45 changed files with 337 additions and 392 deletions
@@ -65,14 +65,14 @@ object IntegerTable {
const val REPEAT_MODE_ALL = 0xA101
/** RepeatMode.TRACK */
const val REPEAT_MODE_TRACK = 0xA102
/** PlaybackMode.IN_GENRE */
const val PLAYBACK_MODE_IN_GENRE = 0xA103
/** PlaybackMode.IN_ARTIST */
const val PLAYBACK_MODE_IN_ARTIST = 0xA104
/** PlaybackMode.IN_ALBUM */
const val PLAYBACK_MODE_IN_ALBUM = 0xA105
/** PlaybackMode.ALL_SONGS */
const val PLAYBACK_MODE_ALL_SONGS = 0xA106
// /** PlaybackMode.IN_GENRE (No longer used but still reserved) */
// const val PLAYBACK_MODE_IN_GENRE = 0xA103
// /** PlaybackMode.IN_ARTIST (No longer used but still reserved) */
// const val PLAYBACK_MODE_IN_ARTIST = 0xA104
// /** PlaybackMode.IN_ALBUM (No longer used but still reserved) */
// const val PLAYBACK_MODE_IN_ALBUM = 0xA105
// /** PlaybackMode.ALL_SONGS (No longer used but still reserved) */
// const val PLAYBACK_MODE_ALL_SONGS = 0xA106
/** MusicMode.SONGS */
const val MUSIC_MODE_SONGS = 0xA10B
/** MusicMode.ALBUMS */
@@ -121,8 +121,8 @@ object IntegerTable {
const val COVER_MODE_MEDIA_STORE = 0xA11D
/** CoverMode.Quality */
const val COVER_MODE_QUALITY = 0xA11E
/** PlaySong.Itself */
const val PLAY_SONG_ITSELF = 0xA11F
/** PlaySong.ByItself */
const val PLAY_SONG_BY_ITSELF = 0xA11F
/** PlaySong.FromAll */
const val PLAY_SONG_FROM_ALL = 0xA120
/** PlaySong.FromAlbum */
@@ -44,7 +44,6 @@ import org.oxycblt.auxio.list.Menu
import org.oxycblt.auxio.list.Sort
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.music.MusicMode
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.music.PlaylistDecision
@@ -180,12 +179,7 @@ class AlbumDetailFragment :
}
override fun onRealClick(item: Song) {
val mode = detailModel.playbackMode
if (mode != null) {
playbackModel.play(item, detailModel.playbackMode ?: MusicMode.ALBUMS)
} else {
playbackModel.playFromAlbum(item)
}
playbackModel.play(item, detailModel.playInAlbumWith)
}
override fun onOpenMenu(item: Song, anchor: View) {
@@ -179,17 +179,7 @@ class ArtistDetailFragment :
override fun onRealClick(item: Music) {
when (item) {
is Album -> detailModel.showAlbum(item)
is Song -> {
val playbackMode = detailModel.playbackMode
if (playbackMode != null) {
playbackModel.play(item, playbackMode)
} else {
// When configured to play from the selected item, we already have an Artist
// to play from.
playbackModel.playFromArtist(
item, unlikelyToBeNull(detailModel.currentArtist.value))
}
}
is Song -> playbackModel.play(item, detailModel.playInArtistWith)
else -> error("Unexpected datatype: ${item::class.simpleName}")
}
}
@@ -42,18 +42,19 @@ import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.music.MusicMode
import org.oxycblt.auxio.music.MusicRepository
import org.oxycblt.auxio.music.MusicSettings
import org.oxycblt.auxio.music.Playlist
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.music.info.ReleaseType
import org.oxycblt.auxio.music.metadata.AudioProperties
import org.oxycblt.auxio.playback.PlaySong
import org.oxycblt.auxio.playback.PlaybackSettings
import org.oxycblt.auxio.util.Event
import org.oxycblt.auxio.util.MutableEvent
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.logW
import org.oxycblt.auxio.util.unlikelyToBeNull
/**
* [ViewModel] that manages the Song, Album, Artist, and Genre detail views. Keeps track of the
@@ -115,6 +116,10 @@ constructor(
currentAlbum.value?.let { refreshAlbumList(it, true) }
}
/** The [PlaySong] instructions to use when playing a [Song] from [Album] details. */
val playInAlbumWith
get() = playbackSettings.inParentPlaybackMode ?: PlaySong.FromAlbum
// --- ARTIST ---
private val _currentArtist = MutableStateFlow<Artist?>(null)
@@ -139,6 +144,10 @@ constructor(
currentArtist.value?.let { refreshArtistList(it, true) }
}
/** The [PlaySong] instructions to use when playing a [Song] from [Artist] details. */
val playInArtistWith
get() = playbackSettings.inParentPlaybackMode ?: PlaySong.FromArtist(currentArtist.value)
// --- GENRE ---
private val _currentGenre = MutableStateFlow<Genre?>(null)
@@ -163,6 +172,10 @@ constructor(
currentGenre.value?.let { refreshGenreList(it, true) }
}
/** The [PlaySong] instructions to use when playing a [Song] from [Genre] details. */
val playInGenreWith
get() = playbackSettings.inParentPlaybackMode ?: PlaySong.FromGenre(currentGenre.value)
// --- PLAYLIST ---
private val _currentPlaylist = MutableStateFlow<Playlist?>(null)
@@ -186,12 +199,11 @@ constructor(
val editedPlaylist: StateFlow<List<Song>?>
get() = _editedPlaylist
/**
* The [MusicMode] to use when playing a [Song] from the UI, or null to play from the currently
* shown item.
*/
val playbackMode: MusicMode?
get() = playbackSettings.inParentPlaybackMode
/** The [PlaySong] instructions to use when playing a [Song] from [Genre] details. */
val playInPlaylistWith
get() =
playbackSettings.inParentPlaybackMode
?: PlaySong.FromPlaylist(unlikelyToBeNull(currentPlaylist.value))
init {
musicRepository.addUpdateListener(this)
@@ -177,17 +177,7 @@ class GenreDetailFragment :
override fun onRealClick(item: Music) {
when (item) {
is Artist -> detailModel.showArtist(item)
is Song -> {
val playbackMode = detailModel.playbackMode
if (playbackMode != null) {
playbackModel.play(item, playbackMode)
} else {
// When configured to play from the selected item, we already have an Genre
// to play from.
playbackModel.playFromGenre(
item, unlikelyToBeNull(detailModel.currentGenre.value))
}
}
is Song -> playbackModel.play(item, detailModel.playInGenreWith)
else -> error("Unexpected datatype: ${item::class.simpleName}")
}
}
@@ -229,7 +229,7 @@ class PlaylistDetailFragment :
}
override fun onRealClick(item: Song) {
playbackModel.playFromPlaylist(item, unlikelyToBeNull(detailModel.currentPlaylist.value))
playbackModel.play(item, detailModel.playInPlaylistWith)
}
override fun onPickUp(viewHolder: RecyclerView.ViewHolder) {
@@ -34,6 +34,7 @@ import org.oxycblt.auxio.music.MusicRepository
import org.oxycblt.auxio.music.MusicSettings
import org.oxycblt.auxio.music.Playlist
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.PlaySong
import org.oxycblt.auxio.playback.PlaybackSettings
import org.oxycblt.auxio.util.Event
import org.oxycblt.auxio.util.MutableEvent
@@ -102,9 +103,9 @@ constructor(
val playlistsInstructions: Event<UpdateInstructions>
get() = _playlistsInstructions
/** The [MusicMode] to use when playing a [Song] from the UI. */
val playbackMode: MusicMode
get() = playbackSettings.inListPlaybackMode
/** The [PlaySong] instructions to use when playing a [Song]. */
val playWith
get() = playbackSettings.playInListWith
/**
* A list of [MusicMode] corresponding to the current [Tab] configuration, excluding invisible
@@ -137,7 +137,7 @@ class SongListFragment :
}
override fun onRealClick(item: Song) {
playbackModel.play(item, homeModel.playbackMode)
playbackModel.play(item, homeModel.playWith)
}
override fun onOpenMenu(item: Song, anchor: View) {
@@ -22,18 +22,13 @@ import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.music.MusicMode
import org.oxycblt.auxio.music.Playlist
sealed interface PlaySong {
val intCode: Int
object ByItself : PlaySong {
override val intCode = IntegerTable.PLAY_SONG_ITSELF
}
object FromAll : PlaySong {
override val intCode = IntegerTable.PLAY_SONG_ITSELF
override val intCode = IntegerTable.PLAY_SONG_FROM_ALL
}
object FromAlbum : PlaySong {
@@ -52,19 +47,14 @@ sealed interface PlaySong {
override val intCode = IntegerTable.PLAY_SONG_FROM_PLAYLIST
}
companion object {
fun fromPlaybackModeTemporary(playbackMode: MusicMode) =
when (playbackMode) {
MusicMode.SONGS -> FromAll
MusicMode.ALBUMS -> FromAlbum
MusicMode.ARTISTS -> FromArtist(null)
MusicMode.GENRES -> FromGenre(null)
MusicMode.PLAYLISTS -> throw IllegalStateException()
}
object ByItself : PlaySong {
override val intCode = IntegerTable.PLAY_SONG_BY_ITSELF
}
companion object {
fun fromIntCode(intCode: Int, inner: Music?): PlaySong? =
when (intCode) {
IntegerTable.PLAY_SONG_ITSELF -> ByItself
IntegerTable.PLAY_SONG_BY_ITSELF -> ByItself
IntegerTable.PLAY_SONG_FROM_ALBUM -> FromAlbum
IntegerTable.PLAY_SONG_FROM_ARTIST ->
if (inner is Artist?) {
@@ -24,7 +24,6 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.MusicMode
import org.oxycblt.auxio.playback.replaygain.ReplayGainMode
import org.oxycblt.auxio.playback.replaygain.ReplayGainPreAmp
import org.oxycblt.auxio.settings.Settings
@@ -46,16 +45,13 @@ interface PlaybackSettings : Settings<PlaybackSettings.Listener> {
val replayGainMode: ReplayGainMode
/** The current ReplayGain pre-amp configuration. */
var replayGainPreAmp: ReplayGainPreAmp
/** How to play a song from a general list of songs, specified by [PlaySong] */
val playInListWith: PlaySong
/**
* What type of MusicParent to play from when a Song is played from a list of other items. Null
* if to play from all Songs.
* How to play a song from a parent item, specified by [PlaySong]. Null if to delegate to the UI
* context.
*/
val inListPlaybackMode: MusicMode
/**
* What type of MusicParent to play from when a Song is played from within an item (ex. like in
* the detail view). Null if to play from the item it was played in.
*/
val inParentPlaybackMode: MusicMode?
val inParentPlaybackMode: PlaySong?
/** Whether to keep shuffle on when playing a new Song. */
val keepShuffle: Boolean
/** Whether to rewind when the skip previous button is pressed before skipping back. */
@@ -75,18 +71,20 @@ interface PlaybackSettings : Settings<PlaybackSettings.Listener> {
class PlaybackSettingsImpl @Inject constructor(@ApplicationContext context: Context) :
Settings.Impl<PlaybackSettings.Listener>(context), PlaybackSettings {
override val inListPlaybackMode: MusicMode
override val playInListWith: PlaySong
get() =
MusicMode.fromIntCode(
PlaySong.fromIntCode(
sharedPreferences.getInt(
getString(R.string.set_key_in_list_playback_mode), Int.MIN_VALUE))
?: MusicMode.SONGS
getString(R.string.set_key_play_in_list_with), Int.MIN_VALUE),
null)
?: PlaySong.FromAll
override val inParentPlaybackMode: MusicMode?
override val inParentPlaybackMode: PlaySong?
get() =
MusicMode.fromIntCode(
PlaySong.fromIntCode(
sharedPreferences.getInt(
getString(R.string.set_key_in_parent_playback_mode), Int.MIN_VALUE))
getString(R.string.set_key_play_in_list_with), Int.MIN_VALUE),
null)
override val barAction: ActionMode
get() =
@@ -132,65 +130,44 @@ class PlaybackSettingsImpl @Inject constructor(@ApplicationContext context: Cont
get() = sharedPreferences.getBoolean(getString(R.string.set_key_repeat_pause), false)
override fun migrate() {
// "Use alternate notification action" was converted to an ActionMode setting in 3.0.0.
if (sharedPreferences.contains(OLD_KEY_ALT_NOTIF_ACTION)) {
logD("Migrating $OLD_KEY_ALT_NOTIF_ACTION")
val mode =
if (sharedPreferences.getBoolean(OLD_KEY_ALT_NOTIF_ACTION, false)) {
ActionMode.SHUFFLE
} else {
ActionMode.REPEAT
}
sharedPreferences.edit {
putInt(getString(R.string.set_key_notif_action), mode.intCode)
remove(OLD_KEY_ALT_NOTIF_ACTION)
apply()
}
}
// PlaybackMode was converted to MusicMode in 3.0.0
fun Int.migratePlaybackMode() =
// MusicMode was converted to PlaySong in 3.2.0
fun Int.migrateMusicMode() =
when (this) {
// Convert PlaybackMode into MusicMode
IntegerTable.PLAYBACK_MODE_ALL_SONGS -> MusicMode.SONGS
IntegerTable.PLAYBACK_MODE_IN_ARTIST -> MusicMode.ARTISTS
IntegerTable.PLAYBACK_MODE_IN_ALBUM -> MusicMode.ALBUMS
IntegerTable.PLAYBACK_MODE_IN_GENRE -> MusicMode.GENRES
IntegerTable.MUSIC_MODE_SONGS -> PlaySong.FromAll
IntegerTable.MUSIC_MODE_ALBUMS -> PlaySong.FromAlbum
IntegerTable.MUSIC_MODE_ARTISTS -> PlaySong.FromArtist(null)
IntegerTable.MUSIC_MODE_GENRES -> PlaySong.FromGenre(null)
else -> null
}
if (sharedPreferences.contains(OLD_KEY_LIB_PLAYBACK_MODE)) {
logD("Migrating $OLD_KEY_LIB_PLAYBACK_MODE")
if (sharedPreferences.contains(OLD_KEY_LIB_MUSIC_PLAYBACK_MODE)) {
logD("Migrating $OLD_KEY_LIB_MUSIC_PLAYBACK_MODE")
val mode =
sharedPreferences
.getInt(OLD_KEY_LIB_PLAYBACK_MODE, IntegerTable.PLAYBACK_MODE_ALL_SONGS)
.migratePlaybackMode()
?: MusicMode.SONGS
.getInt(OLD_KEY_LIB_MUSIC_PLAYBACK_MODE, Int.MIN_VALUE)
.migrateMusicMode()
sharedPreferences.edit {
putInt(getString(R.string.set_key_in_list_playback_mode), mode.intCode)
remove(OLD_KEY_LIB_PLAYBACK_MODE)
putInt(
getString(R.string.set_key_play_in_list_with), mode?.intCode ?: Int.MIN_VALUE)
remove(OLD_KEY_LIB_MUSIC_PLAYBACK_MODE)
apply()
}
}
if (sharedPreferences.contains(OLD_KEY_DETAIL_PLAYBACK_MODE)) {
logD("Migrating $OLD_KEY_DETAIL_PLAYBACK_MODE")
if (sharedPreferences.contains(OLD_KEY_DETAIL_MUSIC_PLAYBACK_MODE)) {
logD("Migrating $OLD_KEY_DETAIL_MUSIC_PLAYBACK_MODE")
val mode =
sharedPreferences
.getInt(OLD_KEY_DETAIL_PLAYBACK_MODE, Int.MIN_VALUE)
.migratePlaybackMode()
.getInt(OLD_KEY_DETAIL_MUSIC_PLAYBACK_MODE, Int.MIN_VALUE)
.migrateMusicMode()
sharedPreferences.edit {
putInt(
getString(R.string.set_key_in_parent_playback_mode),
mode?.intCode ?: Int.MIN_VALUE)
remove(OLD_KEY_DETAIL_PLAYBACK_MODE)
getString(R.string.set_key_play_in_parent_with), mode?.intCode ?: Int.MIN_VALUE)
remove(OLD_KEY_DETAIL_MUSIC_PLAYBACK_MODE)
apply()
}
}
@@ -216,8 +193,7 @@ class PlaybackSettingsImpl @Inject constructor(@ApplicationContext context: Cont
}
private companion object {
const val OLD_KEY_ALT_NOTIF_ACTION = "KEY_ALT_NOTIF_ACTION"
const val OLD_KEY_LIB_PLAYBACK_MODE = "KEY_SONG_PLAY_MODE2"
const val OLD_KEY_DETAIL_PLAYBACK_MODE = "auxio_detail_song_play_mode"
const val OLD_KEY_LIB_MUSIC_PLAYBACK_MODE = "auxio_library_playback_mode"
const val OLD_KEY_DETAIL_MUSIC_PLAYBACK_MODE = "auxio_detail_playback_mode"
}
}
@@ -30,7 +30,6 @@ import kotlinx.coroutines.launch
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.MusicMode
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.MusicRepository
import org.oxycblt.auxio.music.MusicSettings
@@ -180,10 +179,6 @@ constructor(
// --- PLAYING FUNCTIONS ---
fun play(song: Song, playbackMode: MusicMode) {
play(song, PlaySong.fromPlaybackModeTemporary(playbackMode))
}
fun play(song: Song, with: PlaySong) {
logD("Playing $song with $with")
playWithImpl(song, with, isImplicitlyShuffled())
@@ -203,10 +198,6 @@ constructor(
playFromAllImpl(null, true)
}
fun playFromAlbum(song: Song) {
playFromAlbumImpl(song, isImplicitlyShuffled())
}
/**
* Play a [Song] from one of it's [Artist]s.
*
@@ -229,34 +220,20 @@ constructor(
playFromGenreImpl(song, genre, isImplicitlyShuffled())
}
/**
* Play a [Song] from one of it's [Playlist]s.
*
* @param song The [Song] to play.
* @param playlist The [Playlist] to play from. Must be linked to the [Song].
*/
fun playFromPlaylist(song: Song, playlist: Playlist) {
playFromPlaylistImpl(song, playlist, isImplicitlyShuffled())
}
private fun isImplicitlyShuffled() =
playbackManager.queue.isShuffled && playbackSettings.keepShuffle
private fun playWithImpl(song: Song, with: PlaySong, shuffled: Boolean) {
when (with) {
is PlaySong.ByItself -> playItselfImpl(song, shuffled)
is PlaySong.FromAll -> playFromAllImpl(song, shuffled)
is PlaySong.FromAlbum -> playFromAlbumImpl(song, shuffled)
is PlaySong.FromArtist -> playFromArtistImpl(song, with.which, shuffled)
is PlaySong.FromGenre -> playFromGenreImpl(song, with.which, shuffled)
is PlaySong.FromPlaylist -> playFromPlaylistImpl(song, with.which, shuffled)
is PlaySong.ByItself -> playItselfImpl(song, shuffled)
}
}
private fun playItselfImpl(song: Song, shuffled: Boolean) {
playImpl(song, listOf(song), shuffled)
}
private fun playFromAllImpl(song: Song?, shuffled: Boolean) {
playImpl(song, null, shuffled)
}
@@ -296,6 +273,10 @@ constructor(
playImpl(song, playlist, shuffled)
}
private fun playItselfImpl(song: Song, shuffled: Boolean) {
playImpl(song, listOf(song), shuffled)
}
private fun startPlaybackDecision(decision: PlaybackDecision) {
val existing = _playbackDecision.flow.value
if (existing != null) {
@@ -174,7 +174,7 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
override fun onRealClick(item: Music) {
when (item) {
is Song -> playbackModel.play(item, searchModel.playbackMode)
is Song -> playbackModel.play(item, searchModel.playWith)
is Album -> detailModel.showAlbum(item)
is Artist -> detailModel.showArtist(item)
is Genre -> detailModel.showGenre(item)
@@ -38,6 +38,7 @@ import org.oxycblt.auxio.music.MusicRepository
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.music.device.DeviceLibrary
import org.oxycblt.auxio.music.user.UserLibrary
import org.oxycblt.auxio.playback.PlaySong
import org.oxycblt.auxio.playback.PlaybackSettings
import org.oxycblt.auxio.util.logD
@@ -63,9 +64,9 @@ constructor(
val searchResults: StateFlow<List<Item>>
get() = _searchResults
/** The [MusicMode] to use when playing a [Song] from the UI. */
val playbackMode: MusicMode
get() = playbackSettings.inListPlaybackMode
/** The [PlaySong] instructions to use when playing a [Song]. */
val playWith
get() = playbackSettings.playInListWith
init {
musicRepository.addUpdateListener(this)