Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private lateinit var systemPhotoPickManager: SystemPhotoPickManager
- class MainActivity : ComponentActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContent {
- ContentView()
- }
- }
- }
- @OptIn(ExperimentalMaterialApi::class)
- @Composable
- fun ContentView () {
- val contentImage = remember { mutableStateOf("") }
- val state = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
- val scope = rememberCoroutineScope()
- Box(modifier = Modifier
- .fillMaxSize()
- .background(Color.DarkGray))
- {
- BottomActionSheet(
- state = state,
- scope = scope,
- onTakeImage = {
- if (it) {
- //camera
- takePhoto {
- contentImage.value = it
- }
- } else {
- //gallery
- selectPhoto {
- contentImage.value = it
- }
- }
- }
- )
- {
- PhotoView(imagePath = contentImage.value, scope = scope, state = state)
- }
- }
- }
- @OptIn(ExperimentalMaterialApi::class)
- @Composable
- fun PhotoView(
- modifier: Modifier = Modifier,
- imagePath:String,
- scope: CoroutineScope,
- state: ModalBottomSheetState) {
- Image(painter = if (imagePath == "") {
- painterResource(R.drawable.ic_videocam_black_24dp)
- } else {
- rememberAsyncImagePainter(model = ImageRequest.Builder(context = LocalContext.current)
- .crossfade(true).data(imagePath).build(),
- filterQuality = FilterQuality.High
- )
- },
- contentDescription = null,
- modifier = modifier
- .fillMaxWidth()
- .padding(40.dp)
- .defaultMinSize(minHeight = 100.dp)
- .clickable {
- scope.launch {
- state.show()
- }
- }
- .background(Color.Transparent)
- .clip(RoundedCornerShape(16.dp))
- .border(1.dp, Color.Blue, RoundedCornerShape(16.dp)),
- contentScale = if (imagePath =="") {
- ContentScale.Inside
- } else {
- ContentScale.FillWidth
- }
- )
- }
- @OptIn(ExperimentalMaterialApi::class)
- @Composable
- fun BottomActionSheet(
- state:ModalBottomSheetState,
- scope:CoroutineScope,
- onTakeImage: (isCamera: Boolean) -> Unit,
- modalBottomSheetLayoutScope: @Composable () -> Unit)
- {
- ModalBottomSheetLayout(
- sheetState = state,
- sheetContent = {
- Column {
- BottomActionItem(
- title = "camera",
- resource = R.drawable.ic_videocam_black_24dp,
- isCamera = true) { isCamera ->
- scope.launch {
- state.hide()
- }
- onTakeImage(isCamera)
- }
- BottomActionItem(
- title = "gallery",
- resource = R.drawable.ic_videocam_black_24dp,
- isCamera = false) { isCamera ->
- scope.launch {
- state.hide()
- }
- onTakeImage(isCamera)
- }
- }
- }
- )
- {
- modalBottomSheetLayoutScope()
- }
- }
- @Composable
- fun BottomActionItem(
- modifier:Modifier = Modifier,
- title: String,
- resource:Int,
- isCamera:Boolean,
- onTakeImage:(isCamera:Boolean) -> Unit
- ) {
- Row(modifier = modifier
- .fillMaxWidth()
- .height(50.dp)
- .clickable { onTakeImage(isCamera) })
- {
- Image(
- painter = painterResource(resource),
- contentDescription = null,
- modifier = modifier
- .size(40.dp)
- .align(CenterVertically),
- contentScale = ContentScale.Inside)
- Spacer(modifier = modifier.width(10.dp))
- Text(
- text = title,
- color = Color.Black,
- fontSize = 15.sp,
- modifier = modifier.align(CenterVertically))
- }
- }
- private fun takePhoto(onResult:(result:String) ->Unit) {
- systemPhotoPickManager.requestPermission {
- it.takePictureWithFilePath(true) { imagePath ->
- onResult(imagePath)
- }
- }
- }
- private fun selectPhoto(onResult:(result:String) ->Unit) {
- systemPhotoPickManager.requestPermission {
- it.selectPathPicture(true) { imagePath ->
- onResult(imagePath)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement