mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
add a set of tools for JS and Web Compose
This commit is contained in:
parent
8f790360bc
commit
9e84dc5031
18
common/compose/build.gradle
Normal file
18
common/compose/build.gradle
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
alias(libs.plugins.jb.compose)
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppProjectWithSerializationAndComposePresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api project(":micro_utils.common")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package dev.inmo.micro_utils.common.compose
|
||||||
|
|
||||||
|
import androidx.compose.runtime.DisposableEffectResult
|
||||||
|
|
||||||
|
class DefaultDisposableEffectResult(
|
||||||
|
private val onDispose: () -> Unit
|
||||||
|
) : DisposableEffectResult {
|
||||||
|
override fun dispose() {
|
||||||
|
onDispose()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val DoNothing = DefaultDisposableEffectResult {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
package dev.inmo.micro_utils.common.compose
|
||||||
|
|
||||||
|
import org.jetbrains.compose.web.attributes.ATarget
|
||||||
|
|
||||||
|
fun openLink(link: String, mode: ATarget = ATarget.Blank, features: String = "") = dev.inmo.micro_utils.common.openLink(
|
||||||
|
link,
|
||||||
|
mode.targetStr,
|
||||||
|
features
|
||||||
|
)
|
||||||
|
|
1
common/compose/src/main/AndroidManifest.xml
Normal file
1
common/compose/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.common.compose"/>
|
@ -0,0 +1,8 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.browser.window
|
||||||
|
|
||||||
|
fun openLink(link: String, target: String = "_blank", features: String = "") {
|
||||||
|
window.open(link, target, features) ?.focus()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.browser.document
|
||||||
|
import kotlinx.dom.createElement
|
||||||
|
import org.w3c.dom.HTMLElement
|
||||||
|
import org.w3c.dom.HTMLInputElement
|
||||||
|
import org.w3c.files.get
|
||||||
|
|
||||||
|
fun selectFile(
|
||||||
|
inputSetup: HTMLInputElement.() -> Unit = {},
|
||||||
|
onFailure: (Throwable) -> Unit = {},
|
||||||
|
onFile: (MPPFile) -> Unit
|
||||||
|
) {
|
||||||
|
(document.createElement("input") {
|
||||||
|
(this as HTMLInputElement).apply {
|
||||||
|
inputSetup()
|
||||||
|
onchange = {
|
||||||
|
runCatching {
|
||||||
|
files ?.get(0) ?: error("File must not be null")
|
||||||
|
}.onSuccess {
|
||||||
|
onFile(it)
|
||||||
|
}.onFailure {
|
||||||
|
onFailure(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as HTMLElement).click()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.browser.document
|
||||||
|
import org.w3c.dom.HTMLAnchorElement
|
||||||
|
|
||||||
|
fun triggerDownloadFile(filename: String, fileLink: String) {
|
||||||
|
val hiddenElement = document.createElement("a") as HTMLAnchorElement
|
||||||
|
|
||||||
|
hiddenElement.href = fileLink
|
||||||
|
hiddenElement.target = "_blank"
|
||||||
|
hiddenElement.download = filename
|
||||||
|
hiddenElement.click()
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,11 @@ kotlin {
|
|||||||
api libs.kt.coroutines
|
api libs.kt.coroutines
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
jsMain {
|
||||||
|
dependencies {
|
||||||
|
api project(":micro_utils.common")
|
||||||
|
}
|
||||||
|
}
|
||||||
androidMain {
|
androidMain {
|
||||||
dependencies {
|
dependencies {
|
||||||
api libs.kt.coroutines.android
|
api libs.kt.coroutines.android
|
||||||
|
@ -12,6 +12,7 @@ kotlin {
|
|||||||
commonMain {
|
commonMain {
|
||||||
dependencies {
|
dependencies {
|
||||||
api libs.kt.coroutines
|
api libs.kt.coroutines
|
||||||
|
api project(":micro_utils.coroutines")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package dev.inmo.micro_utils.coroutines.compose
|
||||||
|
|
||||||
|
import androidx.compose.runtime.MutableState
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
|
||||||
|
fun <T> Flow<T>.toMutableState(
|
||||||
|
initial: T,
|
||||||
|
scope: CoroutineScope
|
||||||
|
): MutableState<T> {
|
||||||
|
val state = mutableStateOf(initial)
|
||||||
|
subscribeSafelyWithoutExceptions(scope) { state.value = it }
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> StateFlow<T>.toMutableState(
|
||||||
|
scope: CoroutineScope
|
||||||
|
): MutableState<T> = toMutableState(value, scope)
|
||||||
|
|
@ -0,0 +1,42 @@
|
|||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.common.MPPFile
|
||||||
|
import dev.inmo.micro_utils.common.selectFile
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
import org.w3c.dom.HTMLInputElement
|
||||||
|
|
||||||
|
suspend fun selectFile(
|
||||||
|
inputSetup: HTMLInputElement.() -> Unit = {}
|
||||||
|
): MPPFile {
|
||||||
|
val result = CompletableDeferred<MPPFile>()
|
||||||
|
|
||||||
|
selectFile(
|
||||||
|
inputSetup,
|
||||||
|
{
|
||||||
|
result.completeExceptionally(it)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
result.complete(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.await()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun selectOptionalFile(
|
||||||
|
inputSetup: HTMLInputElement.() -> Unit = {},
|
||||||
|
onFailure: (Throwable) -> Unit = {}
|
||||||
|
): MPPFile? {
|
||||||
|
val result = CompletableDeferred<MPPFile?>()
|
||||||
|
|
||||||
|
selectFile(
|
||||||
|
inputSetup,
|
||||||
|
{
|
||||||
|
result.complete(null)
|
||||||
|
onFailure(it)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
result.complete(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.await()
|
||||||
|
}
|
@ -2,6 +2,7 @@ rootProject.name='micro_utils'
|
|||||||
|
|
||||||
String[] includes = [
|
String[] includes = [
|
||||||
":common",
|
":common",
|
||||||
|
":common:compose",
|
||||||
":matrix",
|
":matrix",
|
||||||
":crypto",
|
":crypto",
|
||||||
":selector:common",
|
":selector:common",
|
||||||
|
Loading…
Reference in New Issue
Block a user