Skip to content

Commit

Permalink
Merge EmojiHandler into EmojiManager. (vanniktech#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanniktech authored Jun 27, 2017
1 parent fc2e648 commit b8672b0
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 158 deletions.
2 changes: 1 addition & 1 deletion emoji/src/main/java/com/vanniktech/emoji/EmojiButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public EmojiButton(final Context context, final AttributeSet attrs) {
@Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) {
final CharSequence text = rawText == null ? "" : rawText;
final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
EmojiHandler.replaceWithImages(getContext(), spannableStringBuilder, emojiSize);
EmojiManager.replaceWithImages(getContext(), spannableStringBuilder, emojiSize);
super.setText(spannableStringBuilder, type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public EmojiEditText(final Context context, final AttributeSet attrs) {
}

@Override @CallSuper protected void onTextChanged(final CharSequence text, final int start, final int lengthBefore, final int lengthAfter) {
EmojiHandler.replaceWithImages(getContext(), getText(), emojiSize);
EmojiManager.replaceWithImages(getContext(), getText(), emojiSize);
}

@CallSuper public void backspace() {
Expand Down
37 changes: 0 additions & 37 deletions emoji/src/main/java/com/vanniktech/emoji/EmojiHandler.java

This file was deleted.

26 changes: 26 additions & 0 deletions emoji/src/main/java/com/vanniktech/emoji/EmojiManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.vanniktech.emoji;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Spannable;
import com.vanniktech.emoji.emoji.Emoji;
import com.vanniktech.emoji.emoji.EmojiCategory;
import java.util.ArrayList;
Expand Down Expand Up @@ -110,6 +112,30 @@ static void destroy() {
INSTANCE.emojiRepetitivePattern = null;
}

static void replaceWithImages(final Context context, final Spannable text, final int emojiSize) {
final EmojiManager emojiManager = EmojiManager.getInstance();
final EmojiSpan[] existingSpans = text.getSpans(0, text.length(), EmojiSpan.class);
final List<Integer> existingSpanPositions = new ArrayList<>(existingSpans.length);

final int size = existingSpans.length;
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < size; i++) {
existingSpanPositions.add(text.getSpanStart(existingSpans[i]));
}

final List<EmojiRange> findAllEmojis = emojiManager.findAllEmojis(text);

//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < findAllEmojis.size(); i++) {
final EmojiRange location = findAllEmojis.get(i);

if (!existingSpanPositions.contains(location.start)) {
text.setSpan(new EmojiSpan(context, location.emoji.getResource(), emojiSize),
location.start, location.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}

EmojiCategory[] getCategories() {
verifyInstalled();
return categories; // NOPMD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public EmojiTextView(final Context context, final AttributeSet attrs) {
@Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) {
final CharSequence text = rawText == null ? "" : rawText;
final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
EmojiHandler.replaceWithImages(getContext(), spannableStringBuilder, emojiSize);
EmojiManager.replaceWithImages(getContext(), spannableStringBuilder, emojiSize);
super.setText(spannableStringBuilder, type);
}

Expand Down
118 changes: 0 additions & 118 deletions emoji/src/test/java/com/vanniktech/emoji/EmojiHandlerTest.java

This file was deleted.

93 changes: 93 additions & 0 deletions emoji/src/test/java/com/vanniktech/emoji/EmojiManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.vanniktech.emoji;

import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import com.vanniktech.emoji.EmojiManager.EmojiRange;
import com.vanniktech.emoji.emoji.Emoji;
import com.vanniktech.emoji.emoji.EmojiCategory;
Expand All @@ -12,6 +14,7 @@
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import static org.assertj.core.api.Java6Assertions.assertThat;
Expand Down Expand Up @@ -156,4 +159,94 @@ public void noProviderInstalled() {

assertThat(EmojiManager.getInstance().findAllEmojis("")).isEmpty();
}

@Test public void simple() {
EmojiManager.install(provider);

final Spannable text = new SpannableString(new String(new int[] { 0x1234 }, 0, 1));

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1);
}

@Test public void inString() {
EmojiManager.install(provider);

final Spannable text = new SpannableString("test" + new String(new int[] { 0x1234 }, 0, 1) + "abc");

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1);
}

@Test public void multiple() {
EmojiManager.install(provider);

final Spannable text = new SpannableString(new String(new int[] { 0x1234 }, 0, 1) + new String(new int[] { 0x5678 }, 0, 1));

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(2);
}

@Test public void multipleInString() {
EmojiManager.install(provider);

final Spannable text = new SpannableString("abc" + new String(new int[] { 0x1234 }, 0, 1) + "cba" + new String(new int[] { 0x5678 }, 0, 1) + "xyz");

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(2);
}

@Test public void halfPath() {
EmojiManager.install(provider);

final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321 }, 0, 1));

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1);
}

@Test public void fullPath() {
EmojiManager.install(provider);

final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321, 0x9999 }, 0, 1));

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1);
}

@Test public void takeLongest() {
EmojiManager.install(provider);

final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321 }, 0, 1));

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1);
}

@Test public void empty() {
EmojiManager.install(provider);

final Spannable text = new SpannableString("");

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(0);
}

@Test public void noneInString() {
EmojiManager.install(provider);

final Spannable text = new SpannableString("abcdefg");

EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22);

assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(0);
}
}

0 comments on commit b8672b0

Please sign in to comment.