From 3c7328f9c69942140adf61ea04c3e2b504e6fe38 Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Sun, 7 Jun 2020 23:51:49 +0530 Subject: [PATCH] Fix not reloading when deleting files --- .../activities/albums/AlbumDetailsActivity.kt | 5 +++++ .../albums/AlbumDetailsViewModel.kt | 19 ++++++++++++++++--- .../artists/ArtistDetailActivity.kt | 6 ++++++ .../artists/ArtistDetailsViewModel.kt | 15 ++++++++++++++- .../retromusic/fragments/LibraryViewModel.kt | 2 +- .../retromusic/loaders/PlaylistLoader.kt | 4 ++-- .../main/res/layout/activity_user_info.xml | 4 +--- 7 files changed, 45 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt index 4095bb7c..bd668c01 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt @@ -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" diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt index 29dd6c32..112d7e04 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt @@ -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() private val _artist = MutableLiveData() @@ -28,10 +29,10 @@ class AlbumDetailsViewModel( fun getAlbumInfo(): LiveData = _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() {} } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailActivity.kt index 7fb6ed1b..7cf4e79b 100755 --- a/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailActivity.kt @@ -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 diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModel.kt index fc535b99..502bafe8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModel.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModel.kt @@ -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 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() {} } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt index 57604d50..cf259e87 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt @@ -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 { diff --git a/app/src/main/java/code/name/monkey/retromusic/loaders/PlaylistLoader.kt b/app/src/main/java/code/name/monkey/retromusic/loaders/PlaylistLoader.kt index 12eff6df..71f49017 100644 --- a/app/src/main/java/code/name/monkey/retromusic/loaders/PlaylistLoader.kt +++ b/app/src/main/java/code/name/monkey/retromusic/loaders/PlaylistLoader.kt @@ -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 diff --git a/app/src/main/res/layout/activity_user_info.xml b/app/src/main/res/layout/activity_user_info.xml index 4b852f52..3239486b 100644 --- a/app/src/main/res/layout/activity_user_info.xml +++ b/app/src/main/res/layout/activity_user_info.xml @@ -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" />