Skip to content

Commit

Permalink
#503 Check bounds before setting any Spinner position
Browse files Browse the repository at this point in the history
  • Loading branch information
AEFeinstein committed Jan 27, 2020
1 parent 3ef2403 commit 1466c32
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public void onViewStateRestored(Bundle savedInstanceState) {
Gathering gathering = (Gathering) savedInstanceState.getSerializable(SAVED_GATHERING_KEY);

assert gathering != null;
if (gathering.mDisplayMode >= mDisplayModeSpinner.getAdapter().getCount()) {
gathering.mDisplayMode = 0;
}
mDisplayModeSpinner.setSelection(gathering.mDisplayMode);
ArrayList<GatheringsPlayerData> players = gathering.mPlayerList;
for (GatheringsPlayerData player : players) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mRarityButton.setOnClickListener(v -> showDialog(SearchViewDialogFragment.RARITY_LIST));

/* This is a better default, might want to reorder the array */
mColorSpinner.setSelection(2);
safeSetSelection(mColorSpinner, 2);

/* The button colors change whether an option is selected or not */
checkDialogButtonColors();
Expand Down Expand Up @@ -314,6 +314,26 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
return myFragmentView;
}

/**
* Safely set a spinner's selection by defaulting to if out of bounds
*
* @param spinner The spinner to set a selection
* @param selection The index to set
*/
private void safeSetSelection(Spinner spinner, int selection) {
int spinnerCount = spinner.getAdapter().getCount();
// Make sure the spinner has items
if (0 != spinnerCount) {
if (selection >= spinnerCount) {
// The selection would be out of bounds, just select 0
spinner.setSelection(0);
} else {
// All good, make the selection
spinner.setSelection(selection);
}
}
}

