moxy-community / Moxy

Moxy is MVP library for Android with incremental annotation processor and ktx features
MIT License
324 stars 33 forks source link

How to save command for restoring, but not execute right now #137

Open spectosha opened 2 years ago

spectosha commented 2 years ago

In Activity I fill EditText and each entered character i call presenter.setName(name). In presenter I save name as class parameter. But if I'll rotate screen the name will dissapear because I don't call viewState methods when I save name in presenter. I don't need to call some method in activity to save state like this (viewState.onNameChanged()), but moxy makes me to do it. So I'd like to offer you to create feature for saving view state command without calling methods in activity if activity doesn't need to restore state.

For example when I sets name in presenter i call viewState.onRestoreName(name) and this command will reproduced in activity when activity need to restore state, but not right now.

alaershov commented 2 years ago

Hi!

This is an example of a general problem with Android View system and MVP - state duplication and the lack of a single source of truth.

In general, if you store some state in your Presenter - the name, in your case - it's your responsibility to pass this state to the UI with some viewState calls, for instance, viewState.showName(name). Maybe you don't need this in your particular case, because the name is not changed by the Presenter, but when you add some kind of logic, for example, name validation - you'll have to call viewState anyway.

So, to recap: you should always render the most actual state stored in Presenter, and you do it by calling viewState methods. I don't think that adding another kind of state restoration to Moxy will make things better, because it'll only make the problem of inconsistent state worse. And if you absolutely need to restore some state without calling viewState methods - well, there's always savedInstanceState, which actually should restore your EditText state out of the box.