mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-03 23:29:46 +00:00
updates of dokka
This commit is contained in:
23
android/recyclerview/build.gradle
Normal file
23
android/recyclerview/build.gradle
Normal file
@@ -0,0 +1,23 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
id "kotlin-android-extensions"
|
||||
}
|
||||
|
||||
apply from: "$mppAndroidProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
|
||||
}
|
||||
}
|
||||
androidMain {
|
||||
dependencies {
|
||||
api "androidx.recyclerview:recyclerview:$androidx_recycler_version"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
android/recyclerview/src/main/AndroidManifest.xml
Normal file
1
android/recyclerview/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.micro_utils.android.recyclerview"/>
|
@@ -0,0 +1,22 @@
|
||||
package dev.inmo.micro_utils.android.recyclerview
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
|
||||
abstract class AbstractStandardViewHolder<T>(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
viewId: Int,
|
||||
onViewInflated: ((View) -> Unit)? = null
|
||||
) : AbstractViewHolder<T>(
|
||||
inflater.inflate(viewId, container, false).also {
|
||||
onViewInflated ?.invoke(it)
|
||||
}
|
||||
) {
|
||||
constructor(
|
||||
container: ViewGroup,
|
||||
viewId: Int,
|
||||
onViewInflated: ((View) -> Unit)? = null
|
||||
) : this(LayoutInflater.from(container.context), container, viewId, onViewInflated)
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package dev.inmo.micro_utils.android.recyclerview
|
||||
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
abstract class AbstractViewHolder<in T>(
|
||||
view: View
|
||||
) : RecyclerView.ViewHolder(view) {
|
||||
abstract fun onBind(item: T)
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package dev.inmo.micro_utils.android.recyclerview
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.LinearLayout
|
||||
import androidx.recyclerview.widget.DividerItemDecoration
|
||||
|
||||
val Context.recyclerViewItemsDecoration
|
||||
get() = DividerItemDecoration(this, LinearLayout.VERTICAL)
|
||||
|
@@ -0,0 +1,47 @@
|
||||
package dev.inmo.micro_utils.android.recyclerview
|
||||
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
fun RecyclerView.lastVisibleItemFlow(
|
||||
completingScope: CoroutineScope
|
||||
): Flow<Int> {
|
||||
val lastVisibleElementFun: () -> Int = (layoutManager as? LinearLayoutManager) ?.let { it::findLastVisibleItemPosition } ?: error("Currently supported only linear layout manager")
|
||||
val lastVisibleFlow = MutableStateFlow(lastVisibleElementFun())
|
||||
addOnScrollListener(
|
||||
object : RecyclerView.OnScrollListener() {
|
||||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||
super.onScrolled(recyclerView, dx, dy)
|
||||
lastVisibleFlow.value = lastVisibleElementFun()
|
||||
}
|
||||
}.also { scrollListener ->
|
||||
lastVisibleFlow.onCompletion {
|
||||
removeOnScrollListener(scrollListener)
|
||||
}.launchIn(completingScope)
|
||||
}
|
||||
)
|
||||
return lastVisibleFlow.asStateFlow()
|
||||
}
|
||||
|
||||
inline fun Flow<Int>.mapLeftItems(
|
||||
crossinline countGetter: () -> Int
|
||||
): Flow<Int> = map { countGetter() - it }
|
||||
|
||||
inline fun Flow<Int>.mapRequireFilling(
|
||||
minimalLeftItems: Int,
|
||||
crossinline countGetter: () -> Int
|
||||
): Flow<Int> = mapLeftItems(countGetter).mapNotNull {
|
||||
if (it < minimalLeftItems) {
|
||||
it
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
inline fun RecyclerView.mapRequireFilling(
|
||||
minimalLeftItems: Int,
|
||||
completingScope: CoroutineScope,
|
||||
crossinline countGetter: () -> Int
|
||||
): Flow<Int> = lastVisibleItemFlow(completingScope).mapRequireFilling(minimalLeftItems, countGetter)
|
@@ -0,0 +1,68 @@
|
||||
package dev.inmo.micro_utils.android.recyclerview
|
||||
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
|
||||
abstract class RecyclerViewAdapter<T>(
|
||||
val data: List<T>
|
||||
): RecyclerView.Adapter<AbstractViewHolder<T>>() {
|
||||
var emptyView: View? = null
|
||||
set(value) {
|
||||
field = value
|
||||
checkEmpty()
|
||||
}
|
||||
|
||||
init {
|
||||
registerAdapterDataObserver(
|
||||
object : RecyclerView.AdapterDataObserver() {
|
||||
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
|
||||
super.onItemRangeChanged(positionStart, itemCount)
|
||||
checkEmpty()
|
||||
}
|
||||
|
||||
override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
|
||||
super.onItemRangeChanged(positionStart, itemCount, payload)
|
||||
checkEmpty()
|
||||
}
|
||||
|
||||
override fun onChanged() {
|
||||
super.onChanged()
|
||||
checkEmpty()
|
||||
}
|
||||
|
||||
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
|
||||
super.onItemRangeRemoved(positionStart, itemCount)
|
||||
checkEmpty()
|
||||
}
|
||||
|
||||
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
|
||||
super.onItemRangeMoved(fromPosition, toPosition, itemCount)
|
||||
checkEmpty()
|
||||
}
|
||||
|
||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||
super.onItemRangeInserted(positionStart, itemCount)
|
||||
checkEmpty()
|
||||
}
|
||||
}
|
||||
)
|
||||
checkEmpty()
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
override fun onBindViewHolder(holder: AbstractViewHolder<T>, position: Int) {
|
||||
holder.onBind(data[position])
|
||||
}
|
||||
|
||||
private fun checkEmpty() {
|
||||
emptyView ?. let {
|
||||
if (data.isEmpty()) {
|
||||
it.visibility = View.VISIBLE
|
||||
} else {
|
||||
it.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user