mardi 30 avril 2019

How to avoid ping pong method call in MVP?

In my Android app, I have a Fragment in MVP pattern. Lets assume we have:

  • CalculationFragment (the view)
  • CalculationPresenter (the presenter)
  • CalculationNetwork (the model)

I need a multi-step calculation or REST call:

  1. In CalculationFragment's onViewCreated() I call val sessionId = presenter.firstCall() as String to retrieve a sessionToken for further process.
  2. Then the presenter retrieves the sessionToken via REST. So far so cool. Then I need the next call in chain, what consumes the retrieved sessionToken and a NfcAdapter.getDefaultAdapter(activity).

val jsonObject = secondCall(sessionId, nfcAdapter) .

Since I am in the presenter, I do neither have the activity nor the NfcAdapter here (and I honestly do not want to). I have two options here:

  1. In my presenter I go back to my Fragment with the sessionToken view?.onFirstCallResult(sessionToken) and call from the CalculationFragment's onFirstCallResult() immediately presenter.secondCall(sessionToken, NfcAdapter.getDefaultAdapter(activity)).
  2. I hand over the activity/NfcAdapter for the second call in the first call already and store it in the presenter. I would not need to pingpong between view and presenter a lot. Furtheron, I could remain in the presenter for all my calls?

What would be an elegant solution / pattern here?

Aucun commentaire:

Enregistrer un commentaire