Code refactor

This commit is contained in:
Hemanth S 2020-08-21 18:35:01 +05:30
parent b54c561070
commit f013cf296d
6 changed files with 44 additions and 39 deletions

View file

@ -129,6 +129,7 @@ dependencies {
implementation "com.afollestad.material-dialogs:core:$material_dialog_version"
implementation "com.afollestad.material-dialogs:commons:$material_dialog_version"
implementation 'com.afollestad:material-cab:0.1.12'
implementation 'com.afollestad:recyclical:1.1.1'
implementation 'com.github.bumptech.glide:glide:3.8.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:1.5.0'
@ -180,5 +181,8 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'
}

View file

@ -64,7 +64,7 @@ class HomeAdapter(
when (getItemViewType(position)) {
RECENT_ALBUMS -> {
val viewHolder = holder as AlbumViewHolder
viewHolder.bindView(home.arrayList as List<Album>, R.string.recent_albums)
viewHolder.bindView(home)
viewHolder.clickableArea.setOnClickListener {
activity.findNavController(R.id.fragment_container).navigate(
R.id.detailListFragment,
@ -74,7 +74,7 @@ class HomeAdapter(
}
TOP_ALBUMS -> {
val viewHolder = holder as AlbumViewHolder
viewHolder.bindView(home.arrayList as List<Album>, R.string.top_albums)
viewHolder.bindView(home)
viewHolder.clickableArea.setOnClickListener {
activity.findNavController(R.id.fragment_container).navigate(
R.id.detailListFragment,
@ -84,7 +84,7 @@ class HomeAdapter(
}
RECENT_ARTISTS -> {
val viewHolder = holder as ArtistViewHolder
viewHolder.bindView(home.arrayList, R.string.recent_artists)
viewHolder.bindView(home)
viewHolder.clickableArea.setOnClickListener {
activity.findNavController(R.id.fragment_container).navigate(
R.id.detailListFragment,
@ -94,7 +94,7 @@ class HomeAdapter(
}
TOP_ARTISTS -> {
val viewHolder = holder as ArtistViewHolder
viewHolder.bindView(home.arrayList, R.string.top_artists)
viewHolder.bindView(home)
viewHolder.clickableArea.setOnClickListener {
activity.findNavController(R.id.fragment_container).navigate(
R.id.detailListFragment,
@ -104,11 +104,11 @@ class HomeAdapter(
}
SUGGESTIONS -> {
val viewHolder = holder as SuggestionsViewHolder
viewHolder.bindView(home.arrayList)
viewHolder.bindView(home)
}
FAVOURITES -> {
val viewHolder = holder as PlaylistViewHolder
viewHolder.bindView(home.arrayList, R.string.favorites)
viewHolder.bindView(home)
viewHolder.clickableArea.setOnClickListener {
activity.findNavController(R.id.fragment_container).navigate(
R.id.detailListFragment,
@ -118,7 +118,7 @@ class HomeAdapter(
}
GENRES -> {
val viewHolder = holder as GenreViewHolder
viewHolder.bind(home.arrayList, R.string.genres)
viewHolder.bind(home)
}
PLAYLISTS -> {
@ -136,22 +136,22 @@ class HomeAdapter(
}
private inner class AlbumViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(albums: List<Album>, titleRes: Int) {
title.text = activity.getString(titleRes)
fun bindView(home: Home) {
title.setText(home.titleRes)
recyclerView.apply {
adapter = albumAdapter(albums)
adapter = albumAdapter(home.arrayList as List<Album>)
layoutManager = gridLayoutManager()
}
}
}
private inner class ArtistViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(artists: List<Any>, titleRes: Int) {
fun bindView(home: Home) {
title.setText(home.titleRes)
recyclerView.apply {
layoutManager = linearLayoutManager()
adapter = artistsAdapter(artists as List<Artist>)
adapter = artistsAdapter(home.arrayList as List<Artist>)
}
title.text = activity.getString(titleRes)
}
}
@ -167,8 +167,7 @@ class HomeAdapter(
R.id.image8
)
fun bindView(songs: List<Any>) {
songs as List<Song>
fun bindView(home: Home) {
val color = ThemeStore.accentColor(activity)
itemView.findViewById<TextView>(R.id.message).setTextColor(color)
itemView.findViewById<MaterialCardView>(R.id.card6).apply {
@ -176,9 +175,9 @@ class HomeAdapter(
}
images.forEachIndexed { index, id ->
itemView.findViewById<View>(id).setOnClickListener {
MusicPlayerRemote.playNext(songs[index])
MusicPlayerRemote.playNext(home.arrayList[index] as Song)
}
SongGlideRequest.Builder.from(Glide.with(activity), songs[index])
SongGlideRequest.Builder.from(Glide.with(activity), home.arrayList[index] as Song)
.asBitmap()
.build()
.into(itemView.findViewById(id))
@ -188,28 +187,28 @@ class HomeAdapter(
}
private inner class PlaylistViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(songs: List<Any>, titleRes: Int) {
fun bindView(home: Home) {
title.setText(home.titleRes)
recyclerView.apply {
val songAdapter = SongAdapter(
activity,
songs as MutableList<Song>,
home.arrayList as MutableList<Song>,
R.layout.item_album_card, null
)
layoutManager = linearLayoutManager()
adapter = songAdapter
}
title.text = activity.getString(titleRes)
}
}
private inner class GenreViewHolder(itemView: View) : AbsHomeViewItem(itemView) {
fun bind(genres: List<Any>, titleRes: Int) {
fun bind(home: Home) {
arrow.hide()
title.text = activity.getString(titleRes)
title.setText(home.titleRes)
recyclerView.apply {
layoutManager = GridLayoutManager(activity, 3, GridLayoutManager.HORIZONTAL, false)
val genreAdapter =
GenreAdapter(activity, genres as List<Genre>, R.layout.item_grid_genre)
GenreAdapter(activity, home.arrayList as List<Genre>, R.layout.item_grid_genre)
adapter = genreAdapter
}
}

View file

@ -14,10 +14,13 @@
package code.name.monkey.retromusic.model
import androidx.annotation.StringRes
import code.name.monkey.retromusic.HomeSection
class Home(
val arrayList: List<Any>,
@HomeSection
val homeSection: Int
val homeSection: Int,
@StringRes
val titleRes: Int
)

View file

@ -236,10 +236,6 @@ class RealRepository(
return homeSections
}
override suspend fun playlists(): Home {
val playlist = playlistRepository.playlists()
return Home(playlist, TOP_ALBUMS)
}
override suspend fun playlist(playlistId: Int) =
playlistRepository.playlist(playlistId)
@ -291,33 +287,37 @@ class RealRepository(
NotPlayedPlaylist().songs().shuffled().takeIf {
it.size > 9
} ?: emptyList()
return Home(songs, SUGGESTIONS)
return Home(songs, SUGGESTIONS, R.string.suggestion_songs)
}
override suspend fun genresHome(): Home {
val genres = genreRepository.genres().shuffled()
return Home(genres, GENRES)
return Home(genres, GENRES, R.string.genres)
}
override suspend fun playlists(): Home {
val playlist = playlistRepository.playlists()
return Home(playlist, PLAYLISTS, R.string.playlists)
}
override suspend fun recentArtistsHome(): Home {
val artists = lastAddedRepository.recentArtists().take(5)
return Home(artists, RECENT_ARTISTS)
return Home(artists, RECENT_ARTISTS, R.string.recent_artists)
}
override suspend fun recentAlbumsHome(): Home {
val albums = lastAddedRepository.recentAlbums().take(5)
return Home(albums, RECENT_ALBUMS)
return Home(albums, RECENT_ALBUMS, R.string.recent_albums)
}
override suspend fun topAlbumsHome(): Home {
val albums = playedTracksRepository.topAlbums().take(5)
return Home(albums, TOP_ALBUMS)
return Home(albums, TOP_ALBUMS, R.string.top_albums)
}
override suspend fun topArtistsHome(): Home {
val artists = playedTracksRepository.topArtists().take(5)
return Home(artists, TOP_ARTISTS)
return Home(artists, TOP_ARTISTS, R.string.top_artists)
}
override suspend fun favoritePlaylistHome(): Home {
@ -327,7 +327,7 @@ class RealRepository(
PlaylistSongsLoader.getPlaylistSongList(context, playlists[0])
else emptyList()
return Home(songs, FAVOURITES)
return Home(songs, FAVOURITES, R.string.favorites)
}
override fun songsFlow(): Flow<Result<List<Song>>> = flow {

View file

@ -39,9 +39,8 @@
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:overScrollMode="never"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="3"
tools:itemCount="3"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="@layout/item_album_card" />
<Space