From f9c4cc126b887b034930430a9809208c9349186b Mon Sep 17 00:00:00 2001 From: Erick Martins Date: Tue, 22 Aug 2023 18:01:20 -0300 Subject: [PATCH] fix(select): moved implementation from playgroud --- .../rnfreudds/spinner/FreudDSSpinner.java | 146 ------------------ .../spinner/RNFreudDSSpinnerManager.java | 30 ---- 2 files changed, 176 deletions(-) delete mode 100644 android/src/main/java/com/zenklub/rnfreudds/spinner/FreudDSSpinner.java delete mode 100644 android/src/main/java/com/zenklub/rnfreudds/spinner/RNFreudDSSpinnerManager.java diff --git a/android/src/main/java/com/zenklub/rnfreudds/spinner/FreudDSSpinner.java b/android/src/main/java/com/zenklub/rnfreudds/spinner/FreudDSSpinner.java deleted file mode 100644 index bb4b8e3..0000000 --- a/android/src/main/java/com/zenklub/rnfreudds/spinner/FreudDSSpinner.java +++ /dev/null @@ -1,146 +0,0 @@ -package com.zenklub.rnfreudds.spinner; - - -import android.content.Context; -import android.view.View; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; - -import androidx.appcompat.widget.AppCompatSpinner; - -import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.ReactContext; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.ReadableMap; -import com.facebook.react.bridge.WritableMap; -import com.facebook.react.common.MapBuilder; -import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.uimanager.events.RCTModernEventEmitter; - -import java.util.ArrayList; -import java.util.Map; - -public class FreudDSSpinner extends AppCompatSpinner { - - private Context mContext; - private boolean firstEventFired = false; - private int mSelected = 0; - private int selected; - - private ReadableArray options; - - public FreudDSSpinner(ThemedReactContext context) { - super(context, 0); - mContext = context; - setOnItemSelectedListener(ON_ITEM_SELECTED_LISTENER); - } - - public void setOptions(ReadableArray options) { - this.options = options; - - ArrayList spinnerArray = new ArrayList(); - for (int i = 0; i < options.size(); i++) { - ReadableMap option = options.getMap(i); - String label = option.getString("label"); - spinnerArray.add(label); - } - - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(mContext, - android.R.layout.simple_spinner_item, spinnerArray); - spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - setAdapter(spinnerArrayAdapter); - setSelection(mSelected); - } - - public void setSelected(String value) { - int index = this.getIndexWithValue(value); - if (index == -1 || (selected == mSelected && selected == this.selected)) { - return; - } - mSelected = selected; - setSelection(mSelected); - } - - private ReadableMap getOptionByValue(String value) { - if (this.options != null && value != null) { - for (int i = 0; i < options.size(); i++) { - ReadableMap option = options.getMap(i); - String optionValue = option.getString("value"); - - if (value.equals(optionValue)) { - return option; - } - } - } - - return null; - } - - private int getIndexWithValue(String value) { - if (this.options != null && value != null) { - for (int i = 0; i < options.size(); i++) { - ReadableMap option = options.getMap(i); - String optionValue = option.getString("value"); - - if (value.equals(optionValue)) { - return i; - } - } - } - - return -1; - } - - private final AdapterView.OnItemSelectedListener ON_ITEM_SELECTED_LISTENER = - new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int pos, long id) { - selected = pos; - // It always fire this event when the component starts, thus we need to surpress - // the first event - if (!firstEventFired) { - firstEventFired = true; - return; - } - - ReadableMap option = options.getMap(pos); - String selectedValue = option.getString("value"); - - WritableMap event = Arguments.createMap(); - event.putString("value", selectedValue); - ReactContext reactContext = (ReactContext)getContext(); - - reactContext - .getJSModule(RCTModernEventEmitter.class) - .receiveEvent(getId(), "topChange", event); - } - - @Override - public void onNothingSelected(AdapterView parent) {} - }; - - public Map getExportedCustomBubblingEventTypeConstants() { - return MapBuilder.builder().put( - "topChange", - MapBuilder.of( - "phasedRegistrationNames", - MapBuilder.of("bubbled", "onValueChange") - ) - ).build(); - } - - private final Runnable mLayoutRunnable = new Runnable() { - @Override - public void run() { - measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY)); - layout(getLeft(), getTop(), getRight(), getBottom()); - } - }; - - @Override - public void requestLayout() { - super.requestLayout(); - post(mLayoutRunnable); - } -} diff --git a/android/src/main/java/com/zenklub/rnfreudds/spinner/RNFreudDSSpinnerManager.java b/android/src/main/java/com/zenklub/rnfreudds/spinner/RNFreudDSSpinnerManager.java deleted file mode 100644 index 35e131b..0000000 --- a/android/src/main/java/com/zenklub/rnfreudds/spinner/RNFreudDSSpinnerManager.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.zenklub.rnfreudds.spinner; - -import com.facebook.react.uimanager.SimpleViewManager; -import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.uimanager.annotations.ReactProp; -import com.facebook.react.bridge.ReadableArray; - -public class RNFreudDSSpinner extends SimpleViewManager { - public static final String REACT_CLASS = "RNFreudDSSpinner"; - - @Override - public String getName() { - return REACT_CLASS; - } - - @Override - protected FreudDSSpinner createViewInstance(ThemedReactContext context) { - return new FreudDSSpinner(context); - } - - @ReactProp(name = "options") - public void setOptions(FreudDSSpinner view, ReadableArray options) { - view.setOptions(options); - } - - @ReactProp(name = "selected") - public void setSelected(FreudDSSpinner view, String selected) { - view.setSelected(selected); - } -}