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:
- In CalculationFragment's onViewCreated() I call
val sessionId = presenter.firstCall() as String
to retrieve a sessionToken for further process. - 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:
- In my presenter I go back to my Fragment with the sessionToken
view?.onFirstCallResult(sessionToken)
and call from the CalculationFragment'sonFirstCallResult()
immediatelypresenter.secondCall(sessionToken, NfcAdapter.getDefaultAdapter(activity))
. - 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