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

Homework / recyclerView #17

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
12 changes: 7 additions & 5 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 33

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

Expand All @@ -29,6 +29,9 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}

dependencies {
Expand All @@ -37,7 +40,6 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation deps.recyclerview
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

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

data class ChatItem(
val id: Int,
val userName: String,
val lastMessage: String,
val date: String,
val avatar: String? = null,
val isMuted: Boolean = false,
val isVerified: Boolean = false,
val isScam: Boolean = false,
val isLastMessageMine: Boolean = false,
val isMessageDelivered: Boolean = false,
val isMessageRead:Boolean = false,
val unreadMessageCount: Int = 0
) {
var onClickListener: ((item: ChatItem) -> Unit)? = null
}
100 changes: 98 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,108 @@
package otus.gpb.recyclerview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import otus.gpb.recyclerview.adapter.ChatListAdapter
import otus.gpb.recyclerview.adapter.OnInteractionListener
import otus.gpb.recyclerview.adapter.Stubs
import otus.gpb.recyclerview.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

private val chatAdapter: ChatListAdapter by lazy {
ChatListAdapter(object : OnInteractionListener {
override fun onBindingClick(item: ChatItem, itemPosition: Int) {
chatAdapter.addItem(item.copy(id = ++Stubs.id))
}
})
}
private lateinit var touchHelperCallBack: ItemTouchHelper.Callback

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

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

//due to onStart calls many times, and onCreate calls once,
// all entities that required to be instantiated ONCE go to onCreate

val listToSubmit = Stubs.stubItems.onEach { item ->
item.onClickListener = {
Toast.makeText(
this,
getString(R.string.last_message, item.userName, item.lastMessage),
Toast.LENGTH_SHORT
).show()
}
}

val layoutManager = LinearLayoutManager(binding.recyclerView.context)
binding.recyclerView.apply {
this.layoutManager = layoutManager
adapter = chatAdapter
addItemDecoration(MyDecoration(this.context))

addOnScrollListener(MyPageScrollListener(layoutManager).apply {
setOnLoadMoreListener {
chatAdapter.addItems(listToSubmit)

Toast.makeText(
this@MainActivity,
"Items count now is ${chatAdapter.itemCount}",
Toast.LENGTH_SHORT
).show()
}
}
)
}
chatAdapter.submitList(listToSubmit)

touchHelperCallBack = getTouchHelperCallback()
val itemTouchHelper = ItemTouchHelper(touchHelperCallBack)
itemTouchHelper.attachToRecyclerView(binding.recyclerView)

val snapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(binding.recyclerView)
}

private fun getTouchHelperCallback(): ItemTouchHelper.Callback {
val touchHelperCallBack = object : MyTouchHelperCallBack(binding.recyclerView.context) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
chatAdapter.swapItem(viewHolder.adapterPosition, target.adapterPosition)
return true
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
val item = chatAdapter.currentList[position]

chatAdapter.removeItem(position)

val snackBar = Snackbar.make(
viewHolder.itemView,
"Item was removed from the list.",
Snackbar.LENGTH_LONG
)
snackBar.setAction("RESTORE") {
chatAdapter.restoreItem(item, position)
}
snackBar.setActionTextColor(getColor(R.color.blue_400))
snackBar.show()
}
}
return touchHelperCallBack
}
}
Loading