Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/Chat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package otus.gpb.recyclerview

import androidx.annotation.DrawableRes

data class Chat(
val id: Long, //ID
val title: String, //Название чата
val lastMessage: String, //Последнее сообщение
val lastWriter: String?, //Последний писатель сообщения
val avatar: String, //Аватар
val checkBox: Boolean, //Выделен чат или нет.
val lock: Boolean, //Приватный чат.
val scam: Boolean, //SCAM
val mute: Boolean, //Отключен звук
val verified: Boolean, //Проверенный аккаунт
val dateMessage: String, //Время сообщения
val messageCheck: Boolean, //Прочитано сообщение
val online: Boolean? = null//Онлайн. Только для пользователей.
)
96 changes: 96 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package otus.gpb.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import otus.gpb.recyclerview.ChatRepository.Companion.listChat

class ChatAdapter : ListAdapter<Chat, ChatAdapter.ChatItemViewHolder>(ChatDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatItemViewHolder {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.chat_item, parent, false)
return ChatItemViewHolder(view)
}

override fun onBindViewHolder(holder: ChatItemViewHolder, position: Int) {
holder.bind(getItem(position))
}

fun removeItem(position: Int) {
listChat.removeAt(position)
}

inner class ChatItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {

private val title = view.findViewById<TextView>(R.id.title)
private val nickNameMessage = view.findViewById<TextView>(R.id.nick_name_message)
private val textMessage = view.findViewById<TextView>(R.id.text_message)
private val lock = view.findViewById<ImageView>(R.id.lock_icon_image_view)
private val scam = view.findViewById<ImageView>(R.id.scam_image_view)
private val mute = view.findViewById<ImageView>(R.id.mute_image_view)
private val verified = view.findViewById<ImageView>(R.id.verified_image_view)
private val dateMessageAndCheck = view.findViewById<TextView>(R.id.date_message_and_check)
fun bind(chat: Chat) {
title.text = chat.title
nickNameMessage.text = chat.lastWriter
textMessage.text = chat.lastMessage
if (chat.lock) {
lock.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
} else {
lock.layoutParams.width = 0
}

if (chat.scam) {
scam.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
} else {
scam.layoutParams.width = 0
}

if (chat.mute) {
mute.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
} else {
mute.layoutParams.width = 0
}

if (chat.verified) {
verified.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
} else {
verified.layoutParams.width = 0
}

dateMessageAndCheck.text = chat.dateMessage

if (chat.messageCheck) {
val leftDrawable =
AppCompatResources.getDrawable(itemView.context, R.drawable.check);
dateMessageAndCheck.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);
} else{
val leftDrawable =
AppCompatResources.getDrawable(itemView.context, R.drawable.two_check);
dateMessageAndCheck.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);
}

if (chat.verified) {
verified.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
} else {
verified.layoutParams.width = 0
}
}
}
}

class ChatDiffCallback : DiffUtil.ItemCallback<Chat>() {

override fun areItemsTheSame(oldItem: Chat, newItem: Chat): Boolean =
oldItem.id == newItem.id

override fun areContentsTheSame(oldItem: Chat, newItem: Chat): Boolean =
oldItem == newItem
}
29 changes: 29 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package otus.gpb.recyclerview

class ChatRepository {

companion object {
var listChat = mutableListOf(
Chat(1, "Chat1", "Message1", "Writer1", "", false, false, false, false, true,"11:01", true),
Chat(2, "Chat2", "Message2", "Writer2", "", true, true, false, true, false,"11:02", false),
Chat(3, "Chat3", "Message3", "Writer3", "", false, false, false, false, false,"11:03", true),
Chat(4, "Chat4", "Message4", "Writer4", "", true, false, true, false, true,"11:04", true),
Chat(5, "Chat5", "Message5", "Writer5", "", true, true, false, false, true,"11:05", true),
Chat(6, "Chat6", "Message6", "Writer6", "", false, false, false, true, true,"11:06", false),
Chat(7, "Chat7", "Message7", "Writer7", "", true, false, false, false, false,"11:07", true),
Chat(8, "Chat8", "Message8", "Writer8", "", false, true, false, true, false,"11:08", true),
Chat(9, "Chat9", "Message9", "Writer9", "", true, false, true, false, true,"11:09", true),
Chat(10, "Chat10", "Message10", "Writer10", "", false, false, false, true, true,"11:10", true),
Chat(11, "Chat11", "Message11", "Writer11", "", false, false, false, false, true,"11:11", true),
Chat(12, "Chat12", "Message12", "Writer12", "", true, true, false, true, false,"11:12", true),
Chat(13, "Chat13", "Message13", "Writer13", "", false, false, false, false, false,"11:13", true),
Chat(14, "Chat14", "Message14", "Writer14", "", true, false, true, false, true,"11:14", false),
Chat(15, "Chat15", "Message15", "Writer15", "", true, true, false, false, true,"11:15", true),
Chat(16, "Chat16", "Message16", "Writer16", "", false, false, false, true, true,"11:16", false),
Chat(17, "Chat17", "Message17", "Writer17", "", true, false, false, false, false,"11:17", true),
Chat(18, "Chat18", "Message18", "Writer18", "", false, true, false, true, false,"11:18", true),
Chat(19, "Chat19", "Message19", "Writer19", "", true, false, true, false, true,"11:19", true),
Chat(20, "Chat20", "Message20", "Writer20", "", false, false, false, true, true,"11:20", true)
)
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,65 @@ package otus.gpb.recyclerview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import otus.gpb.recyclerview.ChatRepository.Companion.listChat

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
val chatAdapter = ChatAdapter()
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = chatAdapter
recyclerView.addItemDecoration(RecyclerViewItemDecoration(this, R.drawable.divider))

chatAdapter.submitList(listChat)

val button: Button = findViewById(R.id.add_button)
button.setOnClickListener {
listChat.add(
Chat(
55,
"Chat55",
"Message55",
"Writer55",
"",
false,
false,
false,
false,
true,
"11:55",
true,
)
)
chatAdapter.notifyDataSetChanged()
}

val itemTouchHelper =
ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
return false
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
chatAdapter.removeItem(position)
chatAdapter.notifyDataSetChanged()
}
})

