Merge pull request #388 from InsanusMokrassar/0.20.32

0.20.32
This commit is contained in:
InsanusMokrassar 2024-02-10 17:14:04 +06:00 committed by GitHub
commit f2857ee2be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 61 additions and 9 deletions

View File

@ -1,5 +1,13 @@
# Changelog # Changelog
## 0.20.32
* `Versions`:
* `Okio`: `3.7.0` -> `3.8.0`
* `Resources`:
* Make `StringResource` serializable
* Add several variants of builder usages
## 0.20.31 ## 0.20.31
* `Versions`: * `Versions`:

View File

@ -15,5 +15,5 @@ crypto_js_version=4.1.1
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.20.31 version=0.20.32
android_code_version=237 android_code_version=238

View File

@ -19,7 +19,7 @@ gh-release = "2.5.2"
koin = "3.5.3" koin = "3.5.3"
okio = "3.7.0" okio = "3.8.0"
ksp = "1.9.22-1.0.17" ksp = "1.9.22-1.0.17"
kotlin-poet = "1.16.0" kotlin-poet = "1.16.0"

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@ -1,6 +1,11 @@
package dev.inmo.micro_utils.strings package dev.inmo.micro_utils.strings
import dev.inmo.micro_utils.language_codes.IetfLang import dev.inmo.micro_utils.language_codes.IetfLang
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
/** /**
* Use this class as a type of your strings object fields. For example: * Use this class as a type of your strings object fields. For example:
@ -15,9 +20,10 @@ import dev.inmo.micro_utils.language_codes.IetfLang
* *
* @see buildStringResource * @see buildStringResource
*/ */
class StringResource( @Serializable(StringResource.Companion::class)
data class StringResource(
val default: String, val default: String,
val map: Map<IetfLang, Lazy<String>> val translations: Map<IetfLang, Lazy<String>>
) { ) {
class Builder( class Builder(
var default: String var default: String
@ -31,11 +37,18 @@ class StringResource(
infix fun IetfLang.variant(value: () -> String) = this variant lazy(value) infix fun IetfLang.variant(value: () -> String) = this variant lazy(value)
infix fun IetfLang.variant(value: String) = this variant lazyOf(value) infix fun IetfLang.variant(value: String) = this variant lazyOf(value)
operator fun IetfLang.invoke(value: () -> String) = this variant value
operator fun IetfLang.invoke(value: String) = this variant value
infix fun String.variant(value: Lazy<String>) = IetfLang(this) variant value infix fun String.variant(value: Lazy<String>) = IetfLang(this) variant value
infix fun String.variant(value: () -> String) = IetfLang(this) variant lazy(value) infix fun String.variant(value: () -> String) = IetfLang(this) variant lazy(value)
infix fun String.variant(value: String) = this variant lazyOf(value) infix fun String.variant(value: String) = this variant lazyOf(value)
operator fun String.invoke(value: () -> String) = this variant value
operator fun String.invoke(value: String) = this variant value
fun build() = StringResource(default, map.toMap()) fun build() = StringResource(default, map.toMap())
} }
@ -43,17 +56,48 @@ class StringResource(
if (languageCode == null) { if (languageCode == null) {
return default return default
} }
map[languageCode] ?.let { return it.value } translations[languageCode] ?.let { return it.value }
return languageCode.parentLang ?.let { return languageCode.parentLang ?.let {
map[it] ?.value translations[it] ?.value
} ?: default } ?: default
} }
companion object : KSerializer<StringResource> {
@Serializable
private class Surrogate(
val default: String,
val translations: Map<String, String>
)
override val descriptor: SerialDescriptor
get() = Surrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): StringResource {
val surrogate = Surrogate.serializer().deserialize(decoder)
return StringResource(
surrogate.default,
surrogate.translations.map { IetfLang(it.key) to lazyOf(it.value) }.toMap()
)
}
override fun serialize(encoder: Encoder, value: StringResource) {
Surrogate.serializer().serialize(
encoder,
Surrogate(
value.default,
value.translations.map {
it.key.code to it.value.value
}.toMap()
)
)
}
}
} }
inline fun buildStringResource( inline fun buildStringResource(
default: String, default: String,
builder: StringResource.Builder.() -> Unit builder: StringResource.Builder.() -> Unit = {}
): StringResource { ): StringResource {
return StringResource.Builder(default).apply(builder).build() return StringResource.Builder(default).apply(builder).build()
} }