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
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdk 32
compileSdk 34

defaultConfig {
applicationId "otus.gpb.recyclerview"
minSdk 23
targetSdk 32
minSdk 24
targetSdk 34
versionCode 1
versionName "1.0"

Expand All @@ -29,6 +29,7 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
namespace 'otus.gpb.recyclerview'
}

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="otus.gpb.recyclerview">
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
Expand Down
151 changes: 151 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,151 @@
package otus.gpb.recyclerview

class Chat {
fun fillData(): MutableList<Item> {
return mutableListOf(
Item(
avatar = R.drawable.avatar_pizza,
name = "Pizza",
message = "Yes,they are necessary",
comment = "jija",
date = "11.38 AM",
mute = true,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = false,
scum = false,
pictureInMessage = true
),
Item(
avatar = R.drawable.avatar_elon,
name = "Elon",
message = "I love /r/Reddit!",
comment = "",
date = "12.44 AM",
mute = true,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = false,
scum = false,
pictureInMessage = false
),
Item(
avatar = R.drawable.avatar_pasha,
name = "Pasha",
message = "How are u?",
comment = "",
date = "Fri",
mute = true,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = false,
scum = false,
pictureInMessage = false
),
Item(
avatar = R.drawable.kitty,
name = "Kitty",
message = "The time is now",
comment = "",
date = "Thu",
mute = true,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = true,
scum = false,
pictureInMessage = false
),

Item(
avatar = R.drawable.avatar_telegram,
name = "Telegram Support",
message = "Yes it happened",
comment = "Support",
date = "Thu",
mute = false,
verify = true,
unreadMessages = 1,
check = false,
doubleCheck = false,
scum = false,
pictureInMessage = false
),

Item(
avatar = R.drawable.avatar_karina,
name = "Karina",
message = "Okey",
comment = "",
date = "Wed",
mute = false,
verify = false,
unreadMessages = 0,
check = true,
doubleCheck = false,
scum = false,
pictureInMessage = false
),

Item(
avatar = R.drawable.avatar_marilyn,
name = "Marilyn",
message = "Will it ever happen",
comment = "",
date = "May 02",
mute = false,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = true,
scum = true,
pictureInMessage = false
),
Item(
avatar = R.drawable.avatar_pizza,
name = "Pizza",
message = "Yes,they are necessary",
comment = "jija",
date = "11.38 AM",
mute = true,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = false,
scum = false,
pictureInMessage = false
),
Item(
avatar = R.drawable.avatar_elon,
name = "Elon",
message = "I love r/r/Reddit!",
comment = "",
date = "12.44 AM",
mute = true,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = false,
scum = false,
pictureInMessage = false
),
Item(
avatar = R.drawable.avatar_pasha,
name = "Pasha",
message = "How are u?",
comment = "",
date = "Fri",
mute = true,
verify = false,
unreadMessages = 0,
check = false,
doubleCheck = false,
scum = false,
pictureInMessage = false
)
)
}
}
39 changes: 39 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,39 @@
package otus.gpb.recyclerview

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class ChatAdapter: RecyclerView.Adapter<PersonViewHolder>() {
private var chatList = mutableListOf<Item>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PersonViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.person_item, parent, false)
return PersonViewHolder(itemView)
}

override fun getItemCount() = chatList.size

override fun onBindViewHolder(holder: PersonViewHolder, position: Int) {
val chat = chatList[position]
holder.bind(chat)
}

fun setItems(items: MutableList<Item>) {
chatList = items
notifyDataSetChanged()
}

fun addItems(items: MutableList<Item>) {
chatList.addAll(items)
notifyItemRangeChanged(1,10)
}

fun removeItem(adapterPosition: Int) {
val newList = chatList.toMutableList().apply {
removeAt(adapterPosition)
}
setItems(newList)
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/DividerItemDecoration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package otus.gpb.recyclerview

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import androidx.recyclerview.widget.RecyclerView

class DividerItemDecoration: RecyclerView.ItemDecoration() {

private val bounds = Rect()
private val paint = Paint().apply {
color = Color.parseColor("#8D8E90")
strokeWidth = 2f
}

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

val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
parent.getDecoratedBoundsWithMargins(child, bounds)
canvas.drawLine(
210f,
bounds.top.toFloat(),
bounds.right.toFloat(),
bounds.top.toFloat(),
paint
)
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/Item.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package otus.gpb.recyclerview

data class Item(
val avatar: Int,
val name: String,
val message: String,
val comment: String = "",
val date: String,
val mute: Boolean = false,
val verify: Boolean = false,
val unreadMessages: Int,
val check: Boolean = false,
val doubleCheck: Boolean = false,
val scum: Boolean = false,
val pictureInMessage: Boolean = false
)
83 changes: 83 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ItemTouchHelperCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package otus.gpb.recyclerview

import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class ItemTouchHelperCallback(
private val chatAdapter: ChatAdapter,
private val resources: Resources
) : ItemTouchHelper.Callback() {
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int {
return makeMovementFlags(
ItemTouchHelper.DOWN,
ItemTouchHelper.LEFT
)
}

override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return true
}

override fun onChildDraw(
canvas: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
val itemView = viewHolder.itemView
val background = ColorDrawable(Color.parseColor("#66A9E0"))

background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
background.draw(canvas)

val scaledDensity = resources.displayMetrics.scaledDensity
val density = resources.displayMetrics.density

fun spToPx(sp: Float): Float = sp * scaledDensity
fun dpToPx(dp:Int): Int = (density * dp + 0.5f).toInt()

val archiveIcon = ContextCompat.getDrawable(itemView.context, R.drawable.archive)
val iconHeight = archiveIcon?.intrinsicHeight!!

val iconTop = itemView.top + dpToPx(16)
val iconBottom = iconTop + iconHeight
val iconRight = itemView.right - dpToPx(29)
val iconLeft = iconRight - archiveIcon.intrinsicWidth

archiveIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
archiveIcon.draw(canvas)

val textPaint = Paint().apply {
color = Color.WHITE
textSize = spToPx(13F)
}
val text = "Archive"
val textWidth = textPaint.measureText(text)
val textX = itemView.right - dpToPx(20).toFloat() - textWidth
val textY = itemView.bottom - dpToPx(16).toFloat()

canvas.drawText(text, textX, textY, textPaint)
super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}

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