Skip to content

Instantly share code, notes, and snippets.

@brainail
Created September 17, 2017 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainail/49d33ee38c51dfdaf8bb75c05b5a996d to your computer and use it in GitHub Desktop.
Save brainail/49d33ee38c51dfdaf8bb75c05b5a996d to your computer and use it in GitHub Desktop.
Some Kitsu UI stuff to display it properly
class KitsuViewHolder(parent :ViewGroup) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.kitsu_item, parent, false)) {
var item : KitsuItem? = null
fun bindTo(item : KitsuItem?) {
this.item = item
itemView.itemTypeView.text = item?.type?.capitalize()
?: "Ouhh..."
itemView.itemSubtypeView.text = item?.attributes?.subtype?.capitalize()
?: "Ouhhhhh..."
itemView.itemNameView.text = item?.attributes?.titles?.en_jp?.capitalize()
?: "Ouhhhhhhhh..."
itemView.itemSynopsisView.text = item?.attributes?.synopsis?.capitalize()
?: "Ouhhhhhhhhhhh...\nYou know what?\nThe quick brown fox jumps over the lazy dog!"
val imageUrl = item?.attributes?.posterImage?.small
if (null != imageUrl) {
itemView.itemCoverView.visibility = View.VISIBLE
Glide.with(itemView.context)
.load(imageUrl)
.apply(RequestOptions().placeholder(R.drawable.empty_placeholder))
.transition(DrawableTransitionOptions.withCrossFade())
.into(itemView.itemCoverView)
} else {
Glide.with(itemView.context).clear(itemView.itemCoverView)
itemView.itemCoverView.setImageResource(R.drawable.empty_placeholder)
}
}
}
class KitsuPagedListAdapter : PagedListAdapter<KitsuItem, KitsuViewHolder>(diffCallback) {
override fun onBindViewHolder(holder: KitsuViewHolder, position: Int) {
holder.bindTo(getItem(position))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KitsuViewHolder = KitsuViewHolder(parent)
companion object {
private val diffCallback = object : DiffCallback<KitsuItem>() {
override fun areItemsTheSame(oldItem: KitsuItem, newItem: KitsuItem): Boolean = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: KitsuItem, newItem: KitsuItem): Boolean = oldItem == newItem
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment