Skip to content

Instantly share code, notes, and snippets.

@feresr
Created Oct 19, 2017
Embed
What would you like to do?
package com.feresr.wallpapers.wallpapers
class WallpapersAdapter(private val context: Context, private val inflater: LayoutInflater) : BaseAdapter<Wallpaper>() {
companion object {
val TYPE_WALLPAPER: Int = 0
val TYPE_LOADING: Int = 1
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, item: Wallpaper) {
if (holder is WallpaperViewHolder) {
GlideApp.with(context)
.load(item.thumbUrl)
.placeholder(ColorDrawable(Color.parseColor(item.color)))
.transition(DrawableTransitionOptions.withCrossFade())
.into(holder.wallpaperImageView)
if (favourites.contains(item)) {
holder.favouriteView.setImageResource(R.drawable.ic_heart_filled)
} else {
holder.favouriteView.setImageResource(R.drawable.ic_heart_outline)
}
listener?.apply {
holder.itemView.setOnClickListener {
onWallpaperSelected(item)
}
holder.favouriteView.setOnClickListener {
if (favourites.contains(item)) {
if (onRemoveFromFavouriteSelected(item)) {
holder.favouriteView.setImageResource(R.drawable.ic_heart_out)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
(holder.favouriteView.drawable as AnimatedVectorDrawable).start()
} else {
(holder.favouriteView.drawable as AnimatedVectorDrawableCompat).start()
}
}
} else {
holder.favouriteView.setImageResource(R.drawable.ic_heart_in)
onAddToFavouriteSelected(item)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
(holder.favouriteView.drawable as AnimatedVectorDrawable).start()
} else {
(holder.favouriteView.drawable as AnimatedVectorDrawableCompat).start()
}
}
}
}
}
}
private var listener: WallpaperClickedListener? = null
var favourites: Set<Wallpaper> = HashSet()
fun setWallpaperSelectedListener(listener: WallpaperClickedListener) {
this.listener = listener
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder =
when (viewType) {
TYPE_WALLPAPER -> WallpaperViewHolder(inflater.inflate(R.layout.row_wallpaper, parent, false))
TYPE_LOADING -> LoadingViewHolder(inflater.inflate(R.layout.progress_bar, parent, false))
else -> throw IllegalStateException("Unknown viewType")
}
private class LoadingViewHolder(view: View) : RecyclerView.ViewHolder(view)
class WallpaperViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val wallpaperImageView: ImageView = view.wallpaperImageView
val favouriteView: ImageView = view.favouriteView
}
interface WallpaperClickedListener {
fun onWallpaperSelected(wallpaper: Wallpaper)
fun onWallpaperLongPressed(wallpaper: Wallpaper)
fun onAddToFavouriteSelected(wallpaper: Wallpaper)
/*
* Return true if the heart favourite animation should be run, false otherwise
* */
fun onRemoveFromFavouriteSelected(wallpaper: Wallpaper): Boolean
}
}
abstract class BaseWallpapersFragment : BaseFragment<Explore.Intent, Explore.ViewState>(), BaseAdapter.PagerListener, WallpapersAdapter.WallpaperClickedListener {
private val NUMBER_OF_COLUMNS = 2
private val wallpapersAdapter: WallpapersAdapter by lazy {
//I use the applicationContext because this is a RetainedFragment
WallpapersAdapter(activity.applicationContext, activity.applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment