Skip to content

Commit

Permalink
Use Java 8isms for Span Stripping (#36622)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36622

This replaces Java `SpanPredicate` anonymous inner classes with lambdas and `androidx` `Predicate` (compat version of Java 8 predicate).

This may not be able to be merged yet because of Buck in OSS.

Changelog:
[Internal]

Reviewed By: javache

Differential Revision: D44362123

fbshipit-source-id: 67c9c7aa2acceab406e28ff49546c1240a5e85e6
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Apr 18, 2023
1 parent 07df82b commit f7dc24c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rn_android_library(
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_dep("third-party/android/androidx:autofill"),
react_native_dep("third-party/android/androidx:core"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.util.Predicate;
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
Expand Down Expand Up @@ -676,11 +677,6 @@ private void manageSpans(SpannableStringBuilder spannableStringBuilder) {
}
}

// TODO: Replace with Predicate<T> and lambdas once Java 8 builds in OSS
interface SpanPredicate<T> {
boolean test(T span);
}

/**
* Remove spans from the SpannableStringBuilder which can be represented by TextAppearance
* attributes on the underlying EditText. This works around instability on Samsung devices with
Expand All @@ -690,81 +686,46 @@ private void stripStyleEquivalentSpans(SpannableStringBuilder sb) {
stripSpansOfKind(
sb,
ReactAbsoluteSizeSpan.class,
new SpanPredicate<ReactAbsoluteSizeSpan>() {
@Override
public boolean test(ReactAbsoluteSizeSpan span) {
return span.getSize() == mTextAttributes.getEffectiveFontSize();
}
});
(span) -> span.getSize() == mTextAttributes.getEffectiveFontSize());

stripSpansOfKind(
sb,
ReactBackgroundColorSpan.class,
new SpanPredicate<ReactBackgroundColorSpan>() {
@Override
public boolean test(ReactBackgroundColorSpan span) {
return span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor();
}
});
(span) -> span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor());

stripSpansOfKind(
sb,
ReactForegroundColorSpan.class,
new SpanPredicate<ReactForegroundColorSpan>() {
@Override
public boolean test(ReactForegroundColorSpan span) {
return span.getForegroundColor() == getCurrentTextColor();
}
});
(span) -> span.getForegroundColor() == getCurrentTextColor());

stripSpansOfKind(
sb,
ReactStrikethroughSpan.class,
new SpanPredicate<ReactStrikethroughSpan>() {
@Override
public boolean test(ReactStrikethroughSpan span) {
return (getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) != 0;
}
});
(span) -> (getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) != 0);

stripSpansOfKind(
sb,
ReactUnderlineSpan.class,
new SpanPredicate<ReactUnderlineSpan>() {
@Override
public boolean test(ReactUnderlineSpan span) {
return (getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) != 0;
}
});
sb, ReactUnderlineSpan.class, (span) -> (getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) != 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
stripSpansOfKind(
sb,
CustomLetterSpacingSpan.class,
new SpanPredicate<CustomLetterSpacingSpan>() {
@Override
public boolean test(CustomLetterSpacingSpan span) {
return span.getSpacing() == mTextAttributes.getEffectiveLetterSpacing();
}
});
(span) -> span.getSpacing() == mTextAttributes.getEffectiveLetterSpacing());
}

stripSpansOfKind(
sb,
CustomStyleSpan.class,
new SpanPredicate<CustomStyleSpan>() {
@Override
public boolean test(CustomStyleSpan span) {
return span.getStyle() == mFontStyle
&& Objects.equals(span.getFontFamily(), mFontFamily)
&& span.getWeight() == mFontWeight
&& Objects.equals(span.getFontFeatureSettings(), getFontFeatureSettings());
}
(span) -> {
return span.getStyle() == mFontStyle
&& Objects.equals(span.getFontFamily(), mFontFamily)
&& span.getWeight() == mFontWeight
&& Objects.equals(span.getFontFeatureSettings(), getFontFeatureSettings());
});
}

private <T> void stripSpansOfKind(
SpannableStringBuilder sb, Class<T> clazz, SpanPredicate<T> shouldStrip) {
SpannableStringBuilder sb, Class<T> clazz, Predicate<T> shouldStrip) {
T[] spans = sb.getSpans(0, sb.length(), clazz);

for (T span : spans) {
Expand Down

0 comments on commit f7dc24c

Please sign in to comment.