Provide a second overload to TextDelegate.getText that provides layerName (#1931)
If multiple text layers have the same text, it is impossible to disambiguate which one you are setting the text for. This commit adds a second overload for TextDelegate.getText that lets you receive the layerName in addition to the text.
Fixex #1932
diff --git a/lottie/src/main/java/com/airbnb/lottie/TextDelegate.java b/lottie/src/main/java/com/airbnb/lottie/TextDelegate.java
index 1ae5a6c..f9a2a67 100644
--- a/lottie/src/main/java/com/airbnb/lottie/TextDelegate.java
+++ b/lottie/src/main/java/com/airbnb/lottie/TextDelegate.java
@@ -17,6 +17,7 @@
public class TextDelegate {
private final Map<String, String> stringMap = new HashMap<>();
+
@Nullable private final LottieAnimationView animationView;
@Nullable private final LottieDrawable drawable;
private boolean cacheText = true;
@@ -44,6 +45,17 @@
/**
* Override this to replace the animation text with something dynamic. This can be used for
* translations or custom data.
+ * @param layerName the name of the layer with text
+ * @param input the string at the layer with text
+ * @return a String to use for the specific data, by default this is the same as getText(input)
+ */
+ public String getText(String layerName, String input) {
+ return getText(input);
+ }
+
+ /**
+ * Override this to replace the animation text with something dynamic. This can be used for
+ * translations or custom data.
*/
public String getText(String input) {
return input;
@@ -82,11 +94,11 @@
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
- public final String getTextInternal(String input) {
+ public final String getTextInternal(String layerName, String input) {
if (cacheText && stringMap.containsKey(input)) {
return stringMap.get(input);
}
- String text = getText(input);
+ String text = getText(layerName, input);
if (cacheText) {
stringMap.put(input, text);
}
diff --git a/lottie/src/main/java/com/airbnb/lottie/model/layer/TextLayer.java b/lottie/src/main/java/com/airbnb/lottie/model/layer/TextLayer.java
index b0f3b2d..db93141 100644
--- a/lottie/src/main/java/com/airbnb/lottie/model/layer/TextLayer.java
+++ b/lottie/src/main/java/com/airbnb/lottie/model/layer/TextLayer.java
@@ -245,7 +245,7 @@
String text = documentData.text;
TextDelegate textDelegate = lottieDrawable.getTextDelegate();
if (textDelegate != null) {
- text = textDelegate.getTextInternal(text);
+ text = textDelegate.getTextInternal(getName(), text);
}
fillPaint.setTypeface(typeface);
float textSize;