Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/makeevrserg/klibs.mikro
Kotlin multiplatform library with useful code
https://github.com/makeevrserg/klibs.mikro
kotlin kotlin-multiplatform kotlin-multiplatform-library
Last synced: about 2 months ago
JSON representation
Kotlin multiplatform library with useful code
- Host: GitHub
- URL: https://github.com/makeevrserg/klibs.mikro
- Owner: makeevrserg
- License: apache-2.0
- Created: 2023-07-21T10:14:32.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-28T19:26:38.000Z (3 months ago)
- Last Synced: 2024-10-29T21:12:40.516Z (3 months ago)
- Topics: kotlin, kotlin-multiplatform, kotlin-multiplatform-library
- Language: Kotlin
- Homepage:
- Size: 135 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[![version](https://img.shields.io/maven-central/v/ru.astrainteractive.klibs/mikro-core?style=flat-square)](https://github.com/makeevrserg/kstorage)
[![kotlin_version](https://img.shields.io/badge/kotlin-1.9.0-blueviolet?style=flat-square)](https://github.com/makeevrserg/kstorage)## MiKro
MiKro is super lightweight Kotlin Multiplatform library with simple utilities
## Installation
Gradle
```kotlin
implementation("ru.astrainteractive.klibs:mikro-core:")
implementation("ru.astrainteractive.klibs:mikro-platform:")
implementation("ru.astrainteractive.klibs:mikro-extensions:")
implementation("ru.astrainteractive.klibs:mikro-locale:")
implementation("ru.astrainteractive.klibs:mikro-validation:")
```Version catalogs
```toml
[versions]
klibs-mikro = ""[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" }
```### Platform
```kotlin
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
```### Dispatchers
Use it in repositories
```kotlin
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
}
```### Mapper
```kotlin
class StringMapper : Mapper {
override fun toDTO(it: String): Int {
return it.toInt()
}override fun fromDTO(it: Int): String {
return it.toString()
}
}
```### UseCase
```kotlin
// Use simple UseCase
class IntUseCase : UseCase.Suspended.Simple {
override suspend operator fun invoke(): Int {
return 10
}
}// Or Parametrized and blocking
class MultiplyUseCase : UseCase.Blocking.Parametrized {
class Param(val value: Int, val multiplyBy: Int)override operator fun invoke(input: Param): Int {
return input.value * input.multiplyBy
}
}
```### EnumExt
```kotlin
enum class NumEnum {
ONE, TWO, THREE
}fun sample() {
val oneEnum = NumEnum.ONE
val twoEnum = oneEnum.next()
val oneEnumAgain = oneEnum.next().next()
}
```### MapStateFlowExt
```kotlin
fun sample(): StateFlow {
val stringStateFlow = MutableStateFlow("String")
val intStateFlow = MutableStateFlow(10)
return combineStates(
flow1 = stringStateFlow,
flow2 = intStateFlow,
transform = { s, i ->
"string: $s; int: $i"
}
)
}
```### Validation [Experimental]
Validation allows you to validate changed of your textfields for example
All validations stored in order which it was created
```kotlin
val validator = DefaultValidator {
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 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
```## Locale
Platform: JVM, iOS
```kotlin
// 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"))
```## Extensions
Platform: Jvm,Android,Apple
```kotlin
// 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"))
```