从 Util/Jetpack Composee 访问 rememberLauncherForActivityResult [英] Accessing rememberLauncherForActivityResult from Util / Jetpack Composee

查看:220
本文介绍了从 Util/Jetpack Composee 访问 rememberLauncherForActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可组合的照片,允许用户从他们的移动图库中选择照片.

I have a photo composable that allows the user to pickup the photo from the their mobile gallery.

我想从 Util 类/函数中获得这个函数/服务,因为我有其他需要相同服务的组合,我不想继续复制/粘贴相同的代码.这也允许我通过从 launch { } 更改 Mime 类型来使用它来获取文档但这对我来说看起来很棘手.如果我使用@Composable 函数,我就不能使用 .launch此外,如果我将它保留为一个具有值的类,rememberLauncherForActivityResult 需要可组合.

I want to have this function/service from Util class/Function because i have other composable that needs the same service and i don't want to keep copy/pasting the same code. also this allows me to use it to pick up documents by changing the Mime type from launch { } but this looks tricky for me. if i use a @Composable function, i can't use the .launch also if i kept it as a class with a value the rememberLauncherForActivityResult needs Composable.

有什么帮助吗?

@OptIn(ExperimentalCoilApi::class)
@Composable
fun ContactPagePhoto() {
    val imageUriState = remember {
        mutableStateOf<Uri?>(null)
    }
    val pickingUp = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) {
        imageUriState.value = it
    }
    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .layoutId("userPhoto")
            .fillMaxHeight(0.4f)
            .fillMaxWidth(1f)
    ) {
        imageUriState.value?.let {
            Image(
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .fillMaxSize(1f),
                contentDescription = "UserImage",
                contentScale = ContentScale.Crop,
                painter = rememberImagePainter(data = it)
            )
        }
    }
    TextButton(
        onClick = { pickingUp.launch("image/*") },
        modifier = Modifier.layoutId("changePhoto")
    ) {
        Text(
            text = "Change Photo",
        color = Color.Black)
    }
}

我希望如何使用它...在 ``util``` 包中,我想创建一个包含我需要的服务的新 kotlin 文件

how i expect to use it... inside ``util``` package i want to create a new kotlin file that has the service i need

我可以使用一个@Composable 函数,它允许我使用 rememberLauncherForActivityResult或者创建一个类并将其扩展到 MainActivity 以访问 ComponentActivity() 并使用 registerForActivityResult 获取 Uri

I may use a @Composable function that allow me to use rememberLauncherForActivityResult or make a class and extend it to MainActivity to gain access to ComponentActivity() and use registerForActivityResult to get the Uri

喜欢

class PickUp(){
val launcher = registerForActivityResult...
}

@Composable
fun pickup():MutableState<Uri>{
rememberLauncherForActivityResult ...
}

只是建议...

推荐答案

你可以这样做:

class GetContentActivityResult(
    private val launcher: ManagedActivityResultLauncher<String, Uri>,
    val uri: Uri?
) {
    fun launch(mimeType: String) {
        launcher.launch(mimeType)
    }
}

@Composable
fun rememberGetContentActivityResult(): GetContentActivityResult {
    var uri by rememberSaveable { mutableStateOf<Uri?>(null) }
    val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent(), onResult = {
        uri = it
    })
    return remember(launcher, uri) {
        GetContentActivityResult(launcher, uri)
    }
}

用法:

@Composable
fun ContactPagePhoto() {
    val getContent = rememberGetContentActivityResult()
    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .layoutId("userPhoto")
            .fillMaxHeight(0.4f)
            .fillMaxWidth(1f)
    ) {
        getContent.uri?.let {
            Image(
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .fillMaxSize(1f),
                contentDescription = "UserImage",
                contentScale = ContentScale.Crop,
                painter = rememberImagePainter(data = it)
            )
        }
    }
    TextButton(
        onClick = { getContent.launch("image/*") },
        modifier = Modifier.layoutId("changePhoto")
    ) {
        Text(
            text = "Change Photo",
            color = Color.Black
        )
    }
}

这篇关于从 Util/Jetpack Composee 访问 rememberLauncherForActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