Menu

Two screens need the same warning. Where should the state live?
📰

Two screens need the same warning. Where should the state live?

DEV Community: kotlin·Samya·4 days ago
#28GYgpxT
#devtokotlin#devto#dev#screen#state
Reading 0:00
15s threshold

Imagine an Android app with two screens: an order history screen and an account screen. Both need to warn the user when an earlier order needs attention. From either screen, the user can open a dialog, dismiss it or start a recovery action.

The first version might put the logic in the order history ViewModel. Then the account screen needs it too.

Sharing the recovery use case helps. Both ViewModels can call the same function. But each screen may still need to decide when to show the warning, hold the dialog state and handle the user’s actions. That’s where the duplication starts to feel less trivial.

One option is to extract that behaviour into a small presentation component that each ViewModel uses:

interface OrderWarning { val state : StateFlow < OrderWarningState >

fun check ( ) fun dismiss ( ) fun recover ( ) }

Enter fullscreen mode

Exit fullscreen mode

The component could use the shared domain logic while also managing the state needed to show the warning.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.