Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.skillbranch.devintensive.ui.adapters
- import android.content.Intent
- import android.view.View
- import com.bumptech.glide.Glide
- import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
- import kotlinx.android.synthetic.main.item_chat_archive.*
- import kotlinx.android.synthetic.main.item_chat_group.*
- import kotlinx.android.synthetic.main.item_chat_single.*
- import ru.skillbranch.devintensive.R
- import ru.skillbranch.devintensive.models.data.ChatItem
- import ru.skillbranch.devintensive.models.data.ChatType
- import ru.skillbranch.devintensive.ui.archive.ArchiveActivity
- fun singleChatAdapterDelegate(itemClickListener: (ChatItem) -> Unit) =
- adapterDelegateLayoutContainer(
- R.layout.item_chat_single,
- on = { item: ChatItem, _, _ -> item.chatType == ChatType.SINGLE }
- ) {
- bind { diffPayloads ->
- if (item.avatar == null) {
- Glide.with(itemView).clear(iv_avatar_single)
- iv_avatar_single.setInitials(item.initials)
- } else {
- Glide.with(itemView).load(item.avatar).into(iv_avatar_single)
- }
- sv_indicator.visibility = if (item.isOnline) View.VISIBLE else View.GONE
- tv_title_single.text = item.title
- tv_message_single.text = item.shortDescription
- with(tv_date_single) {
- visibility = if (item.lastMessageDate != null) View.VISIBLE else View.GONE
- text = item.lastMessageDate
- }
- with(tv_counter_single) {
- visibility = if (item.messageCount > 0) View.VISIBLE else View.GONE
- text = item.messageCount.toString()
- }
- itemView.setOnClickListener {
- itemClickListener(item)
- }
- }
- }
- fun groupChatAdapterDelegate(itemClickListener: (ChatItem) -> Unit) =
- adapterDelegateLayoutContainer(
- R.layout.item_chat_group,
- on = { item: ChatItem, _, _ -> item.chatType == ChatType.GROUP }
- ) {
- bind { diffPayloads ->
- iv_avatar_group.setInitials(item.title[0].toString())
- with(tv_date_group) {
- visibility = if (item.lastMessageDate != null) View.VISIBLE else View.GONE
- text = item.lastMessageDate
- }
- with(tv_counter_group) {
- visibility = if (item.messageCount > 0) View.VISIBLE else View.GONE
- text = item.messageCount.toString()
- }
- tv_title_group.text = item.title
- tv_message_group.text = item.shortDescription
- with(tv_message_author) {
- visibility = if (item.author != null) View.VISIBLE else View.GONE
- text = context.resources.getString(R.string.at_name, item.author)
- }
- itemView.setOnClickListener {
- itemClickListener(item)
- }
- }
- }
- fun archiveAdapterDelegate() =
- adapterDelegateLayoutContainer(
- R.layout.item_chat_archive,
- on = { item: ChatItem, _, _ -> item.chatType == ChatType.ARCHIVE }
- ) {
- bind { diffPayloads ->
- with(tv_date_archive) {
- visibility = if (item.lastMessageDate != null) View.VISIBLE else View.GONE
- text = item.lastMessageDate
- }
- with(tv_counter_archive) {
- visibility = if (item.messageCount > 0) View.VISIBLE else View.GONE
- text = item.messageCount.toString()
- }
- tv_message_archive.text = item.shortDescription
- with(tv_message_author_archive) {
- visibility = if (!item.shortDescription.isNullOrEmpty()) View.VISIBLE else View.GONE
- text = context.resources.getString(R.string.at_name, item.author)
- }
- itemView.setOnClickListener {
- val intent = Intent(itemView.context, ArchiveActivity::class.java)
- itemView.context.startActivity(intent)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement