Skip to content

Commit

Permalink
Merge pull request #63 from MohamedRejeb/0.3.1
Browse files Browse the repository at this point in the history
Add WasmJs Support
  • Loading branch information
MohamedRejeb authored Mar 3, 2024
2 parents 519a97d + b9d165c commit 299055f
Show file tree
Hide file tree
Showing 45 changed files with 866 additions and 402 deletions.
9 changes: 6 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
plugins {
id("root.publication")
//trick: for the same plugin versions in all sub-modules
// id("root.publication")
// trick: for the same plugin versions in all sub-modules
alias(libs.plugins.androidApplication).apply(false)
alias(libs.plugins.androidLibrary).apply(false)
alias(libs.plugins.kotlinMultiplatform).apply(false)
alias(libs.plugins.kotlinAndroid).apply(false)
alias(libs.plugins.kotlinJvm).apply(false)
alias(libs.plugins.kotlinSerialization).apply(false)
alias(libs.plugins.composeMultiplatform).apply(false)
}
alias(libs.plugins.conventionPlugin).apply(false)
}

rootPublicationSetup()
46 changes: 3 additions & 43 deletions calf-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.composeMultiplatform)
alias(libs.plugins.androidLibrary)
id("module.publication")
}

kotlin {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
common {
group("nonAndroid") {
withJvm()
withIos()
withJs()
}
}
}
androidTarget {
publishLibraryVariants("release")
compilations.all {
kotlinOptions {
jvmTarget = "11"
}
}
}
jvm("desktop") {
jvmToolchain(11)
}
js(IR) {
browser()
}
iosX64()
iosArm64()
iosSimulatorArm64()
composeMultiplatformSetup()
modulePublicationSetup()

kotlin {
sourceSets.commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
}
}

android {
namespace = "com.mohamedrejeb.calf.core"
compileSdk = libs.versions.android.compileSdk.get().toInt()
defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}
40 changes: 6 additions & 34 deletions calf-file-picker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,24 @@ plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.composeMultiplatform)
alias(libs.plugins.androidLibrary)
id("module.publication")
}

