MiKro is super lightweight Kotlin Multiplatform library with simple utilities
Gradle
implementation("ru.astrainteractive.klibs:mikro-core:<version>")
implementation("ru.astrainteractive.klibs:mikro-platform:<version>")
implementation("ru.astrainteractive.klibs:mikro-extensions:<version>")
implementation("ru.astrainteractive.klibs:mikro-locale:<version>")
implementation("ru.astrainteractive.klibs:mikro-validation:<version>")
Version catalogs
[versions]
klibs-mikro = "<latest-version>"
[libraries]
klibs-mikro-core = { module = "ru.astrainteractive.klibs:mikro-core", version.ref = "klibs-mikro" }
klibs-mikro-platform = { module = "ru.astrainteractive.klibs:mikro-platform", version.ref = "klibs-mikro" }
klibs-mikro-extensions = { module = "ru.astrainteractive.klibs:mikro-extensions", version.ref = "klibs-mikro" }
klibs-mikro-locale = { module = "ru.astrainteractive.klibs:mikro-locale", version.ref = "klibs-mikro" }
klibs-mikro-validation = { module = "ru.astrainteractive.klibs:mikro-validation", version.ref = "klibs-mikro" }
object SomeDI {
lateinit var platform: PlatformConfiguration
}
// android application
fun onCreate() {
SomeDI.platform = DefaultAndroidPlatformConfiguration(applicationContext)
}
// on jvm there's DefaultJVMPlatformConfiguration
// for js there's DefaultJSPlatformConfiguration
// for native there's DefaultNativePlatformConfiguration
Use it in repositories
class MyRepository(
private val customDispatchers: KotlinDispatchers = DefaultKotlinDispatchers
) {
suspend fun getSomeValue() = withContext(customDispatchers.IO) {
// some logic
}
}
// Or create custom
class CustomKDispatchers : KotlinDispatchers {
override val Main: CoroutineDispatcher
get() = Dispatchers.Default
override val IO: CoroutineDispatcher
get() = IODispatcher.Default
override val Default: CoroutineDispatcher
get() = Dispatchers.Default
override val Unconfined: CoroutineDispatcher
get() = Dispatchers.Default
}
class StringMapper : Mapper<String, Int> {
override fun toDTO(it: String): Int {
return it.toInt()
}
override fun fromDTO(it: Int): String {
return it.toString()
}
}
// Use simple UseCase
class IntUseCase : UseCase.Suspended.Simple<Int> {
override suspend operator fun invoke(): Int {
return 10
}
}
// Or Parametrized and blocking
class MultiplyUseCase : UseCase.Blocking.Parametrized<MultiplyUseCase.Param, Int> {
class Param(val value: Int, val multiplyBy: Int)
override operator fun invoke(input: Param): Int {
return input.value * input.multiplyBy
}
}
enum class NumEnum {
ONE, TWO, THREE
}
fun sample() {
val oneEnum = NumEnum.ONE
val twoEnum = oneEnum.next()
val oneEnumAgain = oneEnum.next().next()
}
fun sample(): StateFlow<String> {
val stringStateFlow = MutableStateFlow("String")
val intStateFlow = MutableStateFlow(10)
return combineStates(
flow1 = stringStateFlow,
flow2 = intStateFlow,
transform = { s, i ->
"string: $s; int: $i"
}
)
}
Validation allows you to validate changed of your textfields for example
All validations stored in order which it was created
val validator = DefaultValidator<String, String> {
validate("Not enough length") { it.length > 2 }
validate("Not contains symbol '*'") { it.contains("*") }
}
// will return ValidatorResult.Failure("Not enough length")
validator.validate("_")
// will return ValidatorResult.Failure("Not contains symbol '*'")
validator.validate("___")
// will return ValidatorResult.Success
validator.validate("___*")
// Or create custom validators
object MailValidator : Validator<String, String> by DefaultValidator(
context = {
validate("Not contains @ symbol") {
it.contains("@")
}
}
)
val validationResult = MailValidator.validate("[email protected]")
// Is validation successful
validationResult.isSuccess
// Is validation failed
validationResult.isFailure
// Returns first violation
validationResult.violationOrNull
Platform: JVM, iOS
// Create Locale Provider
val localeProvider = LocaleProvider()
// will get available locales with code, name
localeProvider.availableLocales
// will give current system language
localeProvider.systemDefaultLanguage
// Will return ru_RU locale info
localeProvider.fromCode(SharedLocale.Code("ru_RU"))
localeProvider.fromCode(SharedLocale.Code("ru"))
Platform: Jvm,Android,Apple
// Use time formatter
val timeFormatter: TimeFormatter = TODO()
timeFormatter.format(instant, "yyyy-MM-dd", timeZone)
// Use string provider
val stringProvider: StringProvider = TODO()
stringProvider.toString(StringDesc.Raw("Hello"))