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

Halil Ozercan

09/19/2020, 12:25 PM
I've taken SwipeToRefresh implementation from JetNews into my own project. However, I feel like there is an important bug in the implementation that comes to surface when the state of the UI directly starts in refreshing (loading) status.
Copy code
refreshingState: Boolean,
is an argument to the SwipeToRefresh composable. It makes total sense that refreshing status is controlled by the parent. However, inner swipe state and this received state of refresh are combined together to call
onRefresh
callback inside the listener for swipe state change as follows:
Copy code
val state = rememberSwipeableState(refreshingState) { newValue ->
        // compare both copies of the swipe state before calling onRefresh(). This is a workaround.
        if (newValue && !refreshingState) onRefresh()
        true
    }
However, something goes wrong in this lambda. Even if the
refreshingState
is changed from
true
to
false
by the parent, this lambda will keep reading it as the first time
remember
is called. If
refreshingState
was true at the start, it will remain true in the scope of this lambda until composition of
SwipeRefreshLayout
leaves the UI. As a workaround, I've changed
refreshingState
to a lambda that returns a boolean.
a

Adam Powell

09/19/2020, 11:40 PM
If you've tried this on alpha03 and still see it, please file an issue for it
h

Halil Ozercan

09/20/2020, 2:24 PM
initially experienced this issue on alpha03. Going to file an issue asap
👍 1
a

Arkadii Ivanov

02/26/2021, 11:28 PM
Same for me. @Halil Ozercan could you please post the workaround snippet?
h

Halil Ozercan

02/27/2021, 10:50 AM
@Arkadii Ivanov It's been so long I don't even remember in which project I had this issue. I vaguely remember that hoisting boolean state caused this problem. Now there is
rememberUpdatedState
which might be helpful.
🙏 1
2 Views