1

On MVP pattern, i have string with dynamic value %

Example:

<string name="hello">%s hello</string>

and i need set this text with "my name" on my textview, how i will do this witout reference direct a R.String on my presenter layer.

public void onItemClicked(String name) {
        if (mainView != null) {
             //HOW use R.string.hello from Strings  here? [presenter layer]
            mainView.showMessage(String.format("%s hello", name));
        }
    }

On MVP pattern i cant have any reference an Android class in presenter layer, i dont have any context in this class, but i need use R.string.hello, because translate, how i can take this witouth ruins this MVP pattern

rcorbellini
  • 1,307
  • 1
  • 21
  • 42

2 Answers2

3

Quick answer: you don't

You structure your code so your view method is:

@Override
public void showMessage(String name){

    if (mTextView != null){
        mTextView.setText(String.format(getString(R.string.hello), name));
    }
}

Then your presenter code is:

public void onItemClicked(String name) {
    if (mainView != null) {
        mainView.showMessage(name);
    }
}

MVP is all about clean testable code, in this case all you want to be testing within your presenter is that the presenter passes the correct name to the view. You don't need to be testing String.format() or getting strings from resources (other developers have already done this, ie. the Android devs). I suggest maybe reading a bit deeper into why MVP will benefit your project

Joe Maher
  • 5,354
  • 5
  • 28
  • 44
  • Thx so much, and using that logic can't use in my presenter something like showmessage( getview(R.id.text) , text) because have a R class right? – rcorbellini Feb 21 '17 at 23:53
  • 1
    I've never tried that approach but it might work, but i wouldn't suggest it. As again when unit testing, your unit tests won't have any understanding of that R class – Joe Maher Feb 21 '17 at 23:56
0

There is an overloaded version of getString() which takes varargs for formatting.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • On MVP pattern i cant have any reference an Android class in presenter layer, i dont have any context in this class, but i need use R.string.hello, because translate, how i can take this witouth ruins this MVP pattern – rcorbellini Feb 21 '17 at 22:59
  • @rcorbellini You must have a context to load a string resource. One possibility is to load it somewhere else and pass to the presenter. Alternatively create a wrapper class which the presenter can call. This wrapper then delegates to a context. – Code-Apprentice Feb 21 '17 at 23:03
  • i know i need a context to get a string from resource, but what is the options? Create a class "StringsHelper[with acess to context]" and send to presenter on construct? but this is a indirect dependence (no?) can i use this without broken this patter ? – rcorbellini Feb 21 '17 at 23:12
  • @rcorbellini I gave two suggestions in my earlier comment. – Code-Apprentice Feb 21 '17 at 23:13