Kotlin refactoring
This commit is contained in:
parent
4056a0c1c5
commit
d7aae9270e
17 changed files with 211 additions and 297 deletions
|
@ -13,12 +13,14 @@ import android.view.View
|
|||
import androidx.core.app.ActivityCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.commit
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import code.name.monkey.appthemehelper.ThemeStore.Companion.accentColor
|
||||
import code.name.monkey.appthemehelper.util.ATHUtil.resolveColor
|
||||
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
|
||||
import code.name.monkey.retromusic.*
|
||||
import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity
|
||||
import code.name.monkey.retromusic.dialogs.CreatePlaylistDialog.Companion.create
|
||||
import code.name.monkey.retromusic.fragments.LibraryViewModel
|
||||
import code.name.monkey.retromusic.fragments.albums.AlbumsFragment
|
||||
import code.name.monkey.retromusic.fragments.artists.ArtistsFragment
|
||||
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment
|
||||
|
@ -70,6 +72,7 @@ class MainActivity : AbsSlidingMusicPanelActivity(),
|
|||
const val APP_UPDATE_REQUEST_CODE = 9002
|
||||
}
|
||||
|
||||
lateinit var libraryViewModel: LibraryViewModel
|
||||
private lateinit var cab: MaterialCab
|
||||
private val intentFilter = IntentFilter(Intent.ACTION_SCREEN_OFF)
|
||||
private lateinit var currentFragment: MainActivityFragmentCallbacks
|
||||
|
@ -118,6 +121,9 @@ class MainActivity : AbsSlidingMusicPanelActivity(),
|
|||
hideStatusBar()
|
||||
setBottomBarVisibility(View.VISIBLE)
|
||||
|
||||
libraryViewModel = ViewModelProvider(this).get(LibraryViewModel::class.java)
|
||||
addMusicServiceEventListener(libraryViewModel)
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
selectedFragment(PreferenceUtilKT.lastPage)
|
||||
} else {
|
||||
|
|
|
@ -0,0 +1,136 @@
|
|||
package code.name.monkey.retromusic.fragments
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.Result.Error
|
||||
import code.name.monkey.retromusic.Result.Success
|
||||
import code.name.monkey.retromusic.fragments.ReloadType.*
|
||||
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
|
||||
import code.name.monkey.retromusic.model.*
|
||||
import code.name.monkey.retromusic.providers.RepositoryImpl
|
||||
import code.name.monkey.retromusic.providers.interfaces.Repository
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class LibraryViewModel(application: Application) :
|
||||
AndroidViewModel(application), MusicServiceEventListener {
|
||||
|
||||
private val _repository: Repository = RepositoryImpl(application.applicationContext)
|
||||
private val _albums = MutableLiveData<List<Album>>()
|
||||
private val _songs = MutableLiveData<List<Song>>()
|
||||
private val _artists = MutableLiveData<List<Artist>>()
|
||||
private val _playlist = MutableLiveData<List<Playlist>>()
|
||||
private val _genre = MutableLiveData<List<Genre>>()
|
||||
private val _homeSections = MutableLiveData<List<Home>>()
|
||||
|
||||
fun homeSections(): LiveData<List<Home>> = _homeSections
|
||||
fun allAlbums(): LiveData<List<Album>> = _albums
|
||||
fun allSongs(): LiveData<List<Song>> = _songs
|
||||
fun allArtists(): LiveData<List<Artist>> = _artists
|
||||
fun allPlaylisits(): LiveData<List<Playlist>> = _playlist
|
||||
fun allGenres(): LiveData<List<Genre>> = _genre
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
loadLibraryContent()
|
||||
loadHomeSections()
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadLibraryContent() = viewModelScope.launch {
|
||||
_songs.value = loadSongs.await()
|
||||
_albums.value = loadAlbums.await()
|
||||
_artists.value = loadArtists.await()
|
||||
_playlist.value = loadPlaylists.await()
|
||||
_genre.value = loadGenres.await()
|
||||
}
|
||||
|
||||
private fun loadHomeSections() = viewModelScope.launch {
|
||||
val list = mutableListOf<Home>()
|
||||
val result = listOf(
|
||||
_repository.topArtists(),
|
||||
_repository.topAlbums(),
|
||||
_repository.recentArtists(),
|
||||
_repository.recentAlbums(),
|
||||
_repository.favoritePlaylist()
|
||||
)
|
||||
for (r in result) {
|
||||
if (r is Success) {
|
||||
list.add(r.data)
|
||||
}
|
||||
}
|
||||
_homeSections.value = list
|
||||
}
|
||||
|
||||
private val loadSongs: Deferred<List<Song>>
|
||||
get() = viewModelScope.async(IO) {
|
||||
when (val result = _repository.allSongs()) {
|
||||
is Success -> result.data
|
||||
is Error -> arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
private val loadAlbums: Deferred<List<Album>>
|
||||
get() = viewModelScope.async(IO) {
|
||||
when (val result = _repository.allAlbums()) {
|
||||
is Success -> result.data
|
||||
is Error -> arrayListOf()
|
||||
}
|
||||
}
|
||||
private val loadArtists: Deferred<List<Artist>>
|
||||
get() = viewModelScope.async(IO) {
|
||||
when (val result = _repository.allArtists()) {
|
||||
is Success -> result.data
|
||||
is Error -> arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
private val loadPlaylists: Deferred<List<Playlist>>
|
||||
get() = viewModelScope.async(IO) {
|
||||
when (val result = _repository.allPlaylists()) {
|
||||
is Success -> result.data
|
||||
is Error -> arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
private val loadGenres: Deferred<List<Genre>>
|
||||
get() = viewModelScope.async(IO) {
|
||||
when (val result = _repository.allGenres()) {
|
||||
is Success -> result.data
|
||||
is Error -> arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
fun forceReload(reloadType: ReloadType) = viewModelScope.launch {
|
||||
when (reloadType) {
|
||||
Songs -> _songs.value = loadSongs.await()
|
||||
Albums -> _albums.value = loadAlbums.await()
|
||||
Artists -> _artists.value = loadArtists.await()
|
||||
HomeSections -> _songs.value = loadSongs.await()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMediaStoreChanged() {
|
||||
loadLibraryContent()
|
||||
}
|
||||
|
||||
override fun onServiceConnected() {}
|
||||
override fun onServiceDisconnected() {}
|
||||
override fun onQueueChanged() {}
|
||||
override fun onPlayingMetaChanged() {}
|
||||
override fun onPlayStateChanged() {}
|
||||
override fun onRepeatModeChanged() {}
|
||||
override fun onShuffleModeChanged() {}
|
||||
}
|
||||
|
||||
enum class ReloadType {
|
||||
Songs,
|
||||
Albums,
|
||||
Artists,
|
||||
HomeSections
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package code.name.monkey.retromusic.fragments.albums
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Album
|
||||
import code.name.monkey.retromusic.providers.RepositoryImpl
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class AlbumViewModel(application: Application) : AndroidViewModel(application) {
|
||||
var albums = MutableLiveData<List<Album>>()
|
||||
|
||||
init {
|
||||
getAlbums()
|
||||
}
|
||||
|
||||
fun getAlbums() = viewModelScope.launch {
|
||||
val result = RepositoryImpl(getApplication()).allAlbums()
|
||||
if (result is Result.Success) {
|
||||
albums.value = result.data
|
||||
} else {
|
||||
albums.value = listOf()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,10 +3,10 @@ package code.name.monkey.retromusic.fragments.albums
|
|||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.adapter.album.AlbumAdapter
|
||||
import code.name.monkey.retromusic.fragments.ReloadType
|
||||
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment
|
||||
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks
|
||||
import code.name.monkey.retromusic.util.PreferenceUtilKT
|
||||
|
@ -15,17 +15,16 @@ class AlbumsFragment :
|
|||
AbsLibraryPagerRecyclerViewCustomGridSizeFragment<AlbumAdapter, GridLayoutManager>(),
|
||||
MainActivityFragmentCallbacks {
|
||||
|
||||
private lateinit var albumViewModel: AlbumViewModel
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
albumViewModel = ViewModelProvider(this).get(AlbumViewModel::class.java)
|
||||
albumViewModel.albums.observe(viewLifecycleOwner, Observer { albums ->
|
||||
if (albums.isNotEmpty())
|
||||
adapter?.swapDataSet(albums)
|
||||
else
|
||||
adapter?.swapDataSet(listOf())
|
||||
})
|
||||
mainActivity.libraryViewModel.allAlbums()
|
||||
.observe(viewLifecycleOwner, Observer { albums ->
|
||||
if (albums.isNotEmpty())
|
||||
adapter?.swapDataSet(albums)
|
||||
else
|
||||
adapter?.swapDataSet(listOf())
|
||||
})
|
||||
}
|
||||
|
||||
override val emptyMessage: Int
|
||||
|
@ -74,15 +73,10 @@ class AlbumsFragment :
|
|||
PreferenceUtilKT.albumGridSizeLand = gridColumns
|
||||
}
|
||||
|
||||
override fun onMediaStoreChanged() {
|
||||
albumViewModel.getAlbums()
|
||||
}
|
||||
|
||||
override fun setSortOrder(sortOrder: String) {
|
||||
albumViewModel.getAlbums()
|
||||
mainActivity.libraryViewModel.forceReload(ReloadType.Albums)
|
||||
}
|
||||
|
||||
|
||||
override fun loadLayoutRes(): Int {
|
||||
return PreferenceUtilKT.albumGridStyle
|
||||
}
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
package code.name.monkey.retromusic.fragments.artists
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Artist
|
||||
import code.name.monkey.retromusic.providers.RepositoryImpl
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ArtistViewModel(application: Application) : AndroidViewModel(application) {
|
||||
var artists = MutableLiveData<List<Artist>>()
|
||||
|
||||
init {
|
||||
loadArtists()
|
||||
}
|
||||
|
||||
fun loadArtists() = viewModelScope.launch {
|
||||
val result = RepositoryImpl(getApplication()).allArtists()
|
||||
if (result is Result.Success) {
|
||||
artists.value = result.data
|
||||
} else {
|
||||
artists.value = listOf()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,10 +3,10 @@ package code.name.monkey.retromusic.fragments.artists
|
|||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.adapter.artist.ArtistAdapter
|
||||
import code.name.monkey.retromusic.fragments.ReloadType
|
||||
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment
|
||||
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks
|
||||
import code.name.monkey.retromusic.util.PreferenceUtilKT
|
||||
|
@ -14,20 +14,16 @@ import code.name.monkey.retromusic.util.PreferenceUtilKT
|
|||
class ArtistsFragment :
|
||||
AbsLibraryPagerRecyclerViewCustomGridSizeFragment<ArtistAdapter, GridLayoutManager>(),
|
||||
MainActivityFragmentCallbacks {
|
||||
|
||||
lateinit var artistViewModel: ArtistViewModel
|
||||
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
artistViewModel = ViewModelProvider(this).get(ArtistViewModel::class.java)
|
||||
artistViewModel.artists.observe(viewLifecycleOwner, Observer { artists ->
|
||||
if (artists.isNotEmpty()) {
|
||||
adapter?.swapDataSet(artists)
|
||||
} else {
|
||||
adapter?.swapDataSet(listOf())
|
||||
}
|
||||
})
|
||||
mainActivity.libraryViewModel.allArtists().observe(
|
||||
viewLifecycleOwner, Observer { artists ->
|
||||
if (artists.isNotEmpty()) {
|
||||
adapter?.swapDataSet(artists)
|
||||
} else {
|
||||
adapter?.swapDataSet(listOf())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun handleBackPress(): Boolean {
|
||||
|
@ -37,12 +33,8 @@ class ArtistsFragment :
|
|||
override val emptyMessage: Int
|
||||
get() = R.string.no_artists
|
||||
|
||||
override fun onMediaStoreChanged() {
|
||||
artistViewModel.loadArtists()
|
||||
}
|
||||
|
||||
override fun setSortOrder(sortOrder: String) {
|
||||
artistViewModel.loadArtists()
|
||||
mainActivity.libraryViewModel.forceReload(ReloadType.Artists)
|
||||
}
|
||||
|
||||
override fun createLayoutManager(): GridLayoutManager {
|
||||
|
|
|
@ -6,8 +6,10 @@ import android.view.View
|
|||
import android.view.ViewGroup
|
||||
import androidx.annotation.NonNull
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.fragments.LibraryViewModel
|
||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||
import code.name.monkey.retromusic.util.DensityUtil
|
||||
import code.name.monkey.retromusic.util.ThemedFastScroller.create
|
||||
|
@ -20,6 +22,7 @@ import me.zhanghai.android.fastscroll.FastScrollerBuilder
|
|||
abstract class AbsLibraryPagerRecyclerViewFragment<A : RecyclerView.Adapter<*>, LM : RecyclerView.LayoutManager> :
|
||||
AbsLibraryPagerFragment(), AppBarLayout.OnOffsetChangedListener {
|
||||
|
||||
|
||||
protected var adapter: A? = null
|
||||
protected var layoutManager: LM? = null
|
||||
|
||||
|
@ -90,6 +93,8 @@ abstract class AbsLibraryPagerRecyclerViewFragment<A : RecyclerView.Adapter<*>,
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private fun initLayoutManager() {
|
||||
layoutManager = createLayoutManager()
|
||||
}
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
package code.name.monkey.retromusic.fragments.genres
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.Result.Success
|
||||
import code.name.monkey.retromusic.model.Genre
|
||||
import code.name.monkey.retromusic.providers.RepositoryImpl
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class GenreViewModel(application: Application) : AndroidViewModel(application) {
|
||||
var genres = MutableLiveData<List<Genre>>()
|
||||
|
||||
init {
|
||||
loadGenre()
|
||||
}
|
||||
|
||||
fun loadGenre() = viewModelScope.launch {
|
||||
val result = RepositoryImpl(getApplication()).allGenres()
|
||||
if (result is Success) {
|
||||
genres.value = result.data
|
||||
} else {
|
||||
genres.value = listOf()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,7 +17,6 @@ package code.name.monkey.retromusic.fragments.genres
|
|||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import code.name.monkey.retromusic.App
|
||||
import code.name.monkey.retromusic.R
|
||||
|
@ -28,8 +27,6 @@ import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks
|
|||
class GenresFragment : AbsLibraryPagerRecyclerViewFragment<GenreAdapter, LinearLayoutManager>(),
|
||||
MainActivityFragmentCallbacks {
|
||||
|
||||
private lateinit var genreViewModel: GenreViewModel
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
App.musicComponent.inject(this)
|
||||
|
@ -37,14 +34,14 @@ class GenresFragment : AbsLibraryPagerRecyclerViewFragment<GenreAdapter, LinearL
|
|||
|
||||
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())
|
||||
}
|
||||
})
|
||||
mainActivity.libraryViewModel.allGenres().observe(
|
||||
viewLifecycleOwner, Observer { genres ->
|
||||
if (genres.isNotEmpty()) {
|
||||
adapter?.swapDataSet(genres)
|
||||
} else {
|
||||
adapter?.swapDataSet(listOf())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun handleBackPress(): Boolean {
|
||||
|
@ -63,10 +60,6 @@ class GenresFragment : AbsLibraryPagerRecyclerViewFragment<GenreAdapter, LinearL
|
|||
override val emptyMessage: Int
|
||||
get() = R.string.no_genres
|
||||
|
||||
override fun onMediaStoreChanged() {
|
||||
genreViewModel.loadGenre()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val TAG: String = GenresFragment::class.java.simpleName
|
||||
|
|
|
@ -20,7 +20,7 @@ import android.util.DisplayMetrics
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.adapter.HomeAdapter
|
||||
|
@ -34,7 +34,6 @@ 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.util.NavigationUtil
|
||||
|
||||
import code.name.monkey.retromusic.util.PreferenceUtilKT
|
||||
import com.bumptech.glide.Glide
|
||||
import kotlinx.android.synthetic.main.abs_playlists.*
|
||||
|
@ -42,9 +41,7 @@ import kotlinx.android.synthetic.main.fragment_banner_home.*
|
|||
import kotlinx.android.synthetic.main.home_content.*
|
||||
|
||||
class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallbacks {
|
||||
|
||||
private lateinit var homeAdapter: HomeAdapter
|
||||
private lateinit var homeModel: HomeViewModel
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
|
@ -122,10 +119,12 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
|
|||
layoutManager = LinearLayoutManager(mainActivity)
|
||||
adapter = homeAdapter
|
||||
}
|
||||
homeModel = ViewModelProvider(this).get(HomeViewModel::class.java)
|
||||
homeModel.sections.observe(viewLifecycleOwner, androidx.lifecycle.Observer { sections ->
|
||||
homeAdapter.swapData(sections)
|
||||
})
|
||||
|
||||
mainActivity.libraryViewModel.homeSections()
|
||||
.observe(viewLifecycleOwner, Observer { sections ->
|
||||
homeAdapter.swapData(sections)
|
||||
})
|
||||
|
||||
loadProfile()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
package code.name.monkey.retromusic.fragments.home
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Home
|
||||
import code.name.monkey.retromusic.providers.RepositoryImpl
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
||||
var sections = MutableLiveData<List<Home>>()
|
||||
var repository: RepositoryImpl = RepositoryImpl(getApplication())
|
||||
|
||||
init {
|
||||
loadHome()
|
||||
}
|
||||
|
||||
private 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) {
|
||||
if (r is Result.Success) {
|
||||
list.add(r.data)
|
||||
}
|
||||
}
|
||||
sections.value = list
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package code.name.monkey.retromusic.fragments.playlists
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Playlist
|
||||
import code.name.monkey.retromusic.providers.RepositoryImpl
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class PlaylistViewModel(application: Application) : AndroidViewModel(application) {
|
||||
var playlists = MutableLiveData<List<Playlist>>()
|
||||
|
||||
init {
|
||||
loadPlaylist()
|
||||
}
|
||||
|
||||
fun loadPlaylist() = viewModelScope.launch {
|
||||
val result = RepositoryImpl(getApplication()).allPlaylists()
|
||||
if (result is Result.Success) {
|
||||
playlists.value = result.data
|
||||
} else {
|
||||
playlists.value = listOf()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,7 +5,6 @@ import android.view.Menu
|
|||
import android.view.MenuInflater
|
||||
import android.view.View
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.adapter.playlist.PlaylistAdapter
|
||||
|
@ -16,18 +15,17 @@ class PlaylistsFragment :
|
|||
AbsLibraryPagerRecyclerViewFragment<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())
|
||||
}
|
||||
})
|
||||
mainActivity.libraryViewModel.allPlaylisits()
|
||||
.observe(viewLifecycleOwner, Observer { playlists ->
|
||||
if (playlists.isNotEmpty()) {
|
||||
adapter?.swapDataSet(playlists)
|
||||
} else {
|
||||
adapter?.swapDataSet(listOf())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun handleBackPress(): Boolean {
|
||||
|
@ -50,11 +48,6 @@ class PlaylistsFragment :
|
|||
)
|
||||
}
|
||||
|
||||
override fun onMediaStoreChanged() {
|
||||
super.onMediaStoreChanged()
|
||||
playlistViewModel.loadPlaylist()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater)
|
||||
menu.apply {
|
||||
|
|
|
@ -4,39 +4,35 @@ import android.os.Bundle
|
|||
import android.view.View
|
||||
import androidx.annotation.LayoutRes
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import code.name.monkey.retromusic.App
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.adapter.song.ShuffleButtonSongAdapter
|
||||
import code.name.monkey.retromusic.adapter.song.SongAdapter
|
||||
import code.name.monkey.retromusic.fragments.ReloadType
|
||||
import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewCustomGridSizeFragment
|
||||
import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.mvp.presenter.SongPresenter
|
||||
import code.name.monkey.retromusic.mvp.presenter.SongView
|
||||
import code.name.monkey.retromusic.util.PreferenceUtilKT
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class SongsFragment :
|
||||
AbsLibraryPagerRecyclerViewCustomGridSizeFragment<SongAdapter, GridLayoutManager>(),
|
||||
SongView, MainActivityFragmentCallbacks {
|
||||
|
||||
@Inject
|
||||
lateinit var songPresenter: SongPresenter
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
mainActivity.libraryViewModel.allSongs()
|
||||
.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
|
||||
if (it.isNotEmpty()) {
|
||||
adapter?.swapDataSet(it)
|
||||
} else {
|
||||
adapter?.swapDataSet(listOf())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override val emptyMessage: Int
|
||||
get() = R.string.no_songs
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
App.musicComponent.inject(this)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
songPresenter.attachView(this)
|
||||
}
|
||||
|
||||
override fun createLayoutManager(): GridLayoutManager {
|
||||
return GridLayoutManager(requireActivity(), getGridSize()).apply {
|
||||
spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
|
||||
|
@ -65,10 +61,6 @@ class SongsFragment :
|
|||
adapter?.swapDataSet(songs)
|
||||
}
|
||||
|
||||
override fun onMediaStoreChanged() {
|
||||
songPresenter.loadSongs()
|
||||
}
|
||||
|
||||
override fun loadGridSize(): Int {
|
||||
return PreferenceUtilKT.songGridSize
|
||||
}
|
||||
|
@ -89,17 +81,6 @@ class SongsFragment :
|
|||
adapter?.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (adapter?.dataSet.isNullOrEmpty())
|
||||
songPresenter.loadSongs()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
songPresenter.detachView()
|
||||
}
|
||||
|
||||
override fun showEmptyView() {
|
||||
adapter?.swapDataSet(ArrayList())
|
||||
}
|
||||
|
@ -122,7 +103,7 @@ class SongsFragment :
|
|||
}
|
||||
|
||||
override fun setSortOrder(sortOrder: String) {
|
||||
songPresenter.loadSongs()
|
||||
mainActivity.libraryViewModel.forceReload(ReloadType.Songs)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -131,10 +112,7 @@ class SongsFragment :
|
|||
|
||||
@JvmStatic
|
||||
fun newInstance(): SongsFragment {
|
||||
val args = Bundle()
|
||||
val fragment = SongsFragment()
|
||||
fragment.arguments = args
|
||||
return fragment
|
||||
return SongsFragment()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
package code.name.monkey.retromusic.fragments.songs
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.Result.Success
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.providers.RepositoryImpl
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class SongsViewModel(application: Application) : AndroidViewModel(application) {
|
||||
var songs = MutableLiveData<List<Song>>()
|
||||
|
||||
init {
|
||||
loadSongs()
|
||||
}
|
||||
|
||||
fun loadSongs() = viewModelScope.launch {
|
||||
val result = RepositoryImpl(getApplication()).allSongs()
|
||||
if (result is Success) {
|
||||
songs.value = result.data
|
||||
} else {
|
||||
songs.value = listOf()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,25 +8,16 @@
|
|||
android:orientation="vertical"
|
||||
android:padding="4dp">
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:id="@+id/imageContainer"
|
||||
<code.name.monkey.retromusic.views.RetroShapeableImageView
|
||||
android:id="@+id/image"
|
||||
android:layout_width="112dp"
|
||||
android:layout_height="112dp"
|
||||
app:cardCornerRadius="56dp"
|
||||
app:cardElevation="4dp"
|
||||
android:background="@android:color/transparent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/image"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/transparent"
|
||||
app:srcCompat="@drawable/default_artist_art"
|
||||
tools:src="@tools:sample/avatars" />
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:retroCornerSize="56dp"
|
||||
tools:src="@tools:sample/avatars" />
|
||||
|
||||
<com.google.android.material.textview.MaterialTextView
|
||||
android:id="@+id/title"
|
||||
|
@ -40,6 +31,6 @@
|
|||
android:textColor="?android:attr/textColorPrimary"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/imageContainer"
|
||||
app:layout_constraintTop_toBottomOf="@id/image"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -9,7 +9,6 @@
|
|||
android:padding="4dp"
|
||||
tools:ignore="MissingPrefix">
|
||||
|
||||
|
||||
<code.name.monkey.retromusic.views.CircularImageView
|
||||
android:id="@+id/image"
|
||||
android:layout_width="0dp"
|
||||
|
@ -24,13 +23,12 @@
|
|||
tools:ignore="ContentDescription"
|
||||
tools:src="@tools:sample/avatars" />
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
android:padding="12dp"
|
||||
android:gravity="center"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/image">
|
||||
|
|
Loading…
Reference in a new issue