From b14713d06e8724d00d0bdf945a9f1613767fd69d Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Wed, 22 Mar 2023 13:54:54 -0700 Subject: [PATCH] Minimize EditText Spans 7/9: Avoid temp list (#36576) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36576 This is part of a series of changes to minimize the number of spans committed to EditText, as a mitigation for platform issues on Samsung devices. See this [GitHub thread]( https://github.com/facebook/react-native/issues/35936#issuecomment-1411437789) for greater context on the platform behavior. This change addresses some minor CR feedback and removes the temporary list of spans in favor of applying them directly. Changelog: [Internal] Reviewed By: javache Differential Revision: D44295190 fbshipit-source-id: 243c40b1062d470e44e6c59df2f2146d850fbcd8 --- .../react/views/textinput/ReactEditText.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) 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 01ded5840628ce..2e0fff668da74b 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 @@ -775,33 +775,39 @@ private void restoreStyleEquivalentSpans(SpannableStringBuilder workingText) { // (least precedence). This ensures the span is behind any overlapping spans. spanFlags |= Spannable.SPAN_PRIORITY; - List spans = new ArrayList<>(); - spans.add(new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize())); - spans.add(new ReactForegroundColorSpan(getCurrentTextColor())); + workingText.setSpan( + new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()), + 0, + workingText.length(), + spanFlags); + + workingText.setSpan( + new ReactForegroundColorSpan(getCurrentTextColor()), 0, workingText.length(), spanFlags); int backgroundColor = mReactBackgroundManager.getBackgroundColor(); if (backgroundColor != Color.TRANSPARENT) { - spans.add(new ReactBackgroundColorSpan(backgroundColor)); + workingText.setSpan( + new ReactBackgroundColorSpan(backgroundColor), 0, workingText.length(), spanFlags); } if ((getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) != 0) { - spans.add(new ReactStrikethroughSpan()); + workingText.setSpan(new ReactStrikethroughSpan(), 0, workingText.length(), spanFlags); } if ((getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) != 0) { - spans.add(new ReactUnderlineSpan()); + workingText.setSpan(new ReactUnderlineSpan(), 0, workingText.length(), spanFlags); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { float effectiveLetterSpacing = mTextAttributes.getEffectiveLetterSpacing(); if (!Float.isNaN(effectiveLetterSpacing)) { - spans.add(new CustomLetterSpacingSpan(effectiveLetterSpacing)); + workingText.setSpan( + new CustomLetterSpacingSpan(effectiveLetterSpacing), + 0, + workingText.length(), + spanFlags); } } - - for (Object span : spans) { - workingText.setSpan(span, 0, workingText.length(), spanFlags); - } } private static boolean sameTextForSpan(