Fixed string loading, replaced hardcoded strings into string resources
This commit is contained in:
parent
277c29c8aa
commit
437ca3ae51
8 changed files with 219 additions and 103 deletions
98
integrations/java/pl/jakubweg/StringRef.java
Normal file
98
integrations/java/pl/jakubweg/StringRef.java
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package pl.jakubweg;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class StringRef {
|
||||
public static final String TAG = "jakubweg.StringRef";
|
||||
|
||||
private static Resources resources;
|
||||
private static String packageName;
|
||||
|
||||
/**
|
||||
* Called in Application onCreate, should be called as soon as possible when after application startup
|
||||
* @param context Any context, it will be used to obtain string resources
|
||||
*/
|
||||
public static void setContext(Context context) {
|
||||
if (context == null) return;
|
||||
resources = context.getApplicationContext().getResources();
|
||||
packageName = context.getPackageName();
|
||||
}
|
||||
|
||||
private static HashMap<String, StringRef> strings = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Gets strings reference from shared collection or creates if not exists yet,
|
||||
* this method should be called if you want to get StringRef
|
||||
* @param id string resource name/id
|
||||
* @return String reference that'll resolve to excepted string, may be from cache
|
||||
*/
|
||||
@NonNull
|
||||
public static StringRef sf(@NonNull String id) {
|
||||
StringRef ref = strings.get(id);
|
||||
if (ref == null) {
|
||||
ref = new StringRef(id);
|
||||
strings.put(id, ref);
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets string value by string id, shorthand for <code>sf(id).toString()</code>
|
||||
* @param id string resource name/id
|
||||
* @return String value from string.xml
|
||||
*/
|
||||
@NonNull
|
||||
public static String str(@NonNull String id) {
|
||||
return sf(id).toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a StringRef object that'll not change it's value
|
||||
* @param value value which toString() method returns when invoked on returned object
|
||||
* @return Unique StringRef instance, its value will never change
|
||||
*/
|
||||
@NonNull
|
||||
public static StringRef constant(@NonNull String value) {
|
||||
final StringRef ref = new StringRef(value);
|
||||
ref.resolved = true;
|
||||
return ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand for <code>constant("")</code>
|
||||
* Its value always resolves to empty string
|
||||
*/
|
||||
@NonNull
|
||||
public static final StringRef empty = constant("");
|
||||
|
||||
@NonNull
|
||||
private String value;
|
||||
private boolean resolved;
|
||||
|
||||
public StringRef(@NonNull String resName) {
|
||||
this.value = resName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public String toString() {
|
||||
if (!resolved) {
|
||||
resolved = true;
|
||||
if (resources != null) {
|
||||
final int identifier = resources.getIdentifier(value, "string", packageName);
|
||||
if (identifier == 0)
|
||||
Log.e(TAG, "Resource not found: " + value);
|
||||
else
|
||||
value = resources.getString(identifier);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue