galex

    galex

    2 years ago
    Ok I might be too tired to work on this at 11pm instead of going to sleep, but what is wrong with this that the load of the value doesn’t trigger recomposition?
    @Composable
    fun ProfileScreen(userUid: String) {
    
        val userRepository = UserRepository()
        val user = mutableStateOf<User?>(null)
    
        launchInComposition {
            user.value = userRepository.get(userUid)
            Log.d(TAG, "url = ${user.value?.profileUrl}")
        }
    
        user.value?.profileUrl?.let { CoilImage(data = it) } ?: Text("No Image!")
    }
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago
    val userRepository = UserRepository()
    Is
    UserRepository
    a regular constructor or function? If so, this should be
    remember { UserRepository() }
    val user = mutableStateOf<User?>(null)
    Similar here, this should be
    remember { mutableStateOf<User?>(null) }
    launchInComposition {
    If you want this coroutine to re-launch when the user ID changes, do
    launchInComposition(userUid) {
    .
    There’s also a
    state<User?> { null }
    function, but that’s being deprecated and replaced by
    remember { mutableStateOf() }
    .
    Without the
    remember
    calls, both your repository and that
    MutableState
    object will be instantiated on every composition pass.
    The documentation on
    launchInComposition
    explains what the
    key
    parameter is for: https://developer.android.com/reference/kotlin/androidx/compose/package-summary#launchincomposition_1
    galex

    galex

    2 years ago
    Thank you for your explanations, it starts to become clearer! Here
    UserRepository
    is a new Object which will be passed to this Composable. And yes I misued
    launchInComposition
    , I thought that as it sounds like
    launch
    it’s the same signature but no 🙂