Skip to content

Commit

Permalink
Added example usage of PeekabooCamera composable in sample app (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
onseok committed Dec 15, 2023
1 parent 999f645 commit fca3f61
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 60 deletions.
2 changes: 1 addition & 1 deletion sample/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ kotlin {

// peekaboo
implementation(projects.peekabooImagePicker)
implementation(projects.peekabooCamera)
implementation(projects.peekabooUi)
}
}

Expand Down
127 changes: 68 additions & 59 deletions sample/common/src/commonMain/kotlin/com/preat/peekaboo/common/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package com.preat.peekaboo.common

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.SnackbarHost
import androidx.compose.material.SnackbarHostState
Expand All @@ -29,10 +28,12 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import com.preat.peekaboo.camera.rememberCameraCaptureLauncher
import com.preat.peekaboo.image.picker.SelectionMode
import com.preat.peekaboo.image.picker.rememberImagePickerLauncher
import com.preat.peekaboo.image.picker.toImageBitmap
import com.preat.peekaboo.ui.BackButton
import com.preat.peekaboo.ui.PeekabooCamera
import com.preat.peekaboo.ui.style.PeekabooTheme
import kotlinx.coroutines.launch

@Suppress("FunctionName")
Expand All @@ -41,6 +42,7 @@ fun App() {
val scope = rememberCoroutineScope()
var images by remember { mutableStateOf(listOf<ImageBitmap>()) }
val snackbarHostState = remember { SnackbarHostState() }
var showCamera by remember { mutableStateOf(false) }

val singleImagePicker =
rememberImagePickerLauncher(
Expand All @@ -67,69 +69,76 @@ fun App() {
},
)

val cameraCaptureLauncher =
rememberCameraCaptureLauncher(
scope = scope,
onResult = {
it?.let { capturedImage ->
images = listOf(capturedImage.toImageBitmap())
}
},
onCameraPermissionDenied = {
scope.launch {
snackbarHostState.showSnackbar(
message =
"Camera permission has been denied.\n" +
"To use the camera, please change the settings.",
)
}
},
)

MaterialTheme {
PeekabooTheme {
Scaffold(snackbarHost = { SnackbarHost(snackbarHostState) }) {
Column(
modifier =
Modifier
.fillMaxSize()
.padding(20.dp),
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
LazyRow(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
items(images) { image ->
Image(
bitmap = image,
contentDescription = "Selected Image",
modifier =
Modifier
.size(100.dp)
.clip(CircleShape),
contentScale = ContentScale.Crop,
if (showCamera) {
Box {
if (showCamera) {
PeekabooCamera(
modifier = Modifier.fillMaxSize(),
onCapture = { byteArray ->
byteArray?.let {
images = listOf(it.toImageBitmap())
} ?: scope.launch {
snackbarHostState.showSnackbar(
message =
"Error occurred.",
)
}
showCamera = false
},
)
}
TopLayout(
alignLeftContent = {
BackButton {
showCamera = false
}
},
alignRightContent = {},
)
}
}
Spacer(modifier = Modifier.height(20.dp))
Button(
onClick = {
singleImagePicker.launch()
},
) {
Text("Pick Single Image")
}
Button(
onClick = {
multipleImagePicker.launch()
},
) {
Text("Pick Multiple Images")
}
Button(
onClick = {
cameraCaptureLauncher.launch()
},
) {
Text("Capture Camera Image")
} else {
LazyRow(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
items(images) { image ->
Image(
bitmap = image,
contentDescription = "Selected Image",
modifier =
Modifier
.size(100.dp)
.clip(CircleShape),
contentScale = ContentScale.Crop,
)
}
}
Spacer(modifier = Modifier.height(20.dp))
Button(
onClick = {
singleImagePicker.launch()
},
) {
Text("Pick Single Image")
}
Button(
onClick = {
multipleImagePicker.launch()
},
) {
Text("Pick Multiple Images")
}
Button(
onClick = {
showCamera = true
},
) {
Text("Capture Camera Image")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.preat.peekaboo.common

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Suppress("FunctionName")
@Composable
fun TopLayout(
alignLeftContent: @Composable () -> Unit = {},
alignRightContent: @Composable () -> Unit = {},
) {
Box(
Modifier
.fillMaxWidth()
.windowInsetsPadding(WindowInsets.systemBars)
.padding(12.dp),
) {
Row(Modifier.align(Alignment.CenterStart)) {
alignLeftContent()
}
Row(Modifier.align(Alignment.CenterEnd)) {
alignRightContent()
}
}
}

0 comments on commit fca3f61

Please sign in to comment.