jean
11/06/2026, 8:21 AMinterface CorruptedSaleChecker {
val corruptedSalesInfo: State<UserInfo.CorruptedSales?>
suspend fun promptIfCorruptedSales()
suspend fun resolveCorruptedSales()
fun dismissCorruptedSales()
}
class DefaultCorruptedSaleChecker() { ... }
class SaleHistoryViewModel(
...,
corruptedSaleChecker: CorruptedSaleChecker,
) : ViewModel(),
... // other shared capabilities, not tied to ViewModel from android
CorruptedSaleChecker by corruptedSaleChecker {
...
}
This way I can share the logic to show and hide dialog + the “action” (resolveCorruptedSales in this case) so a user can trigger it through a button later on if they don’t want to do whatever the dialog is recommending when navigating to the screen.
I did think of having a composable with a dedicated view model that would own all the logic to check statuses and ui logic for the dialog, but I would miss the ability of triggering the action from outside the dialog.
What do you all think of that? Have you solved something similar in another way?jean
11/06/2026, 8:24 AMState object from the interface. My view models have a centralized StateFlow<ScreenState> property that let the UI observe a single source of state, but now I have two or more if more capabilities are shared.Ronaldo Pace
11/06/2026, 9:48 AMUser Interface
-------------
View Model
-------------
Use Cases
-------------
Repositories
-------------
Services
(reminder I'm not advocating to anyone to implement all this on their app, those are established tools and patterns and it's up to us to apply accordingly when needed)
So there’s two patterns there between ViewModel and Service that could be applied to your case.
There’s CheckCorruptSalesUseCase or a SalesRepository that can implement the business logic. Then the ViewModel just receive an instance of this UseCase/Repo via the dependency injection. Given you got 4 functions together, maybe a repository is the winner here to group together them.
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Your other question is about the ScreenState. And I would definitely favour a single State coming from the ViewModel. What I’ve done in the past is to add the re-usable state inside the screen state. For example:
data class MyScreenState (
val id: String,
val a: Something,
val b: OtherSomething,
val corruptedSales: UserInfo.CorruptedSales?
)
this can be trivially combined using combine(Flow<T>, Flow<R>): Flow<O>.
And similar pattern to add it to user interaction/events:
enum class CorruptedSalesEvents {
Prompt, Resolve, Dismiss
}
// and then in the ViewModel
sealed class ScreenUiEvents {
data object OnCtaTap: ScreenUiEvents()
data class OnCorruptedSales(val event: CorruptedSalesEvents): ScreenUiEvents()
}
this makes it easy to build your re-usable composable based only on UserInfo.CorruptedSales? and CorruptedSalesEvents but the API between any ViewModel and any Screen in the whole project is always consistent containing 1 ScreenState and 1 ScreenEvents.
Does that make sense? I hope it helps.jean
11/06/2026, 9:54 AMCorruptedSaleChecker build on top of it. The implementation of suspend fun resolveCorruptedSales() from the interface is the use case. I did have the use case as a dependency in each view model and they all had a state with val corruptedSales: UserInfo.CorruptedSales? like you described. It works fine but I felt like it was “a lot” of duplicated code to add the feature where it’s needed.Ronaldo Pace
11/06/2026, 10:19 AMcombine<> the data into 1 stream, and whenever the user send events to the ViewModel, it simply viewModelScope.launch { repo.resolveCorruptSales() }
Also important to just repeat the warning:
I'm not advocating to anyone to implement all this on their app, those are established tools and patterns and it's up to us to apply accordingly when needed
Are you saying that you made a repository, and then use cases and then to the ViewModel? Cause that I agree it’s a funny overkill.Ronaldo Pace
11/06/2026, 10:23 AMclass MyViewModel(
private val salesRepo: CorruptedSaleChecker
): ViewModel() {
private val viewModelData = MutableStateFlow<Something>
val state: StateFlow<ScreenState> = combine(viewModelData, salesRepo.corruptedSalesInfo) { state, sales ->
ScreenState(state.foo, state.bar, sales)
}
fun onUiEvent(event: UiEvent) = when(event) {
is PromptCorruptedSales -> viewModelScope.launch { salesRepo.promptIfCorruptedSales() }
}
is ... other events
}
it’s just the ViewModel delegating the sales checking to something else.jean
11/06/2026, 10:34 AMSaleRepository abstracting all the calls to our Room database.
• ResolveCorruptedSales is our usecase, a pure domain logic function indenpendent of Room (through SaleRepository and from Android).
• Then our view model has a dependency on ResolveCorruptedSales
The duplication I talked about is that we need the same code combining whatever comes from the usecase in the screen state, and the onUiEvent . We have UiEvent equivalent types but we limit them to each screen, so it means the same events in each screens too.
What I tried with the interface delegation was to group the usecase dependency, event handling and state handling in one “capability class” that each view model can use. It’s really just an experimentation, I could keep the code we have that is very similar to what you say.Ronaldo Pace
11/06/2026, 10:51 AM// reusable component
// state
data class ReusableState(....)
// event
enum class ReusableEvent { ... } // or maybe it is a sealed class
// handling:
val state: StateFlow<ReusableState>
fun onReusableEvent(event: ReusableEvent) { ...
then on the ViewModel:
data class ScreenState(
val reusableState: ReusableState,
... other
)
sealed class ScreenEvent {
data class OnReusableEvent(val event: ReusableEvent): ScreenEvent
}
this leads IMHO the ViewModel code duplication to be a matter of 4 lines of code. The combine as example on my previous message, and the event like:
when(screenEvent) {
is OnReusableEvent -> reusable.onReusableEvent(screenEvent.event)
}
So the ViewModel just encapsulates the models from the different layer and then delegates its actions to it.
But yes, I agree there’s a little repetition that I worked around by structuring the ViewModel and the other layer in manner to minimize it.Ronaldo Pace
11/06/2026, 10:51 AMCiaran Sloan
11/06/2026, 11:56 PMSamyaraj Das
11/08/2026, 3:07 AMCorruptedSaleChecker instance across the two ViewModels.
If they share an instance, should dismissing the warning on one screen also dismiss it on the other? If each has its own instance, what should happen if both try to resolve the same sale?
It might be useful to test those two cases explicitly. I haven’t tried this with your implementation, but it seems like a good way to decide which state belongs to the screen and which belongs in the shared data layer.Guillaume B
11/09/2026, 8:58 AMSamyaraj Das
11/11/2026, 5:05 AM