From f7d2d32982a8ead2f62d706a163dcf4fc7416d19 Mon Sep 17 00:00:00 2001 From: h4h13 Date: Sat, 25 Apr 2020 21:29:52 +0530 Subject: [PATCH] Added viewmodel --- app/build.gradle | 6 ++ .../retromusic/activities/MainActivity.java | 14 +-- .../retromusic/dagger/MusicComponent.kt | 7 +- .../fragments/albums/AlbumViewModel.kt | 25 ++++++ .../AlbumsFragment.kt | 64 ++++---------- .../fragments/artists/ArtistViewModel.kt | 25 ++++++ .../ArtistsFragment.kt | 86 +++++++------------ .../fragments/genres/GenreViewModel.kt | 25 ++++++ .../GenresFragment.kt | 63 ++++++-------- .../BannerHomeFragment.kt | 42 +++------ .../fragments/home/HomeViewModel.kt | 36 ++++++++ .../fragments/playlists/PlaylistViewModel.kt | 25 ++++++ .../PlaylistsFragment.kt | 65 +++++--------- .../PlayingQueueFragment.kt | 2 +- .../{mainactivity => songs}/SongsFragment.kt | 47 ++++------ .../fragments/songs/SongsViewModel.kt | 25 ++++++ 16 files changed, 303 insertions(+), 254 deletions(-) create mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumViewModel.kt rename app/src/main/java/code/name/monkey/retromusic/fragments/{mainactivity => albums}/AlbumsFragment.kt (69%) create mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistViewModel.kt rename app/src/main/java/code/name/monkey/retromusic/fragments/{mainactivity => artists}/ArtistsFragment.kt (69%) create mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenreViewModel.kt rename app/src/main/java/code/name/monkey/retromusic/fragments/{mainactivity => genres}/GenresFragment.kt (71%) rename app/src/main/java/code/name/monkey/retromusic/fragments/{mainactivity => home}/BannerHomeFragment.kt (88%) create mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeViewModel.kt create mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistViewModel.kt rename app/src/main/java/code/name/monkey/retromusic/fragments/{mainactivity => playlists}/PlaylistsFragment.kt (57%) rename app/src/main/java/code/name/monkey/retromusic/fragments/{mainactivity => queue}/PlayingQueueFragment.kt (98%) rename app/src/main/java/code/name/monkey/retromusic/fragments/{mainactivity => songs}/SongsFragment.kt (79%) create mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsViewModel.kt diff --git a/app/build.gradle b/app/build.gradle index c3022ad0..161deb51 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -189,4 +189,10 @@ dependencies { implementation "androidx.room:room-runtime:$room_version" kapt "androidx.room:room-compiler:$room_version" implementation "androidx.room:room-ktx:$room_version" + + def lifecycle_version = "2.2.0" + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" + kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.java b/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.java index ea69fb81..ad33cfa8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.java +++ b/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.java @@ -59,14 +59,14 @@ import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity; import code.name.monkey.retromusic.dialogs.CreatePlaylistDialog; import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment; -import code.name.monkey.retromusic.fragments.mainactivity.AlbumsFragment; -import code.name.monkey.retromusic.fragments.mainactivity.ArtistsFragment; -import code.name.monkey.retromusic.fragments.mainactivity.BannerHomeFragment; +import code.name.monkey.retromusic.fragments.albums.AlbumsFragment; +import code.name.monkey.retromusic.fragments.artists.ArtistsFragment; +import code.name.monkey.retromusic.fragments.home.BannerHomeFragment; import code.name.monkey.retromusic.fragments.mainactivity.FoldersFragment; -import code.name.monkey.retromusic.fragments.mainactivity.GenresFragment; -import code.name.monkey.retromusic.fragments.mainactivity.PlayingQueueFragment; -import code.name.monkey.retromusic.fragments.mainactivity.PlaylistsFragment; -import code.name.monkey.retromusic.fragments.mainactivity.SongsFragment; +import code.name.monkey.retromusic.fragments.genres.GenresFragment; +import code.name.monkey.retromusic.fragments.queue.PlayingQueueFragment; +import code.name.monkey.retromusic.fragments.playlists.PlaylistsFragment; +import code.name.monkey.retromusic.fragments.songs.SongsFragment; import code.name.monkey.retromusic.helper.MusicPlayerRemote; import code.name.monkey.retromusic.helper.SearchQueryHelper; import code.name.monkey.retromusic.helper.SortOrder.AlbumSortOrder; diff --git a/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt b/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt index 361bb91a..86703f4b 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt @@ -17,7 +17,12 @@ package code.name.monkey.retromusic.dagger import code.name.monkey.retromusic.activities.* import code.name.monkey.retromusic.dagger.module.AppModule import code.name.monkey.retromusic.dagger.module.PresenterModule -import code.name.monkey.retromusic.fragments.mainactivity.* +import code.name.monkey.retromusic.fragments.albums.AlbumsFragment +import code.name.monkey.retromusic.fragments.artists.ArtistsFragment +import code.name.monkey.retromusic.fragments.genres.GenresFragment +import code.name.monkey.retromusic.fragments.home.BannerHomeFragment +import code.name.monkey.retromusic.fragments.playlists.PlaylistsFragment +import code.name.monkey.retromusic.fragments.songs.SongsFragment import dagger.Component import javax.inject.Singleton 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 new file mode 100644 index 00000000..d5531d76 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumViewModel.kt @@ -0,0 +1,25 @@ +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) { + lateinit var albums: MutableLiveData> + + init { + getAlbums() + } + + fun getAlbums() = viewModelScope.launch { + val result = RepositoryImpl(getApplication()).allAlbums() + if (result is Result.Success) { + albums = MutableLiveData(result.data) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/AlbumsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt similarity index 69% rename from app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/AlbumsFragment.kt rename to app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt index 32c374d7..cb0a2c02 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/AlbumsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt @@ -1,50 +1,31 @@ -package code.name.monkey.retromusic.fragments.mainactivity +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.App import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.album.AlbumAdapter import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks -import code.name.monkey.retromusic.model.Album -import code.name.monkey.retromusic.mvp.presenter.AlbumsPresenter -import code.name.monkey.retromusic.mvp.presenter.AlbumsView import code.name.monkey.retromusic.util.PreferenceUtil -import javax.inject.Inject class AlbumsFragment : AbsLibraryPagerRecyclerViewCustomGridSizeFragment(), - AlbumsView, MainActivityFragmentCallbacks { + MainActivityFragmentCallbacks { - @Inject - lateinit var albumsPresenter: AlbumsPresenter - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - App.musicComponent.inject(this) - } + lateinit var albumViewModel: AlbumViewModel override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - albumsPresenter.attachView(this) - } - - override fun onResume() { - super.onResume() - if (adapter!!.dataSet.isNullOrEmpty()) { - albumsPresenter.loadAlbums() - } - } - - override fun onDestroyView() { - super.onDestroyView() - albumsPresenter.detachView() - } - - override fun albums(albums: List) { - adapter?.swapDataSet(albums) + albumViewModel = ViewModelProvider(this).get(AlbumViewModel::class.java) + albumViewModel.albums.observe(viewLifecycleOwner, Observer { albums -> + if (albums.isNotEmpty()) + adapter?.swapDataSet(albums) + else + adapter?.swapDataSet(listOf()) + }) } override val emptyMessage: Int @@ -94,15 +75,11 @@ class AlbumsFragment : } override fun onMediaStoreChanged() { - albumsPresenter.loadAlbums() + albumViewModel.getAlbums() } override fun setSortOrder(sortOrder: String) { - albumsPresenter.loadAlbums() - } - - override fun showEmptyView() { - adapter?.swapDataSet(ArrayList()) + albumViewModel.getAlbums() } override fun setLayoutRes(layoutRes: Int) { @@ -116,20 +93,17 @@ class AlbumsFragment : PreferenceUtil.getInstance(requireContext()).albumGridStyle = layoutRes } + override fun handleBackPress(): Boolean { + return false + } + companion object { @JvmField var TAG: String = AlbumsFragment::class.java.simpleName @JvmStatic fun newInstance(): AlbumsFragment { - val args = Bundle() - val fragment = AlbumsFragment() - fragment.arguments = args - return fragment + return AlbumsFragment() } } - - override fun handleBackPress(): Boolean { - return false - } } 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 new file mode 100644 index 00000000..c5991b1e --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistViewModel.kt @@ -0,0 +1,25 @@ +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) { + lateinit var artists: MutableLiveData> + + init { + loadArtists() + } + + fun loadArtists() = viewModelScope.launch { + val result = RepositoryImpl(getApplication()).allArtists() + if (result is Result.Success) { + artists = MutableLiveData(result.data) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/ArtistsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt similarity index 69% rename from app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/ArtistsFragment.kt rename to app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt index 6f4ded1c..14aa7269 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/ArtistsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt @@ -1,65 +1,48 @@ -package code.name.monkey.retromusic.fragments.mainactivity +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.App import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.artist.ArtistAdapter import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks -import code.name.monkey.retromusic.model.Artist -import code.name.monkey.retromusic.mvp.presenter.ArtistsPresenter -import code.name.monkey.retromusic.mvp.presenter.ArtistsView import code.name.monkey.retromusic.util.PreferenceUtil -import javax.inject.Inject class ArtistsFragment : AbsLibraryPagerRecyclerViewCustomGridSizeFragment(), - ArtistsView, MainActivityFragmentCallbacks { + 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()) + } + }) + } override fun handleBackPress(): Boolean { return false } - override fun artists(artists: List) { - adapter?.swapDataSet(artists) - } - - @Inject - lateinit var artistsPresenter: ArtistsPresenter - override val emptyMessage: Int get() = R.string.no_artists - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - App.musicComponent.inject(this) - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - artistsPresenter.attachView(this) - } - - override fun onResume() { - super.onResume() - if (adapter!!.dataSet.isNullOrEmpty()) { - artistsPresenter.loadArtists() - } - } - - override fun onDestroyView() { - super.onDestroyView() - artistsPresenter.detachView() - } - override fun onMediaStoreChanged() { - artistsPresenter.loadArtists() + artistViewModel.loadArtists() } override fun setSortOrder(sortOrder: String) { - artistsPresenter.loadArtists() + artistViewModel.loadArtists() } override fun createLayoutManager(): GridLayoutManager { @@ -105,23 +88,6 @@ class ArtistsFragment : PreferenceUtil.getInstance(requireContext()).artistSortOrder = sortOrder } - override fun showEmptyView() { - adapter?.swapDataSet(ArrayList()) - } - - companion object { - @JvmField - val TAG: String = ArtistsFragment::class.java.simpleName - - @JvmStatic - fun newInstance(): ArtistsFragment { - val args = Bundle() - val fragment = ArtistsFragment() - fragment.arguments = args - return fragment - } - } - override fun setLayoutRes(layoutRes: Int) { } @@ -132,4 +98,14 @@ class ArtistsFragment : override fun saveLayoutRes(layoutRes: Int) { PreferenceUtil.getInstance(requireContext()).artistGridStyle = layoutRes } + + companion object { + @JvmField + val TAG: String = ArtistsFragment::class.java.simpleName + + @JvmStatic + fun newInstance(): ArtistsFragment { + return ArtistsFragment() + } + } } \ No newline at end of file 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 new file mode 100644 index 00000000..9ee42715 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenreViewModel.kt @@ -0,0 +1,25 @@ +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) { + lateinit var genres: MutableLiveData> + + init { + loadGenre() + } + + fun loadGenre() = viewModelScope.launch { + val result = RepositoryImpl(getApplication()).allGenres() + if (result is Success) { + genres = MutableLiveData(result.data) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/GenresFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt similarity index 71% rename from app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/GenresFragment.kt rename to app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt index bf0dc49c..2541365e 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/GenresFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt @@ -12,35 +12,45 @@ * See the GNU General Public License for more details. */ -package code.name.monkey.retromusic.fragments.mainactivity +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 import code.name.monkey.retromusic.adapter.GenreAdapter import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewFragment import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks -import code.name.monkey.retromusic.model.Genre -import code.name.monkey.retromusic.mvp.presenter.GenresPresenter -import code.name.monkey.retromusic.mvp.presenter.GenresView -import javax.inject.Inject class GenresFragment : AbsLibraryPagerRecyclerViewFragment(), - GenresView, MainActivityFragmentCallbacks { + MainActivityFragmentCallbacks { + + lateinit var genreViewModel: GenreViewModel + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + App.musicComponent.inject(this) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + genreViewModel = ViewModelProvider(this).get(GenreViewModel::class.java) + genreViewModel.genres.observe(viewLifecycleOwner, Observer { genres -> + if (genres.isNotEmpty()) { + adapter?.swapDataSet(genres) + } else { + adapter?.swapDataSet(listOf()) + } + }) + } override fun handleBackPress(): Boolean { return false } - override fun genres(genres: List) { - adapter?.swapDataSet(genres) - } - - override fun showEmptyView() { - } - override fun createLayoutManager(): LinearLayoutManager { return LinearLayoutManager(activity) } @@ -53,33 +63,8 @@ class GenresFragment : AbsLibraryPagerRecyclerViewFragment) { - homeAdapter.swapData(sections) - } + lateinit var homeModel: HomeViewModel override fun onCreateView( inflater: LayoutInflater, @@ -110,6 +100,7 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + setStatusBarColorAuto(view) bannerImage?.setOnClickListener { @@ -140,7 +131,7 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba NavigationUtil.goToPlaylistNew(requireActivity(), HistoryPlaylist(requireActivity())) } - userImage?.setOnClickListener { + userImage.setOnClickListener { val options = ActivityOptions.makeSceneTransitionAnimation( mainActivity, userImage, @@ -151,15 +142,15 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba titleWelcome?.text = String.format("%s", PreferenceUtil.getInstance(requireContext()).userName) - App.musicComponent.inject(this) homeAdapter = HomeAdapter(mainActivity, displayMetrics) - recyclerView.apply { layoutManager = LinearLayoutManager(mainActivity) adapter = homeAdapter } - homePresenter.attachView(this) - homePresenter.loadSections() + homeModel = ViewModelProvider(this).get(HomeViewModel::class.java) + homeModel.sections.observe(viewLifecycleOwner, androidx.lifecycle.Observer { sections -> + homeAdapter.swapData(sections) + }) } override fun handleBackPress(): Boolean { @@ -171,18 +162,9 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba getTimeOfTheDay() } - override fun onDestroyView() { - super.onDestroyView() - homePresenter.detachView() - } - - override fun showEmptyView() { - emptyContainer.show() - } - private fun getTimeOfTheDay() { - val c = Calendar.getInstance() - val timeOfDay = c.get(Calendar.HOUR_OF_DAY) + val calendar = Calendar.getInstance() + val timeOfDay = calendar.get(Calendar.HOUR_OF_DAY) var images = arrayOf() when (timeOfDay) { in 0..5 -> images = resources.getStringArray(R.array.night) 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 new file mode 100644 index 00000000..1471b13e --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeViewModel.kt @@ -0,0 +1,36 @@ +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) { + lateinit var sections: MutableLiveData> + var repository: RepositoryImpl = RepositoryImpl(getApplication()) + + init { + loadHome() + } + + fun loadHome() = viewModelScope.launch { + val list = mutableListOf() + val result = listOf( + repository.topArtists(), + repository.topAlbums(), + repository.recentArtists(), + repository.recentAlbums(), + repository.favoritePlaylist() + ) + for (r in result) { + when (r) { + is Result.Success -> list.add(r.data) + } + } + sections = MutableLiveData(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 new file mode 100644 index 00000000..554f2c81 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistViewModel.kt @@ -0,0 +1,25 @@ +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) { + lateinit var playlists: MutableLiveData> + + init { + loadPlaylist() + } + + fun loadPlaylist() = viewModelScope.launch { + val result = RepositoryImpl(getApplication()).allPlaylists() + if (result is Result.Success) { + playlists = MutableLiveData(result.data) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlaylistsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt similarity index 57% rename from app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlaylistsFragment.kt rename to app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt index 8aaf99fd..98443c71 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlaylistsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt @@ -1,44 +1,42 @@ -package code.name.monkey.retromusic.fragments.mainactivity +package code.name.monkey.retromusic.fragments.playlists import android.os.Bundle 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.App import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.playlist.PlaylistAdapter import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewFragment import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks -import code.name.monkey.retromusic.model.Playlist -import code.name.monkey.retromusic.mvp.presenter.PlaylistView -import code.name.monkey.retromusic.mvp.presenter.PlaylistsPresenter -import javax.inject.Inject class PlaylistsFragment : - AbsLibraryPagerRecyclerViewFragment(), PlaylistView, + 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()) + } + }) + } + override fun handleBackPress(): Boolean { return false } - @Inject - lateinit var playlistsPresenter: PlaylistsPresenter - override val emptyMessage: Int get() = R.string.no_playlists - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - App.musicComponent.inject(this) - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - playlistsPresenter.attachView(this) - } - override fun createLayoutManager(): GridLayoutManager { return GridLayoutManager(requireContext(), 1) } @@ -52,29 +50,9 @@ class PlaylistsFragment : ) } - override fun onResume() { - super.onResume() - if (adapter!!.dataSet.isNullOrEmpty()) { - playlistsPresenter.playlists() - } - } - - override fun onDestroyView() { - playlistsPresenter.detachView() - super.onDestroyView() - } - override fun onMediaStoreChanged() { super.onMediaStoreChanged() - playlistsPresenter.playlists() - } - - override fun showEmptyView() { - adapter?.swapDataSet(ArrayList()) - } - - override fun playlists(playlists: List) { - adapter?.swapDataSet(playlists) + playlistViewModel.loadPlaylist() } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { @@ -91,10 +69,7 @@ class PlaylistsFragment : @JvmStatic fun newInstance(): PlaylistsFragment { - val args = Bundle() - val fragment = PlaylistsFragment() - fragment.arguments = args - return fragment + return PlaylistsFragment() } } } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlayingQueueFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/queue/PlayingQueueFragment.kt similarity index 98% rename from app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlayingQueueFragment.kt rename to app/src/main/java/code/name/monkey/retromusic/fragments/queue/PlayingQueueFragment.kt index 112136ae..a618c66e 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlayingQueueFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/queue/PlayingQueueFragment.kt @@ -11,7 +11,7 @@ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. */ -package code.name.monkey.retromusic.fragments.mainactivity +package code.name.monkey.retromusic.fragments.queue import android.os.Bundle import android.view.View diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/SongsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt similarity index 79% rename from app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/SongsFragment.kt rename to app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt index ffd07b18..6b9d6320 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/SongsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt @@ -1,8 +1,9 @@ -package code.name.monkey.retromusic.fragments.mainactivity +package code.name.monkey.retromusic.fragments.songs import android.os.Bundle import android.view.View import androidx.annotation.LayoutRes +import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.GridLayoutManager import code.name.monkey.retromusic.App import code.name.monkey.retromusic.R @@ -10,19 +11,13 @@ import code.name.monkey.retromusic.adapter.song.ShuffleButtonSongAdapter import code.name.monkey.retromusic.adapter.song.SongAdapter 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.PreferenceUtil -import java.util.* -import javax.inject.Inject class SongsFragment : AbsLibraryPagerRecyclerViewCustomGridSizeFragment(), - SongView, MainActivityFragmentCallbacks { + MainActivityFragmentCallbacks { - @Inject - lateinit var songPresenter: SongPresenter + lateinit var songViewModel: SongsViewModel override val emptyMessage: Int get() = R.string.no_songs @@ -35,7 +30,14 @@ class SongsFragment : override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - songPresenter.attachView(this) + songViewModel = ViewModelProvider(this).get(SongsViewModel::class.java) + songViewModel.songs.observe(viewLifecycleOwner, + androidx.lifecycle.Observer { songs -> + if (songs.isNotEmpty()) + adapter?.swapDataSet(songs) + else + adapter?.swapDataSet(listOf()) + }) } override fun createLayoutManager(): GridLayoutManager { @@ -62,12 +64,8 @@ class SongsFragment : ) } - override fun songs(songs: List) { - adapter?.swapDataSet(songs) - } - override fun onMediaStoreChanged() { - songPresenter.loadSongs() + //songPresenter.loadSongs() } override fun loadGridSize(): Int { @@ -90,20 +88,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()) - } override fun loadSortOrder(): String { return PreferenceUtil.getInstance(requireContext()).songSortOrder @@ -114,7 +98,7 @@ class SongsFragment : } override fun setSortOrder(sortOrder: String) { - songPresenter.loadSongs() + songViewModel.loadSongs() } companion object { @@ -125,7 +109,8 @@ class SongsFragment : @JvmStatic fun newInstance(): SongsFragment { val args = Bundle() - val fragment = SongsFragment() + val fragment = + SongsFragment() fragment.arguments = args return fragment } 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 new file mode 100644 index 00000000..b4f34c47 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsViewModel.kt @@ -0,0 +1,25 @@ +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) { + lateinit var songs: MutableLiveData> + + init { + loadSongs() + } + + fun loadSongs() = viewModelScope.launch { + val result = RepositoryImpl(getApplication()).allSongs() + if (result is Success) { + songs = MutableLiveData(result.data) + } + } +} \ No newline at end of file