Every ViewModel tutorial I read handed me the finished file: @HiltViewModel, a private MutableStateFlow, a public StateFlow, viewModelScope.launch, a sealed class. It compiled. I copied it. I understood nothing.

The problem with starting from finished code is that it looks arbitrary. Why two properties instead of one? Why init? Why start at Loading?

So I did it backwards. I threw the code away and asked: what problems is this thing actually solving? There are 8. Stack the 8 answers and the file writes itself — nothing in it is a style choice.

Here's the version I wish I had.

The 6-year-old version