From f7dc24c9584d4f8dcd2d6d385c226e5201c6e8d8 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 18 Apr 2023 03:25:02 -0700 Subject: [PATCH] Use Java 8isms for Span Stripping (#36622) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../com/facebook/react/views/textinput/BUCK | 1 + .../react/views/textinput/ReactEditText.java | 65 ++++--------------- 2 files changed, 14 insertions(+), 52 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK index cd066554bd8c4f..b5fb5c0014cfcf 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK @@ -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"), diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index 290bd23f2e0b65..74b6610e531357 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -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; @@ -676,11 +677,6 @@ private void manageSpans(SpannableStringBuilder spannableStringBuilder) { } } - // TODO: Replace with Predicate and lambdas once Java 8 builds in OSS - interface SpanPredicate { - 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 @@ -690,81 +686,46 @@ private void stripStyleEquivalentSpans(SpannableStringBuilder sb) { stripSpansOfKind( sb, ReactAbsoluteSizeSpan.class, - new SpanPredicate() { - @Override - public boolean test(ReactAbsoluteSizeSpan span) { - return span.getSize() == mTextAttributes.getEffectiveFontSize(); - } - }); + (span) -> span.getSize() == mTextAttributes.getEffectiveFontSize()); stripSpansOfKind( sb, ReactBackgroundColorSpan.class, - new SpanPredicate() { - @Override - public boolean test(ReactBackgroundColorSpan span) { - return span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor(); - } - }); + (span) -> span.getBackgroundColor() == mReactBackgroundManager.getBackgroundColor()); stripSpansOfKind( sb, ReactForegroundColorSpan.class, - new SpanPredicate() { - @Override - public boolean test(ReactForegroundColorSpan span) { - return span.getForegroundColor() == getCurrentTextColor(); - } - }); + (span) -> span.getForegroundColor() == getCurrentTextColor()); stripSpansOfKind( sb, ReactStrikethroughSpan.class, - new SpanPredicate() { - @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() { - @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() { - @Override - public boolean test(CustomLetterSpacingSpan span) { - return span.getSpacing() == mTextAttributes.getEffectiveLetterSpacing(); - } - }); + (span) -> span.getSpacing() == mTextAttributes.getEffectiveLetterSpacing()); } stripSpansOfKind( sb, CustomStyleSpan.class, - new SpanPredicate() { - @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 void stripSpansOfKind( - SpannableStringBuilder sb, Class clazz, SpanPredicate shouldStrip) { + SpannableStringBuilder sb, Class clazz, Predicate shouldStrip) { T[] spans = sb.getSpans(0, sb.length(), clazz); for (T span : spans) {