itemTouchHelper.attachToRecyclerView(recyclerView)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package otus.gpb.recyclerview

import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView

class RecyclerViewItemDecoration(
context: Context,
resId: Int
) : RecyclerView.ItemDecoration() {

private var mDivider: Drawable = ContextCompat.getDrawable(context, resId)!!

override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDraw(c, parent, state)

val dividerLeft = 32

val dividerRight = parent.width - 32

for (i in 0 until parent.childCount) {

val childAt = parent.getChildAt(i)
val params = childAt.layoutParams as RecyclerView.LayoutParams

val dividerTop: Int = childAt.bottom + params.bottomMargin
val dividerBottom: Int = dividerTop + mDivider.intrinsicHeight

mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom)
mDivider.draw(c)
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/action_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="59dp"
android:height="59dp"
android:viewportWidth="59"
android:viewportHeight="59">
<path
android:pathData="M29.5,28.5m-26.5,0a26.5,26.5 0,1 1,53 0a26.5,26.5 0,1 1,-53 0"
android:fillColor="#51AEE7"/>
<path
android:pathData="M38.675,22.491C39.108,22.058 39.108,21.336 38.675,20.925L36.075,18.325C35.664,17.892 34.942,17.892 34.509,18.325L32.465,20.358L36.631,24.524L38.675,22.491ZM19,33.834V38H23.166L35.453,25.702L31.287,21.536L19,33.834Z"
android:fillColor="#ffffff"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/check.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="14dp"
android:height="10dp"
android:viewportWidth="14"
android:viewportHeight="10">
<path
android:pathData="M1,5.909L4.29,9L13,1"
android:strokeLineJoin="round"
android:strokeWidth="1.75926"
android:fillColor="#00000000"
android:strokeColor="#48A938"
android:strokeLineCap="round"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/divider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="2dp" />
<solid android:color="@color/devider" />
</shape>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/lock.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="10dp"
android:height="14dp"
android:viewportWidth="10"
android:viewportHeight="14">
<path
android:pathData="M10,7.557V12.386C10,13.001 9.537,13.5 8.966,13.5H1.034C0.463,13.5 0,13.001 0,12.386V7.557C0,6.942 0.463,6.443 1.034,6.443H1.552V4.214C1.552,2.976 2.241,0.5 5,0.5C7.759,0.5 8.448,2.976 8.448,4.214V6.443H8.966C9.537,6.443 10,6.942 10,7.557ZM7.069,4.214V6.443H2.931V4.214C2.931,3.471 3.345,1.986 5,1.986C6.655,1.986 7.069,3.471 7.069,4.214Z"
android:fillColor="#47A938"
android:fillType="evenOdd"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/mute.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="9dp"
android:height="12dp"
android:viewportWidth="9"
android:viewportHeight="12">
<path
android:pathData="M0,4.434C0,4.154 0.212,3.927 0.472,3.927H1.283L6.142,9.439V10.791C6.142,11.482 5.67,11.742 5.197,11.235L2.097,8.332H0.472C0.212,8.332 0,8.104 0,7.825V4.434Z"
android:fillColor="#BDC1C4"
android:fillType="evenOdd"/>
<path
android:pathData="M0.563,0.502C0.477,0.519 0.397,0.561 0.331,0.624C0.266,0.687 0.218,0.769 0.194,0.859C0.169,0.95 0.169,1.046 0.192,1.137C0.216,1.228 0.263,1.31 0.327,1.374L8.16,9.785C8.202,9.84 8.254,9.885 8.313,9.917C8.372,9.95 8.437,9.968 8.503,9.972C8.569,9.976 8.635,9.964 8.697,9.939C8.759,9.913 8.815,9.874 8.862,9.824C8.909,9.774 8.945,9.713 8.969,9.647C8.992,9.581 9.003,9.51 8.999,9.439C8.996,9.368 8.979,9.298 8.948,9.235C8.918,9.171 8.876,9.116 8.825,9.071L6.942,7.05V1.515C6.942,0.534 6.03,0.544 5.365,1.15L3.222,3.056L0.992,0.66C0.943,0.604 0.883,0.561 0.817,0.534C0.75,0.506 0.679,0.495 0.608,0.502C0.593,0.501 0.578,0.501 0.563,0.502Z"
android:fillColor="#BDC1C4"/>
</vector>
Loading