Added viewmodel

This commit is contained in:
h4h13 2020-04-25 21:29:52 +05:30
parent 2da9d1c5b7
commit f7d2d32982
16 changed files with 303 additions and 254 deletions

View file

@ -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"
}

View file

@ -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;

View file

@ -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

View file

@ -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<List<Album>>
init {
getAlbums()
}
fun getAlbums() = viewModelScope.launch {
val result = RepositoryImpl(getApplication()).allAlbums()
if (result is Result.Success) {
albums = MutableLiveData(result.data)
}
}
}

View file

@ -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<AlbumAdapter, GridLayoutManager>(),
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<Album>) {
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
}
}

View file

@ -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<List<Artist>>
init {
loadArtists()
}
fun loadArtists() = viewModelScope.launch {
val result = RepositoryImpl(getApplication()).allArtists()
if (result is Result.Success) {
artists = MutableLiveData(result.data)
}
}
}

View file

@ -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<ArtistAdapter, GridLayoutManager>(),
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<Artist>) {
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()
}
}
}

View file

@ -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<List<Genre>>
init {
loadGenre()
}
fun loadGenre() = viewModelScope.launch {
val result = RepositoryImpl(getApplication()).allGenres()
if (result is Success) {
genres = MutableLiveData(result.data)
}
}
}

View file

@ -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<GenreAdapter, LinearLayoutManager>(),
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<Genre>) {
adapter?.swapDataSet(genres)
}
override fun showEmptyView() {
}
override fun createLayoutManager(): LinearLayoutManager {
return LinearLayoutManager(activity)
}
@ -53,33 +63,8 @@ class GenresFragment : AbsLibraryPagerRecyclerViewFragment<GenreAdapter, LinearL
override val emptyMessage: Int
get() = R.string.no_genres
@Inject
lateinit var genresPresenter: GenresPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.musicComponent.inject(this)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
genresPresenter.attachView(this)
}
override fun onResume() {
super.onResume()
if (adapter!!.dataSet.isNullOrEmpty()) {
genresPresenter.loadGenres()
}
}
override fun onDestroyView() {
super.onDestroyView()
genresPresenter.detachView()
}
override fun onMediaStoreChanged() {
genresPresenter.loadGenres()
genreViewModel.loadGenre()
}
companion object {

View file

@ -12,7 +12,7 @@
* See the GNU General Public License for more details.
*/
package code.name.monkey.retromusic.fragments.mainactivity
package code.name.monkey.retromusic.fragments.home
import android.app.ActivityOptions
import android.os.Bundle
@ -20,25 +20,21 @@ import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.TintHelper
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.Constants
import code.name.monkey.retromusic.Constants.USER_BANNER
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.adapter.HomeAdapter
import code.name.monkey.retromusic.extensions.show
import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks
import code.name.monkey.retromusic.loaders.SongLoader
import code.name.monkey.retromusic.model.Home
import code.name.monkey.retromusic.model.smartplaylist.HistoryPlaylist
import code.name.monkey.retromusic.model.smartplaylist.LastAddedPlaylist
import code.name.monkey.retromusic.model.smartplaylist.MyTopTracksPlaylist
import code.name.monkey.retromusic.mvp.presenter.HomePresenter
import code.name.monkey.retromusic.mvp.presenter.HomeView
import code.name.monkey.retromusic.util.NavigationUtil
import code.name.monkey.retromusic.util.PreferenceUtil
import com.bumptech.glide.Glide
@ -48,17 +44,11 @@ import kotlinx.android.synthetic.main.fragment_banner_home.*
import kotlinx.android.synthetic.main.home_content.*
import java.io.File
import java.util.*
import javax.inject.Inject
class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallbacks, HomeView {
@Inject
lateinit var homePresenter: HomePresenter
class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallbacks {
private lateinit var homeAdapter: HomeAdapter
override fun sections(sections: List<Home>) {
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<String>()
when (timeOfDay) {
in 0..5 -> images = resources.getStringArray(R.array.night)

View file

@ -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<List<Home>>
var repository: RepositoryImpl = RepositoryImpl(getApplication())
init {
loadHome()
}
fun loadHome() = viewModelScope.launch {
val list = mutableListOf<Home>()
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)
}
}

View file

@ -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<List<Playlist>>
init {
loadPlaylist()
}
fun loadPlaylist() = viewModelScope.launch {
val result = RepositoryImpl(getApplication()).allPlaylists()
if (result is Result.Success) {
playlists = MutableLiveData(result.data)
}
}
}

View file

@ -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<PlaylistAdapter, GridLayoutManager>(), PlaylistView,
AbsLibraryPagerRecyclerViewFragment<PlaylistAdapter, GridLayoutManager>(),
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<Playlist>) {
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()
}
}
}

View file

@ -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

View file

@ -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<SongAdapter, GridLayoutManager>(),
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<Song>) {
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
}

View file

@ -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<List<Song>>
init {
loadSongs()
}
fun loadSongs() = viewModelScope.launch {
val result = RepositoryImpl(getApplication()).allSongs()
if (result is Success) {
songs = MutableLiveData(result.data)
}
}
}