Skip to content

Commit

Permalink
remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
fabOnReact committed Oct 31, 2022
1 parent 08d6f43 commit 145303e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.uimanager.util.ReactFindViewUtil;
import com.facebook.react.views.text.ReactSpan;
import com.facebook.react.views.text.ReactTtsSpan;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -671,10 +672,10 @@ protected boolean onPerformActionForVirtualView(
public static class AccessibilityLinks {
private final List<AccessibleLink> mLinks;

public AccessibilityLinks(ReactTtsSpan[] spans, Spannable text) {
public AccessibilityLinks(ReactSpan[] spans, Spannable text) {
ArrayList<AccessibleLink> links = new ArrayList<>();
for (int i = 0; i < spans.length; i++) {
ReactTtsSpan span = spans[i];
ReactSpan span = spans[i];
int start = text.getSpanStart(span);
int end = text.getSpanEnd(span);
// zero length spans, and out of range spans should not be included.
Expand All @@ -683,39 +684,18 @@ public AccessibilityLinks(ReactTtsSpan[] spans, Spannable text) {
}

final AccessibleLink link = new AccessibleLink();
link.span = span;
SpannableString spannableDescription = new SpannableString(text.subSequence(start, end));
link.description = spannableDescription.toString();
link.start = spannableDescription.getSpanStart(span);
link.end = spannableDescription.getSpanEnd(span);

// ID is the reverse of what is expected, since the ClickableSpans are returned in reverse
// order due to being added in reverse order. If we don't do this, focus will move to the
// last link first and move backwards.
//
// If this approach becomes unreliable, we should instead look at their start position and
// order them manually.
link.id = spans.length - 1 - i;
links.add(link);
}
mLinks = links;
}

public AccessibilityLinks(ClickableSpan[] spans, Spannable text) {
ArrayList<AccessibleLink> links = new ArrayList<>();
for (int i = 0; i < spans.length; i++) {
ClickableSpan span = spans[i];
int start = text.getSpanStart(span);
int end = text.getSpanEnd(span);
// zero length spans, and out of range spans should not be included.
if (start == end || start < 0 || end < 0 || start > text.length() || end > text.length()) {
continue;
if (span instanceof ReactTtsSpan) {
link.span = span;
SpannableString spannableDescription = new SpannableString(text.subSequence(start, end));
link.description = spannableDescription.toString();
link.start = spannableDescription.getSpanStart(span);
link.end = spannableDescription.getSpanEnd(span);
}
if (span instanceof ClickableSpan) {
link.description = text.subSequence(start, end).toString();
link.start = start;
link.end = end;
}

final AccessibleLink link = new AccessibleLink();
link.description = text.subSequence(start, end).toString();
link.start = start;
link.end = end;

// ID is the reverse of what is expected, since the ClickableSpans are returned in reverse
// order due to being added in reverse order. If we don't do this, focus will move to the
Expand Down Expand Up @@ -757,7 +737,7 @@ public int size() {

private static class AccessibleLink {
public String description;
public ReactTtsSpan span;
public ReactSpan span;
public int start;
public int end;
public int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
Expand Down Expand Up @@ -102,7 +101,6 @@ private static void buildSpannedFromShadowNode(
boolean supportsInlineViews,
Map<Integer, ReactShadowNode> inlineViews,
int start) {
Log.w("TESTING::ReactBaseTextShadowNode", "buildSpannedFromShadowNode");

TextAttributes textAttributes;
if (parentTextAttributes != null) {
Expand Down Expand Up @@ -485,12 +483,10 @@ public void setColor(@Nullable Integer color) {

@ReactProp(name = ViewProps.BACKGROUND_COLOR, customType = "Color")
public void setBackgroundColor(@Nullable Integer color) {
Log.w("TESTING::ReactBaseTextShadowNode", "setBackgroundColor");
// Background color needs to be handled here for virtual nodes so it can be incorporated into
// the span. However, it doesn't need to be applied to non-virtual nodes because non-virtual
// nodes get mapped to native views and native views get their background colors get set via
// {@link BaseViewManager}.
Log.w("TESTING::ReactBaseTextShadowNode", "isVirtual(): " + (isVirtual()));
if (isVirtual()) {
mIsBackgroundColorSet = (color != null);
if (mIsBackgroundColorSet) {
Expand Down

0 comments on commit 145303e

Please sign in to comment.