Skip to content

Commit

Permalink
Provide a second overload to TextDelegate.getText that provides layer…
Browse files Browse the repository at this point in the history
…Name (#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
  • Loading branch information
greggiacovelli authored Nov 2, 2021
1 parent 8ae55a9 commit 8fd567b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lottie/src/main/java/com/airbnb/lottie/TextDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8fd567b

Please sign in to comment.