From 8718c5e3104b92c95a771d8b3c8e9600ef73ba5a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 21 Dec 2024 09:02:31 +0600 Subject: [PATCH] update dependencies and add kdocs for percentage --- CHANGELOG.md | 5 ++ .../dev/inmo/micro_utils/common/Percentage.kt | 48 ++++++++++++++++--- gradle/libs.versions.toml | 8 ++-- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 838d5fa1a6b..64e1e21a4b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.24.0 +* `Versions`: + * `Coroutines`: `1.9.0` -> `1.10.1` + * `KSLog`: `1.3.6` -> `1.4.0` + * `Compose`: `1.7.1` -> `1.7.3` + * `Ktor`: `3.0.2` -> `3.0.3` * `Common`: * Rename `Progress` to more common `Percentage`. `Progress` now is typealias * Fix of `Progress.compareTo` extension diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Percentage.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Percentage.kt index 607ac9b2b33..5fe8f035faf 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Percentage.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Percentage.kt @@ -3,26 +3,45 @@ package dev.inmo.micro_utils.common import kotlinx.serialization.Serializable import kotlin.jvm.JvmInline +/** + * Contains [of1] as main value, where 100% of percentage is when of1 == 1 + * + * @see invoke + * @see partOfTotal + * @see of100 + */ @Serializable @JvmInline value class Percentage private constructor( + /** + * Value of percentage. When it equals to 1, means 100% + */ val of1: Double ) { + /** + * Same as [of1], but float (using [Double.toFloat]) + */ val of1Float get() = of1.toFloat() + + /** + * Represent this percentage as common percentage where 100% is 100% + */ val of100 get() = of1 * 100 + + /** + * Same as [of100], but float (using [Double.toFloat]) + */ val of100Float get() = of100.toFloat() + + /** + * Same as [of100], but int (using [Double.toInt]) + */ val of100Int get() = of100.toInt() - init { - require(of1 in rangeOfValues) { - "Progress main value should be in $rangeOfValues, but incoming value is $of1" - } - } - companion object { val rangeOfValues = 0.0 .. 1.0 @@ -33,8 +52,25 @@ value class Percentage private constructor( operator fun invoke(part: Number, total: Number) = Percentage( part.toDouble() / total.toDouble() ) + fun of1(of1: Double) = Percentage(of1 = of1) fun of100(of100: Double) = Percentage(of1 = of100 / 100) + fun partOfTotal(part: Number, total: Number) = Percentage(part = part, total = total) } } typealias Progress = Percentage + +/** + * Will return [this] [Progress] if [Percentage.of1] in `0 .. 1` range + */ +fun Progress.ensureStrictOrNull(): Progress? = if (of1 in Percentage.rangeOfValues) this else null +/** + * Will return [this] [Progress] if [Percentage.of1] in `0 .. 1` range. Otherwise, will throw error + * [IllegalArgumentException] due to [require] failure + */ +fun Progress.ensureStrictOrThrow(): Progress { + require(of1 in Percentage.rangeOfValues) { + "For strict checks value of percentage must be in ${Percentage.rangeOfValues}, but actual value is $of1" + } + return this +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5c0cb6139cb..8d2b8087ac9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,11 +2,11 @@ kt = "2.1.0" kt-serialization = "1.7.3" -kt-coroutines = "1.9.0" +kt-coroutines = "1.10.1" -kslog = "1.3.6" +kslog = "1.4.0" -jb-compose = "1.7.1" +jb-compose = "1.7.3" jb-exposed = "0.57.0" jb-dokka = "1.9.20" @@ -15,7 +15,7 @@ sqlite = "3.47.1.0" korlibs = "5.4.0" uuid = "0.8.4" -ktor = "3.0.2" +ktor = "3.0.3" gh-release = "2.5.2"