This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object AnySerializer : KSerializer<Any> { | |
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Any") | |
override fun serialize(encoder: Encoder, value: Any) { | |
val jsonEncoder = encoder as JsonEncoder | |
val jsonElement = serializeAny(value) | |
jsonEncoder.encodeJsonElement(jsonElement) | |
} | |
private fun serializeAny(value: Any?): JsonElement = when (value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Compat версия метода [View.getWindowInsetsController]. */ | |
fun View.windowInsetsControllerCompat(): WindowInsetsControllerCompat? = | |
ViewCompat.getWindowInsetsController(this) | |
/** Показать клавиатуру и установить фокус на [view]. */ | |
fun showKeyboard(view: EditText): Boolean = | |
view.windowInsetsControllerCompat()?.let { controller -> | |
view.requestFocus() | |
view.post { controller.show(WindowInsetsCompat.Type.ime()) } | |
} != null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Перевод из Px в Dp. */ | |
fun Int.pxToDp(displayMetrics: DisplayMetrics = Resources.getSystem().displayMetrics): Int = | |
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, toFloat(), displayMetrics).roundToInt() | |
/** Перевод из Dp в Px. */ | |
fun Int.dpToPx(displayMetrics: DisplayMetrics = Resources.getSystem().displayMetrics): Int = | |
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, toFloat(), displayMetrics).roundToInt() | |
/** Перевод из Sp в Px. */ | |
fun Int.spToPx(displayMetrics: DisplayMetrics = Resources.getSystem().displayMetrics): Int = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Монада [Either] создан, чтобы передать состояние, которое может находиться в одном из двух вариантов: | |
* [Either.Left] и [Either.Right]. Если Вы передаёте ошибку и результат, то ошибка должна быть [Either.Left], | |
* а результат [Either.Right]. Пример: `Either<NetworkError, LoadedUser>`. | |
*/ | |
sealed class Either<out A, out B> { | |
/** Левая ветвь. */ | |
data class Left<T>(val value: T) : Either<T, Nothing>() | |
/** Правая ветвь. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Найти цвет в точке между двумя цветами в заданном месте. | |
* @param percent Значение в диапазоне от 0 до 1. | |
*/ | |
fun pointBetweenColors(from: Float, to: Float, percent: Float): Float = | |
from + percent * (to - from) | |
/** | |
* Найти цвет в точке между двумя цветами в заданном месте. | |
* @param percent Значение в диапазоне от 0 до 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> List<T>.hasDuplicates(): Boolean { | |
val accumulator = mutableSetOf<T>() | |
return any { !accumulator.add(it) } | |
} | |
fun main() { | |
println(listOf(1, 2, 5).hasDuplicates()) | |
println(listOf(-5, 8, 0, 8).hasDuplicates()) | |
println(emptyList<Int>().hasDuplicates()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lens<Entity : Any, Value : Any?>( | |
val get: (Entity) -> Value, | |
val set: (Entity, Value) -> Entity | |
) | |
fun <Entity : Any, Value : Any?> Flow<Entity>.lens(lens: Lens<Entity, Value>): Flow<Value> = | |
map { lens.get(it) }.distinctUntilChanged() | |
fun <Entity1 : Any, Entity2 : Any, Value : Any?> Lens<Entity1, Entity2>.compose(other: Lens<Entity2, Value>): Lens<Entity1, Value> = | |
Lens( |