Skip to content

Commit

Permalink
Fix PickerAndroid will reset selected value during items update. (#24793
Browse files Browse the repository at this point in the history
)

Summary:
Fixes #13351

Two root causes:
1. Android Spinner will reset selection to undefined after setAdapter()
   which will trigger onValueChange().
   The behavior is not expected for RN.
   And the solution is to setSelection() explicitly

2. In original implementation, it setups `items` immediately,
   but delays the `selected` setup after update transaction.
   There will be some race condition and incosistency
   if update `items` only.
   The fix will do the setup all after update transaction.

[Android] [Fixed] - Fix #13351 PickerAndroid will reset selected value during items update.
Pull Request resolved: #24793

Differential Revision: D15293516

Pulled By: cpojer

fbshipit-source-id: 5a99a60015c7e1b2968252cdc0b2661d52a15b9d
  • Loading branch information
Kudo authored and facebook-github-bot committed May 10, 2019
1 parent ebeb893 commit 310cc38
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import com.facebook.react.common.annotations.VisibleForTesting;

Expand All @@ -23,6 +24,7 @@ public class ReactPicker extends AppCompatSpinner {
private int mMode = Spinner.MODE_DIALOG;
private @Nullable Integer mPrimaryColor;
private @Nullable OnSelectListener mOnSelectListener;
private @Nullable SpinnerAdapter mStagedAdapter;
private @Nullable Integer mStagedSelection;

private final OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() {
Expand Down Expand Up @@ -111,33 +113,42 @@ public void setOnSelectListener(@Nullable OnSelectListener onSelectListener) {
return mOnSelectListener;
}

/* package */ void setStagedAdapter(final SpinnerAdapter adapter) {
mStagedAdapter = adapter;
}

/**
* Will cache "selection" value locally and set it only once {@link #updateStagedSelection} is
* Will cache "selection" value locally and set it only once {@link #commitStagedData} is
* called
*/
public void setStagedSelection(int selection) {
/* package */ void setStagedSelection(int selection) {
mStagedSelection = selection;
}

public void updateStagedSelection() {
if (mStagedSelection != null) {
setSelectionWithSuppressEvent(mStagedSelection);
mStagedSelection = null;
}
}

/**
* Set the selection while suppressing the follow-up {@link OnSelectListener#onItemSelected(int)}
* event. This is used so we don't get an event when changing the selection ourselves.
*
* @param position the position of the selected item
* Used to commit staged data into ReactPicker view.
* During this period, we will disable {@link OnSelectListener#onItemSelected(int)} temporarily,
* so we don't get an event when changing the items/selection ourselves.
*/
private void setSelectionWithSuppressEvent(int position) {
if (position != getSelectedItemPosition()) {
setOnItemSelectedListener(null);
setSelection(position, false);
setOnItemSelectedListener(mItemSelectedListener);
/* package */ void commitStagedData() {
setOnItemSelectedListener(null);

final int origSelection = getSelectedItemPosition();
if (mStagedAdapter != null && mStagedAdapter != getAdapter()) {
setAdapter(mStagedAdapter);
// After setAdapter(), Spinner will reset selection and cause unnecessary onValueChange event.
// Explicitly setup selection again to prevent this.
// Ref: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/AbsSpinner.java#123
setSelection(origSelection, false);
mStagedAdapter = null;
}

if (mStagedSelection != null && mStagedSelection != origSelection) {
setSelection(mStagedSelection, false);
mStagedSelection = null;
}

setOnItemSelectedListener(mItemSelectedListener);
}

public @Nullable Integer getPrimaryColor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public void setItems(ReactPicker view, @Nullable ReadableArray items) {
}
ReactPickerAdapter adapter = new ReactPickerAdapter(view.getContext(), data);
adapter.setPrimaryTextColor(view.getPrimaryColor());
view.setAdapter(adapter);
view.setStagedAdapter(adapter);
} else {
view.setAdapter(null);
view.setStagedAdapter(null);
}
}

Expand Down Expand Up @@ -77,7 +77,7 @@ public void setSelected(ReactPicker view, int selected) {
@Override
protected void onAfterUpdateTransaction(ReactPicker view) {
super.onAfterUpdateTransaction(view);
view.updateStagedSelection();
view.commitStagedData();
}

@Override
Expand Down

0 comments on commit 310cc38

Please sign in to comment.