Skip to content

Instantly share code, notes, and snippets.

@vishvendra01
Created September 30, 2018 06:33
Show Gist options
  • Save vishvendra01/82d3d06253624da9fc958e46f5c724fb to your computer and use it in GitHub Desktop.
Save vishvendra01/82d3d06253624da9fc958e46f5c724fb to your computer and use it in GitHub Desktop.
A helper class to show strings from strings.xml in presenters which can be mocked later in tests
public final class StringHelper {
private Context mContext;
@SuppressLint("StaticFieldLeak")
private static StringHelper sInstance;
private StringHelper(Context context) {
this.mContext = context;
}
public static void init(Context context) {
if (sInstance == null) {
sInstance = new StringHelper(context.getApplicationContext());
}
}
public static StringHelper getInstance() {
if (sInstance == null) {
throw new IllegalStateException("Initialize instance before getting instance");
}
return sInstance;
}
public String getString(@StringRes int value) {
return mContext.getString(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment