https://kotlinlang.org logo
#compose
Title
# compose
m

Manuel Lorenzo

11/25/2020, 12:59 PM
hi everybody, how do you guys show and hide a progress indicator while some data is being loaded?
k

Kaustubh Patange

11/25/2020, 1:05 PM
Compose allow us to write code in a declarative approach. All you have to do is describe how the layout should look.
Copy code
@Composeable fun compose() {
   ...
   if (!isLoaded) {
       ShowProgressBar()
   } 
   ...
}
m

Manuel Lorenzo

11/25/2020, 1:06 PM
well in my case, I have something similar and it doesn’t fully work
Copy code
@Composable
fun RemotiCircularProgressIndicator(
    modifier: Modifier = Modifier,
    isLoading: Boolean = true,
    setIsLoading: (Boolean) -> Unit = {}
) {
    if (isLoading) {
        CircularProgressIndicator(
            modifier.then(
                Modifier
                    .fillMaxHeight(0.25f)
                    .fillMaxWidth(0.25f)
            )
        )
    }
}
in my case, that’s always being shown
I call it from another composable with
Copy code
val (isLoading, setIsLoading) = remember { mutableStateOf(viewState.loading) }
RemotiCircularProgressIndicator(
            modifier = Modifier.align(Alignment.CenterHorizontally),
            isLoading = isLoading,
            setIsLoading = setIsLoading,
        )
p

ppvi

11/25/2020, 1:22 PM
well looks like isLoading is never changing, looks like viewState.loading should be converted to state in a different way.
LiveData.observeAsState()
for example
👍 1
m

Manuel Lorenzo

11/25/2020, 1:25 PM
mmmm I didn’t think of converting it to a state itself
p

ppvi

11/25/2020, 1:26 PM
otherwise compose doesn't know that it has to recompose
👍 3
m

Manuel Lorenzo

11/25/2020, 1:47 PM
thanks for the tip Jose 👍
c

Colton Idle

11/25/2020, 8:43 PM
@ppvi do you think there would be a benefit to write a lint check that basically somehow be able to detect "Your type is not observable and so it will never recompose". I've hit this issue myself and so I'm just curious if there's more we can do here.