From 8fd567bd85b5a5b7e84e4925a73664240da8fc65 Mon Sep 17 00:00:00 2001 From: Greg Giacovelli <466521+greggiacovelli@users.noreply.github.com> Date: Tue, 2 Nov 2021 13:57:48 -0700 Subject: [PATCH] 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 --- .../java/com/airbnb/lottie/TextDelegate.java | 16 ++++++++++++++-- .../com/airbnb/lottie/model/layer/TextLayer.java | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lottie/src/main/java/com/airbnb/lottie/TextDelegate.java b/lottie/src/main/java/com/airbnb/lottie/TextDelegate.java index 1ae5a6ccef..f9a2a67785 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 stringMap = new HashMap<>(); + @Nullable private final LottieAnimationView animationView; @Nullable private final LottieDrawable drawable; private boolean cacheText = true; @@ -41,6 +42,17 @@ public TextDelegate(@SuppressWarnings("NullableProblems") LottieDrawable drawabl animationView = null; } + /** + * 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. @@ -82,11 +94,11 @@ public void invalidateAllText() { } @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 b0f3b2d56d..db931411b4 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 @@ private void drawTextWithFont( 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;