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: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding = true
}
}

dependencies {
Expand All @@ -38,6 +41,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.8.7'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7'
implementation 'androidx.fragment:fragment-ktx:1.8.6'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
58 changes: 58 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,58 @@
package otus.gpb.recyclerview

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

class ChatAdapter(private val listener: Listener): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var list = listOf<ChatListItem>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
ViewTypes.CHAT.id -> {
val view = LayoutInflater.from(parent.context).inflate(R.layout.chat_item, parent, false)

ChatViewHolder(view, listener)
}
ViewTypes.LOAD.id -> {
val view = LayoutInflater.from(parent.context).inflate(R.layout.load_item, parent, false)

LoadViewHolder(view)
}
else -> throw IllegalArgumentException("Not found view type for chat adapter")
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = list.getOrNull(position) ?: return

when (item) {
is ChatListItem.ChatItem -> {
(holder as? ChatViewHolder)?.bind(item)
}
is ChatListItem.LoadItem -> {
return
}
}
}

override fun getItemViewType(position: Int): Int {
return when (list[position]) {
is ChatListItem.ChatItem -> ViewTypes.CHAT.id
is ChatListItem.LoadItem -> ViewTypes.LOAD.id
}
}

override fun getItemCount(): Int = list.size

fun setItems(items: List<ChatListItem>) {
list = items
notifyDataSetChanged()
}

enum class ViewTypes(val id: Int) {
CHAT(R.layout.chat_item),
LOAD(R.layout.load_item)
}
}
84 changes: 84 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package otus.gpb.recyclerview

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.fragment.app.viewModels
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class ChatFragment : Fragment() {
companion object {
fun newInstance() = ChatFragment()
}

private lateinit var recyclerView: RecyclerView
private val viewModel: ChatViewModel by viewModels()

private val itemTouchHelperCallback = 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) {
viewModel.removeItem(viewHolder.adapterPosition)
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_chat, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupRecyclerView(view)
}

private fun setupRecyclerView(view: View) {
recyclerView = view.findViewById(R.id.recyclerView)

val dividerItemDecoration = DividerItemDecoration(context, LinearLayoutManager.VERTICAL)
recyclerView.addItemDecoration(dividerItemDecoration)
recyclerView.adapter = viewModel.chatAdapter

val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
itemTouchHelper.attachToRecyclerView(recyclerView)

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val visibleItemCount = layoutManager.childCount
val totalItemCount = layoutManager.itemCount
val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()

if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0) {
viewModel.loadNewData()
}
}
})

viewModel.setupData()
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package otus.gpb.recyclerview

import androidx.annotation.LayoutRes
import java.time.LocalDate

sealed class ChatListItem: WithLayoutId {
data class ChatItem(private val chatMessage: ChatMessage) : ChatListItem(), WithLayoutId by ChatItem {
companion object : WithLayoutId {
@get:LayoutRes
override val layoutId: Int = R.layout.chat_item
}

val user: String get() = chatMessage.user
val message: String get() = chatMessage.message
val date: LocalDate get() = chatMessage.date
}

data class LoadItem(private val id: String = "1") : ChatListItem(), WithLayoutId by LoadItem {
companion object : WithLayoutId {
@get:LayoutRes
override val layoutId: Int = R.layout.load_item
}
}
}

24 changes: 24 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatMessage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package otus.gpb.recyclerview

import java.sql.Date
import java.time.LocalDate

data class ChatMessage(
val id: Int,
val user: String,
val message: String,
val date: LocalDate
)

val chatMessages = listOf(
ChatListItem.ChatItem(ChatMessage(0, "Dave", message = "Hello everyone!", date = LocalDate.now())),
ChatListItem.ChatItem(ChatMessage(1, "Ana", message = "I want pizza for lunch", date = LocalDate.now().plusDays(1))),
ChatListItem.ChatItem(ChatMessage(2, "Micky", message = "TDS helps you create your own concepts and learn new things", date = LocalDate.now().plusDays(2))),
ChatListItem.ChatItem(ChatMessage(3, "Sarah", message = "Has anyone finished the project yet?", date = LocalDate.now().minusDays(1))),
ChatListItem.ChatItem(ChatMessage(4, "John", message = "Meeting at 3 PM tomorrow", date = LocalDate.now().plusDays(3))),
ChatListItem.ChatItem(ChatMessage(5, "Emma", message = "Don't forget to submit your reports", date = LocalDate.now().plusDays(1))),
ChatListItem.ChatItem(ChatMessage(6, "Mike", message = "The new update is amazing!", date = LocalDate.now().minusDays(2))),
ChatListItem.ChatItem(ChatMessage(7, "Lisa", message = "Can someone help me with Kotlin?", date = LocalDate.now())),
ChatListItem.ChatItem(ChatMessage(8, "Alex", message = "Let's schedule a team building event", date = LocalDate.now().plusDays(5))),
ChatListItem.ChatItem(ChatMessage(9, "Tina", message = "Happy Friday everyone!", date = LocalDate.now().plusDays(4)))
)
32 changes: 32 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package otus.gpb.recyclerview

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale

class ChatViewHolder(private val view: View, private val listener: Listener) : RecyclerView.ViewHolder(view) {
private val nameTV: TextView by lazy { view.findViewById(R.id.nameTV) }
private val userTV: TextView by lazy { view.findViewById(R.id.userTV) }
private val messageTV: TextView by lazy { view.findViewById(R.id.messageTV) }
private val dateTV: TextView by lazy { view.findViewById(R.id.dateTV) }

fun bind(item: ChatListItem.ChatItem) {
nameTV.text = item.user.firstOrNull().toString()
userTV.text = item.user
messageTV.text = item.message
dateTV.text = formatDate(item.date)

view.setOnClickListener {
listener.onItemClicked(layoutPosition)
}
}

private fun formatDate(date: LocalDate): String {
val formatter = DateTimeFormatter.ofPattern("EEE d", Locale.getDefault())
return date.format(formatter)
}
}
42 changes: 42 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package otus.gpb.recyclerview

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ChatViewModel : ViewModel(), Listener {
val chatAdapter = ChatAdapter(this)

private var items: MutableList<ChatListItem> = chatMessages.toMutableList()
private var isDataLoading: Boolean = false

fun setupData() {
chatAdapter.setItems(items)
}

fun removeItem(id: Int) {
items.removeAt(id)
setupData()
}

fun loadNewData() {
if (isDataLoading) { return }

isDataLoading = true

viewModelScope.launch {
items.add(ChatListItem.LoadItem())
setupData()

delay(3000L)

items.removeLast()
items.addAll(chatMessages)
setupData()
isDataLoading = false
}
}

override fun onItemClicked(id: Int) { }
}
5 changes: 5 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/Listener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package otus.gpb.recyclerview

interface Listener {
fun onItemClicked(id: Int)
}
10 changes: 10 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/LoadViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package otus.gpb.recyclerview

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale

class LoadViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { }
4 changes: 4 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

supportFragmentManager.beginTransaction()
.replace(R.id.container, ChatFragment.newInstance())
.commit()
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/WithLayoutId.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package otus.gpb.recyclerview

import androidx.annotation.LayoutRes

interface WithLayoutId {
@get:LayoutRes
val layoutId: Int
}
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="1dp" />
<solid android:color="@android:color/darker_gray" />
</shape>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/rounded_corners.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp"/>
<solid android:color="@android:color/transparent"/>
</shape>
10 changes: 3 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Loading