0

I have a special case in my project that relies on MVP pattern.

The use case is that when clicking a button an contact email should be sent. The email is created dynamically, a combination from some input fields and some data from repository and string resources.

The flow starts like this:

Fragment/View:

sendButton.setOnClickListener { presenter.onSendButtonClick(
    emailField.text.toString(),
    questionField.text.toString(),
    getSubjectWithTimeStamp(presenter.getMyObject(), getString(R.string.app_name))
)}

private fun getMailSubjectTimeStamp(object: MyObject? = null, appName: String): String {
    val timeStamp = MAIL_SUBJECT_TIMESTAMP_FORMAT.format(Date())
    return if (object != null) {
        getString(
            R.string.help_contactForm_subject_with_data,
            appName,
            object.someInfo,
            timeStamp
        )
    } else {
        getString(
            R.string.help_contactForm_subject,
            appName,
            timeStamp
        )
    }
}

Presenter:

private var lastKnownObject: MyObject? = null

override fun onBound(view: ContactContract.View) {
    super.onBound(view)
    scope.launch {
        // This is fetched from DB behind the scene via the Interactor.
        lastKnownObject = myObjectInteractor.readLastKnownObject()
    }
}

override fun getLastKnownObject(): MyObject? {
    return lastKnownObject
}

override fun onSendButtonClick(userMail: String, questionText: String, subject: String) {
    sendMailAndShowFeedback(userMail, questionText, subject)
}

This works, but I'm not very happy with the fact that I need to expose a variable from my presenter and manually access it before triggering the flow with onSendButtonClick. Any other ideas on how to achieve this kind of combination and still have my separation of concerns?

Ionut Negru
  • 6,186
  • 4
  • 48
  • 78
  • Create a StringProvider and inject it to the presenter. The string provider implementation will internally use the app context to get strings. Then move the `getMailSubjectTimeStamp` into the presenter keeping view as simple as possible. That way you can easily unit test the presenter logic. – Derek K Aug 22 '22 at 18:13
  • Thanks @DerekK, that makes sense as it will keep the presenter cleaner (instead of having that getter on it). The downside is that I will add Context aware components in my presenter. Will give it a go nonetheless. – Ionut Negru Aug 24 '22 at 09:35

0 Answers0