Fix home crashing

This commit is contained in:
h4h13 2019-10-12 13:43:33 +05:30
parent df8a9c447e
commit 613f6845ca
3 changed files with 55 additions and 52 deletions

View file

@ -15,20 +15,27 @@ import code.name.monkey.retromusic.adapter.song.SongAdapter
import code.name.monkey.retromusic.loaders.PlaylistSongsLoader import code.name.monkey.retromusic.loaders.PlaylistSongsLoader
import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.model.Home
import code.name.monkey.retromusic.model.Playlist import code.name.monkey.retromusic.model.Playlist
import code.name.monkey.retromusic.providers.interfaces.Repository
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
import com.google.android.material.textview.MaterialTextView import com.google.android.material.textview.MaterialTextView
class HomeAdapter( class HomeAdapter(
private val activity: AppCompatActivity, private val activity: AppCompatActivity,
private var homes: List<Home>, private val displayMetrics: DisplayMetrics,
private val displayMetrics: DisplayMetrics private val repository: Repository
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun getItemViewType(position: Int): Int { override fun getItemViewType(position: Int): Int {
return homes[position].homeSection return when (position) {
0 -> TOP_ARTISTS
1 -> TOP_ALBUMS
2 -> RECENT_ARTISTS
3 -> RECENT_ALBUMS
4 -> PLAYLISTS
else -> -1
}
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
@ -43,87 +50,92 @@ class HomeAdapter(
} }
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val home = homes[position] println(getItemViewType(position))
when (getItemViewType(position)) { when (getItemViewType(position)) {
RECENT_ALBUMS -> {
RECENT_ALBUMS, TOP_ALBUMS -> {
val viewHolder = holder as AlbumViewHolder val viewHolder = holder as AlbumViewHolder
viewHolder.bindView(home) viewHolder.bindView(repository.recentAlbums(), R.string.recent_albums, R.string.recent_added_albums)
} }
RECENT_ARTISTS, TOP_ARTISTS -> { TOP_ALBUMS -> {
val viewHolder = holder as AlbumViewHolder
viewHolder.bindView(repository.topAlbums(), R.string.top_albums, R.string.most_played_albums)
}
RECENT_ARTISTS -> {
val viewHolder = holder as ArtistViewHolder val viewHolder = holder as ArtistViewHolder
viewHolder.bindView(home) viewHolder.bindView(repository.recentArtists(), R.string.recent_artists, R.string.recent_added_artists)
}
TOP_ARTISTS -> {
val viewHolder = holder as ArtistViewHolder
viewHolder.bindView(repository.recentArtists(), R.string.top_artists, R.string.most_played_artists)
} }
PLAYLISTS -> { PLAYLISTS -> {
val viewHolder = holder as PlaylistViewHolder val viewHolder = holder as PlaylistViewHolder
viewHolder.bindView(home) viewHolder.bindView(repository.favoritePlaylist, R.string.favorites, R.string.favorites_songs)
} }
} }
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
return homes.size return 5
}
fun swapData(finalList: List<Home>) {
homes = finalList
notifyDataSetChanged()
} }
companion object { companion object {
@IntDef(RECENT_ALBUMS, TOP_ALBUMS, RECENT_ARTISTS, TOP_ARTISTS, GENRES, PLAYLISTS) @IntDef(RECENT_ALBUMS, TOP_ALBUMS, RECENT_ARTISTS, TOP_ARTISTS, PLAYLISTS)
@Retention(AnnotationRetention.SOURCE) @Retention(AnnotationRetention.SOURCE)
annotation class HomeSection annotation class HomeSection
const val RECENT_ALBUMS = 0 const val RECENT_ALBUMS = 3
const val TOP_ALBUMS = 1 const val TOP_ALBUMS = 1
const val RECENT_ARTISTS = 2 const val RECENT_ARTISTS = 2
const val TOP_ARTISTS = 3 const val TOP_ARTISTS = 0
const val GENRES = 4 const val PLAYLISTS = 4
const val PLAYLISTS = 5
} }
private inner class AlbumViewHolder(view: View) : AbsHomeViewItem(view) { private inner class AlbumViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(home: Home) { fun bindView(list: ArrayList<Album>, titleRes: Int, subtitleRes: Int) {
recyclerView.apply { recyclerView.apply {
adapter = AlbumFullWidthAdapter(activity, home.arrayList as ArrayList<Album>, displayMetrics) adapter = AlbumFullWidthAdapter(activity, list, displayMetrics)
} }
title.text = activity.getString(home.title) title.text = activity.getString(titleRes)
text.text = activity.getString(home.subTitle) text.text = activity.getString(subtitleRes)
} }
} }
private inner class ArtistViewHolder(view: View) : AbsHomeViewItem(view) { inner class ArtistViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(home: Home) { fun bindView(list: ArrayList<Artist>, titleRes: Int, subtitleRes: Int) {
recyclerView.apply { recyclerView.apply {
layoutManager = GridLayoutManager(activity, 1, GridLayoutManager.HORIZONTAL, false) layoutManager = GridLayoutManager(activity, 1, GridLayoutManager.HORIZONTAL, false)
val artistAdapter = ArtistAdapter(activity, home.arrayList as ArrayList<Artist>, val artistAdapter = ArtistAdapter(activity, list,
PreferenceUtil.getInstance(activity).getHomeGridStyle(activity), false, null) PreferenceUtil.getInstance(activity).getHomeGridStyle(activity), false, null)
adapter = artistAdapter adapter = artistAdapter
} }
title.text = activity.getString(home.title) title.text = activity.getString(titleRes)
text.text = activity.getString(home.subTitle) text.text = activity.getString(subtitleRes)
} }
} }
private inner class PlaylistViewHolder(view: View) : AbsHomeViewItem(view) { private inner class PlaylistViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(home: Home) { fun bindView(arrayList: ArrayList<Playlist>, titleRes: Int, subtitleRes: Int) {
val songs = PlaylistSongsLoader.getPlaylistSongList(activity, home.arrayList[0] as Playlist) if (arrayList.isNotEmpty()) {
recyclerView.apply { val songs = PlaylistSongsLoader.getPlaylistSongList(activity, arrayList[0])
val songAdapter = SongAdapter(activity, songs, R.layout.item_album_card, false, null) recyclerView.apply {
layoutManager = GridLayoutManager(activity, 1, GridLayoutManager.HORIZONTAL, false) val songAdapter = SongAdapter(activity, songs, R.layout.item_album_card, false, null)
adapter = songAdapter layoutManager = GridLayoutManager(activity, 1, GridLayoutManager.HORIZONTAL, false)
adapter = songAdapter
}
title.text = activity.getString(titleRes)
text.text = activity.getString(subtitleRes)
} }
title.text = activity.getString(home.title)
text.text = activity.getString(home.subTitle)
} }
} }
private open inner class AbsHomeViewItem(itemView: View) : RecyclerView.ViewHolder(itemView) { open inner class AbsHomeViewItem(itemView: View) : RecyclerView.ViewHolder(itemView) {
val recyclerView: RecyclerView = itemView.findViewById(R.id.recyclerView) val recyclerView: RecyclerView = itemView.findViewById(R.id.recyclerView)
val title: MaterialTextView = itemView.findViewById(R.id.title) val title: MaterialTextView = itemView.findViewById(R.id.title)
val text: MaterialTextView = itemView.findViewById(R.id.text) val text: MaterialTextView = itemView.findViewById(R.id.text)

View file

@ -28,8 +28,8 @@ 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.mvp.presenter.HomeView
import code.name.monkey.retromusic.providers.interfaces.Repository
import code.name.monkey.retromusic.util.Compressor import code.name.monkey.retromusic.util.Compressor
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
@ -48,17 +48,14 @@ import javax.inject.Inject
class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallbacks, HomeView { class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallbacks, HomeView {
private lateinit var homeAdapter: HomeAdapter private lateinit var homeAdapter: HomeAdapter
@Inject @Inject
lateinit var homePresenter: HomePresenter lateinit var repository: Repository
private var disposable: CompositeDisposable = CompositeDisposable() private var disposable: CompositeDisposable = CompositeDisposable()
private lateinit var toolbar: Toolbar private lateinit var toolbar: Toolbar
override fun sections(sections: ArrayList<Home>) { override fun sections(sections: ArrayList<Home>) {
val finalList = sections.sortedWith(compareBy { it.priority }) val finalList = sections.sortedWith(compareBy { it.priority })
homeAdapter.swapData(finalList)
if (sections.isEmpty()) { if (sections.isEmpty()) {
showEmptyView() showEmptyView()
@ -67,7 +64,6 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
} }
} }
override fun onCreateView(inflater: LayoutInflater, viewGroup: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, viewGroup: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(if (PreferenceUtil.getInstance(requireContext()).isHomeBanner) R.layout.fragment_banner_home else R.layout.fragment_home, viewGroup, false) return inflater.inflate(if (PreferenceUtil.getInstance(requireContext()).isHomeBanner) R.layout.fragment_banner_home else R.layout.fragment_home, viewGroup, false)
} }
@ -137,10 +133,7 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
titleWelcome.text = String.format("%s", PreferenceUtil.getInstance(requireContext()).userName) titleWelcome.text = String.format("%s", PreferenceUtil.getInstance(requireContext()).userName)
App.musicComponent.inject(this) App.musicComponent.inject(this)
homePresenter.attachView(this) homeAdapter = HomeAdapter(mainActivity, displayMetrics, repository)
homePresenter.loadSections()
homeAdapter = HomeAdapter(mainActivity, ArrayList(), displayMetrics)
recyclerView.apply { recyclerView.apply {
layoutManager = LinearLayoutManager(mainActivity) layoutManager = LinearLayoutManager(mainActivity)
@ -183,7 +176,6 @@ class BannerHomeFragment : AbsMainActivityFragment(), MainActivityFragmentCallba
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
disposable.dispose() disposable.dispose()
homePresenter.detachView()
} }
override fun showEmptyView() { override fun showEmptyView() {

View file

@ -60,7 +60,6 @@
android:id="@+id/recyclerView" android:id="@+id/recyclerView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:nestedScrollingEnabled="false" android:nestedScrollingEnabled="false"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"