Skip to content

Commit

Permalink
Fixed emotes with text before and after the gif
Browse files Browse the repository at this point in the history
Fixed the emote scaling

Fixed the emote animation setting to be consistent
  • Loading branch information
edgan committed Dec 19, 2024
1 parent da67428 commit 5284dc5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 56 deletions.
87 changes: 33 additions & 54 deletions app/src/main/java/me/ccrama/redditslide/SpoilerRobotoTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,6 @@ private String saveEmotesFromDestruction(String html) {
return html;
}

/**
* Helper method to create a transparent placeholder drawable.
*
* @param size The size (width and height) of the placeholder in pixels.
* @return A transparent ColorDrawable.
*/
private Drawable createPlaceholder(int size) {
ColorDrawable placeholder = new ColorDrawable(Color.TRANSPARENT);
placeholder.setBounds(0, 0, size, size);
return placeholder;
}

// List to keep track of active GifDrawables to manage their lifecycle
private List<GifDrawable> activeGifDrawables = new ArrayList<>();

Expand Down Expand Up @@ -300,6 +288,14 @@ private static class EmoteDrawInfo {

private final List<PendingEmoteSpan> pendingSpans = new ArrayList<>();

public static boolean findObjectReplacementChar(CharSequence text) {
boolean atStart = text.charAt(0) == '\uFFFC';

if (atStart) return true;

return false;
}

private void setEmoteSpan(DynamicDrawableSpan span, String emoteName, int position) {
if (span == null || emoteName == null) {
Log.e("EmoteDebug", "Null span or emote name in setEmoteSpan");
Expand Down Expand Up @@ -347,11 +343,13 @@ private void setEmoteSpan(DynamicDrawableSpan span, String emoteName, int positi
// Set the new AnimatedImageSpan
text.setSpan(span, pos, pos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Start the GIF animation and add to active list if it's an AnimatedImageSpan
if (span instanceof AnimatedImageSpan) {
AnimatedImageSpan animatedSpan = (AnimatedImageSpan) span;
animatedSpan.start();
emoteDrawables.add(new EmoteDrawInfo(animatedSpan.getGifDrawable(), pos));
if (SettingValues.commentEmoteAnimation) {
// Start the GIF animation and add to active list if it's an AnimatedImageSpan
if (span instanceof AnimatedImageSpan) {
AnimatedImageSpan animatedSpan = (AnimatedImageSpan) span;
animatedSpan.start();
emoteDrawables.add(new EmoteDrawInfo(animatedSpan.getGifDrawable(), pos));
}
}

// Force layout update
Expand Down Expand Up @@ -461,27 +459,12 @@ private void setEmoteText(String text, TextView textView) {
// Create builder and ensure it's a SpannableStringBuilder
SpannableStringBuilder builder = new SpannableStringBuilder(processedText);

float scale = 1.0f; // Adjusted scaling factor for appropriate size
int placeholderSize = (int) (getTextSize() * scale);

// Set initial text with the builder
setText(builder);

// Set initial placeholders with transparent drawables
for (EmoteSpanRequest request : spanRequests) {
try {
Drawable placeholder = createPlaceholder(placeholderSize);
DynamicDrawableSpan placeholderSpan = new ImageSpan(placeholder, ImageSpan.ALIGN_BOTTOM);
setEmoteSpan(placeholderSpan, request.emoteName, request.start);
} catch (Exception e) {
Log.e("EmoteDebug", "Error setting placeholder span", e);
}
}

// Load GIFs
for (EmoteSpanRequest request : spanRequests) {
// Pass the 'pos' parameter (using 'request.start')
loadGifEmote(request, textView, scale, request.start);
loadGifEmote(request, textView, request.start);
}

} catch (Exception e) {
Expand Down Expand Up @@ -532,34 +515,27 @@ private void processPattern(String text, Pattern pattern, StringBuilder processe
}
}

private void loadGifEmote(EmoteSpanRequest request, TextView textView, float scale, int pos) {
private void loadGifEmote(EmoteSpanRequest request, TextView textView, int pos) {
Log.d("EmoteDebug", "Starting GIF download for: " + request.gifUrl);

GifUtils.downloadGif(request.gifUrl, new GifUtils.GifDownloadCallback() {
@Override
public void onGifDownloaded(File gifFile) {
float scale = 0.5f;
try {
Movie movie = Movie.decodeFile(gifFile.getAbsolutePath());
if (movie != null) {
// After decoding the movie and obtaining width and height
int intrinsicWidth = movie.width();
int intrinsicHeight = movie.height();
int intrinsicWidth = movie.width();

int size = (int) (textView.getTextSize() * scale);

// Adjust size proportionally if the GIF is larger than the base size
if (intrinsicHeight > size) {
size = (int) (intrinsicHeight * scale);
}

// Optional: Enforce a maximum size to prevent excessively large GIFs
int maxHeight = (int) (textView.getTextSize() * 3); // Example maximum height
size = Math.min(size, maxHeight);
int height = (int) (intrinsicHeight * scale);
int width = (int) (intrinsicWidth * scale);

GifDrawable gifDrawable = new GifDrawable(movie, null);
gifDrawable.setBounds(0, 0, size, size);
gifDrawable.setBounds(0, 0, 0, height);

Log.d("EmoteDebug", "Created drawable for " + request.emoteName + " with bounds " + size + "x" + size);
Log.d("EmoteDebug", "Created drawable for " + request.emoteName + " with bounds " + height + "x" + width);

// Wrap GifDrawable in AnimatedImageSpan
AnimatedImageSpan animatedSpan = new AnimatedImageSpan(gifDrawable, SpoilerRobotoTextView.this);
Expand All @@ -574,17 +550,20 @@ public void onGifDownloaded(File gifFile) {
return;
}

// Replace the placeholder span with AnimatedImageSpan
Log.e("EmoteDebug", getText().toString());
CharSequence currentText = getText();
if (currentText instanceof Spannable) {
Spannable spannable = (Spannable) currentText;
ImageSpan[] imageSpans = spannable.getSpans(pos, pos + 1, ImageSpan.class);
if (imageSpans != null && imageSpans.length > 0) {
for (ImageSpan span : imageSpans) {
spannable.removeSpan(span);
}
int start = spannable.toString().indexOf(currentText.toString());
int end = start + currentText.toString().length();

boolean found = findObjectReplacementChar(currentText);
Log.d("EmoteDebug", "test " + found);
if (found) {
spannable.setSpan(animatedSpan, start, start + 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
} else {
spannable.setSpan(animatedSpan, end - 2, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
spannable.setSpan(animatedSpan, pos, pos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

if (SettingValues.commentEmoteAnimation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.util.Log;
import android.graphics.Rect;

import me.ccrama.redditslide.SettingValues;

public class AnimatedImageSpan extends DynamicDrawableSpan {
private final GifDrawable drawable;
private final View view;
Expand Down Expand Up @@ -80,7 +82,7 @@ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.Fon
if (drawableHeight > textHeight) {
int heightDiff = drawableHeight - textHeight;
// Distribute the extra height equally above and below
int extraSpace = heightDiff / 2;
int extraSpace = drawableHeight;

fm.top = originalFm.top - extraSpace;
fm.ascent = originalFm.ascent - extraSpace;
Expand Down Expand Up @@ -123,7 +125,9 @@ public void onAttached() {
if (!isAttached) {
isAttached = true;
handler.post(invalidateRunnable);
drawable.start();
if (SettingValues.commentEmoteAnimation) {
drawable.start();
}
}
}

Expand Down

0 comments on commit 5284dc5

Please sign in to comment.