Compare commits

..

11 Commits

10 changed files with 105 additions and 5 deletions

View File

@@ -1,5 +1,15 @@
# Changelog
## 0.20.22
* `Common`:
* Add opportunity to create own `Diff` with base constructor
## 0.20.21
* `Resources`:
* Inited
## 0.20.20
* `Repos`:

View File

@@ -27,7 +27,7 @@ private inline fun <T> getObject(
* @see calculateDiff
*/
@Serializable
data class Diff<T> internal constructor(
data class Diff<T> @Warning(warning) constructor(
val removed: List<@Serializable(IndexedValueSerializer::class) IndexedValue<T>>,
/**
* Old-New values pairs
@@ -36,6 +36,10 @@ data class Diff<T> internal constructor(
val added: List<@Serializable(IndexedValueSerializer::class) IndexedValue<T>>
) {
fun isEmpty(): Boolean = removed.isEmpty() && replaced.isEmpty() && added.isEmpty()
companion object {
private const val warning = "This feature can be changed without any warranties. Use with caution and only in case you know what you are doing"
}
}
fun <T> emptyDiff(): Diff<T> = Diff(emptyList(), emptyList(), emptyList())

View File

@@ -15,5 +15,5 @@ crypto_js_version=4.1.1
# Project data
group=dev.inmo
version=0.20.20
android_code_version=226
version=0.20.22
android_code_version=228

View File

@@ -21,7 +21,7 @@ koin = "3.5.0"
okio = "3.6.0"
ksp = "1.9.21-1.0.15"
ksp = "1.9.21-1.0.16"
kotlin-poet = "1.15.3"
versions = "0.50.0"

View File

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

21
resources/build.gradle Normal file
View File

@@ -0,0 +1,21 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppJvmJsAndroidLinuxMingwLinuxArm64ProjectPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":micro_utils.language_codes")
}
}
androidMain {
dependsOn(jvmMain)
}
}
}

View File

@@ -0,0 +1,21 @@
package dev.inmo.micro_utils.strings
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import dev.inmo.micro_utils.language_codes.toIetfLanguageCode
fun StringResource.translation(configuration: Configuration): String = translation(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.locales[0]
} else {
configuration.locale
}
)
fun StringResource.translation(resources: Resources): String = translation(resources.configuration)
fun StringResource.translation(context: Context): String = translation(context.resources)
fun Configuration.translation(resource: StringResource): String = resource.translation(this)
fun Resources.translation(resource: StringResource): String = configuration.translation(resource)
fun Context.translation(resource: StringResource): String = resources.translation(resource)

View File

@@ -0,0 +1,32 @@
package dev.inmo.micro_utils.strings
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
class StringResource(
val default: String,
val map: Map<IetfLanguageCode, Lazy<String>>
) {
class Builder(
var default: String
) {
private val map = mutableMapOf<IetfLanguageCode, Lazy<String>>()
infix fun IetfLanguageCode.variant(value: Lazy<String>) {
map[this] = value
}
infix fun IetfLanguageCode.variant(value: String) = this variant lazyOf(value)
infix fun String.variant(value: Lazy<String>) = IetfLanguageCode(this) variant value
infix fun String.variant(value: String) = this variant lazyOf(value)
fun build() = StringResource(default, map.toMap())
}
fun translation(languageCode: IetfLanguageCode): String = (map[languageCode] ?: map[IetfLanguageCode(languageCode.withoutDialect)]) ?.value ?: default
}
inline fun buildStringResource(
default: String,
builder: StringResource.Builder.() -> Unit
): StringResource {
return StringResource.Builder(default).apply(builder).build()
}

View File

@@ -0,0 +1,10 @@
package dev.inmo.micro_utils.strings
import dev.inmo.micro_utils.language_codes.toIetfLanguageCode
import java.util.Locale
fun StringResource.translation(locale: Locale = Locale.getDefault()): String {
return translation(locale.toIetfLanguageCode())
}
fun Locale.translation(resource: StringResource): String = resource.translation(this)

View File

@@ -43,6 +43,8 @@ String[] includes = [
":startup:plugin",
":startup:launcher",
":resources",
":fsm:common",
":fsm:repos:common",