kotlin {
kotlin.applyDefaultHierarchyTemplate()
androidTarget {
publishLibraryVariants("release")
compilations.all {
kotlinOptions {
jvmTarget = "11"
}
}
}
jvm("desktop") {
jvmToolchain(11)
}
js(IR) {
browser()
}
iosX64()
iosArm64()
iosSimulatorArm64()
composeMultiplatformSetup()
modulePublicationSetup()

sourceSets.commonMain.get().dependencies {
kotlin {
sourceSets.commonMain.dependencies {
api(projects.calfIo)

implementation(compose.runtime)
implementation(compose.foundation)
}
sourceSets.commonTest.get().dependencies {

sourceSets.commonTest.dependencies {
implementation(libs.kotlin.test)
}

sourceSets.androidMain.dependencies {
implementation(libs.activity.compose)
}
}

android {
namespace = "com.mohamedrejeb.calf.picker"
compileSdk = libs.versions.android.compileSdk.get().toInt()
defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ actual fun rememberFilePickerLauncher(
fileDialogVisible = false
})

js("fileInputElement.click();")
js("fileInputElement.click()")

Unit
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mohamedrejeb.calf.picker

import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toComposeImageBitmap
import org.jetbrains.skia.Image

actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap()
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.mohamedrejeb.calf.picker

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import com.mohamedrejeb.calf.io.KmpFile
import kotlinx.browser.document
import org.w3c.dom.Element
import org.w3c.files.File

@Composable
actual fun rememberFilePickerLauncher(
type: FilePickerFileType,
selectionMode: FilePickerSelectionMode,
onResult: (List<KmpFile>) -> Unit,
): FilePickerLauncher {
var fileDialogVisible by rememberSaveable { mutableStateOf(false) }

return remember {
FilePickerLauncher(
type = type,
selectionMode = selectionMode,
onLaunch = {
val fileInputElement = document.createElement("input")
fileInputElement.setAttribute("style", "display='none'")
fileInputElement.setAttribute("type", "file")
fileInputElement.setAttribute("name", "file")

fileInputElement.setAttribute("accept", type.value.joinToString(", "))

if (selectionMode == FilePickerSelectionMode.Multiple)
fileInputElement.setAttribute("multiple", "true")
else
fileInputElement.removeAttribute("multiple")

fileInputElement.addEventListener("change") {
val filesCount = getInputElementFilesCount(fileInputElement)
val files =
List(filesCount) { index ->
getInputElementFile(fileInputElement, index)
}
onResult(files.map { KmpFile(it) })
fileDialogVisible = false
}

openFileDialog(fileInputElement)
},
)
}
}

actual class FilePickerLauncher actual constructor(
type: FilePickerFileType,
selectionMode: FilePickerSelectionMode,
private val onLaunch: () -> Unit,
) {
actual fun launch() {
onLaunch()
}
}

private fun getInputElementFilesCount(element: Element): Int = js("element.files.length")

private fun getInputElementFile(
element: Element,
index: Int,
): File = js("element.files[index]")

private fun openFileDialog(element: Element): Unit = js("element.click()")
5 changes: 2 additions & 3 deletions calf-geo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.composeMultiplatform)
alias(libs.plugins.androidLibrary)
// id("module.publication")
}

kotlin {
Expand All @@ -25,11 +24,11 @@ kotlin {
iosArm64()
iosSimulatorArm64()

sourceSets.commonMain.get().dependencies {
sourceSets.commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
}
sourceSets.androidMain.get().dependencies {
sourceSets.androidMain.dependencies {
implementation(libs.appcompat)
implementation(libs.lifecycle.extensions)
implementation(libs.play.services.location)
Expand Down
37 changes: 5 additions & 32 deletions calf-io/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidLibrary)
id("module.publication")
}

kotlin {
kotlin.applyDefaultHierarchyTemplate()
androidTarget {
publishLibraryVariants("release")
compilations.all {
kotlinOptions {
jvmTarget = "11"
}
}
}
jvm("desktop") {
jvmToolchain(11)
}
js(IR) {
browser()
}
iosX64()
iosArm64()
iosSimulatorArm64()
kotlinMultiplatformSetup()
modulePublicationSetup()

kotlin {
sourceSets.commonMain.dependencies {
api(projects.calfCore)

implementation(libs.documentfile)
}
}

android {
namespace = "com.mohamedrejeb.calf.io"
compileSdk = libs.versions.android.compileSdk.get().toInt()
defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceSets.androidMain.dependencies {
implementation(libs.documentfile)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mohamedrejeb.calf.io

import com.mohamedrejeb.calf.core.PlatformContext
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
import org.w3c.dom.events.Event
import org.w3c.files.File
import org.w3c.files.FileReader
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

/**
* Represents a file in a platform-agnostic way.
*/
actual class KmpFile(
val file: File,
)

actual fun KmpFile.exists(context: PlatformContext) = true

actual suspend fun KmpFile.readByteArray(context: PlatformContext): ByteArray =
suspendCoroutine { continuation ->
val fileReader = FileReader()
fileReader.readAsArrayBuffer(file)
fileReader.onloadend = { event ->
val readyState = getReadyState(event)
if (readyState == FileReader.DONE) {
val arrayBuffer: ArrayBuffer = getResult(event)
val array = Uint8Array(arrayBuffer)
val byteArray =
ByteArray(array.length) { index ->
array[index]
}
continuation.resume(byteArray)
} else {
continuation.resume(ByteArray(0))
}
}
}

actual fun KmpFile.getName(context: PlatformContext): String? = file.name

actual fun KmpFile.getPath(context: PlatformContext): String? = file.name

actual fun KmpFile.isDirectory(context: PlatformContext): Boolean = !file.name.contains(".")

private fun getReadyState(event: Event): Short = js("event.target.readyState")

private fun getResult(event: Event): ArrayBuffer = js("event.target.result")
5 changes: 2 additions & 3 deletions calf-maps/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.composeMultiplatform)
alias(libs.plugins.androidLibrary)
// id("module.publication")
}

kotlin {
Expand All @@ -25,12 +24,12 @@ kotlin {
iosArm64()
iosSimulatorArm64()

sourceSets.commonMain.get().dependencies {
sourceSets.commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
}
sourceSets.commonTest.get().dependencies {
sourceSets.commonTest.dependencies {
implementation(libs.kotlin.test)
}
}
Expand Down
5 changes: 2 additions & 3 deletions calf-media/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.composeMultiplatform)
alias(libs.plugins.androidLibrary)
// id("module.publication")
}

kotlin {
Expand All @@ -25,12 +24,12 @@ kotlin {
iosArm64()
iosSimulatorArm64()

sourceSets.commonMain.get().dependencies {
sourceSets.commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
}
sourceSets.commonTest.get().dependencies {
sourceSets.commonTest.dependencies {
implementation(libs.kotlin.test)
}
}
Expand Down
Loading

0 comments on commit 299055f

Please sign in to comment.