From d7aae9270e1d2bc2cd188ec6889159641ea25525 Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Sat, 23 May 2020 20:39:07 +0530 Subject: [PATCH] Kotlin refactoring --- .../retromusic/activities/MainActivity.kt | 6 + .../retromusic/fragments/LibraryViewModel.kt | 136 ++++++++++++++++++ .../fragments/albums/AlbumViewModel.kt | 27 ---- .../fragments/albums/AlbumsFragment.kt | 24 ++-- .../fragments/artists/ArtistViewModel.kt | 27 ---- .../fragments/artists/ArtistsFragment.kt | 28 ++-- .../AbsLibraryPagerRecyclerViewFragment.kt | 5 + .../fragments/genres/GenreViewModel.kt | 27 ---- .../fragments/genres/GenresFragment.kt | 23 ++- .../fragments/home/BannerHomeFragment.kt | 15 +- .../fragments/home/HomeViewModel.kt | 36 ----- .../fragments/playlists/PlaylistViewModel.kt | 27 ---- .../fragments/playlists/PlaylistsFragment.kt | 23 ++- .../fragments/songs/SongsFragment.kt | 50 ++----- .../fragments/songs/SongsViewModel.kt | 27 ---- app/src/main/res/layout/item_artist.xml | 23 +-- app/src/main/res/layout/item_grid_circle.xml | 4 +- 17 files changed, 211 insertions(+), 297 deletions(-) create mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt delete mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumViewModel.kt delete mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistViewModel.kt delete mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenreViewModel.kt delete mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeViewModel.kt delete mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistViewModel.kt delete mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsViewModel.kt diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt index 63f367e3..5c911142 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt @@ -13,12 +13,14 @@ import android.view.View import androidx.core.app.ActivityCompat import androidx.fragment.app.Fragment import androidx.fragment.app.commit +import androidx.lifecycle.ViewModelProvider import code.name.monkey.appthemehelper.ThemeStore.Companion.accentColor import code.name.monkey.appthemehelper.util.ATHUtil.resolveColor import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper import code.name.monkey.retromusic.* import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity import code.name.monkey.retromusic.dialogs.CreatePlaylistDialog.Companion.create +import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.albums.AlbumsFragment import code.name.monkey.retromusic.fragments.artists.ArtistsFragment import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment @@ -70,6 +72,7 @@ class MainActivity : AbsSlidingMusicPanelActivity(), const val APP_UPDATE_REQUEST_CODE = 9002 } + lateinit var libraryViewModel: LibraryViewModel private lateinit var cab: MaterialCab private val intentFilter = IntentFilter(Intent.ACTION_SCREEN_OFF) private lateinit var currentFragment: MainActivityFragmentCallbacks @@ -118,6 +121,9 @@ class MainActivity : AbsSlidingMusicPanelActivity(), hideStatusBar() setBottomBarVisibility(View.VISIBLE) + libraryViewModel = ViewModelProvider(this).get(LibraryViewModel::class.java) + addMusicServiceEventListener(libraryViewModel) + if (savedInstanceState == null) { selectedFragment(PreferenceUtilKT.lastPage) } else { 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 new file mode 100644 index 00000000..754182fa --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt @@ -0,0 +1,136 @@ +package code.name.monkey.retromusic.fragments + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope +import code.name.monkey.retromusic.Result.Error +import code.name.monkey.retromusic.Result.Success +import code.name.monkey.retromusic.fragments.ReloadType.* +import code.name.monkey.retromusic.interfaces.MusicServiceEventListener +import code.name.monkey.retromusic.model.* +import code.name.monkey.retromusic.providers.RepositoryImpl +import code.name.monkey.retromusic.providers.interfaces.Repository +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.async +import kotlinx.coroutines.launch + +class LibraryViewModel(application: Application) : + AndroidViewModel(application), MusicServiceEventListener { + + private val _repository: Repository = RepositoryImpl(application.applicationContext) + private val _albums = MutableLiveData>() + private val _songs = MutableLiveData>() + private val _artists = MutableLiveData>() + private val _playlist = MutableLiveData>() + private val _genre = MutableLiveData>() + private val _homeSections = MutableLiveData>() + + fun homeSections(): LiveData> = _homeSections + fun allAlbums(): LiveData> = _albums + fun allSongs(): LiveData> = _songs + fun allArtists(): LiveData> = _artists + fun allPlaylisits(): LiveData> = _playlist + fun allGenres(): LiveData> = _genre + + init { + viewModelScope.launch { + loadLibraryContent() + loadHomeSections() + } + } + + private fun loadLibraryContent() = viewModelScope.launch { + _songs.value = loadSongs.await() + _albums.value = loadAlbums.await() + _artists.value = loadArtists.await() + _playlist.value = loadPlaylists.await() + _genre.value = loadGenres.await() + } + + private fun loadHomeSections() = viewModelScope.launch { + val list = mutableListOf() + val result = listOf( + _repository.topArtists(), + _repository.topAlbums(), + _repository.recentArtists(), + _repository.recentAlbums(), + _repository.favoritePlaylist() + ) + for (r in result) { + if (r is Success) { + list.add(r.data) + } + } + _homeSections.value = list + } + + private val loadSongs: Deferred> + get() = viewModelScope.async(IO) { + when (val result = _repository.allSongs()) { + is Success -> result.data + is Error -> arrayListOf() + } + } + + private val loadAlbums: Deferred> + get() = viewModelScope.async(IO) { + when (val result = _repository.allAlbums()) { + is Success -> result.data + is Error -> arrayListOf() + } + } + private val loadArtists: Deferred> + get() = viewModelScope.async(IO) { + when (val result = _repository.allArtists()) { + is Success -> result.data + is Error -> arrayListOf() + } + } + + private val loadPlaylists: Deferred> + get() = viewModelScope.async(IO) { + when (val result = _repository.allPlaylists()) { + is Success -> result.data + is Error -> arrayListOf() + } + } + + private val loadGenres: Deferred> + get() = viewModelScope.async(IO) { + when (val result = _repository.allGenres()) { + is Success -> result.data + is Error -> arrayListOf() + } + } + + fun forceReload(reloadType: ReloadType) = viewModelScope.launch { + when (reloadType) { + Songs -> _songs.value = loadSongs.await() + Albums -> _albums.value = loadAlbums.await() + Artists -> _artists.value = loadArtists.await() + HomeSections -> _songs.value = loadSongs.await() + } + } + + override fun onMediaStoreChanged() { + loadLibraryContent() + } + + override fun onServiceConnected() {} + override fun onServiceDisconnected() {} + override fun onQueueChanged() {} + override fun onPlayingMetaChanged() {} + override fun onPlayStateChanged() {} + override fun onRepeatModeChanged() {} + override fun onShuffleModeChanged() {} +} + +enum class ReloadType { + Songs, + Albums, + Artists, + HomeSections +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumViewModel.kt deleted file mode 100644 index 40771a08..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumViewModel.kt +++ /dev/null @@ -1,27 +0,0 @@ -package code.name.monkey.retromusic.fragments.albums - -import android.app.Application -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.viewModelScope -import code.name.monkey.retromusic.Result -import code.name.monkey.retromusic.model.Album -import code.name.monkey.retromusic.providers.RepositoryImpl -import kotlinx.coroutines.launch - -class AlbumViewModel(application: Application) : AndroidViewModel(application) { - var albums = MutableLiveData>() - - init { - getAlbums() - } - - fun getAlbums() = viewModelScope.launch { - val result = RepositoryImpl(getApplication()).allAlbums() - if (result is Result.Success) { - albums.value = result.data - } else { - albums.value = listOf() - } - } -} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt index 45490d8d..86028dfd 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt @@ -3,10 +3,10 @@ package code.name.monkey.retromusic.fragments.albums import android.os.Bundle import android.view.View import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.GridLayoutManager import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.album.AlbumAdapter +import code.name.monkey.retromusic.fragments.ReloadType import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks import code.name.monkey.retromusic.util.PreferenceUtilKT @@ -15,17 +15,16 @@ class AlbumsFragment : AbsLibraryPagerRecyclerViewCustomGridSizeFragment(), MainActivityFragmentCallbacks { - private lateinit var albumViewModel: AlbumViewModel override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - albumViewModel = ViewModelProvider(this).get(AlbumViewModel::class.java) - albumViewModel.albums.observe(viewLifecycleOwner, Observer { albums -> - if (albums.isNotEmpty()) - adapter?.swapDataSet(albums) - else - adapter?.swapDataSet(listOf()) - }) + mainActivity.libraryViewModel.allAlbums() + .observe(viewLifecycleOwner, Observer { albums -> + if (albums.isNotEmpty()) + adapter?.swapDataSet(albums) + else + adapter?.swapDataSet(listOf()) + }) } override val emptyMessage: Int @@ -74,15 +73,10 @@ class AlbumsFragment : PreferenceUtilKT.albumGridSizeLand = gridColumns } - override fun onMediaStoreChanged() { - albumViewModel.getAlbums() - } - override fun setSortOrder(sortOrder: String) { - albumViewModel.getAlbums() + mainActivity.libraryViewModel.forceReload(ReloadType.Albums) } - override fun loadLayoutRes(): Int { return PreferenceUtilKT.albumGridStyle } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistViewModel.kt deleted file mode 100644 index 9a5d7e40..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistViewModel.kt +++ /dev/null @@ -1,27 +0,0 @@ -package code.name.monkey.retromusic.fragments.artists - -import android.app.Application -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.viewModelScope -import code.name.monkey.retromusic.Result -import code.name.monkey.retromusic.model.Artist -import code.name.monkey.retromusic.providers.RepositoryImpl -import kotlinx.coroutines.launch - -class ArtistViewModel(application: Application) : AndroidViewModel(application) { - var artists = MutableLiveData>() - - init { - loadArtists() - } - - fun loadArtists() = viewModelScope.launch { - val result = RepositoryImpl(getApplication()).allArtists() - if (result is Result.Success) { - artists.value = result.data - } else { - artists.value = listOf() - } - } -} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt index ecffd3ec..4db95193 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt @@ -3,10 +3,10 @@ package code.name.monkey.retromusic.fragments.artists import android.os.Bundle import android.view.View import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.GridLayoutManager import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.artist.ArtistAdapter +import code.name.monkey.retromusic.fragments.ReloadType import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks import code.name.monkey.retromusic.util.PreferenceUtilKT @@ -14,20 +14,16 @@ import code.name.monkey.retromusic.util.PreferenceUtilKT class ArtistsFragment : AbsLibraryPagerRecyclerViewCustomGridSizeFragment(), MainActivityFragmentCallbacks { - - lateinit var artistViewModel: ArtistViewModel - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - artistViewModel = ViewModelProvider(this).get(ArtistViewModel::class.java) - artistViewModel.artists.observe(viewLifecycleOwner, Observer { artists -> - if (artists.isNotEmpty()) { - adapter?.swapDataSet(artists) - } else { - adapter?.swapDataSet(listOf()) - } - }) + mainActivity.libraryViewModel.allArtists().observe( + viewLifecycleOwner, Observer { artists -> + if (artists.isNotEmpty()) { + adapter?.swapDataSet(artists) + } else { + adapter?.swapDataSet(listOf()) + } + }) } override fun handleBackPress(): Boolean { @@ -37,12 +33,8 @@ class ArtistsFragment : override val emptyMessage: Int get() = R.string.no_artists - override fun onMediaStoreChanged() { - artistViewModel.loadArtists() - } - override fun setSortOrder(sortOrder: String) { - artistViewModel.loadArtists() + mainActivity.libraryViewModel.forceReload(ReloadType.Artists) } override fun createLayoutManager(): GridLayoutManager { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsLibraryPagerRecyclerViewFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsLibraryPagerRecyclerViewFragment.kt index 890c6898..2060cef5 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsLibraryPagerRecyclerViewFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsLibraryPagerRecyclerViewFragment.kt @@ -6,8 +6,10 @@ import android.view.View import android.view.ViewGroup import androidx.annotation.NonNull import androidx.annotation.StringRes +import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.RecyclerView import code.name.monkey.retromusic.R +import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.util.DensityUtil import code.name.monkey.retromusic.util.ThemedFastScroller.create @@ -20,6 +22,7 @@ import me.zhanghai.android.fastscroll.FastScrollerBuilder abstract class AbsLibraryPagerRecyclerViewFragment, LM : RecyclerView.LayoutManager> : AbsLibraryPagerFragment(), AppBarLayout.OnOffsetChangedListener { + protected var adapter: A? = null protected var layoutManager: LM? = null @@ -90,6 +93,8 @@ abstract class AbsLibraryPagerRecyclerViewFragment, } } + + private fun initLayoutManager() { layoutManager = createLayoutManager() } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenreViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenreViewModel.kt deleted file mode 100644 index 4aac159a..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenreViewModel.kt +++ /dev/null @@ -1,27 +0,0 @@ -package code.name.monkey.retromusic.fragments.genres - -import android.app.Application -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.viewModelScope -import code.name.monkey.retromusic.Result.Success -import code.name.monkey.retromusic.model.Genre -import code.name.monkey.retromusic.providers.RepositoryImpl -import kotlinx.coroutines.launch - -class GenreViewModel(application: Application) : AndroidViewModel(application) { - var genres = MutableLiveData>() - - init { - loadGenre() - } - - fun loadGenre() = viewModelScope.launch { - val result = RepositoryImpl(getApplication()).allGenres() - if (result is Success) { - genres.value = result.data - } else { - genres.value = listOf() - } - } -} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt index 469c2916..6c10551c 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt @@ -17,7 +17,6 @@ package code.name.monkey.retromusic.fragments.genres import android.os.Bundle import android.view.View import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import code.name.monkey.retromusic.App import code.name.monkey.retromusic.R @@ -28,8 +27,6 @@ import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks class GenresFragment : AbsLibraryPagerRecyclerViewFragment(), MainActivityFragmentCallbacks { - private lateinit var genreViewModel: GenreViewModel - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) App.musicComponent.inject(this) @@ -37,14 +34,14 @@ class GenresFragment : AbsLibraryPagerRecyclerViewFragment - if (genres.isNotEmpty()) { - adapter?.swapDataSet(genres) - } else { - adapter?.swapDataSet(listOf()) - } - }) + mainActivity.libraryViewModel.allGenres().observe( + viewLifecycleOwner, Observer { genres -> + if (genres.isNotEmpty()) { + adapter?.swapDataSet(genres) + } else { + adapter?.swapDataSet(listOf()) + } + }) } override fun handleBackPress(): Boolean { @@ -63,10 +60,6 @@ class GenresFragment : AbsLibraryPagerRecyclerViewFragment - homeAdapter.swapData(sections) - }) + + mainActivity.libraryViewModel.homeSections() + .observe(viewLifecycleOwner, Observer { sections -> + homeAdapter.swapData(sections) + }) + loadProfile() } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeViewModel.kt deleted file mode 100644 index aa192985..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeViewModel.kt +++ /dev/null @@ -1,36 +0,0 @@ -package code.name.monkey.retromusic.fragments.home - -import android.app.Application -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.viewModelScope -import code.name.monkey.retromusic.Result -import code.name.monkey.retromusic.model.Home -import code.name.monkey.retromusic.providers.RepositoryImpl -import kotlinx.coroutines.launch - -class HomeViewModel(application: Application) : AndroidViewModel(application) { - var sections = MutableLiveData>() - var repository: RepositoryImpl = RepositoryImpl(getApplication()) - - init { - loadHome() - } - - private fun loadHome() = viewModelScope.launch { - val list = mutableListOf() - val result = listOf( - repository.topArtists(), - repository.topAlbums(), - repository.recentArtists(), - repository.recentAlbums(), - repository.favoritePlaylist() - ) - for (r in result) { - if (r is Result.Success) { - list.add(r.data) - } - } - sections.value = list - } -} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistViewModel.kt deleted file mode 100644 index 71d8df24..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistViewModel.kt +++ /dev/null @@ -1,27 +0,0 @@ -package code.name.monkey.retromusic.fragments.playlists - -import android.app.Application -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.viewModelScope -import code.name.monkey.retromusic.Result -import code.name.monkey.retromusic.model.Playlist -import code.name.monkey.retromusic.providers.RepositoryImpl -import kotlinx.coroutines.launch - -class PlaylistViewModel(application: Application) : AndroidViewModel(application) { - var playlists = MutableLiveData>() - - init { - loadPlaylist() - } - - fun loadPlaylist() = viewModelScope.launch { - val result = RepositoryImpl(getApplication()).allPlaylists() - if (result is Result.Success) { - playlists.value = result.data - } else { - playlists.value = listOf() - } - } -} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt index 98443c71..962e3200 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt @@ -5,7 +5,6 @@ import android.view.Menu import android.view.MenuInflater import android.view.View import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.GridLayoutManager import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.playlist.PlaylistAdapter @@ -16,18 +15,17 @@ class PlaylistsFragment : AbsLibraryPagerRecyclerViewFragment(), MainActivityFragmentCallbacks { - lateinit var playlistViewModel: PlaylistViewModel override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - playlistViewModel = ViewModelProvider(this).get(PlaylistViewModel::class.java) - playlistViewModel.playlists.observe(viewLifecycleOwner, Observer { playlists -> - if (playlists.isNotEmpty()) { - adapter?.swapDataSet(playlists) - } else { - adapter?.swapDataSet(listOf()) - } - }) + mainActivity.libraryViewModel.allPlaylisits() + .observe(viewLifecycleOwner, Observer { playlists -> + if (playlists.isNotEmpty()) { + adapter?.swapDataSet(playlists) + } else { + adapter?.swapDataSet(listOf()) + } + }) } override fun handleBackPress(): Boolean { @@ -50,11 +48,6 @@ class PlaylistsFragment : ) } - override fun onMediaStoreChanged() { - super.onMediaStoreChanged() - playlistViewModel.loadPlaylist() - } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { super.onCreateOptionsMenu(menu, inflater) menu.apply { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt index 8c7201fc..add25fc4 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt @@ -4,39 +4,35 @@ import android.os.Bundle import android.view.View import androidx.annotation.LayoutRes import androidx.recyclerview.widget.GridLayoutManager -import code.name.monkey.retromusic.App import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.song.ShuffleButtonSongAdapter import code.name.monkey.retromusic.adapter.song.SongAdapter +import code.name.monkey.retromusic.fragments.ReloadType import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks import code.name.monkey.retromusic.model.Song -import code.name.monkey.retromusic.mvp.presenter.SongPresenter import code.name.monkey.retromusic.mvp.presenter.SongView import code.name.monkey.retromusic.util.PreferenceUtilKT import java.util.* -import javax.inject.Inject class SongsFragment : AbsLibraryPagerRecyclerViewCustomGridSizeFragment(), SongView, MainActivityFragmentCallbacks { - - @Inject - lateinit var songPresenter: SongPresenter + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + mainActivity.libraryViewModel.allSongs() + .observe(viewLifecycleOwner, androidx.lifecycle.Observer { + if (it.isNotEmpty()) { + adapter?.swapDataSet(it) + } else { + adapter?.swapDataSet(listOf()) + } + }) + } override val emptyMessage: Int get() = R.string.no_songs - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - App.musicComponent.inject(this) - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - songPresenter.attachView(this) - } - override fun createLayoutManager(): GridLayoutManager { return GridLayoutManager(requireActivity(), getGridSize()).apply { spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { @@ -65,10 +61,6 @@ class SongsFragment : adapter?.swapDataSet(songs) } - override fun onMediaStoreChanged() { - songPresenter.loadSongs() - } - override fun loadGridSize(): Int { return PreferenceUtilKT.songGridSize } @@ -89,17 +81,6 @@ class SongsFragment : adapter?.notifyDataSetChanged() } - override fun onResume() { - super.onResume() - if (adapter?.dataSet.isNullOrEmpty()) - songPresenter.loadSongs() - } - - override fun onDestroyView() { - super.onDestroyView() - songPresenter.detachView() - } - override fun showEmptyView() { adapter?.swapDataSet(ArrayList()) } @@ -122,7 +103,7 @@ class SongsFragment : } override fun setSortOrder(sortOrder: String) { - songPresenter.loadSongs() + mainActivity.libraryViewModel.forceReload(ReloadType.Songs) } companion object { @@ -131,10 +112,7 @@ class SongsFragment : @JvmStatic fun newInstance(): SongsFragment { - val args = Bundle() - val fragment = SongsFragment() - fragment.arguments = args - return fragment + return SongsFragment() } } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsViewModel.kt deleted file mode 100644 index 3a15ff2f..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsViewModel.kt +++ /dev/null @@ -1,27 +0,0 @@ -package code.name.monkey.retromusic.fragments.songs - -import android.app.Application -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.viewModelScope -import code.name.monkey.retromusic.Result.Success -import code.name.monkey.retromusic.model.Song -import code.name.monkey.retromusic.providers.RepositoryImpl -import kotlinx.coroutines.launch - -class SongsViewModel(application: Application) : AndroidViewModel(application) { - var songs = MutableLiveData>() - - init { - loadSongs() - } - - fun loadSongs() = viewModelScope.launch { - val result = RepositoryImpl(getApplication()).allSongs() - if (result is Success) { - songs.value = result.data - } else { - songs.value = listOf() - } - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/item_artist.xml b/app/src/main/res/layout/item_artist.xml index 7a2e4330..fadcff2f 100644 --- a/app/src/main/res/layout/item_artist.xml +++ b/app/src/main/res/layout/item_artist.xml @@ -8,25 +8,16 @@ android:orientation="vertical" android:padding="4dp"> - - - - - + app:layout_constraintTop_toTopOf="parent" + app:retroCornerSize="56dp" + tools:src="@tools:sample/avatars" /> \ No newline at end of file diff --git a/app/src/main/res/layout/item_grid_circle.xml b/app/src/main/res/layout/item_grid_circle.xml index c3574cc3..5308f9a9 100644 --- a/app/src/main/res/layout/item_grid_circle.xml +++ b/app/src/main/res/layout/item_grid_circle.xml @@ -9,7 +9,6 @@ android:padding="4dp" tools:ignore="MissingPrefix"> - -