start add recycler view

This commit is contained in:
InsanusMokrassar 2020-10-28 13:51:16 +06:00
parent cc18f58e4c
commit 347e9c32fe
5 changed files with 100 additions and 0 deletions

15
front/common/build.gradle Normal file
View File

@ -0,0 +1,15 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
}
apply from: "$mppJsProjectPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api internalProject("micro_utils.common")
}
}
}
}

View File

@ -0,0 +1,38 @@
package dev.inmo.micro_utils.front.recycler_view
import dev.inmo.micro_utils.common.isOnScreen
import kotlinx.browser.document
import org.w3c.dom.HTMLDivElement
abstract class RecyclerViewAdapter<T>(
protected val data: List<T>,
protected val drawer: RecyclerViewDrawer,
private val visibleOutsideOfViewportElements: Int = 1
) {
val element: HTMLDivElement = document.createElement("div") as HTMLDivElement
protected val itemsToElements = mutableMapOf<T, RecyclerViewElement>()
abstract fun createViewFactory(element: T): RecyclerViewElement
fun updateVisibleElements() {
var firstVisibleElementIndex = -1
var lastVisibleElementIndex = -1
for ((i, item) in data.withIndex()) {
itemsToElements[item] ?.let {
firstVisibleElementIndex = when {
firstVisibleElementIndex == -1 -> i
it.element.isOnScreen -> minOf(firstVisibleElementIndex, i)
else -> firstVisibleElementIndex
}
lastVisibleElementIndex = when {
lastVisibleElementIndex == -1 -> i
it.element.isOnScreen -> maxOf(lastVisibleElementIndex, i)
else -> lastVisibleElementIndex
}
}
}
}
}

View File

@ -0,0 +1,8 @@
package dev.inmo.micro_utils.front.recycler_view
import org.w3c.dom.HTMLElement
interface RecyclerViewDrawer {
fun drawBefore(what: HTMLElement, before: HTMLElement)
fun drawAfter(what: HTMLElement, after: HTMLElement)
}

View File

@ -0,0 +1,9 @@
package dev.inmo.micro_utils.front.recycler_view
import org.w3c.dom.HTMLElement
interface RecyclerViewElement {
val element: HTMLElement
suspend fun onOutsideOfViewport()
suspend fun onInsideOfViewport()
}

30
mppJsProject Normal file
View File

@ -0,0 +1,30 @@
project.version = "$version"
project.group = "$group"
apply from: "$publishGradlePath"
kotlin {
js (BOTH) {
browser()
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib')
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jsTest {
dependencies {
implementation kotlin('test-js')
implementation kotlin('test-junit')
}
}
}
}