add selector

This commit is contained in:
InsanusMokrassar 2020-12-14 19:17:16 +06:00
parent 25e9345d02
commit 12e37184e1
7 changed files with 109 additions and 1 deletions

View File

@ -2,6 +2,9 @@
## 0.4.12
* `Selector`
* Project created
## 0.4.11
* `Common`

View File

@ -17,6 +17,7 @@ You always can look at the <a href="https://github.com/InsanusMokrassar/MicroUti
## Projects
* `common` contains common tools for platform which usually are absent out-of-the-box when you starting project
* `selector` contains tools to use `Selector` interface with things like `RecyclerView` in android or other selection needs
* `coroutines` is a module for `Kotlin Coroutines` with different things like subscribing on flows (`onEach` + `launchIn` shortcut :) )
* `ktor` is a set of modules for `client`s and `server`s
* `mime_types` is NOT lightweight set of `MimeType`s with a lot of different objected and serializable (with `Kotlin Serialization`) mime types

View File

@ -35,7 +35,7 @@ espresso_core=3.3.0
# Dokka
dokka_version=1.4.21
dokka_version=1.4.20
# Project data

View File

@ -0,0 +1,17 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
}
}
}
}

View File

@ -0,0 +1,85 @@
package dev.inmo.micro_utils.selector
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.Mutex
interface Selector<T> {
val selectedItems: List<T>
val itemSelected: SharedFlow<T>
val itemUnselected: SharedFlow<T>
suspend fun toggleSelection(element: T)
}
@Suppress("NOTHING_TO_INLINE")
inline operator fun <T> Selector<T>.contains(element: T) = selectedItems.contains(element)
@Suppress("NOTHING_TO_INLINE")
inline fun <T> Selector<T>.nothingSelected(): Boolean = selectedItems.isEmpty()
class SingleSelector<T>(
selectedItem: T? = null,
useMutex: Boolean = true
) : Selector<T> {
var selectedItem: T? = selectedItem
private set
override val selectedItems: List<T>
get() = selectedItem ?.let { listOf(it) } ?: emptyList()
private val _itemSelected = MutableSharedFlow<T>()
override val itemSelected: SharedFlow<T> = _itemSelected.asSharedFlow()
private val _itemUnselected = MutableSharedFlow<T>()
override val itemUnselected: SharedFlow<T> = _itemUnselected.asSharedFlow()
private val mutex = if (useMutex) {
Mutex()
} else {
null
}
override suspend fun toggleSelection(element: T) {
mutex ?.lock()
if (selectedItem == element) {
selectedItem = null
_itemUnselected.emit(element)
} else {
val previouslySelected = selectedItem
selectedItem = null
if (previouslySelected != null) {
_itemUnselected.emit(previouslySelected)
}
selectedItem = element
_itemSelected.emit(element)
}
mutex ?.unlock()
}
}
class MultipleSelector<T>(
selectedItems: List<T>,
useMutex: Boolean = true
) : Selector<T> {
private val _selectedItems: MutableList<T> = selectedItems.toMutableList()
override val selectedItems: List<T> = _selectedItems
private val _itemSelected = MutableSharedFlow<T>()
override val itemSelected: SharedFlow<T> = _itemSelected.asSharedFlow()
private val _itemUnselected = MutableSharedFlow<T>()
override val itemUnselected: SharedFlow<T> = _itemUnselected.asSharedFlow()
private val mutex = if (useMutex) {
Mutex()
} else {
null
}
override suspend fun toggleSelection(element: T) {
mutex ?.lock()
if (_selectedItems.remove(element)) {
_itemUnselected.emit(element)
} else {
_selectedItems.add(element)
_itemSelected.emit(element)
}
mutex ?.unlock()
}
}

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.selector"/>

View File

@ -2,6 +2,7 @@ rootProject.name='micro_utils'
String[] includes = [
":common",
":selector:common",
":pagination:common",
":pagination:exposed",
":pagination:ktor:common",