Skip to content

Commit

Permalink
✨ Add rememberTakePhotoLauncher
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceglb committed Nov 6, 2024
1 parent bc82580 commit e626638
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
17 changes: 13 additions & 4 deletions filekit-dialog-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,44 @@ kotlin {
implementation(libs.kotlinx.coroutines.core)

// FileKit Dialog
api(projects.filekitCore)
api(projects.filekitDialog)
}

val nonWebMain by creating {
dependsOn(commonMain.get())
}

val mobileMain by creating {
dependsOn(nonWebMain)
}

val nonAndroidMain by creating {
dependsOn(commonMain.get())
}

androidMain {
dependsOn(nonWebMain)
dependsOn(mobileMain)
dependencies {
implementation(libs.androidx.activity.compose)
}
}

nativeMain {
dependsOn(nonWebMain)
dependsOn(nonAndroidMain)
}
jvmMain {
dependsOn(nonWebMain)
dependsOn(nonAndroidMain)
dependencies {
implementation(compose.ui)
}
}

nativeMain {
dependsOn(nonWebMain)
dependsOn(nonAndroidMain)
}
iosMain.get().dependsOn(mobileMain)

wasmJsMain.get().dependsOn(nonAndroidMain)
jsMain.get().dependsOn(nonAndroidMain)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.vinceglb.filekit.dialog.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import io.github.vinceglb.filekit.FileKit
import io.github.vinceglb.filekit.PlatformFile
import io.github.vinceglb.filekit.dialog.takePhoto
import kotlinx.coroutines.launch

@Composable
public fun rememberTakePhotoLauncher(
onResult: (PlatformFile?) -> Unit,
): PhotoResultLauncher {
// Init FileKit
InitFileKit()

// Coroutine
val coroutineScope = rememberCoroutineScope()

// Updated state
val currentOnResult by rememberUpdatedState(onResult)

// FileKit
val fileKit = remember { FileKit }

// FileKit launcher
val returnedLauncher = remember {
PhotoResultLauncher {
coroutineScope.launch {
val result = fileKit.takePhoto()
currentOnResult(result)
}
}
}

return returnedLauncher
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.vinceglb.filekit.dialog.compose

public class PhotoResultLauncher(
private val onLaunch: () -> Unit,
) {
public fun launch() {
onLaunch()
}
}

0 comments on commit e626638

Please sign in to comment.