Separated Suggestions from Home sections
This commit is contained in:
parent
75cdf4d57f
commit
80f3926210
9 changed files with 114 additions and 122 deletions
|
@ -15,12 +15,10 @@
|
|||
package code.name.monkey.retromusic.adapter
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.SystemClock
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.widget.AppCompatTextView
|
||||
import androidx.core.os.bundleOf
|
||||
|
@ -30,30 +28,22 @@ import androidx.navigation.fragment.FragmentNavigatorExtras
|
|||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import code.name.monkey.appthemehelper.ThemeStore
|
||||
import code.name.monkey.appthemehelper.util.ColorUtil
|
||||
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.fragments.home.HomeFragment
|
||||
import code.name.monkey.retromusic.glide.GlideApp
|
||||
import code.name.monkey.retromusic.glide.RetroGlideExtension
|
||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||
import code.name.monkey.retromusic.interfaces.IAlbumClickListener
|
||||
import code.name.monkey.retromusic.interfaces.IArtistClickListener
|
||||
import code.name.monkey.retromusic.interfaces.IGenreClickListener
|
||||
import code.name.monkey.retromusic.model.*
|
||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
|
||||
class HomeAdapter(
|
||||
private val activity: AppCompatActivity
|
||||
) : RecyclerView.Adapter<RecyclerView.ViewHolder>(), IArtistClickListener, IAlbumClickListener,
|
||||
IGenreClickListener {
|
||||
|
||||
private var mLastClickTime: Long = 0
|
||||
|
||||
private var list = listOf<Home>()
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
|
@ -68,13 +58,7 @@ class HomeAdapter(
|
|||
FAVOURITES -> PlaylistViewHolder(layout)
|
||||
TOP_ALBUMS, RECENT_ALBUMS -> AlbumViewHolder(layout)
|
||||
else -> {
|
||||
SuggestionsViewHolder(
|
||||
LayoutInflater.from(activity).inflate(
|
||||
R.layout.item_suggestions,
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
ArtistViewHolder(layout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -126,10 +110,6 @@ class HomeAdapter(
|
|||
)
|
||||
}
|
||||
}
|
||||
SUGGESTIONS -> {
|
||||
val viewHolder = holder as SuggestionsViewHolder
|
||||
viewHolder.bindView(home)
|
||||
}
|
||||
FAVOURITES -> {
|
||||
val viewHolder = holder as PlaylistViewHolder
|
||||
viewHolder.bindView(home)
|
||||
|
@ -174,52 +154,6 @@ class HomeAdapter(
|
|||
}
|
||||
}
|
||||
|
||||
private inner class SuggestionsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
private val images = listOf(
|
||||
R.id.image1,
|
||||
R.id.image2,
|
||||
R.id.image3,
|
||||
R.id.image4,
|
||||
R.id.image5,
|
||||
R.id.image6,
|
||||
R.id.image7,
|
||||
R.id.image8
|
||||
)
|
||||
|
||||
fun bindView(home: Home) {
|
||||
val color = ThemeStore.accentColor(activity)
|
||||
itemView.findViewById<TextView>(R.id.message).apply {
|
||||
setTextColor(color)
|
||||
setOnClickListener {
|
||||
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
|
||||
return@setOnClickListener
|
||||
}
|
||||
mLastClickTime = SystemClock.elapsedRealtime()
|
||||
MusicPlayerRemote.playNext((home.arrayList as List<Song>).subList(0, 8))
|
||||
if (!MusicPlayerRemote.isPlaying) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
}
|
||||
}
|
||||
}
|
||||
itemView.findViewById<MaterialCardView>(R.id.card6).apply {
|
||||
setCardBackgroundColor(ColorUtil.withAlpha(color, 0.12f))
|
||||
}
|
||||
images.forEachIndexed { index, id ->
|
||||
itemView.findViewById<View>(id).setOnClickListener {
|
||||
MusicPlayerRemote.playNext(home.arrayList[index] as Song)
|
||||
if (!MusicPlayerRemote.isPlaying) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
}
|
||||
}
|
||||
GlideApp.with(activity)
|
||||
.asBitmap()
|
||||
.songCoverOptions(home.arrayList[index] as Song)
|
||||
.load(RetroGlideExtension.getSongModel(home.arrayList[index] as Song))
|
||||
.into(itemView.findViewById(id))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private inner class PlaylistViewHolder(view: View) : AbsHomeViewItem(view) {
|
||||
fun bindView(home: Home) {
|
||||
title.setText(home.titleRes)
|
||||
|
|
|
@ -40,6 +40,7 @@ class LibraryViewModel(
|
|||
|
||||
private val _paletteColor = MutableLiveData<Int>()
|
||||
private val home = MutableLiveData<List<Home>>()
|
||||
private val suggestions = MutableLiveData<List<Song>>()
|
||||
private val albums = MutableLiveData<List<Album>>()
|
||||
private val songs = MutableLiveData<List<Song>>()
|
||||
private val artists = MutableLiveData<List<Artist>>()
|
||||
|
@ -56,6 +57,7 @@ class LibraryViewModel(
|
|||
|
||||
private fun loadLibraryContent() = viewModelScope.launch(IO) {
|
||||
fetchHomeSections()
|
||||
fetchSuggestions()
|
||||
fetchSongs()
|
||||
fetchAlbums()
|
||||
fetchArtists()
|
||||
|
@ -93,6 +95,10 @@ class LibraryViewModel(
|
|||
return home
|
||||
}
|
||||
|
||||
fun getSuggestions(): LiveData<List<Song>> {
|
||||
return suggestions
|
||||
}
|
||||
|
||||
fun getFabMargin(): LiveData<Int> {
|
||||
return fabMargin
|
||||
}
|
||||
|
@ -132,6 +138,10 @@ class LibraryViewModel(
|
|||
home.postValue(repository.homeSections())
|
||||
}
|
||||
|
||||
private suspend fun fetchSuggestions() {
|
||||
suggestions.postValue(repository.suggestions())
|
||||
}
|
||||
|
||||
fun search(query: String?, filter: Filter) {
|
||||
viewModelScope.launch(IO) {
|
||||
val result = repository.search(query, filter)
|
||||
|
@ -147,6 +157,7 @@ class LibraryViewModel(
|
|||
HomeSections -> fetchHomeSections()
|
||||
Playlists -> fetchPlaylists()
|
||||
Genres -> fetchGenres()
|
||||
Suggestions -> fetchSuggestions()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -391,4 +402,5 @@ enum class ReloadType {
|
|||
HomeSections,
|
||||
Playlists,
|
||||
Genres,
|
||||
Suggestions
|
||||
}
|
||||
|
|
|
@ -28,4 +28,5 @@ class HomeBindingAdapter(
|
|||
?: bannerHomeBinding?.homeContent?.recyclerView!!
|
||||
val titleWelcome = homeBinding?.titleWelcome ?: bannerHomeBinding?.titleWelcome!!
|
||||
val appNameText = homeBinding?.appNameText ?: bannerHomeBinding?.appNameText!!
|
||||
val suggestions = homeBinding?.homeContent?.suggestions ?: bannerHomeBinding?.homeContent?.suggestions!!
|
||||
}
|
|
@ -28,7 +28,9 @@ import androidx.core.view.doOnPreDraw
|
|||
import androidx.navigation.fragment.FragmentNavigatorExtras
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import code.name.monkey.appthemehelper.ThemeStore
|
||||
import code.name.monkey.appthemehelper.common.ATHToolbarActivity
|
||||
import code.name.monkey.appthemehelper.util.ColorUtil
|
||||
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
|
||||
import code.name.monkey.retromusic.*
|
||||
import code.name.monkey.retromusic.adapter.HomeAdapter
|
||||
|
@ -43,7 +45,9 @@ import code.name.monkey.retromusic.fragments.ReloadType
|
|||
import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment
|
||||
import code.name.monkey.retromusic.glide.GlideApp
|
||||
import code.name.monkey.retromusic.glide.RetroGlideExtension
|
||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||
import code.name.monkey.retromusic.interfaces.IScrollHelper
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||
import com.google.android.gms.cast.framework.CastButtonFactory
|
||||
import com.google.android.material.shape.MaterialShapeDrawable
|
||||
|
@ -76,6 +80,9 @@ class HomeFragment :
|
|||
libraryViewModel.getHome().observe(viewLifecycleOwner, {
|
||||
homeAdapter.swapData(it)
|
||||
})
|
||||
libraryViewModel.getSuggestions().observe(viewLifecycleOwner, {
|
||||
loadSuggestions(it)
|
||||
})
|
||||
|
||||
loadProfile()
|
||||
setupTitle()
|
||||
|
@ -151,6 +158,8 @@ class HomeFragment :
|
|||
)
|
||||
)
|
||||
}
|
||||
// Reload suggestions
|
||||
binding.suggestions.refreshButton.setOnClickListener { libraryViewModel.forceReload(ReloadType.Suggestions) }
|
||||
}
|
||||
|
||||
private fun getBinding(homeBanner: Boolean, view: View): HomeBindingAdapter {
|
||||
|
@ -235,6 +244,47 @@ class HomeFragment :
|
|||
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Y, false)
|
||||
}
|
||||
|
||||
private fun loadSuggestions(songs: List<Song>) {
|
||||
val images = listOf(
|
||||
binding.suggestions.image1,
|
||||
binding.suggestions.image2,
|
||||
binding.suggestions.image3,
|
||||
binding.suggestions.image4,
|
||||
binding.suggestions.image5,
|
||||
binding.suggestions.image6,
|
||||
binding.suggestions.image7,
|
||||
binding.suggestions.image8
|
||||
)
|
||||
val color = ThemeStore.accentColor(requireContext())
|
||||
binding.suggestions.message.apply {
|
||||
setTextColor(color)
|
||||
setOnClickListener {
|
||||
it.isClickable = false
|
||||
it.postDelayed({ it.isClickable = true }, 500)
|
||||
MusicPlayerRemote.playNext(songs.subList(0, 8))
|
||||
if (!MusicPlayerRemote.isPlaying) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
}
|
||||
}
|
||||
}
|
||||
binding.suggestions.card6.setCardBackgroundColor(ColorUtil.withAlpha(color, 0.12f))
|
||||
images.forEachIndexed { index, imageView ->
|
||||
imageView.setOnClickListener {
|
||||
it.isClickable = false
|
||||
it.postDelayed({ it.isClickable = true }, 500)
|
||||
MusicPlayerRemote.playNext(songs)
|
||||
if (!MusicPlayerRemote.isPlaying) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
}
|
||||
}
|
||||
GlideApp.with(this)
|
||||
.asBitmap()
|
||||
.songCoverOptions(songs[index])
|
||||
.load(RetroGlideExtension.getSongModel(songs[index]))
|
||||
.into(imageView)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
const val TAG: String = "BannerHomeFragment"
|
||||
|
|
|
@ -69,6 +69,7 @@ interface Repository {
|
|||
suspend fun recentAlbumsHome(): Home
|
||||
suspend fun favoritePlaylistHome(): Home
|
||||
suspend fun suggestionsHome(): Home
|
||||
suspend fun suggestions(): List<Song>
|
||||
suspend fun genresHome(): Home
|
||||
suspend fun playlists(): Home
|
||||
suspend fun homeSections(): List<Home>
|
||||
|
@ -240,19 +241,15 @@ class RealRepository(
|
|||
override suspend fun homeSections(): List<Home> {
|
||||
val homeSections = mutableListOf<Home>()
|
||||
val sections: List<Home> = listOf(
|
||||
suggestionsHome(),
|
||||
topArtistsHome(),
|
||||
topAlbumsHome(),
|
||||
recentArtistsHome(),
|
||||
recentAlbumsHome(),
|
||||
favoritePlaylistHome()
|
||||
// genresHome()
|
||||
)
|
||||
for (section in sections) {
|
||||
if (section.arrayList.isNotEmpty()) {
|
||||
if (section.homeSection != SUGGESTIONS || PreferenceUtil.homeSuggestions) {
|
||||
homeSections.add(section)
|
||||
}
|
||||
homeSections.add(section)
|
||||
}
|
||||
}
|
||||
return homeSections
|
||||
|
@ -374,6 +371,13 @@ class RealRepository(
|
|||
return suggestions
|
||||
}
|
||||
|
||||
override suspend fun suggestions(): List<Song> {
|
||||
if (!PreferenceUtil.homeSuggestions) return listOf<Song>()
|
||||
return NotPlayedPlaylist().songs().shuffled().takeIf {
|
||||
it.size > 9
|
||||
} ?: emptyList()
|
||||
}
|
||||
|
||||
override suspend fun genresHome(): Home {
|
||||
val genres = genreRepository.genres().shuffled()
|
||||
return Home(genres, GENRES, R.string.genres)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue