Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw recyclerView #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding true
}
}

dependencies {
Expand Down
110 changes: 110 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,110 @@
package otus.gpb.recyclerview

class Chat(
val id: Int = 0,
val avatarImage: Int ,
val previewImage: Int? ,
val nickName: String = "NickName",
val group: String = "",
val message: String = "",
val lastTime: String = "Sun",
val messCount: Int = 0,
val check: Boolean = false,
val isViewed: Boolean = false,
val scam: Boolean = false,
val mute: Boolean = false,
val verified: Boolean = false
) {
companion object {
val pizza = Chat(
id = 1,
avatarImage = R.drawable.pizza_avatar,
previewImage = R.drawable.preview,
nickName = "Pizza",
group = "jija",
message = "Yes, they are necessary",
lastTime = "11:38 AM",
mute = true
)
val elon = Chat(
id = 2,
avatarImage = R.drawable.ilon_avatar,
previewImage = null,
nickName = "Elon",
message = "I love /r/Reddit!",
lastTime = "12:44 AM",
mute = true
)
val pasha = Chat(
id = 3,
avatarImage = R.drawable.pasha_avatar,
previewImage = null,
nickName = "Pasha",
message = "How are u?",
lastTime = "Fri",
mute = true,
verified = true
)
val pasha2 = Chat(
id = 4,
avatarImage = R.drawable.pasha_avatar,
previewImage = null,
nickName = "Pasha2",
message = "what are u doing now",
lastTime = "Thu",
isViewed = true,
mute = true
)
val telegramSupport = Chat(
id = 5,
avatarImage = R.drawable.tg_avatar,
previewImage = null,
nickName = "Telegram Support",
group = "Support",
message = "Yes it happened.",
lastTime = "Thu",
messCount = 1,
verified = true
)
val karina = Chat(
id = 6,
avatarImage = R.drawable.karina_avatar,
previewImage = null,
nickName = "Karina",
message = "Okay",
lastTime = "Wed",
check = true
)
val marilyn = Chat(
id = 7,
avatarImage = R.drawable.marlin_avatar,
previewImage = null,
nickName = "Marilyn",
message = "Will it ever happen",
lastTime = "May 02",
isViewed = true,
scam = true
)
val chatList = mutableListOf(
pizza,
elon,
pasha,
pasha2,
telegramSupport,
karina,
marilyn
)
}
}












88 changes: 88 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,88 @@
package otus.gpb.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import otus.gpb.recyclerview.databinding.ItemRecyclerBinding

class ChatAdapter(private var chatList: MutableList<Chat>) : Adapter<ChatViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatViewHolder {
val binding =
ItemRecyclerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ChatViewHolder(binding)
}

override fun getItemCount(): Int {
return chatList.size
}

override fun onBindViewHolder(holder: ChatViewHolder, position: Int) {
holder.bind(chatList[position])
}

fun setChats(newChatList: MutableList<Chat>) {
chatList = newChatList
notifyDataSetChanged()
}

fun deleteItem(position: Int) {
chatList.removeAt(position)
notifyItemRemoved(position)
}
}

class ChatViewHolder(private val binding: ItemRecyclerBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(model: Chat) {
binding.imageViewAvatar.setImageResource(model.avatarImage)
model.previewImage?.let { binding.imageViewPreview.setImageResource(it) }

binding.textViewNickname.text = model.nickName
binding.textViewMessage.text = model.message
binding.textViewMessTime.text = model.lastTime
binding.textViewMessCount.text = model.messCount.toString()

if (model.group.isEmpty())
binding.textViewGroup.visibility = View.GONE
else
binding.textViewGroup.text = model.group

if (model.check) {
binding.imageViewCheck.setImageResource(R.drawable.one_check)
binding.imageViewCheck.visibility = View.VISIBLE
} else if (model.isViewed) {
binding.imageViewCheck.setImageResource(R.drawable.read_check)
binding.imageViewCheck.visibility = View.VISIBLE
} else {
binding.imageViewCheck.visibility = View.GONE
}

if (model.messCount == 0) {
binding.imageViewMessCountCircle.visibility = View.GONE
binding.textViewMessCount.visibility = View.GONE
} else {
binding.imageViewMessCountCircle.visibility = View.VISIBLE
binding.textViewMessCount.visibility = View.VISIBLE
binding.textViewMessCount.text = model.messCount.toString()
}

if (model.previewImage == null) {
binding.imageViewPreview.visibility = View.GONE
} else {
binding.imageViewPreview.visibility = View.VISIBLE
binding.imageViewPreview.setImageResource(model.previewImage!!)
}

if (!model.scam)
binding.imageViewScam.visibility = View.GONE

if (!model.mute)
binding.imageViewMute.visibility = View.GONE

if (!model.verified)
binding.imageViewVerified.visibility = View.GONE
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/CustomItemDecorator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package otus.gpb.recyclerview

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class CustomItemDecorator : RecyclerView.ItemDecoration() {

override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
outRect.apply {
left = 0
right = 0
top = 5
bottom = 5
}
}
}

98 changes: 96 additions & 2 deletions app/src/main/java/otus/gpb/recyclerview/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,106 @@
package otus.gpb.recyclerview

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import otus.gpb.recyclerview.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var adapter: ChatAdapter

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

private fun initializeViewBinding() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}

private fun setupRecyclerView() {
adapter = ChatAdapter(chatList = Chat.chatList)
binding.recyclerView.adapter = adapter
binding.recyclerView.layoutManager = LinearLayoutManager(this)
adapter.setChats(Chat.chatList)
binding.recyclerView.addItemDecoration(CustomItemDecorator())
}

private fun setupSwipeToDelete() {
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) {
adapter.deleteItem(viewHolder.adapterPosition)
}

override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
viewHolder.itemView.apply {
ContextCompat.getDrawable(context, R.drawable.archive)?.apply {
val background = ColorDrawable()
background.color = ContextCompat.getColor(context, R.color.blue)

val iconMargin = ((bottom - top) - intrinsicHeight) / 2
val iconTop = top + iconMargin / 2
val iconBottom = iconTop + intrinsicHeight
val iconRight = right - iconMargin
val iconLeft = iconRight - intrinsicWidth

setBounds(iconLeft, iconTop, iconRight, iconBottom)
background.setBounds(right + dX.toInt(), top, right, bottom)

background.draw(c)
draw(c)
}
val textPaint = Paint().apply {
color = Color.WHITE
textSize = 25f
}
val text = "Archive"
val textWidth = textPaint.measureText(text)
val textX = right - textWidth - 30f
val textY = bottom - 20f
c.drawText(text, textX, textY, textPaint)
}

super.onChildDraw(
c,
recyclerView,
viewHolder,
dX,
dY,
actionState,
isCurrentlyActive
)
}
})

itemTouchHelper.attachToRecyclerView(binding.recyclerView)
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/archive.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20.54,5.23l-1.39,-1.68C18.88,3.21 18.47,3 18,3H6c-0.47,0 -0.88,0.21 -1.16,0.55L3.46,5.23C3.17,5.57 3,6.02 3,6.5V19c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V6.5c0,-0.48 -0.17,-0.93 -0.46,-1.27zM12,17.5L6.5,12H10v-2h4v2h3.5L12,17.5zM5.12,5l0.81,-1h12l0.94,1H5.12z"/>
</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/red" />

<padding
android:left="5dp"
android:right="5dp" />
</shape>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/blue"
android:pathData="M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z" />
</vector>
Binary file added app/src/main/res/drawable/ilon_avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/karina_avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/line.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">
<solid android:color="#D9D9D9" />

</shape>
Binary file added app/src/main/res/drawable/marlin_avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/one_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="18dp"
android:height="18dp"
android:viewportWidth="18"
android:viewportHeight="18">
<path
android:pathData="M3.176,9.842L6.37,12.706L14.823,5.294"
android:strokeLineJoin="round"
android:strokeWidth="1.5"
android:fillColor="#00000000"
android:strokeColor="#48A938"
android:strokeLineCap="round"/>
</vector>
Binary file added app/src/main/res/drawable/pasha_avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/pizza_avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading