music: re-add picker reloading

Fix a regression where the music picker would not reload if the library
changes.
This commit is contained in:
Alexander Capehart 2022-11-19 17:11:31 -07:00
parent fd61f5bb9f
commit 2242c413d8
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
5 changed files with 20 additions and 10 deletions

View file

@ -116,6 +116,10 @@ class DetailViewModel(application: Application) :
currentGenre.value?.let(::refreshGenreData)
}
init {
musicStore.addCallback(this)
}
fun setSongUid(uid: Music.UID) {
if (_currentSong.value?.run { song.uid } == uid) return
val library = unlikelyToBeNull(musicStore.library)
@ -153,10 +157,6 @@ class DetailViewModel(application: Application) :
refreshGenreData(genre)
}
init {
musicStore.addCallback(this)
}
private fun generateDetailSong(song: Song) {
currentSongJob?.cancel()
_currentSong.value = DetailSong(song, null)

View file

@ -107,9 +107,6 @@ class MusicStore private constructor() {
/** Sanitize an old item to find the corresponding item in a new library. */
fun sanitize(song: Song) = find<Song>(song.uid)
/** Sanitize an old item to find the corresponding item in a new library. */
fun sanitize(songs: List<Song>) = songs.mapNotNull { sanitize(it) }
/** Sanitize an old item to find the corresponding item in a new library. */
fun sanitize(album: Album) = find<Album>(album.uid)

View file

@ -44,7 +44,7 @@ abstract class ArtistPickerDialog :
override fun onBindingCreated(binding: DialogMusicPickerBinding, savedInstanceState: Bundle?) {
binding.pickerRecycler.adapter = artistAdapter
collectImmediately(pickerModel.currentArtists) { artists ->
if (artists != null) {
if (!artists.isNullOrEmpty()) {
artistAdapter.submitList(artists)
} else {
findNavController().navigateUp()

View file

@ -9,7 +9,7 @@ import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.util.unlikelyToBeNull
class MusicPickerViewModel : ViewModel() {
class MusicPickerViewModel : ViewModel(), MusicStore.Callback {
private val musicStore = MusicStore.getInstance()
private val _currentSong = MutableStateFlow<Song?>(null)
@ -28,4 +28,17 @@ class MusicPickerViewModel : ViewModel() {
val library = unlikelyToBeNull(musicStore.library)
_currentArtists.value = uids.mapNotNull { library.find<Artist>(it) }.ifEmpty { null }
}
override fun onLibraryChanged(library: MusicStore.Library?) {
if (library != null) {
val song = _currentSong.value
val artists = _currentArtists.value
if (song != null) {
_currentSong.value = library.sanitize(song)
_currentArtists.value = _currentSong.value?.artists
} else if (artists != null){
_currentArtists.value = artists.mapNotNull { library.sanitize(it) }
}
}
}
}

View file

@ -456,7 +456,7 @@ class PlaybackStateManager private constructor() {
}
}
_queue = newLibrary.sanitize(_queue).toMutableList()
_queue = _queue.mapNotNullTo(mutableListOf()) { newLibrary.sanitize(it) }
while (song?.uid != oldSongUid && index > -1) {
index--