186 lines
6.2 KiB
Kotlin
186 lines
6.2 KiB
Kotlin
package code.name.monkey.retromusic.fragments
|
|
|
|
import android.os.Bundle
|
|
import android.view.View
|
|
import android.widget.ImageView
|
|
import androidx.lifecycle.Observer
|
|
import androidx.lifecycle.lifecycleScope
|
|
import androidx.navigation.fragment.navArgs
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import code.name.monkey.retromusic.*
|
|
import code.name.monkey.retromusic.adapter.album.AlbumAdapter
|
|
import code.name.monkey.retromusic.adapter.artist.ArtistAdapter
|
|
import code.name.monkey.retromusic.adapter.song.SongAdapter
|
|
import code.name.monkey.retromusic.db.toSong
|
|
import code.name.monkey.retromusic.fragments.albums.AlbumClickListener
|
|
import code.name.monkey.retromusic.fragments.artists.ArtistClickListener
|
|
import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment
|
|
import code.name.monkey.retromusic.model.Album
|
|
import code.name.monkey.retromusic.model.Artist
|
|
import code.name.monkey.retromusic.repository.RealRepository
|
|
import kotlinx.android.synthetic.main.fragment_playlist_detail.*
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.withContext
|
|
import org.koin.android.ext.android.inject
|
|
|
|
class DetailListFragment : AbsMainActivityFragment(R.layout.fragment_playlist_detail),
|
|
ArtistClickListener, AlbumClickListener {
|
|
private val args by navArgs<DetailListFragmentArgs>()
|
|
private val repository by inject<RealRepository>()
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
super.onActivityCreated(savedInstanceState)
|
|
mainActivity.setSupportActionBar(toolbar)
|
|
mainActivity.hideBottomBarVisibility(false)
|
|
progressIndicator.hide()
|
|
when (args.type) {
|
|
TOP_ARTISTS -> {
|
|
loadArtists(R.string.top_artists, TOP_ARTISTS)
|
|
}
|
|
RECENT_ARTISTS -> {
|
|
loadArtists(R.string.recent_artists, RECENT_ARTISTS)
|
|
}
|
|
TOP_ALBUMS -> {
|
|
loadAlbums(R.string.top_albums, TOP_ALBUMS)
|
|
}
|
|
RECENT_ALBUMS -> {
|
|
loadAlbums(R.string.recent_albums, RECENT_ALBUMS)
|
|
}
|
|
FAVOURITES -> loadFavorite()
|
|
HISTORY_PLAYLIST -> loadHistory()
|
|
LAST_ADDED_PLAYLIST -> lastAddedSongs()
|
|
TOP_PLAYED_PLAYLIST -> topPlayed()
|
|
}
|
|
}
|
|
|
|
private fun lastAddedSongs() {
|
|
toolbar.setTitle(R.string.last_added)
|
|
val songAdapter = SongAdapter(
|
|
requireActivity(),
|
|
mutableListOf(),
|
|
R.layout.item_list, null
|
|
)
|
|
recyclerView.apply {
|
|
adapter = songAdapter
|
|
layoutManager = linearLayoutManager()
|
|
}
|
|
lifecycleScope.launch(IO) {
|
|
val songs = repository.recentSongs()
|
|
withContext(Main) { songAdapter.swapDataSet(songs) }
|
|
}
|
|
}
|
|
|
|
private fun topPlayed() {
|
|
toolbar.setTitle(R.string.my_top_tracks)
|
|
val songAdapter = SongAdapter(
|
|
requireActivity(),
|
|
mutableListOf(),
|
|
R.layout.item_list, null
|
|
)
|
|
recyclerView.apply {
|
|
adapter = songAdapter
|
|
layoutManager = linearLayoutManager()
|
|
}
|
|
lifecycleScope.launch(IO) {
|
|
val songs = repository.recentSongs()
|
|
withContext(Main) { songAdapter.swapDataSet(songs) }
|
|
}
|
|
}
|
|
|
|
private fun loadHistory() {
|
|
toolbar.setTitle(R.string.history)
|
|
|
|
val songAdapter = SongAdapter(
|
|
requireActivity(),
|
|
mutableListOf(),
|
|
R.layout.item_list, null
|
|
)
|
|
recyclerView.apply {
|
|
adapter = songAdapter
|
|
layoutManager = linearLayoutManager()
|
|
}
|
|
repository.historySong().observe(viewLifecycleOwner, Observer {
|
|
val songs = it.map { historyEntity -> historyEntity.toSong() }
|
|
songAdapter.swapDataSet(songs)
|
|
})
|
|
}
|
|
|
|
private fun loadFavorite() {
|
|
toolbar.setTitle(R.string.favorites)
|
|
val songAdapter = SongAdapter(
|
|
requireActivity(),
|
|
mutableListOf(),
|
|
R.layout.item_list, null
|
|
)
|
|
recyclerView.apply {
|
|
adapter = songAdapter
|
|
layoutManager = linearLayoutManager()
|
|
}
|
|
repository.favorites().observe(viewLifecycleOwner, Observer {
|
|
println(it.size)
|
|
val songs = it.map { songEntity -> songEntity.toSong() }
|
|
songAdapter.swapDataSet(songs)
|
|
})
|
|
}
|
|
|
|
private fun loadArtists(title: Int, type: Int) {
|
|
toolbar.setTitle(title)
|
|
lifecycleScope.launch(IO) {
|
|
val artists =
|
|
if (type == TOP_ARTISTS) repository.topArtists() else repository.recentArtists()
|
|
withContext(Main) {
|
|
recyclerView.apply {
|
|
adapter = artistAdapter(artists)
|
|
layoutManager = gridLayoutManager()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun loadAlbums(title: Int, type: Int) {
|
|
toolbar.setTitle(title)
|
|
lifecycleScope.launch(IO) {
|
|
val albums =
|
|
if (type == TOP_ALBUMS) repository.topAlbums() else repository.recentAlbums()
|
|
withContext(Main) {
|
|
recyclerView.apply {
|
|
adapter = albumAdapter(albums)
|
|
layoutManager = gridLayoutManager()
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun artistAdapter(artists: List<Artist>): ArtistAdapter = ArtistAdapter(
|
|
requireActivity(),
|
|
artists,
|
|
R.layout.item_grid_circle,
|
|
null, this@DetailListFragment
|
|
)
|
|
|
|
private fun albumAdapter(albums: List<Album>): AlbumAdapter = AlbumAdapter(
|
|
requireActivity(),
|
|
albums,
|
|
R.layout.item_grid,
|
|
null, this@DetailListFragment
|
|
)
|
|
|
|
private fun linearLayoutManager(): LinearLayoutManager =
|
|
LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
|
|
|
|
private fun gridLayoutManager(): GridLayoutManager =
|
|
GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
|
|
|
|
|
|
override fun onArtist(artistId: Int, imageView: ImageView) {
|
|
|
|
}
|
|
|
|
override fun onAlbumClick(albumId: Int, view: View) {
|
|
|
|
}
|
|
} |