Merge pull request #721 from h4h13/songViewModel

Song view model
main
Hemanth S 2020-04-26 15:50:42 +05:30 committed by GitHub
commit 3ae88a4e5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 309 additions and 260 deletions

View File

@ -189,4 +189,10 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$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.activities.base.AbsSlidingMusicPanelActivity;
import code.name.monkey.retromusic.dialogs.CreatePlaylistDialog; import code.name.monkey.retromusic.dialogs.CreatePlaylistDialog;
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment; import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment;
import code.name.monkey.retromusic.fragments.mainactivity.AlbumsFragment; import code.name.monkey.retromusic.fragments.albums.AlbumsFragment;
import code.name.monkey.retromusic.fragments.mainactivity.ArtistsFragment; import code.name.monkey.retromusic.fragments.artists.ArtistsFragment;
import code.name.monkey.retromusic.fragments.mainactivity.BannerHomeFragment; import code.name.monkey.retromusic.fragments.home.BannerHomeFragment;
import code.name.monkey.retromusic.fragments.mainactivity.FoldersFragment; import code.name.monkey.retromusic.fragments.mainactivity.FoldersFragment;
import code.name.monkey.retromusic.fragments.mainactivity.GenresFragment; import code.name.monkey.retromusic.fragments.genres.GenresFragment;
import code.name.monkey.retromusic.fragments.mainactivity.PlayingQueueFragment; import code.name.monkey.retromusic.fragments.queue.PlayingQueueFragment;
import code.name.monkey.retromusic.fragments.mainactivity.PlaylistsFragment; import code.name.monkey.retromusic.fragments.playlists.PlaylistsFragment;
import code.name.monkey.retromusic.fragments.mainactivity.SongsFragment; import code.name.monkey.retromusic.fragments.songs.SongsFragment;
import code.name.monkey.retromusic.helper.MusicPlayerRemote; import code.name.monkey.retromusic.helper.MusicPlayerRemote;
import code.name.monkey.retromusic.helper.SearchQueryHelper; import code.name.monkey.retromusic.helper.SearchQueryHelper;
import code.name.monkey.retromusic.helper.SortOrder.AlbumSortOrder; 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.activities.*
import code.name.monkey.retromusic.dagger.module.AppModule import code.name.monkey.retromusic.dagger.module.AppModule
import code.name.monkey.retromusic.dagger.module.PresenterModule 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 dagger.Component
import javax.inject.Singleton 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.os.Bundle
import android.view.View import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.adapter.album.AlbumAdapter import code.name.monkey.retromusic.adapter.album.AlbumAdapter
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks 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 code.name.monkey.retromusic.util.PreferenceUtil
import javax.inject.Inject
class AlbumsFragment : class AlbumsFragment :
AbsLibraryPagerRecyclerViewCustomGridSizeFragment<AlbumAdapter, GridLayoutManager>(), AbsLibraryPagerRecyclerViewCustomGridSizeFragment<AlbumAdapter, GridLayoutManager>(),
AlbumsView, MainActivityFragmentCallbacks { MainActivityFragmentCallbacks {
@Inject lateinit var albumViewModel: AlbumViewModel
lateinit var albumsPresenter: AlbumsPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.musicComponent.inject(this)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
albumsPresenter.attachView(this) albumViewModel = ViewModelProvider(this).get(AlbumViewModel::class.java)
} albumViewModel.albums.observe(viewLifecycleOwner, Observer { albums ->
if (albums.isNotEmpty())
override fun onResume() { adapter?.swapDataSet(albums)
super.onResume() else
if (adapter!!.dataSet.isNullOrEmpty()) { adapter?.swapDataSet(listOf())
albumsPresenter.loadAlbums() })
}
}
override fun onDestroyView() {
super.onDestroyView()
albumsPresenter.detachView()
}
override fun albums(albums: List<Album>) {
adapter?.swapDataSet(albums)
} }
override val emptyMessage: Int override val emptyMessage: Int
@ -94,15 +75,11 @@ class AlbumsFragment :
} }
override fun onMediaStoreChanged() { override fun onMediaStoreChanged() {
albumsPresenter.loadAlbums() albumViewModel.getAlbums()
} }
override fun setSortOrder(sortOrder: String) { override fun setSortOrder(sortOrder: String) {
albumsPresenter.loadAlbums() albumViewModel.getAlbums()
}
override fun showEmptyView() {
adapter?.swapDataSet(ArrayList())
} }
override fun setLayoutRes(layoutRes: Int) { override fun setLayoutRes(layoutRes: Int) {
@ -116,20 +93,17 @@ class AlbumsFragment :
PreferenceUtil.getInstance(requireContext()).albumGridStyle = layoutRes PreferenceUtil.getInstance(requireContext()).albumGridStyle = layoutRes
} }
override fun handleBackPress(): Boolean {
return false
}
companion object { companion object {
@JvmField @JvmField
var TAG: String = AlbumsFragment::class.java.simpleName var TAG: String = AlbumsFragment::class.java.simpleName
@JvmStatic @JvmStatic
fun newInstance(): AlbumsFragment { fun newInstance(): AlbumsFragment {
val args = Bundle() return AlbumsFragment()
val fragment = AlbumsFragment()
fragment.arguments = args
return fragment
} }
} }
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.os.Bundle
import android.view.View import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.adapter.artist.ArtistAdapter import code.name.monkey.retromusic.adapter.artist.ArtistAdapter
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks 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 code.name.monkey.retromusic.util.PreferenceUtil
import javax.inject.Inject
class ArtistsFragment : class ArtistsFragment :
AbsLibraryPagerRecyclerViewCustomGridSizeFragment<ArtistAdapter, GridLayoutManager>(), 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 { override fun handleBackPress(): Boolean {
return false return false
} }
override fun artists(artists: List<Artist>) {
adapter?.swapDataSet(artists)
}
@Inject
lateinit var artistsPresenter: ArtistsPresenter
override val emptyMessage: Int override val emptyMessage: Int
get() = R.string.no_artists 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() { override fun onMediaStoreChanged() {
artistsPresenter.loadArtists() artistViewModel.loadArtists()
} }
override fun setSortOrder(sortOrder: String) { override fun setSortOrder(sortOrder: String) {
artistsPresenter.loadArtists() artistViewModel.loadArtists()
} }
override fun createLayoutManager(): GridLayoutManager { override fun createLayoutManager(): GridLayoutManager {
@ -105,23 +88,6 @@ class ArtistsFragment :
PreferenceUtil.getInstance(requireContext()).artistSortOrder = sortOrder 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) { override fun setLayoutRes(layoutRes: Int) {
} }
@ -132,4 +98,14 @@ class ArtistsFragment :
override fun saveLayoutRes(layoutRes: Int) { override fun saveLayoutRes(layoutRes: Int) {
PreferenceUtil.getInstance(requireContext()).artistGridStyle = layoutRes 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. * 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.os.Bundle
import android.view.View import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.retromusic.App import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.adapter.GenreAdapter import code.name.monkey.retromusic.adapter.GenreAdapter
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewFragment import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewFragment
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks 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>(), 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 { override fun handleBackPress(): Boolean {
return false return false
} }
override fun genres(genres: List<Genre>) {
adapter?.swapDataSet(genres)
}
override fun showEmptyView() {
}
override fun createLayoutManager(): LinearLayoutManager { override fun createLayoutManager(): LinearLayoutManager {
return LinearLayoutManager(activity) return LinearLayoutManager(activity)
} }
@ -53,33 +63,8 @@ class GenresFragment : AbsLibraryPagerRecyclerViewFragment<GenreAdapter, LinearL
override val emptyMessage: Int override val emptyMessage: Int
get() = R.string.no_genres 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() { override fun onMediaStoreChanged() {
genresPresenter.loadGenres() genreViewModel.loadGenre()
} }
companion object { companion object {

View File

@ -12,7 +12,7 @@
* See the GNU General Public License for more details. * 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.app.ActivityOptions
import android.os.Bundle import android.os.Bundle
@ -20,25 +20,21 @@ import android.util.DisplayMetrics
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.TintHelper 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
import code.name.monkey.retromusic.Constants.USER_BANNER import code.name.monkey.retromusic.Constants.USER_BANNER
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.adapter.HomeAdapter 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.fragments.base.AbsMainActivityFragment
import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks
import code.name.monkey.retromusic.loaders.SongLoader 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.HistoryPlaylist
import code.name.monkey.retromusic.model.smartplaylist.LastAddedPlaylist import code.name.monkey.retromusic.model.smartplaylist.LastAddedPlaylist
import code.name.monkey.retromusic.model.smartplaylist.MyTopTracksPlaylist 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.NavigationUtil
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
import com.bumptech.glide.Glide 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 kotlinx.android.synthetic.main.home_content.*
import java.io.File import java.io.File
import java.util.* import java.util.*
import javax.inject.Inject
class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallbacks, HomeView { class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallbacks {
@Inject
lateinit var homePresenter: HomePresenter
private lateinit var homeAdapter: HomeAdapter private lateinit var homeAdapter: HomeAdapter
lateinit var homeModel: HomeViewModel
override fun sections(sections: List<Home>) {
homeAdapter.swapData(sections)
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -110,6 +100,7 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
setStatusBarColorAuto(view) setStatusBarColorAuto(view)
bannerImage?.setOnClickListener { bannerImage?.setOnClickListener {
@ -140,7 +131,7 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
NavigationUtil.goToPlaylistNew(requireActivity(), HistoryPlaylist(requireActivity())) NavigationUtil.goToPlaylistNew(requireActivity(), HistoryPlaylist(requireActivity()))
} }
userImage?.setOnClickListener { userImage.setOnClickListener {
val options = ActivityOptions.makeSceneTransitionAnimation( val options = ActivityOptions.makeSceneTransitionAnimation(
mainActivity, mainActivity,
userImage, userImage,
@ -151,15 +142,15 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
titleWelcome?.text = titleWelcome?.text =
String.format("%s", PreferenceUtil.getInstance(requireContext()).userName) String.format("%s", PreferenceUtil.getInstance(requireContext()).userName)
App.musicComponent.inject(this)
homeAdapter = HomeAdapter(mainActivity, displayMetrics) homeAdapter = HomeAdapter(mainActivity, displayMetrics)
recyclerView.apply { recyclerView.apply {
layoutManager = LinearLayoutManager(mainActivity) layoutManager = LinearLayoutManager(mainActivity)
adapter = homeAdapter adapter = homeAdapter
} }
homePresenter.attachView(this) homeModel = ViewModelProvider(this).get(HomeViewModel::class.java)
homePresenter.loadSections() homeModel.sections.observe(viewLifecycleOwner, androidx.lifecycle.Observer { sections ->
homeAdapter.swapData(sections)
})
} }
override fun handleBackPress(): Boolean { override fun handleBackPress(): Boolean {
@ -171,18 +162,9 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
getTimeOfTheDay() getTimeOfTheDay()
} }
override fun onDestroyView() {
super.onDestroyView()
homePresenter.detachView()
}
override fun showEmptyView() {
emptyContainer.show()
}
private fun getTimeOfTheDay() { private fun getTimeOfTheDay() {
val c = Calendar.getInstance() val calendar = Calendar.getInstance()
val timeOfDay = c.get(Calendar.HOUR_OF_DAY) val timeOfDay = calendar.get(Calendar.HOUR_OF_DAY)
var images = arrayOf<String>() var images = arrayOf<String>()
when (timeOfDay) { when (timeOfDay) {
in 0..5 -> images = resources.getStringArray(R.array.night) 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.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.View import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.adapter.playlist.PlaylistAdapter import code.name.monkey.retromusic.adapter.playlist.PlaylistAdapter
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewFragment import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewFragment
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks 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 : class PlaylistsFragment :
AbsLibraryPagerRecyclerViewFragment<PlaylistAdapter, GridLayoutManager>(), PlaylistView, AbsLibraryPagerRecyclerViewFragment<PlaylistAdapter, GridLayoutManager>(),
MainActivityFragmentCallbacks { 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 { override fun handleBackPress(): Boolean {
return false return false
} }
@Inject
lateinit var playlistsPresenter: PlaylistsPresenter
override val emptyMessage: Int override val emptyMessage: Int
get() = R.string.no_playlists 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 { override fun createLayoutManager(): GridLayoutManager {
return GridLayoutManager(requireContext(), 1) 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() { override fun onMediaStoreChanged() {
super.onMediaStoreChanged() super.onMediaStoreChanged()
playlistsPresenter.playlists() playlistViewModel.loadPlaylist()
}
override fun showEmptyView() {
adapter?.swapDataSet(ArrayList())
}
override fun playlists(playlists: List<Playlist>) {
adapter?.swapDataSet(playlists)
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@ -91,10 +69,7 @@ class PlaylistsFragment :
@JvmStatic @JvmStatic
fun newInstance(): PlaylistsFragment { fun newInstance(): PlaylistsFragment {
val args = Bundle() return PlaylistsFragment()
val fragment = PlaylistsFragment()
fragment.arguments = args
return fragment
} }
} }
} }

View File

@ -11,7 +11,7 @@
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * 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.os.Bundle
import android.view.View 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.os.Bundle
import android.view.View import android.view.View
import androidx.annotation.LayoutRes import androidx.annotation.LayoutRes
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import code.name.monkey.retromusic.App import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R 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.adapter.song.SongAdapter
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks 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 code.name.monkey.retromusic.util.PreferenceUtil
import java.util.*
import javax.inject.Inject
class SongsFragment : class SongsFragment :
AbsLibraryPagerRecyclerViewCustomGridSizeFragment<SongAdapter, GridLayoutManager>(), AbsLibraryPagerRecyclerViewCustomGridSizeFragment<SongAdapter, GridLayoutManager>(),
SongView, MainActivityFragmentCallbacks { MainActivityFragmentCallbacks {
@Inject lateinit var songViewModel: SongsViewModel
lateinit var songPresenter: SongPresenter
override val emptyMessage: Int override val emptyMessage: Int
get() = R.string.no_songs get() = R.string.no_songs
@ -35,7 +30,14 @@ class SongsFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) 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 { override fun createLayoutManager(): GridLayoutManager {
@ -62,12 +64,8 @@ class SongsFragment :
) )
} }
override fun songs(songs: List<Song>) {
adapter?.swapDataSet(songs)
}
override fun onMediaStoreChanged() { override fun onMediaStoreChanged() {
songPresenter.loadSongs() //songPresenter.loadSongs()
} }
override fun loadGridSize(): Int { override fun loadGridSize(): Int {
@ -90,20 +88,6 @@ class SongsFragment :
adapter?.notifyDataSetChanged() 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 { override fun loadSortOrder(): String {
return PreferenceUtil.getInstance(requireContext()).songSortOrder return PreferenceUtil.getInstance(requireContext()).songSortOrder
@ -114,7 +98,7 @@ class SongsFragment :
} }
override fun setSortOrder(sortOrder: String) { override fun setSortOrder(sortOrder: String) {
songPresenter.loadSongs() songViewModel.loadSongs()
} }
companion object { companion object {
@ -125,7 +109,8 @@ class SongsFragment :
@JvmStatic @JvmStatic
fun newInstance(): SongsFragment { fun newInstance(): SongsFragment {
val args = Bundle() val args = Bundle()
val fragment = SongsFragment() val fragment =
SongsFragment()
fragment.arguments = args fragment.arguments = args
return fragment 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)
}
}
}

View File

@ -84,7 +84,6 @@ public class AlbumGlideRequest {
//noinspection unchecked //noinspection unchecked
return createBaseRequest(requestManager, song, ignoreMediaStore) return createBaseRequest(requestManager, song, ignoreMediaStore)
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY) .diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.placeholder(DEFAULT_ERROR_IMAGE)
.error(DEFAULT_ERROR_IMAGE) .error(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION) .animate(DEFAULT_ANIMATION)
.signature(createSignature(song)); .signature(createSignature(song));
@ -103,15 +102,15 @@ public class AlbumGlideRequest {
return createBaseRequest(builder.requestManager, builder.song, builder.ignoreMediaStore) return createBaseRequest(builder.requestManager, builder.song, builder.ignoreMediaStore)
.asBitmap() .asBitmap()
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY) .diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.placeholder(DEFAULT_ERROR_IMAGE)
.error(DEFAULT_ERROR_IMAGE) .error(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION) .animate(DEFAULT_ANIMATION)
.dontTransform()
.signature(createSignature(builder.song)); .signature(createSignature(builder.song));
} }
} }
public static class PaletteBuilder { public static class PaletteBuilder {
private final Context context; private final Context context;
private final Builder builder; private final Builder builder;
PaletteBuilder(Builder builder, Context context) { PaletteBuilder(Builder builder, Context context) {
@ -126,7 +125,6 @@ public class AlbumGlideRequest {
.asBitmap() .asBitmap()
.transcode(new BitmapPaletteTranscoder(context), BitmapPaletteWrapper.class) .transcode(new BitmapPaletteTranscoder(context), BitmapPaletteWrapper.class)
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY) .diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.placeholder(DEFAULT_ERROR_IMAGE)
.error(DEFAULT_ERROR_IMAGE) .error(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION) .animate(DEFAULT_ANIMATION)
.signature(createSignature(builder.song)); .signature(createSignature(builder.song));

View File

@ -88,10 +88,10 @@ public class ArtistGlideRequest {
//noinspection unchecked //noinspection unchecked
return createBaseRequest(requestManager, artist, noCustomImage, forceDownload) return createBaseRequest(requestManager, artist, noCustomImage, forceDownload)
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY) .diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.placeholder(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION) .animate(DEFAULT_ANIMATION)
.priority(Priority.LOW) .priority(Priority.LOW)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.dontTransform()
.signature(createSignature(artist)); .signature(createSignature(artist));
} }
@ -127,6 +127,7 @@ public class ArtistGlideRequest {
.animate(DEFAULT_ANIMATION) .animate(DEFAULT_ANIMATION)
.priority(Priority.LOW) .priority(Priority.LOW)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.dontTransform()
.signature(createSignature(builder.artist)); .signature(createSignature(builder.artist));
} }
} }
@ -149,10 +150,10 @@ public class ArtistGlideRequest {
.asBitmap() .asBitmap()
.transcode(new BitmapPaletteTranscoder(context), BitmapPaletteWrapper.class) .transcode(new BitmapPaletteTranscoder(context), BitmapPaletteWrapper.class)
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY) .diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.placeholder(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION) .animate(DEFAULT_ANIMATION)
.priority(Priority.LOW) .priority(Priority.LOW)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.dontTransform()
.signature(createSignature(builder.artist)); .signature(createSignature(builder.artist));
} }
} }

View File

@ -32,6 +32,7 @@
<com.google.android.material.appbar.MaterialToolbar <com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar" android:id="@+id/toolbar"
style="@style/Toolbar" style="@style/Toolbar"
android:background="?attr/colorSurface"
app:navigationIcon="@drawable/ic_keyboard_backspace_black_24dp" /> app:navigationIcon="@drawable/ic_keyboard_backspace_black_24dp" />
<ViewStub <ViewStub