Fix not reloading when deleting files

This commit is contained in:
Hemanth S 2020-06-07 23:51:49 +05:30
parent 3184d33886
commit 3c7328f9c6
7 changed files with 45 additions and 10 deletions

View file

@ -104,6 +104,7 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
ActivityCompat.postponeEnterTransition(this)
val viewModelFactory = AlbumDetailsViewModelFactory(application, albumId)
viewModel = ViewModelProvider(this, viewModelFactory).get(AlbumDetailsViewModel::class.java)
addMusicServiceEventListener(viewModel)
viewModel.getAlbum().observe(this, androidx.lifecycle.Observer {
ActivityCompat.startPostponedEnterTransition(this@AlbumDetailsActivity)
album(it)
@ -388,6 +389,10 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
}
}
override fun onDestroy() {
super.onDestroy()
removeMusicServiceEventListener(viewModel)
}
companion object {
const val EXTRA_ALBUM_ID = "extra_album_id"

View file

@ -5,6 +5,7 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.providers.RepositoryImpl
@ -17,7 +18,7 @@ import kotlinx.coroutines.launch
class AlbumDetailsViewModel(
application: Application,
private val albumId: Int
) : AndroidViewModel(application) {
) : AndroidViewModel(application), MusicServiceEventListener {
private val _repository = RepositoryImpl(application.applicationContext)
private val _album = MutableLiveData<Album>()
private val _artist = MutableLiveData<Artist>()
@ -28,10 +29,10 @@ class AlbumDetailsViewModel(
fun getAlbumInfo(): LiveData<LastFmAlbum> = _lastFmAlbum
init {
loadDetails()
loadAlbumDetails()
}
private fun loadDetails() = viewModelScope.launch {
private fun loadAlbumDetails() = viewModelScope.launch {
val album = loadAlbumAsync.await() ?: throw NullPointerException("Album couldn't found")
_album.postValue(album)
}
@ -50,4 +51,16 @@ class AlbumDetailsViewModel(
get() = viewModelScope.async(Dispatchers.IO) {
_repository.albumById(albumId)
}
override fun onMediaStoreChanged() {
loadAlbumDetails()
}
override fun onServiceConnected() {}
override fun onServiceDisconnected() {}
override fun onQueueChanged() {}
override fun onPlayingMetaChanged() {}
override fun onPlayStateChanged() {}
override fun onRepeatModeChanged() {}
override fun onShuffleModeChanged() {}
}

View file

@ -98,6 +98,7 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
val viewModelFactory = ArtistDetailsViewModelFactory(application, artistId)
viewModel =
ViewModelProvider(this, viewModelFactory).get(ArtistDetailsViewModel::class.java)
addMusicServiceEventListener(viewModel)
viewModel.getArtist().observe(this, androidx.lifecycle.Observer {
ActivityCompat.startPostponedEnterTransition(this@ArtistDetailActivity)
artist(it)
@ -320,6 +321,11 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
}
}
override fun onDestroy() {
super.onDestroy()
removeMusicServiceEventListener(viewModel)
}
companion object {
const val EXTRA_ARTIST_ID = "extra_artist_id"
const val REQUEST_CODE_SELECT_IMAGE = 9003

View file

@ -5,6 +5,7 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.providers.RepositoryImpl
import code.name.monkey.retromusic.rest.model.LastFmArtist
@ -16,7 +17,7 @@ import kotlinx.coroutines.launch
class ArtistDetailsViewModel(
application: Application,
private val artistId: Int
) : AndroidViewModel(application) {
) : AndroidViewModel(application), MusicServiceEventListener {
private val loadArtistDetailsAsync: Deferred<Artist?>
get() = viewModelScope.async(Dispatchers.IO) {
@ -43,4 +44,16 @@ class ArtistDetailsViewModel(
val info = _repository.artistInfo(name, lang, cache)
_lastFmArtist.postValue(info)
}
override fun onMediaStoreChanged() {
loadArtistDetails()
}
override fun onServiceConnected() {}
override fun onServiceDisconnected() {}
override fun onQueueChanged() {}
override fun onPlayingMetaChanged() {}
override fun onPlayStateChanged() {}
override fun onRepeatModeChanged() {}
override fun onShuffleModeChanged() {}
}

View file

@ -36,7 +36,6 @@ class LibraryViewModel(application: Application) :
init {
viewModelScope.launch {
loadLibraryContent()
loadHomeSections()
}
}
@ -46,6 +45,7 @@ class LibraryViewModel(application: Application) :
_artists.value = loadArtists.await()
_playlist.value = loadPlaylists.await()
_genre.value = loadGenres.await()
loadHomeSections()
}
private fun loadHomeSections() = viewModelScope.launch {

View file

@ -106,8 +106,8 @@ object PlaylistLoader {
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
arrayOf(
BaseColumns._ID, /* 0 */
PlaylistsColumns.NAME
), /* 1 */
PlaylistsColumns.NAME /* 1 */
),
selection,
values,
MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER

View file

@ -88,9 +88,7 @@
android:layout_gravity="center"
android:scaleType="centerCrop"
app:retroCornerSize="36dp"
app:srcCompat="@drawable/ic_person_flat"
app:strokeColor="?attr/colorSurface"
app:strokeWidth="2dp" />
app:srcCompat="@drawable/ic_person_flat" />
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"