mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
add selector
This commit is contained in:
parent
25e9345d02
commit
12e37184e1
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
## 0.4.12
|
## 0.4.12
|
||||||
|
|
||||||
|
* `Selector`
|
||||||
|
* Project created
|
||||||
|
|
||||||
## 0.4.11
|
## 0.4.11
|
||||||
|
|
||||||
* `Common`
|
* `Common`
|
||||||
|
@ -17,6 +17,7 @@ You always can look at the <a href="https://github.com/InsanusMokrassar/MicroUti
|
|||||||
## Projects
|
## Projects
|
||||||
|
|
||||||
* `common` contains common tools for platform which usually are absent out-of-the-box when you starting project
|
* `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 :) )
|
* `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
|
* `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
|
* `mime_types` is NOT lightweight set of `MimeType`s with a lot of different objected and serializable (with `Kotlin Serialization`) mime types
|
||||||
|
@ -35,7 +35,7 @@ espresso_core=3.3.0
|
|||||||
|
|
||||||
# Dokka
|
# Dokka
|
||||||
|
|
||||||
dokka_version=1.4.21
|
dokka_version=1.4.20
|
||||||
|
|
||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
|
17
selector/common/build.gradle
Normal file
17
selector/common/build.gradle
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
1
selector/common/src/main/AndroidManifest.xml
Normal file
1
selector/common/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.selector"/>
|
@ -2,6 +2,7 @@ rootProject.name='micro_utils'
|
|||||||
|
|
||||||
String[] includes = [
|
String[] includes = [
|
||||||
":common",
|
":common",
|
||||||
|
":selector:common",
|
||||||
":pagination:common",
|
":pagination:common",
|
||||||
":pagination:exposed",
|
":pagination:exposed",
|
||||||
":pagination:ktor:common",
|
":pagination:ktor:common",
|
||||||
|
Loading…
Reference in New Issue
Block a user