Skip to content

Commit

Permalink
Expanding TextDelegate to provide similar functionality as iOS TextPr…
Browse files Browse the repository at this point in the history
…ovider
  • Loading branch information
greggiacovelli committed Oct 22, 2021
1 parent 6c21891 commit a9bcd9b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1,152 deletions.
77 changes: 14 additions & 63 deletions lottie/src/main/java/com/airbnb/lottie/TextDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;

import com.airbnb.lottie.model.KeyPath;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -16,41 +18,7 @@
*/
public class TextDelegate {

private static class Key {
private final String layerName;
private final String input;
private Key(String layerName, String input) {
this.layerName = layerName;
this.input = input;
}

private Key(String input) {
this(null, input);
}

@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Key key = (Key) o;

if (layerName != null ? !layerName.equals(key.layerName) : key.layerName != null) {
return false;
}
return input != null ? input.equals(key.input) : key.input == null;
}

@Override public int hashCode() {
int result = layerName != null ? layerName.hashCode() : 0;
result = 31 * result + (input != null ? input.hashCode() : 0);
return result;
}
}
private final Map<Key, String> stringMap = new HashMap<>();
private final Map<String, String> stringMap = new HashMap<>();

@Nullable private final LottieAnimationView animationView;
@Nullable private final LottieDrawable drawable;
Expand All @@ -77,13 +45,13 @@ public TextDelegate(@SuppressWarnings("NullableProblems") LottieDrawable drawabl
}

/**
* 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
* Override this to replace the animation text with something dynamic. This can be used for
* translations or custom data.
* @param path 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) {
public String getText(KeyPath path, String input) {
return getText(input);
}

Expand All @@ -99,12 +67,7 @@ public String getText(String input) {
* Update the text that will be rendered for the given input text.
*/
public void setText(String input, String output) {
stringMap.put(new Key(input), output);
invalidate();
}

public void setText(String layerName, String input, String output) {
stringMap.put(new Key(layerName, input), output);
stringMap.put(input, output);
invalidate();
}

Expand All @@ -116,15 +79,11 @@ public void setCacheText(boolean cacheText) {
this.cacheText = cacheText;
}

public void invalidateText(String layerName, String input) {
stringMap.remove(new Key(layerName, input));
invalidate();
}
/**
* Invalidates a cached string with the given input.
*/
public void invalidateText(String input) {
stringMap.remove(new Key(input));
stringMap.remove(input);
invalidate();
}

Expand All @@ -137,21 +96,13 @@ public void invalidateAllText() {
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
public final String getTextInternal(String layerName, String input) {
Key key = new Key(layerName, input);
if (cacheText) {
if (stringMap.containsKey(key)) {
return stringMap.get(key);
}
Key inputKey = new Key(input);
if (stringMap.containsKey(inputKey)) {
return stringMap.get(inputKey);
}
public final String getTextInternal(KeyPath path, String input) {
if (cacheText && stringMap.containsKey(input)) {
return stringMap.get(input);
}

String text = getText(layerName, input);
String text = getText(path, input);
if (cacheText) {
stringMap.put(key, text);
stringMap.put(input, text);
}
return text;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.airbnb.lottie.model.DocumentData.Justification;
import com.airbnb.lottie.model.Font;
import com.airbnb.lottie.model.FontCharacter;
import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.model.animatable.AnimatableTextProperties;
import com.airbnb.lottie.model.content.ShapeGroup;
import com.airbnb.lottie.utils.Utils;
Expand Down Expand Up @@ -245,7 +246,7 @@ private void drawTextWithFont(
String text = documentData.text;
TextDelegate textDelegate = lottieDrawable.getTextDelegate();
if (textDelegate != null) {
text = textDelegate.getTextInternal(getName(), text);
text = textDelegate.getTextInternal(new KeyPath(getName()), text);
}
fillPaint.setTypeface(typeface);
float textSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class HappoSnapshotter(
if (response.isSuccessful) {
Log.d(TAG, "Uploaded $reportName to happo")
} else {
println("$json")
throw IllegalStateException("Failed to upload $reportName to Happo. Failed with code ${response.code}. " + response.body?.string())
}
}
Expand Down
Loading

0 comments on commit a9bcd9b

Please sign in to comment.