Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.03 KB | None | 0 0
  1. package ru.skillbranch.devintensive.ui.adapters
  2.  
  3. import android.content.Intent
  4. import android.view.View
  5. import com.bumptech.glide.Glide
  6. import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
  7. import kotlinx.android.synthetic.main.item_chat_archive.*
  8. import kotlinx.android.synthetic.main.item_chat_group.*
  9. import kotlinx.android.synthetic.main.item_chat_single.*
  10. import ru.skillbranch.devintensive.R
  11. import ru.skillbranch.devintensive.models.data.ChatItem
  12. import ru.skillbranch.devintensive.models.data.ChatType
  13. import ru.skillbranch.devintensive.ui.archive.ArchiveActivity
  14.  
  15. fun singleChatAdapterDelegate(itemClickListener: (ChatItem) -> Unit) =
  16.     adapterDelegateLayoutContainer(
  17.         R.layout.item_chat_single,
  18.         on = { item: ChatItem, _, _ -> item.chatType == ChatType.SINGLE }
  19.     ) {
  20.         bind { diffPayloads ->
  21.             if (item.avatar == null) {
  22.                 Glide.with(itemView).clear(iv_avatar_single)
  23.                 iv_avatar_single.setInitials(item.initials)
  24.             } else {
  25.                 Glide.with(itemView).load(item.avatar).into(iv_avatar_single)
  26.             }
  27.  
  28.             sv_indicator.visibility = if (item.isOnline) View.VISIBLE else View.GONE
  29.             tv_title_single.text = item.title
  30.             tv_message_single.text = item.shortDescription
  31.  
  32.             with(tv_date_single) {
  33.                 visibility = if (item.lastMessageDate != null) View.VISIBLE else View.GONE
  34.                 text = item.lastMessageDate
  35.             }
  36.             with(tv_counter_single) {
  37.                 visibility = if (item.messageCount > 0) View.VISIBLE else View.GONE
  38.                 text = item.messageCount.toString()
  39.             }
  40.             itemView.setOnClickListener {
  41.                 itemClickListener(item)
  42.             }
  43.         }
  44.     }
  45.  
  46. fun groupChatAdapterDelegate(itemClickListener: (ChatItem) -> Unit) =
  47.     adapterDelegateLayoutContainer(
  48.         R.layout.item_chat_group,
  49.         on = { item: ChatItem, _, _ -> item.chatType == ChatType.GROUP }
  50.     ) {
  51.         bind { diffPayloads ->
  52.             iv_avatar_group.setInitials(item.title[0].toString())
  53.  
  54.             with(tv_date_group) {
  55.                 visibility = if (item.lastMessageDate != null) View.VISIBLE else View.GONE
  56.                 text = item.lastMessageDate
  57.             }
  58.             with(tv_counter_group) {
  59.                 visibility = if (item.messageCount > 0) View.VISIBLE else View.GONE
  60.                 text = item.messageCount.toString()
  61.             }
  62.             tv_title_group.text = item.title
  63.             tv_message_group.text = item.shortDescription
  64.             with(tv_message_author) {
  65.                 visibility = if (item.author != null) View.VISIBLE else View.GONE
  66.                 text = context.resources.getString(R.string.at_name, item.author)
  67.             }
  68.             itemView.setOnClickListener {
  69.                 itemClickListener(item)
  70.             }
  71.         }
  72.     }
  73.  
  74. fun archiveAdapterDelegate() =
  75.     adapterDelegateLayoutContainer(
  76.         R.layout.item_chat_archive,
  77.         on = { item: ChatItem, _, _ -> item.chatType == ChatType.ARCHIVE }
  78.     ) {
  79.         bind { diffPayloads ->
  80.             with(tv_date_archive) {
  81.                 visibility = if (item.lastMessageDate != null) View.VISIBLE else View.GONE
  82.                 text = item.lastMessageDate
  83.             }
  84.             with(tv_counter_archive) {
  85.                 visibility = if (item.messageCount > 0) View.VISIBLE else View.GONE
  86.                 text = item.messageCount.toString()
  87.             }
  88.             tv_message_archive.text = item.shortDescription
  89.             with(tv_message_author_archive) {
  90.                 visibility = if (!item.shortDescription.isNullOrEmpty()) View.VISIBLE else View.GONE
  91.                 text = context.resources.getString(R.string.at_name, item.author)
  92.             }
  93.             itemView.setOnClickListener {
  94.                 val intent = Intent(itemView.context, ArchiveActivity::class.java)
  95.                 itemView.context.startActivity(intent)
  96.             }
  97.         }
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement