samedi 6 octobre 2018

How to save model state in MVP and Kotlin?

I've implemented a basic MVP where the model has a set of messages which are displayed in a round robin fashion to the user when the screen is touched. Each message is incapsulated in State class and passed to the CircularModel implementation.

This is the Viewer code that I used to instantiate the Model and the Presenter (is it legit that they are created by the Viewer?), and also the section to restore the Model state:

class MainActivity : AppCompatActivity(), ViewMVC {

    private lateinit var presenter: Presenter
    private lateinit var model : Model

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        model = if(savedInstanceState == null ) { CircularModel(LinkedList<State>(Arrays.asList(
                State("First"),
                State("Second"),
                State("Third"),
                State("Fourth"),
                State("Fifth")

        )))
        } else {
            savedInstanceState.getParcelable<Model>("model")
        }
        presenter = PresenterImpl(this, model)
    }
    // Rest of Viewer's code...
}

Since I want to save the state of CirciularModel, I made it @Parcelize:

@Parcelize
class CircularModel constructor(var states: @RawValue Deque<State>?) : Model, Parcelable {
    ...
}

However, in this way I have a type mismatch warning of Model and Model?. However, if I declare as private lateinit var model : Model? I get an error that lateinitcan't be used with nullable types.

How can I solve this? Am I doing something wrong in the pattern implementation?

Aucun commentaire:

Enregistrer un commentaire