mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-06 07:39:27 +00:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
a36828116e | |||
14aa9ca26c | |||
069e51f2ff | |||
a15cbdfb1a | |||
4af8114eda | |||
90dc84e900 | |||
67c595b440 | |||
830b7aee56 | |||
1890608cb3 | |||
bd396959a9 | |||
d5fe19f0a5 | |||
46bfb09415 | |||
a60cb596d1 | |||
6f9d5e2d5f | |||
80bc226ee1 | |||
12e37184e1 | |||
25e9345d02 | |||
ccc4d030c3 |
25
CHANGELOG.md
25
CHANGELOG.md
@@ -1,5 +1,30 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.4.14
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `Kotlin`: `1.4.20` -> `1.4.21`
|
||||||
|
* `Ktor`: `1.4.3` -> `1.5.0`
|
||||||
|
* `Klock`: `2.0.1` -> `2.0.2`
|
||||||
|
* `Coroutines`:
|
||||||
|
* Add global variable `defaultSafelyExceptionHandler`
|
||||||
|
* Add `SafelyExceptionHandlerKey` and `SafelyExceptionHandler` classes to be able to overwrite
|
||||||
|
`defaultSafelyExceptionHandler` using context of coroutine
|
||||||
|
|
||||||
|
## 0.4.13
|
||||||
|
|
||||||
|
* `Common`
|
||||||
|
* `Android`
|
||||||
|
* Add expand/collapse functionality for horizontal expand/collapse
|
||||||
|
|
||||||
|
## 0.4.12
|
||||||
|
|
||||||
|
* `Coroutines`
|
||||||
|
* `JVM`
|
||||||
|
* Update `launchSynchronously` signature
|
||||||
|
* `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
|
||||||
|
@@ -5,23 +5,44 @@ import android.view.ViewGroup
|
|||||||
import android.view.animation.Animation
|
import android.view.animation.Animation
|
||||||
import android.view.animation.Transformation
|
import android.view.animation.Transformation
|
||||||
|
|
||||||
@PreviewFeature
|
private fun View.performExpand(
|
||||||
fun View.expand(
|
|
||||||
duration: Long = 500,
|
duration: Long = 500,
|
||||||
targetWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT,
|
targetWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
targetHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT
|
targetHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||||
|
onMeasured: View.() -> Unit,
|
||||||
|
onPerformAnimation: View.(interpolatedTime: Float, t: Transformation?) -> Unit
|
||||||
) {
|
) {
|
||||||
measure(targetWidth, targetHeight)
|
measure(targetWidth, targetHeight)
|
||||||
val measuredHeight: Int = measuredHeight
|
onMeasured()
|
||||||
layoutParams.height = 0
|
show()
|
||||||
visibility = View.VISIBLE
|
|
||||||
val a: Animation = object : Animation() {
|
val a: Animation = object : Animation() {
|
||||||
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
|
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
|
||||||
super.applyTransformation(interpolatedTime, t)
|
super.applyTransformation(interpolatedTime, t)
|
||||||
layoutParams.height = if (interpolatedTime == 1f) targetHeight else (measuredHeight * interpolatedTime).toInt()
|
onPerformAnimation(interpolatedTime, t)
|
||||||
requestLayout()
|
requestLayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun willChangeBounds(): Boolean = true
|
||||||
|
}
|
||||||
|
|
||||||
|
a.duration = duration
|
||||||
|
startAnimation(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun View.performCollapse(
|
||||||
|
duration: Long = 500,
|
||||||
|
onPerformAnimation: View.(interpolatedTime: Float, t: Transformation?) -> Unit
|
||||||
|
) {
|
||||||
|
val a: Animation = object : Animation() {
|
||||||
|
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
|
||||||
|
if (interpolatedTime == 1f) {
|
||||||
|
gone()
|
||||||
|
} else {
|
||||||
|
onPerformAnimation(interpolatedTime, t)
|
||||||
|
requestLayout()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun willChangeBounds(): Boolean {
|
override fun willChangeBounds(): Boolean {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -32,27 +53,58 @@ fun View.expand(
|
|||||||
startAnimation(a)
|
startAnimation(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
fun View.expand(
|
||||||
|
duration: Long = 500,
|
||||||
|
targetWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
targetHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||||
|
) {
|
||||||
|
var measuredHeight = 0
|
||||||
|
performExpand(
|
||||||
|
duration,
|
||||||
|
targetWidth,
|
||||||
|
targetHeight,
|
||||||
|
{
|
||||||
|
measuredHeight = this.measuredHeight
|
||||||
|
}
|
||||||
|
) { interpolatedTime, _ ->
|
||||||
|
layoutParams.height = if (interpolatedTime == 1f) targetHeight else (measuredHeight * interpolatedTime).toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
fun View.expandHorizontally(
|
||||||
|
duration: Long = 500,
|
||||||
|
targetWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
targetHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||||
|
) {
|
||||||
|
var measuredWidth = 0
|
||||||
|
performExpand(
|
||||||
|
duration,
|
||||||
|
targetWidth,
|
||||||
|
targetHeight,
|
||||||
|
{
|
||||||
|
measuredWidth = this.measuredWidth
|
||||||
|
}
|
||||||
|
) { interpolatedTime, _ ->
|
||||||
|
layoutParams.width = if (interpolatedTime == 1f) targetWidth else (measuredWidth * interpolatedTime).toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
fun View.collapse(duration: Long = 500) {
|
fun View.collapse(duration: Long = 500) {
|
||||||
val initialHeight: Int = measuredHeight
|
val initialHeight: Int = measuredHeight
|
||||||
val a: Animation = object : Animation() {
|
performCollapse(duration) { interpolatedTime, _ ->
|
||||||
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
|
|
||||||
if (interpolatedTime == 1f) {
|
|
||||||
visibility = View.GONE
|
|
||||||
} else {
|
|
||||||
layoutParams.height = initialHeight - (initialHeight * interpolatedTime).toInt()
|
layoutParams.height = initialHeight - (initialHeight * interpolatedTime).toInt()
|
||||||
requestLayout()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun willChangeBounds(): Boolean {
|
@PreviewFeature
|
||||||
return true
|
fun View.collapseHorizontally(duration: Long = 500) {
|
||||||
|
val initialWidth: Int = measuredWidth
|
||||||
|
performCollapse(duration) { interpolatedTime, _ ->
|
||||||
|
layoutParams.width = initialWidth - (initialWidth * interpolatedTime).toInt()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
a.duration = duration
|
|
||||||
|
|
||||||
startAnimation(a)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
@@ -74,3 +126,15 @@ fun View.toggleExpandState(duration: Long = 500): Boolean = if (isCollapsed) {
|
|||||||
collapse(duration)
|
collapse(duration)
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true in case of expanding
|
||||||
|
*/
|
||||||
|
@PreviewFeature
|
||||||
|
fun View.toggleExpandHorizontallyState(duration: Long = 500): Boolean = if (isCollapsed) {
|
||||||
|
expandHorizontally(duration)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
collapseHorizontally(duration)
|
||||||
|
false
|
||||||
|
}
|
||||||
|
@@ -19,7 +19,7 @@ fun <T> CoroutineScope.actor(
|
|||||||
|
|
||||||
inline fun <T> CoroutineScope.safeActor(
|
inline fun <T> CoroutineScope.safeActor(
|
||||||
channelCapacity: Int = Channel.UNLIMITED,
|
channelCapacity: Int = Channel.UNLIMITED,
|
||||||
noinline onException: ExceptionHandler<Unit> = {},
|
noinline onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
|
||||||
crossinline block: suspend (T) -> Unit
|
crossinline block: suspend (T) -> Unit
|
||||||
): Channel<T> = actor(
|
): Channel<T> = actor(
|
||||||
channelCapacity
|
channelCapacity
|
||||||
|
@@ -16,7 +16,7 @@ inline fun <T> Flow<T>.subscribe(scope: CoroutineScope, noinline block: suspend
|
|||||||
*/
|
*/
|
||||||
inline fun <T> Flow<T>.subscribeSafely(
|
inline fun <T> Flow<T>.subscribeSafely(
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
noinline onException: ExceptionHandler<Unit> = { throw it },
|
noinline onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
|
||||||
noinline block: suspend (T) -> Unit
|
noinline block: suspend (T) -> Unit
|
||||||
) = subscribe(scope) {
|
) = subscribe(scope) {
|
||||||
safely(onException) {
|
safely(onException) {
|
||||||
|
@@ -2,23 +2,72 @@ package dev.inmo.micro_utils.coroutines
|
|||||||
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.supervisorScope
|
import kotlinx.coroutines.supervisorScope
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.coroutineContext
|
||||||
|
|
||||||
typealias ExceptionHandler<T> = suspend (Throwable) -> T
|
typealias ExceptionHandler<T> = suspend (Throwable) -> T
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This instance will be used in all calls of [safely] where exception handler has not been passed
|
||||||
|
*/
|
||||||
|
var defaultSafelyExceptionHandler: ExceptionHandler<Nothing> = { throw it }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key for [SafelyExceptionHandler] which can be used in [CoroutineContext.get] to get current default
|
||||||
|
* [SafelyExceptionHandler]
|
||||||
|
*/
|
||||||
|
class SafelyExceptionHandlerKey<T> : CoroutineContext.Key<SafelyExceptionHandler<T>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut for creating instance of [SafelyExceptionHandlerKey]
|
||||||
|
*/
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <T> safelyExceptionHandlerKey() = SafelyExceptionHandlerKey<T>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for [ExceptionHandler] which can be used in [CoroutineContext] to set local (for [CoroutineContext]) default
|
||||||
|
* [ExceptionHandler]. To get it use [CoroutineContext.get] with key [SafelyExceptionHandlerKey]
|
||||||
|
*
|
||||||
|
* @see SafelyExceptionHandlerKey
|
||||||
|
* @see ExceptionHandler
|
||||||
|
*/
|
||||||
|
class SafelyExceptionHandler<T>(
|
||||||
|
val handler: ExceptionHandler<T>
|
||||||
|
) : CoroutineContext.Element {
|
||||||
|
|
||||||
|
override val key: CoroutineContext.Key<*> = safelyExceptionHandlerKey<T>()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions
|
* It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions
|
||||||
*
|
*
|
||||||
|
* Priorities of [ExceptionHandler]s:
|
||||||
|
*
|
||||||
|
* * [onException] In case if custom (will be used anyway if not [defaultSafelyExceptionHandler])
|
||||||
|
* * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key
|
||||||
|
* * [defaultSafelyExceptionHandler]
|
||||||
|
*
|
||||||
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
|
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
|
||||||
* exception will be available for catching
|
* exception will be available for catching
|
||||||
|
*
|
||||||
|
* @see defaultSafelyExceptionHandler
|
||||||
|
* @see safelyWithoutExceptions
|
||||||
|
* @see SafelyExceptionHandlerKey
|
||||||
|
* @see SafelyExceptionHandler
|
||||||
*/
|
*/
|
||||||
suspend inline fun <T> safely(
|
suspend inline fun <T> safely(
|
||||||
noinline onException: ExceptionHandler<T> = { throw it },
|
noinline onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
||||||
noinline block: suspend CoroutineScope.() -> T
|
noinline block: suspend CoroutineScope.() -> T
|
||||||
): T {
|
): T {
|
||||||
return try {
|
return try {
|
||||||
supervisorScope(block)
|
supervisorScope(block)
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
onException(e)
|
val handler = if (onException == defaultSafelyExceptionHandler) {
|
||||||
|
coroutineContext[safelyExceptionHandlerKey<T>()] ?.handler ?: onException
|
||||||
|
} else {
|
||||||
|
onException
|
||||||
|
}
|
||||||
|
handler(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,12 +2,12 @@ package dev.inmo.micro_utils.coroutines
|
|||||||
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
fun <T> launchSynchronously(scope: CoroutineScope = CoroutineScope(Dispatchers.Default), block: suspend CoroutineScope.() -> T): T {
|
fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T): T {
|
||||||
var throwable: Throwable? = null
|
var throwable: Throwable? = null
|
||||||
var result: T? = null
|
var result: T? = null
|
||||||
val objectToSynchronize = java.lang.Object()
|
val objectToSynchronize = java.lang.Object()
|
||||||
val launchCallback = {
|
val launchCallback = {
|
||||||
scope.launch {
|
launch {
|
||||||
safely(
|
safely(
|
||||||
{
|
{
|
||||||
throwable = it
|
throwable = it
|
||||||
@@ -26,3 +26,5 @@ fun <T> launchSynchronously(scope: CoroutineScope = CoroutineScope(Dispatchers.D
|
|||||||
}
|
}
|
||||||
throw throwable ?: return result!!
|
throw throwable ?: return result!!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> launchSynchronously(block: suspend CoroutineScope.() -> T): T = CoroutineScope(Dispatchers.Default).launchSynchronously(block)
|
||||||
|
@@ -6,14 +6,14 @@ kotlin.incremental.js=true
|
|||||||
android.useAndroidX=true
|
android.useAndroidX=true
|
||||||
android.enableJetifier=true
|
android.enableJetifier=true
|
||||||
|
|
||||||
kotlin_version=1.4.20
|
kotlin_version=1.4.21
|
||||||
kotlin_coroutines_version=1.4.2
|
kotlin_coroutines_version=1.4.2
|
||||||
kotlin_serialisation_core_version=1.0.1
|
kotlin_serialisation_core_version=1.0.1
|
||||||
kotlin_exposed_version=0.28.1
|
kotlin_exposed_version=0.28.1
|
||||||
|
|
||||||
ktor_version=1.4.3
|
ktor_version=1.5.0
|
||||||
|
|
||||||
klockVersion=2.0.1
|
klockVersion=2.0.2
|
||||||
|
|
||||||
github_release_plugin_version=2.2.12
|
github_release_plugin_version=2.2.12
|
||||||
|
|
||||||
@@ -40,5 +40,5 @@ dokka_version=1.4.20
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.4.11
|
version=0.4.14
|
||||||
android_code_version=15
|
android_code_version=18
|
||||||
|
@@ -25,7 +25,7 @@ inline fun <T> HttpClient.createStandardWebsocketFlow(
|
|||||||
val producerScope = this@channelFlow
|
val producerScope = this@channelFlow
|
||||||
do {
|
do {
|
||||||
val reconnect = try {
|
val reconnect = try {
|
||||||
safely ({ throw it }) {
|
safely {
|
||||||
ws(correctedUrl) {
|
ws(correctedUrl) {
|
||||||
for (received in incoming) {
|
for (received in incoming) {
|
||||||
when (received) {
|
when (received) {
|
||||||
|
@@ -10,21 +10,6 @@ import kotlin.coroutines.Continuation
|
|||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
import kotlin.coroutines.resumeWithException
|
import kotlin.coroutines.resumeWithException
|
||||||
|
|
||||||
private data class CallbackContinuationPair<T> (
|
|
||||||
val callback: suspend SQLiteDatabase.() -> T,
|
|
||||||
val continuation: Continuation<T>
|
|
||||||
) {
|
|
||||||
suspend fun SQLiteDatabase.execute() {
|
|
||||||
safely(
|
|
||||||
{
|
|
||||||
continuation.resumeWithException(it)
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
continuation.resume(callback())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class StandardSQLHelper(
|
class StandardSQLHelper(
|
||||||
context: Context,
|
context: Context,
|
||||||
name: String,
|
name: String,
|
||||||
|
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,168 @@
|
|||||||
|
package dev.inmo.micro_utils.selector
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unified interface which can be used in any system which require some selection functionality
|
||||||
|
*/
|
||||||
|
interface Selector<T> {
|
||||||
|
val selectedItems: List<T>
|
||||||
|
val itemSelected: SharedFlow<T>
|
||||||
|
val itemUnselected: SharedFlow<T>
|
||||||
|
|
||||||
|
suspend fun toggleSelection(element: T)
|
||||||
|
suspend fun forceSelect(element: T)
|
||||||
|
suspend fun forceDeselect(element: T)
|
||||||
|
suspend fun clearSelection()
|
||||||
|
}
|
||||||
|
|
||||||
|
@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()
|
||||||
|
suspend inline fun <T> Selector<T>.toggleSelection(elements: List<T>) = elements.forEach { toggleSelection(it) }
|
||||||
|
suspend inline fun <T> Selector<T>.forceSelect(elements: List<T>) = elements.forEach { forceSelect(it) }
|
||||||
|
suspend inline fun <T> Selector<T>.forceDeselect(elements: List<T>) = elements.forEach { forceDeselect(it) }
|
||||||
|
suspend inline fun <T> Selector<T>.toggleSelection(firstElement: T, vararg elements: T) = toggleSelection(listOf(firstElement) + elements.toList())
|
||||||
|
suspend inline fun <T> Selector<T>.forceSelect(firstElement: T, vararg elements: T) = forceSelect(listOf(firstElement) + elements.toList())
|
||||||
|
suspend inline fun <T> Selector<T>.forceDeselect(firstElement: T, vararg elements: T) = forceDeselect(listOf(firstElement) + elements.toList())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Realization of [Selector] with one or without selected element. This realization will always have empty
|
||||||
|
* [selectedItems] when nothing selected and one element in [selectedItems] when something selected. Contains
|
||||||
|
* [selectedItem] value for simple access to currently selected item.
|
||||||
|
*
|
||||||
|
* On calling of [toggleSelection] previous selection will be erased and [itemUnselected] will emit this element.
|
||||||
|
*
|
||||||
|
* @param safeChanges Set to false to disable using of [mutex] for synchronizing changes on [toggleSelection]
|
||||||
|
*/
|
||||||
|
class SingleSelector<T>(
|
||||||
|
selectedItem: T? = null,
|
||||||
|
safeChanges: 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 (safeChanges) {
|
||||||
|
Mutex()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun forceDeselect(element: T) {
|
||||||
|
mutex ?.lock()
|
||||||
|
if (selectedItem == element) {
|
||||||
|
selectedItem = null
|
||||||
|
_itemUnselected.emit(element)
|
||||||
|
}
|
||||||
|
mutex ?.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun forceSelect(element: T) {
|
||||||
|
mutex ?.lock()
|
||||||
|
if (selectedItem != element) {
|
||||||
|
selectedItem = element
|
||||||
|
_itemSelected.emit(element)
|
||||||
|
}
|
||||||
|
mutex ?.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun clearSelection() {
|
||||||
|
selectedItem ?.let { forceDeselect(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Realization of [Selector] with multiple selected elements. On calling of [toggleSelection] this realization will select passed element OR deselect it if it is already in
|
||||||
|
* [selectedItems]
|
||||||
|
*
|
||||||
|
* @param safeChanges Set to false to disable using of [mutex] for synchronizing changes on [toggleSelection]
|
||||||
|
*/
|
||||||
|
class MultipleSelector<T>(
|
||||||
|
selectedItems: List<T> = emptyList(),
|
||||||
|
safeChanges: 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 (safeChanges) {
|
||||||
|
Mutex()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun forceDeselect(element: T) {
|
||||||
|
mutex ?.lock()
|
||||||
|
if (_selectedItems.remove(element)) {
|
||||||
|
_itemUnselected.emit(element)
|
||||||
|
}
|
||||||
|
mutex ?.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun forceSelect(element: T) {
|
||||||
|
mutex ?.lock()
|
||||||
|
if (element !in _selectedItems && _selectedItems.add(element)) {
|
||||||
|
_itemSelected.emit(element)
|
||||||
|
}
|
||||||
|
mutex ?.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun toggleSelection(element: T) {
|
||||||
|
mutex ?.lock()
|
||||||
|
if (_selectedItems.remove(element)) {
|
||||||
|
_itemUnselected.emit(element)
|
||||||
|
} else {
|
||||||
|
_selectedItems.add(element)
|
||||||
|
_itemSelected.emit(element)
|
||||||
|
}
|
||||||
|
mutex ?.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun clearSelection() {
|
||||||
|
mutex ?.lock()
|
||||||
|
val preSelectedItems = _selectedItems.toList()
|
||||||
|
_selectedItems.clear()
|
||||||
|
preSelectedItems.forEach { _itemUnselected.emit(it) }
|
||||||
|
mutex ?.unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("FunctionName", "NOTHING_TO_INLINE")
|
||||||
|
inline fun <T> Selector(
|
||||||
|
multiple: Boolean,
|
||||||
|
safeChanges: Boolean = true
|
||||||
|
): Selector<T> = if (multiple) {
|
||||||
|
MultipleSelector(safeChanges = safeChanges)
|
||||||
|
} else {
|
||||||
|
SingleSelector(safeChanges = safeChanges)
|
||||||
|
}
|
@@ -0,0 +1,17 @@
|
|||||||
|
package dev.inmo.micro_utils.selector
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Returned [SharedFlow] will emit true when [element] has been selected in [this] [Selector] and will emit
|
||||||
|
* false when this [element] was deselected
|
||||||
|
*
|
||||||
|
* @see [Selector]
|
||||||
|
* @see [Selector.itemSelected]
|
||||||
|
* @see [Selector.itemUnselected]
|
||||||
|
*/
|
||||||
|
fun <T> Selector<T>.itemSelectionFlow(element: T, scope: CoroutineScope): SharedFlow<Boolean> = MutableSharedFlow<Boolean>().apply {
|
||||||
|
itemSelected.onEach { if (it == element) emit(true) }.launchIn(scope)
|
||||||
|
itemUnselected.onEach { if (it == element) emit(false) }.launchIn(scope)
|
||||||
|
}.asSharedFlow()
|
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",
|
||||||
|
Reference in New Issue
Block a user