private static class BuildAutocompleteTask extends AsyncTask<SearchViewFragment, Void, SearchViewFragment> {
@Override
protected SearchViewFragment doInBackground(SearchViewFragment... frags) {
Expand Down Expand Up @@ -497,7 +517,7 @@ public void onResume() {

/* Do we want to consolidate different printings of the same card in results, or not? */
boolean consolidate = PreferenceAdapter.getConsolidateSearch(getContext());
mSetSpinner.setSelection(consolidate ? CardDbAdapter.MOST_RECENT_PRINTING : CardDbAdapter.ALL_PRINTINGS);
safeSetSelection(mSetSpinner, consolidate ? CardDbAdapter.MOST_RECENT_PRINTING : CardDbAdapter.ALL_PRINTINGS);

// Load the saved criteria
if (PreferenceAdapter.getPersistSearchOptions(getContext())) {
Expand Down Expand Up @@ -793,29 +813,29 @@ private void clear() {
mCheckboxR.setChecked(false);
mCheckboxG.setChecked(false);
mCheckboxL.setChecked(false);
mColorSpinner.setSelection(2);
safeSetSelection(mColorSpinner, 2);

mCheckboxWIdentity.setChecked(false);
mCheckboxUIdentity.setChecked(false);
mCheckboxBIdentity.setChecked(false);
mCheckboxRIdentity.setChecked(false);
mCheckboxGIdentity.setChecked(false);
mCheckboxLIdentity.setChecked(false);
mColorIdentitySpinner.setSelection(0);

mTextSpinner.setSelection(0);
mTypeSpinner.setSelection(0);
mSetSpinner.setSelection(0);

mPowLogic.setSelection(0);
mPowChoice.setSelection(0);
mTouLogic.setSelection(0);
mTouChoice.setSelection(0);
mCmcLogic.setSelection(0);
mCmcLogic.setSelection(1); /* CMC should default to < */
mCmcChoice.setSelection(0);
safeSetSelection(mColorIdentitySpinner, 0);

safeSetSelection(mTextSpinner, 0);
safeSetSelection(mTypeSpinner, 0);
safeSetSelection(mSetSpinner, 0);

safeSetSelection(mPowLogic, 0);
safeSetSelection(mPowChoice, 0);
safeSetSelection(mTouLogic, 0);
safeSetSelection(mTouChoice, 0);
safeSetSelection(mCmcLogic, 0);
safeSetSelection(mCmcLogic, 1); /* CMC should default to < */
safeSetSelection(mCmcChoice, 0);
clearTextAndTokens(mManaCostTextView);
mManaComparisonSpinner.setSelection(Comparison.EMPTY.ordinal());
safeSetSelection(mManaComparisonSpinner, Comparison.EMPTY.ordinal());

if (mSetCheckedIndices != null) {
mSetCheckedIndices = new int[0];
Expand Down Expand Up @@ -894,11 +914,11 @@ private void setFieldsFromCriteria(SearchCriteria criteria) {
} else {
clearTextAndTokens(mSubtypeField);
}
mTypeSpinner.setSelection(criteria.typeLogic);
safeSetSelection(mTypeSpinner, criteria.typeLogic);

/* Set text fields */
mTextField.setText(criteria.text);
mTextSpinner.setSelection(criteria.textLogic);
safeSetSelection(mTextSpinner, criteria.textLogic);

/* Set color fields */
if (criteria.color != null) {
Expand All @@ -909,7 +929,7 @@ private void setFieldsFromCriteria(SearchCriteria criteria) {
mCheckboxG.setChecked(criteria.color.contains("G"));
mCheckboxL.setChecked(criteria.color.contains("L"));
}
mColorSpinner.setSelection(criteria.colorLogic);
safeSetSelection(mColorSpinner, criteria.colorLogic);

if (criteria.colorIdentity != null) {
mCheckboxWIdentity.setChecked(criteria.colorIdentity.contains("W"));
Expand All @@ -919,69 +939,69 @@ private void setFieldsFromCriteria(SearchCriteria criteria) {
mCheckboxGIdentity.setChecked(criteria.colorIdentity.contains("G"));
mCheckboxLIdentity.setChecked(criteria.colorIdentity.contains("L"));
}
mColorIdentitySpinner.setSelection(criteria.colorIdentityLogic);
safeSetSelection(mColorIdentitySpinner, criteria.colorIdentityLogic);

/* Set power and toughness fields */
List<String> logicChoices = Arrays.asList(getResources().getStringArray(R.array.logic_spinner));
mPowLogic.setSelection(logicChoices.indexOf(criteria.powLogic));
safeSetSelection(mPowLogic, logicChoices.indexOf(criteria.powLogic));
List<String> ptList = Arrays.asList(getResources().getStringArray(R.array.pt_spinner));
float p = criteria.powChoice;
if (p != CardDbAdapter.NO_ONE_CARES) {
if (p == CardDbAdapter.STAR)
mPowChoice.setSelection(ptList.indexOf("*"));
safeSetSelection(mPowChoice, ptList.indexOf("*"));
else if (p == CardDbAdapter.ONE_PLUS_STAR)
mPowChoice.setSelection(ptList.indexOf("1+*"));
safeSetSelection(mPowChoice, ptList.indexOf("1+*"));
else if (p == CardDbAdapter.TWO_PLUS_STAR)
mPowChoice.setSelection(ptList.indexOf("2+*"));
safeSetSelection(mPowChoice, ptList.indexOf("2+*"));
else if (p == CardDbAdapter.SEVEN_MINUS_STAR)
mPowChoice.setSelection(ptList.indexOf("7-*"));
safeSetSelection(mPowChoice, ptList.indexOf("7-*"));
else if (p == CardDbAdapter.STAR_SQUARED)
mPowChoice.setSelection(ptList.indexOf("*^2"));
safeSetSelection(mPowChoice, ptList.indexOf("*^2"));
else if (p == CardDbAdapter.X)
mPowChoice.setSelection(ptList.indexOf("X"));
safeSetSelection(mPowChoice, ptList.indexOf("X"));
else if (p == CardDbAdapter.QUESTION_MARK)
mPowChoice.setSelection(ptList.indexOf("?"));
safeSetSelection(mPowChoice, ptList.indexOf("?"));
else if (p == CardDbAdapter.INFINITY)
mPowChoice.setSelection(ptList.indexOf("∞"));
safeSetSelection(mPowChoice, ptList.indexOf("∞"));
else {
if (p == (int) p) {
mPowChoice.setSelection(ptList.indexOf(((int) p) + ""));
safeSetSelection(mPowChoice, ptList.indexOf(((int) p) + ""));
} else {
mPowChoice.setSelection(ptList.indexOf(p + ""));
safeSetSelection(mPowChoice, ptList.indexOf(p + ""));
}
}
}
mTouLogic.setSelection(logicChoices.indexOf(criteria.touLogic));
safeSetSelection(mTouLogic, logicChoices.indexOf(criteria.touLogic));
float t = criteria.touChoice;
if (t != CardDbAdapter.NO_ONE_CARES) {
if (t == CardDbAdapter.STAR)
mTouChoice.setSelection(ptList.indexOf("*"));
safeSetSelection(mTouChoice, ptList.indexOf("*"));
else if (t == CardDbAdapter.ONE_PLUS_STAR)
mTouChoice.setSelection(ptList.indexOf("1+*"));
safeSetSelection(mTouChoice, ptList.indexOf("1+*"));
else if (t == CardDbAdapter.TWO_PLUS_STAR)
mTouChoice.setSelection(ptList.indexOf("2+*"));
safeSetSelection(mTouChoice, ptList.indexOf("2+*"));
else if (t == CardDbAdapter.SEVEN_MINUS_STAR)
mTouChoice.setSelection(ptList.indexOf("7-*"));
safeSetSelection(mTouChoice, ptList.indexOf("7-*"));
else if (t == CardDbAdapter.STAR_SQUARED)
mTouChoice.setSelection(ptList.indexOf("*^2"));
safeSetSelection(mTouChoice, ptList.indexOf("*^2"));
else if (t == CardDbAdapter.X)
mTouChoice.setSelection(ptList.indexOf("X"));
safeSetSelection(mTouChoice, ptList.indexOf("X"));
else if (t == CardDbAdapter.QUESTION_MARK)
mTouChoice.setSelection(ptList.indexOf("?"));
safeSetSelection(mTouChoice, ptList.indexOf("?"));
else if (t == CardDbAdapter.INFINITY)
mTouChoice.setSelection(ptList.indexOf("∞"));
safeSetSelection(mTouChoice, ptList.indexOf("∞"));
else {
if (t == (int) t) {
mTouChoice.setSelection(ptList.indexOf(((int) t) + ""));
safeSetSelection(mTouChoice, ptList.indexOf(((int) t) + ""));
} else {
mTouChoice.setSelection(ptList.indexOf(t + ""));
safeSetSelection(mTouChoice, ptList.indexOf(t + ""));
}
}
}

/* Set CMC fields */
mCmcLogic.setSelection(logicChoices.indexOf(criteria.cmcLogic));
mCmcChoice.setSelection(Arrays.asList(getResources().getStringArray(R.array.cmc_spinner))
safeSetSelection(mCmcLogic, logicChoices.indexOf(criteria.cmcLogic));
safeSetSelection(mCmcChoice, Arrays.asList(getResources().getStringArray(R.array.cmc_spinner))
.indexOf(String.valueOf(criteria.cmc)));

/* Set mana fields */
Expand All @@ -993,7 +1013,7 @@ else if (t == CardDbAdapter.INFINITY)
clearTextAndTokens(mManaCostTextView);
}
if (null != criteria.manaCostLogic) {
mManaComparisonSpinner.setSelection(criteria.manaCostLogic.ordinal());
safeSetSelection(mManaComparisonSpinner, criteria.manaCostLogic.ordinal());
}

/* set format */
Expand Down Expand Up @@ -1027,7 +1047,7 @@ else if (t == CardDbAdapter.INFINITY)
} else {
clearTextAndTokens(mSetField);
}
mSetSpinner.setSelection(criteria.setLogic);
safeSetSelection(mSetSpinner, criteria.setLogic);

/* Set text fields at the end */
mWatermarkField.setText(criteria.watermark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {

getParentGatheringsFragment().mCurrentGatheringName = GatheringsIO.ReadGatheringNameFromXML(fGatherings[position],
getActivity().getFilesDir());
if (gathering.mDisplayMode >= getParentGatheringsFragment().mDisplayModeSpinner.getAdapter().getCount()) {
gathering.mDisplayMode = 0;
}
getParentGatheringsFragment().mDisplayModeSpinner.setSelection(gathering.mDisplayMode);
ArrayList<GatheringsPlayerData> players = gathering.mPlayerList;
for (GatheringsPlayerData player : players) {
Expand Down

0 comments on commit 1466c32

Please sign in to comment.