Trying to follow the guidance of treating events/e...
# android
t
Trying to follow the guidance of treating events/effects as state https://developer.android.com/jetpack/guide/ui-layer/events#views_1 For some things where you need to be truly imperative with views, such as
webView.goBackward()/webView.goForward()
, how do you encode them as state? They’re very event-like and need to happen just once given certain conditions, and could get complicated when state objects are `data class`es and you use
.copy
to update parts of state. More in 🧵
For example, if you had:
Copy code
data class State(val goBackInWebView: Boolean, val buttonColor: Color)
You might do something like:
Copy code
// business logic change
state.copy(goBackInWebView = true) // setting it to true

// theme change
state.copy(buttonColor = Red) // goBackInWebView is still true
The first line causes
goBackInWebView
to be true, but after the second line, you might read the state where
goBackInWebView
is still true. Setting
goBackInWebView = false
in the second line where you’re changing a part of the state in response to a theme change only is weird. OR: are `data class`es a bad option in this case, and a
sealed class
is better? Not sure what that would look like but something like
Copy code
sealed class State {
    data class ColorChanged(val buttonColor: Color): State()
    object WebViewBack: State()
}
b
🙏🏼 1
t
That’s super helpful; thank you!