/
SearchViewFragment.java
1141 lines (1023 loc) · 46.7 KB
/
SearchViewFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017 Adam Feinstein
*
* This file is part of MTG Familiar.
*
* MTG Familiar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MTG Familiar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MTG Familiar. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gelakinetic.mtgfam.fragments;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.gelakinetic.mtgfam.FamiliarActivity;
import com.gelakinetic.mtgfam.R;
import com.gelakinetic.mtgfam.fragments.dialogs.FamiliarDialogFragment;
import com.gelakinetic.mtgfam.fragments.dialogs.SearchViewDialogFragment;
import com.gelakinetic.mtgfam.helpers.AutocompleteCursorAdapter;
import com.gelakinetic.mtgfam.helpers.PreferenceAdapter;
import com.gelakinetic.mtgfam.helpers.SearchCriteria;
import com.gelakinetic.mtgfam.helpers.SnackbarWrapper;
import com.gelakinetic.mtgfam.helpers.database.CardDbAdapter;
import com.gelakinetic.mtgfam.helpers.database.DatabaseManager;
import com.gelakinetic.mtgfam.helpers.database.FamiliarDbException;
import com.gelakinetic.mtgfam.helpers.database.FamiliarDbHandle;
import com.gelakinetic.mtgfam.helpers.model.Comparison;
import com.gelakinetic.mtgfam.helpers.view.ComparisonSpinner;
import com.gelakinetic.mtgfam.helpers.view.CompletionView;
import com.gelakinetic.mtgfam.helpers.view.ManaCostTextView;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* This fragment lets users configure search parameters, and then search for a card
* The actual database query is handled in the ResultListFragment
*/
public class SearchViewFragment extends FamiliarFragment {
/* String keys */
public static final String CRITERIA_FLAG = "criteria_flag";
/* Default search file */
private static final String DEFAULT_CRITERIA_FILE = "defaultSearchCriteria.ser";
/* Keys for persisting state */
private static final String SAVED_SET_KEY = "SAVED_SET_KEY";
private static final String SAVED_RARITY_KEY = "SAVED_RARITY_KEY";
private static final String SAVED_FORMAT_KEY = "SAVED_FORMAT_KEY";
/* Spinner Data Structures */
private String[] mSetNames;
private int[] mSetCheckedIndices;
private String[] mSetSymbols;
public String[] mFormatNames;
private char[] mRarityCodes;
public String[] mRarityNames;
public int[] mRarityCheckedIndices;
public int mSelectedFormat;
/* Autocomplete data structures */
private String[] mSupertypes = null;
private String[] mSubtypes = null;
private String[] mArtists = null;
private String[] mWatermarks = null;
/* UI Elements */
private AutoCompleteTextView mNameField;
private EditText mTextField;
private CompletionView mSupertypeField = null;
private CompletionView mSubtypeField = null;
private EditText mCollectorsNumberField;
private CheckBox mCheckboxW;
private CheckBox mCheckboxU;
private CheckBox mCheckboxB;
private CheckBox mCheckboxR;
private CheckBox mCheckboxG;
private CheckBox mCheckboxL;
private Spinner mColorSpinner;
private CheckBox mCheckboxWIdentity;
private CheckBox mCheckboxUIdentity;
private CheckBox mCheckboxBIdentity;
private CheckBox mCheckboxRIdentity;
private CheckBox mCheckboxGIdentity;
private CheckBox mCheckboxLIdentity;
private Spinner mColorIdentitySpinner;
private CompletionView mSetField;
private Button mFormatButton;
private Button mRarityButton;
private Spinner mPowLogic;
private Spinner mPowChoice;
private Spinner mTouLogic;
private Spinner mTouChoice;
private Spinner mCmcLogic;
private Spinner mCmcChoice;
private ComparisonSpinner mManaComparisonSpinner;
private ManaCostTextView mManaCostTextView;
public Dialog mFormatDialog;
public Dialog mRarityDialog;
private EditText mFlavorField;
private AutoCompleteTextView mArtistField = null;
private AutoCompleteTextView mWatermarkField = null;
private Spinner mTextSpinner;
private Spinner mTypeSpinner;
private Spinner mSetSpinner;
/**
* This will query the database to populate the set and format spinner dialogs.
* The rarity dialog is pulled from resources
*
* @param savedInstanceState If the fragment is being re-created from a previous saved state, this is the state.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Get the different rarities out of resources to populate the list of choices with */
Resources res = getResources();
TypedArray mRarityNamesTemp = res.obtainTypedArray(R.array.rarities);
int i = mRarityNamesTemp.length();
mRarityNames = new String[i];
mRarityCodes = new char[i];
if (savedInstanceState != null) {
mSelectedFormat = savedInstanceState.getInt(SAVED_FORMAT_KEY);
mRarityCheckedIndices = savedInstanceState.getIntArray(SAVED_RARITY_KEY);
mSetCheckedIndices = savedInstanceState.getIntArray(SAVED_SET_KEY);
} else {
mRarityCheckedIndices = new int[0];
mSelectedFormat = -1;
}
while (i-- > 0) {
int resID = mRarityNamesTemp.peekValue(i).resourceId;
String resEntryName = res.getResourceEntryName(resID);
int p = resEntryName.lastIndexOf("_");
if (-1 != p && p + 1 < resEntryName.length())
mRarityCodes[i] = resEntryName.charAt(p + 1);
else mRarityCodes[i] = ' ';
mRarityNames[i] = res.getString(resID);
}
mRarityNamesTemp.recycle();
}
/**
* Find all the UI elements. set actions for buttons. Attach array adapters for autocomplete
*
* @param inflater The LayoutInflater object that can be used to inflate any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's UI should be attached to. The
* fragment should not add the view itself, but this can be used to generate the
* LayoutParams of the view.
* @param savedInstanceState If non-null, this fragment is being re-constructed from a previous saved state as given
* here.
* @return the inflated view
*/
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/* Inflate the view */
View myFragmentView = inflater.inflate(R.layout.search_frag, container, false);
assert myFragmentView != null;
/* Get references to UI elements. When a search is preformed, these values will be queried */
mNameField = myFragmentView.findViewById(R.id.name_search);
mTextField = myFragmentView.findViewById(R.id.textsearch);
mSupertypeField = myFragmentView.findViewById(R.id.supertypesearch);
mSubtypeField = myFragmentView.findViewById(R.id.subtypesearch);
mFlavorField = myFragmentView.findViewById(R.id.flavorsearch);
mArtistField = myFragmentView.findViewById(R.id.artistsearch);
mWatermarkField = myFragmentView.findViewById(R.id.watermarksearch);
mCollectorsNumberField = myFragmentView.findViewById(R.id.collectorsnumbersearch);
Button searchButton = myFragmentView.findViewById(R.id.searchbutton);
mCheckboxW = myFragmentView.findViewById(R.id.checkBoxW);
mCheckboxU = myFragmentView.findViewById(R.id.checkBoxU);
mCheckboxB = myFragmentView.findViewById(R.id.checkBoxB);
mCheckboxR = myFragmentView.findViewById(R.id.checkBoxR);
mCheckboxG = myFragmentView.findViewById(R.id.checkBoxG);
mCheckboxL = myFragmentView.findViewById(R.id.checkBoxL);
mCheckboxWIdentity = myFragmentView.findViewById(R.id.checkBoxW_identity);
mCheckboxUIdentity = myFragmentView.findViewById(R.id.checkBoxU_identity);
mCheckboxBIdentity = myFragmentView.findViewById(R.id.checkBoxB_identity);
mCheckboxRIdentity = myFragmentView.findViewById(R.id.checkBoxR_identity);
mCheckboxGIdentity = myFragmentView.findViewById(R.id.checkBoxG_identity);
mCheckboxLIdentity = myFragmentView.findViewById(R.id.checkBoxL_identity);
mColorSpinner = myFragmentView.findViewById(R.id.colorlogic);
mColorIdentitySpinner = myFragmentView.findViewById(R.id.coloridentitylogic);
mTextSpinner = myFragmentView.findViewById(R.id.textlogic);
mTypeSpinner = myFragmentView.findViewById(R.id.typelogic);
mSetSpinner = myFragmentView.findViewById(R.id.setlogic);
mSetField = myFragmentView.findViewById(R.id.setsearch);
mFormatButton = myFragmentView.findViewById(R.id.formatsearch);
mRarityButton = myFragmentView.findViewById(R.id.raritysearch);
mPowLogic = myFragmentView.findViewById(R.id.powLogic);
mPowChoice = myFragmentView.findViewById(R.id.powChoice);
mTouLogic = myFragmentView.findViewById(R.id.touLogic);
mTouChoice = myFragmentView.findViewById(R.id.touChoice);
mCmcLogic = myFragmentView.findViewById(R.id.cmcLogic);
mCmcChoice = myFragmentView.findViewById(R.id.cmcChoice);
mManaCostTextView = myFragmentView.findViewById(R.id.manaCostTextView);
mManaComparisonSpinner = myFragmentView.findViewById(R.id.comparisonSpinner);
/* Now we need to apply a different TextView to our Spinners to center the items */
ArrayAdapter<String> logicAdapter = new ArrayAdapter<>(getContext(), R.layout.centered_spinner_text, getResources().getStringArray(R.array.logic_spinner));
logicAdapter.setDropDownViewResource(R.layout.centered_spinner_text);
mPowLogic.setAdapter(logicAdapter);
mTouLogic.setAdapter(logicAdapter);
mCmcLogic.setAdapter(logicAdapter);
ArrayAdapter<String> ptChoiceAdapter = new ArrayAdapter<>(getContext(), R.layout.centered_spinner_text, getResources().getStringArray(R.array.pt_spinner));
ptChoiceAdapter.setDropDownViewResource(R.layout.centered_spinner_text);
mPowChoice.setAdapter(ptChoiceAdapter);
mTouChoice.setAdapter(ptChoiceAdapter);
ArrayAdapter<String> cmcChoiceAdapter = new ArrayAdapter<>(getContext(), R.layout.centered_spinner_text, getResources().getStringArray(R.array.cmc_spinner));
cmcChoiceAdapter.setDropDownViewResource(R.layout.centered_spinner_text);
mCmcChoice.setAdapter(cmcChoiceAdapter);
/* set the buttons to open the dialogs */
mFormatButton.setOnClickListener(v -> showDialog(SearchViewDialogFragment.FORMAT_LIST));
mRarityButton.setOnClickListener(v -> showDialog(SearchViewDialogFragment.RARITY_LIST));
/* This is a better default, might want to reorder the array */
mColorSpinner.setSelection(2);
/* The button colors change whether an option is selected or not */
checkDialogButtonColors();
/* This listener will do searches directly from the TextViews. Attach it to everything! */
TextView.OnEditorActionListener doSearchListener = (arg0, arg1, arg2) -> {
if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
doSearch();
return true;
}
return false;
};
mNameField.setOnEditorActionListener(doSearchListener);
mTextField.setOnEditorActionListener(doSearchListener);
mSupertypeField.setOnEditorActionListener(doSearchListener);
mSubtypeField.setOnEditorActionListener(doSearchListener);
mSetField.setOnEditorActionListener(doSearchListener);
mManaCostTextView.setOnEditorActionListener(doSearchListener);
mFlavorField.setOnEditorActionListener(doSearchListener);
mArtistField.setOnEditorActionListener(doSearchListener);
mWatermarkField.setOnEditorActionListener(doSearchListener);
mCollectorsNumberField.setOnEditorActionListener(doSearchListener);
/* set the autocomplete for card names */
mNameField.setAdapter(new AutocompleteCursorAdapter(this, new String[]{CardDbAdapter.KEY_NAME}, new int[]{R.id.text1}, mNameField, true));
mNameField.setOnItemClickListener((adapterView, view, i, l) -> {
SearchCriteria searchCriteria = new SearchCriteria();
searchCriteria.name = ((TextView) view.findViewById(R.id.text1)).getText().toString();
Bundle args = new Bundle();
args.putBoolean(CRITERIA_FLAG, true);
PreferenceAdapter.setSearchCriteria(getContext(), searchCriteria);
ResultListFragment rlFrag = new ResultListFragment();
startNewFragment(rlFrag, args);
});
/* Disable system level autocomplete for fields which do it already */
mNameField.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mArtistField.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mWatermarkField.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mSupertypeField.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mSubtypeField.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mSetField.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mManaCostTextView.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
/* Get a bunch of database info in a background task */
new BuildAutocompleteTask().execute(this);
/* set the search button! */
searchButton.setOnClickListener(v -> doSearch());
myFragmentView.findViewById(R.id.camera_button).setVisibility(View.GONE);
return myFragmentView;
}
private static class BuildAutocompleteTask extends AsyncTask<SearchViewFragment, Void, SearchViewFragment> {
@Override
protected SearchViewFragment doInBackground(SearchViewFragment... frags) {
SearchViewFragment frag = frags[0];
/* Only actually get data if the arrays are null */
Cursor formatCursor = null;
Cursor setCursor = null;
FamiliarDbHandle handle = new FamiliarDbHandle();
try {
SQLiteDatabase database = DatabaseManager.openDatabase(frag.getActivity(), false, handle);
if (frag.mSetNames == null) {
/* Query the database for all sets and fill the arrays to populate the list of choices with */
setCursor = CardDbAdapter.fetchAllSets(database);
setCursor.moveToFirst();
frag.mSetNames = new String[setCursor.getCount()];
frag.mSetSymbols = new String[setCursor.getCount()];
/* If this wasn't persisted, create it new */
if (frag.mSetCheckedIndices == null) {
frag.mSetCheckedIndices = new int[0];
}
for (int i = 0; i < setCursor.getCount(); i++) {
frag.mSetSymbols[i] = setCursor.getString(setCursor.getColumnIndex(CardDbAdapter.KEY_CODE));
frag.mSetNames[i] = setCursor.getString(setCursor.getColumnIndex(CardDbAdapter.KEY_NAME));
setCursor.moveToNext();
}
}
if (frag.mFormatNames == null) {
/* Query the database for all formats and fill the arrays to populate the list of choices with */
formatCursor = CardDbAdapter.fetchAllFormats(database);
formatCursor.moveToFirst();
frag.mFormatNames = new String[formatCursor.getCount()];
for (int i = 0; i < formatCursor.getCount(); i++) {
frag.mFormatNames[i] = formatCursor.getString(formatCursor.getColumnIndex(CardDbAdapter.KEY_NAME));
formatCursor.moveToNext();
}
}
if (frag.mSupertypes == null) {
String[] supertypes = CardDbAdapter.getUniqueColumnArray(CardDbAdapter.KEY_SUPERTYPE, true, database);
frag.mSupertypes = tokenStringsFromTypes(supertypes);
}
if (frag.mSubtypes == null) {
String[] subtypes = CardDbAdapter.getUniqueColumnArray(CardDbAdapter.KEY_SUBTYPE, true, database);
frag.mSubtypes = tokenStringsFromTypes(subtypes);
}
if (frag.mArtists == null) {
frag.mArtists = CardDbAdapter.getUniqueColumnArray(CardDbAdapter.KEY_ARTIST, false, database);
}
if (frag.mWatermarks == null) {
frag.mWatermarks = CardDbAdapter.getUniqueColumnArray(CardDbAdapter.KEY_WATERMARK, false, database);
}
} catch (SQLiteException | FamiliarDbException e) {
frag.handleFamiliarDbException(false);
} finally {
if (null != setCursor) {
setCursor.close();
}
if (null != formatCursor) {
formatCursor.close();
}
DatabaseManager.closeDatabase(frag.getActivity(), handle);
}
return frag;
}
private String[] tokenStringsFromTypes(String[] types) {
String tokenStrings[] = new String[types.length * 2];
System.arraycopy(types, 0, tokenStrings, 0, types.length);
for (int i = 0; i < types.length; i++) {
tokenStrings[types.length + i] = CardDbAdapter.EXCLUDE_TOKEN + types[i];
}
return tokenStrings;
}
@Override
protected void onPostExecute(SearchViewFragment frag) {
super.onPostExecute(frag);
Activity activity = frag.getActivity();
if (null == activity) {
return;
}
try {
/* set the autocomplete for supertypes */
if (null != frag.mSupertypes) {
ArrayAdapter<String> supertypeAdapter = new ArrayAdapter<>(
frag.getActivity(), R.layout.list_item_1, frag.mSupertypes);
frag.mSupertypeField.setAdapter(supertypeAdapter);
}
if (null != frag.mSubtypes) {
/* set the autocomplete for subtypes */
ArrayAdapter<String> subtypeAdapter = new ArrayAdapter<>(
frag.getActivity(), R.layout.list_item_1, frag.mSubtypes);
frag.mSubtypeField.setAdapter(subtypeAdapter);
}
if (null != frag.mArtists) {
/* set the autocomplete for sets */
final SetAdapter setAdapter = new SetAdapter(frag);
frag.mSetField.setAdapter(setAdapter);
/* set the autocomplete for artists */
ArrayAdapter<String> artistAdapter = new ArrayAdapter<>(
frag.getActivity(), R.layout.list_item_1, frag.mArtists);
frag.mArtistField.setThreshold(1);
frag.mArtistField.setAdapter(artistAdapter);
}
if (null != frag.mWatermarks) {
/* set the autocomplete for watermarks */
ArrayAdapter<String> watermarkAdapter = new ArrayAdapter<>(
frag.getActivity(), R.layout.list_item_1, frag.mWatermarks);
frag.mWatermarkField.setThreshold(1);
frag.mWatermarkField.setAdapter(watermarkAdapter);
}
} catch (NullPointerException e) {
/* If the UI thread isn't here, eat it */
}
}
}
private static class SetAdapter extends ArrayAdapter<String> {
final Map<String, String> symbolsByAutocomplete = new LinkedHashMap<>();
SetAdapter(SearchViewFragment frag) {
super(frag.getActivity(), R.layout.list_item_1);
for (int index = 0; index < frag.mSetSymbols.length; index++) {
String autocomplete = "[" + frag.mSetSymbols[index] + "] " + frag.mSetNames[index];
String set = frag.mSetSymbols[index];
symbolsByAutocomplete.put(autocomplete, set);
this.add(autocomplete);
}
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
((TextView) view.findViewById(R.id.text1)).setText(super.getItem(position));
return view;
}
@Nullable
@Override
public String getItem(int position) {
String key = super.getItem(position);
return symbolsByAutocomplete.get(key);
}
}
@Override
public void onPause() {
super.onPause();
// Save the search criteria
try {
PreferenceAdapter.setSearchViewCriteria(getContext(), parseForm());
} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
/* Eat it */
}
}
/**
* Generic onResume. Catches when consolidation is changed in preferences
*/
@Override
public void onResume() {
super.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);
// Load the saved criteria
setFieldsFromCriteria(PreferenceAdapter.getSearchViewCriteria(getContext()));
}
/**
* Save the state of the dialog selections
*
* @param outState Bundle in which to place your saved state.
*/
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putInt(SAVED_FORMAT_KEY, mSelectedFormat);
outState.putIntArray(SAVED_RARITY_KEY, mRarityCheckedIndices);
outState.putIntArray(SAVED_SET_KEY, mSetCheckedIndices);
super.onSaveInstanceState(outState);
}
/**
* This function creates a results fragment, sends it the search criteria, and starts it
*/
private void doSearch() {
Bundle args = new Bundle();
args.putBoolean(CRITERIA_FLAG, true);
try {
PreferenceAdapter.setSearchCriteria(getContext(), parseForm());
ResultListFragment rlFrag = new ResultListFragment();
startNewFragment(rlFrag, args);
} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
SnackbarWrapper.makeAndShowText(getActivity(), R.string.judges_corner_error, SnackbarWrapper.LENGTH_SHORT);
}
}
/**
* This function combs through all the UI elements and returns a SearchCriteria with the current search options
*
* @return a SearchCriteria with what the user wants to search for
*/
private SearchCriteria parseForm() throws ArrayIndexOutOfBoundsException, NullPointerException {
SearchCriteria searchCriteria = new SearchCriteria();
/* Because Android Studio whines */
assert mNameField.getText() != null;
assert mTextField.getText() != null;
assert mSupertypeField.getText() != null;
assert mSubtypeField.getText() != null;
assert mFlavorField.getText() != null;
assert mArtistField.getText() != null;
assert mWatermarkField.getText() != null;
assert mSetField.getText() != null;
assert mCollectorsNumberField.getText() != null;
assert mManaCostTextView.getText() != null;
/* Read EditTexts */
searchCriteria.name = mNameField.getText().toString().trim();
searchCriteria.text = mTextField.getText().toString().trim();
searchCriteria.superTypes = mSupertypeField.getObjects();
searchCriteria.subTypes = mSubtypeField.getObjects();
searchCriteria.flavor = mFlavorField.getText().toString().trim();
searchCriteria.artist = mArtistField.getText().toString().trim();
searchCriteria.watermark = mWatermarkField.getText().toString().trim();
searchCriteria.collectorsNumber = mCollectorsNumberField.getText().toString().trim();
searchCriteria.sets = mSetField.getObjects();
searchCriteria.manaCost = mManaCostTextView.getObjects();
searchCriteria.manaCostLogic = (Comparison) mManaComparisonSpinner.getSelectedItem();
if (searchCriteria.name.length() == 0) {
searchCriteria.name = null;
}
if (searchCriteria.text.length() == 0) {
searchCriteria.text = null;
}
if (searchCriteria.superTypes.size() == 0) {
searchCriteria.superTypes = null;
}
if (searchCriteria.subTypes.size() == 0) {
searchCriteria.subTypes = null;
}
if (searchCriteria.flavor.length() == 0) {
searchCriteria.flavor = null;
}
if (searchCriteria.artist.length() == 0) {
searchCriteria.artist = null;
}
if (searchCriteria.collectorsNumber.length() == 0) {
searchCriteria.collectorsNumber = null;
}
if (searchCriteria.manaCost.size() == 0) {
searchCriteria.manaCost = null;
}
if (searchCriteria.sets.size() == 0) {
searchCriteria.sets = null;
}
if (searchCriteria.watermark.length() == 0) {
searchCriteria.watermark = null;
}
/* Build a color string. capital letters means the user is search for that color */
searchCriteria.color = null;
if (mCheckboxW.isChecked()) {
searchCriteria.color = "W";
} else {
searchCriteria.color = "w";
}
if (mCheckboxU.isChecked()) {
searchCriteria.color += "U";
} else {
searchCriteria.color += "u";
}
if (mCheckboxB.isChecked()) {
searchCriteria.color += "B";
} else {
searchCriteria.color += "b";
}
if (mCheckboxR.isChecked()) {
searchCriteria.color += "R";
} else {
searchCriteria.color += "r";
}
if (mCheckboxG.isChecked()) {
searchCriteria.color += "G";
} else {
searchCriteria.color += "g";
}
if (mCheckboxL.isChecked()) {
searchCriteria.color += "L";
} else {
searchCriteria.color += "l";
}
searchCriteria.colorLogic = mColorSpinner.getSelectedItemPosition();
/* Build a color identity string */
searchCriteria.colorIdentity = "";
if (mCheckboxWIdentity.isChecked()) {
searchCriteria.colorIdentity += "W";
} else {
searchCriteria.colorIdentity += "w";
}
if (mCheckboxUIdentity.isChecked()) {
searchCriteria.colorIdentity += "U";
} else {
searchCriteria.colorIdentity += "u";
}
if (mCheckboxBIdentity.isChecked()) {
searchCriteria.colorIdentity += "B";
} else {
searchCriteria.colorIdentity += "b";
}
if (mCheckboxRIdentity.isChecked()) {
searchCriteria.colorIdentity += "R";
} else {
searchCriteria.colorIdentity += "r";
}
if (mCheckboxGIdentity.isChecked()) {
searchCriteria.colorIdentity += "G";
} else {
searchCriteria.colorIdentity += "g";
}
if (mCheckboxLIdentity.isChecked()) {
searchCriteria.colorIdentity += "L";
} else {
searchCriteria.colorIdentity += "l";
}
searchCriteria.colorIdentityLogic = mColorIdentitySpinner.getSelectedItemPosition();
searchCriteria.format = null;
if (mSelectedFormat != -1 && mFormatNames != null) {
searchCriteria.format = mFormatNames[mSelectedFormat];
}
StringBuilder rarityBuilder = new StringBuilder();
for (int index : mRarityCheckedIndices) {
rarityBuilder.append(mRarityCodes[index]);
}
if (rarityBuilder.length() > 0) {
searchCriteria.rarity = rarityBuilder.toString();
} else {
searchCriteria.rarity = null;
}
String[] logicChoices = getResources().getStringArray(R.array.logic_spinner);
String power = getResources().getStringArray(R.array.pt_spinner)[mPowChoice.getSelectedItemPosition()];
String toughness = getResources().getStringArray(R.array.pt_spinner)[mTouChoice.getSelectedItemPosition()];
float pow = CardDbAdapter.NO_ONE_CARES;
try {
pow = Float.parseFloat(power);
} catch (NumberFormatException e) {
switch (power) {
case "*":
pow = CardDbAdapter.STAR;
break;
case "1+*":
pow = CardDbAdapter.ONE_PLUS_STAR;
break;
case "2+*":
pow = CardDbAdapter.TWO_PLUS_STAR;
break;
case "7-*":
pow = CardDbAdapter.SEVEN_MINUS_STAR;
break;
case "*^2":
pow = CardDbAdapter.STAR_SQUARED;
break;
case "X":
pow = CardDbAdapter.X;
break;
case "?":
pow = CardDbAdapter.QUESTION_MARK;
break;
case "∞":
pow = CardDbAdapter.INFINITY;
break;
}
}
searchCriteria.powChoice = pow;
searchCriteria.powLogic = logicChoices[mPowLogic.getSelectedItemPosition()];
float tou = CardDbAdapter.NO_ONE_CARES;
try {
tou = Float.parseFloat(toughness);
} catch (NumberFormatException e) {
switch (toughness) {
case "*":
tou = CardDbAdapter.STAR;
break;
case "1+*":
tou = CardDbAdapter.ONE_PLUS_STAR;
break;
case "2+*":
tou = CardDbAdapter.TWO_PLUS_STAR;
break;
case "7-*":
tou = CardDbAdapter.SEVEN_MINUS_STAR;
break;
case "*^2":
tou = CardDbAdapter.STAR_SQUARED;
break;
case "X":
tou = CardDbAdapter.X;
break;
case "?":
tou = CardDbAdapter.QUESTION_MARK;
break;
case "∞":
tou = CardDbAdapter.INFINITY;
break;
}
}
searchCriteria.touChoice = tou;
searchCriteria.touLogic = logicChoices[mTouLogic.getSelectedItemPosition()];
String[] cmcChoices = getResources().getStringArray(R.array.cmc_spinner);
int cmc;
try {
cmc = Integer.parseInt(cmcChoices[mCmcChoice.getSelectedItemPosition()]);
} catch (NumberFormatException e) {
cmc = -1;
}
searchCriteria.cmc = cmc;
searchCriteria.cmcLogic = logicChoices[mCmcLogic.getSelectedItemPosition()];
searchCriteria.typeLogic = mTypeSpinner.getSelectedItemPosition();
searchCriteria.textLogic = mTextSpinner.getSelectedItemPosition();
searchCriteria.setLogic = mSetSpinner.getSelectedItemPosition();
return searchCriteria;
}
/**
* This function clears all the search options, it's called from the ActionBar
*/
private void clear() {
mNameField.setText("");
mSupertypeField.clearTextAndTokens();
mSubtypeField.clearTextAndTokens();
mTextField.setText("");
mArtistField.setText("");
mWatermarkField.setText("");
mFlavorField.setText("");
mCollectorsNumberField.setText("");
mSetField.clearTextAndTokens();
mCheckboxW.setChecked(false);
mCheckboxU.setChecked(false);
mCheckboxB.setChecked(false);
mCheckboxR.setChecked(false);
mCheckboxG.setChecked(false);
mCheckboxL.setChecked(false);
mColorSpinner.setSelection(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);
mManaCostTextView.clearTextAndTokens();
mManaComparisonSpinner.setSelection(Comparison.EMPTY.ordinal());
if (mSetCheckedIndices != null) {
mSetCheckedIndices = new int[0];
}
mSelectedFormat = -1;
mRarityCheckedIndices = new int[0];
this.removeDialog(getFragmentManager());
checkDialogButtonColors();
}
/**
* This function saves the current search options into a file, so the user can have a default search
*/
private void persistOptions() {
try {
FileOutputStream fileStream = this.getActivity()
.openFileOutput(DEFAULT_CRITERIA_FILE, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(parseForm());
os.close();
} catch (IOException | ArrayIndexOutOfBoundsException | NullPointerException e) {
SnackbarWrapper.makeAndShowText(this.getActivity(), R.string.search_toast_cannot_save, SnackbarWrapper.LENGTH_LONG);
}
}
/**
* This function reads the saved default search options and populates the UI elements
*/
private void fetchPersistedOptions() {
try {
FileInputStream fileInputStream = this.getActivity().openFileInput(DEFAULT_CRITERIA_FILE);
ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
SearchCriteria criteria = (SearchCriteria) oInputStream.readObject();
oInputStream.close();
setFieldsFromCriteria(criteria);
} catch (IOException | ClassNotFoundException e) {
SnackbarWrapper.makeAndShowText(this.getActivity(), R.string.search_toast_cannot_load, SnackbarWrapper.LENGTH_LONG);
}
}
private void setFieldsFromCriteria(SearchCriteria criteria) {
/* Set name */
if (null != criteria.name) {
mNameField.setText(criteria.name);
}
/* Set type fields */
if (null != criteria.superTypes && criteria.superTypes.size() > 0) {
for (String supertype : criteria.superTypes) {
mSupertypeField.addObject(supertype);
}
} else {
mSupertypeField.clearTextAndTokens();
}
if (null != criteria.subTypes && criteria.subTypes.size() > 0) {
for (String subtype : criteria.subTypes) {
mSubtypeField.addObject(subtype);
}
} else {
mSubtypeField.clearTextAndTokens();
}
mTypeSpinner.setSelection(criteria.typeLogic);
/* Set text fields */
mTextField.setText(criteria.text);
mTextSpinner.setSelection(criteria.textLogic);
/* Set color fields */
if (criteria.color != null) {
mCheckboxW.setChecked(criteria.color.contains("W"));
mCheckboxU.setChecked(criteria.color.contains("U"));
mCheckboxB.setChecked(criteria.color.contains("B"));
mCheckboxR.setChecked(criteria.color.contains("R"));
mCheckboxG.setChecked(criteria.color.contains("G"));
mCheckboxL.setChecked(criteria.color.contains("L"));
}
mColorSpinner.setSelection(criteria.colorLogic);
if (criteria.colorIdentity != null) {
mCheckboxWIdentity.setChecked(criteria.colorIdentity.contains("W"));
mCheckboxUIdentity.setChecked(criteria.colorIdentity.contains("U"));
mCheckboxBIdentity.setChecked(criteria.colorIdentity.contains("B"));
mCheckboxRIdentity.setChecked(criteria.colorIdentity.contains("R"));
mCheckboxGIdentity.setChecked(criteria.colorIdentity.contains("G"));
mCheckboxLIdentity.setChecked(criteria.colorIdentity.contains("L"));
}
mColorIdentitySpinner.setSelection(criteria.colorIdentityLogic);
/* Set power and toughness fields */
List<String> logicChoices = Arrays.asList(getResources().getStringArray(R.array.logic_spinner));
mPowLogic.setSelection(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("*"));
else if (p == CardDbAdapter.ONE_PLUS_STAR)
mPowChoice.setSelection(ptList.indexOf("1+*"));
else if (p == CardDbAdapter.TWO_PLUS_STAR)
mPowChoice.setSelection(ptList.indexOf("2+*"));
else if (p == CardDbAdapter.SEVEN_MINUS_STAR)
mPowChoice.setSelection(ptList.indexOf("7-*"));
else if (p == CardDbAdapter.STAR_SQUARED)
mPowChoice.setSelection(ptList.indexOf("*^2"));
else if (p == CardDbAdapter.X)
mPowChoice.setSelection(ptList.indexOf("X"));
else if (p == CardDbAdapter.QUESTION_MARK)
mPowChoice.setSelection(ptList.indexOf("?"));
else if (p == CardDbAdapter.INFINITY)
mPowChoice.setSelection(ptList.indexOf("∞"));
else {
if (p == (int) p) {
mPowChoice.setSelection(ptList.indexOf(((int) p) + ""));
} else {
mPowChoice.setSelection(ptList.indexOf(p + ""));
}
}
}
mTouLogic.setSelection(logicChoices.indexOf(criteria.touLogic));
float t = criteria.touChoice;
if (t != CardDbAdapter.NO_ONE_CARES) {
if (t == CardDbAdapter.STAR)
mTouChoice.setSelection(ptList.indexOf("*"));
else if (t == CardDbAdapter.ONE_PLUS_STAR)
mTouChoice.setSelection(ptList.indexOf("1+*"));
else if (t == CardDbAdapter.TWO_PLUS_STAR)
mTouChoice.setSelection(ptList.indexOf("2+*"));
else if (t == CardDbAdapter.SEVEN_MINUS_STAR)
mTouChoice.setSelection(ptList.indexOf("7-*"));
else if (t == CardDbAdapter.STAR_SQUARED)
mTouChoice.setSelection(ptList.indexOf("*^2"));
else if (t == CardDbAdapter.X)
mTouChoice.setSelection(ptList.indexOf("X"));
else if (t == CardDbAdapter.QUESTION_MARK)
mTouChoice.setSelection(ptList.indexOf("?"));
else if (t == CardDbAdapter.INFINITY)
mTouChoice.setSelection(ptList.indexOf("∞"));
else {
if (t == (int) t) {
mTouChoice.setSelection(ptList.indexOf(((int) t) + ""));
} else {
mTouChoice.setSelection(ptList.indexOf(t + ""));
}
}
}
/* Set CMC fields */
mCmcLogic.setSelection(logicChoices.indexOf(criteria.cmcLogic));
mCmcChoice.setSelection(Arrays.asList(getResources().getStringArray(R.array.cmc_spinner))
.indexOf(String.valueOf(criteria.cmc)));
/* Set mana fields */
if (criteria.manaCost != null && criteria.manaCost.size() > 0) {
for (String mana : criteria.manaCost) {
mManaCostTextView.addObject(mana);
}
} else {
mManaCostTextView.clearTextAndTokens();
}
if (null != criteria.manaCostLogic) {
mManaComparisonSpinner.setSelection(criteria.manaCostLogic.ordinal());
}
/* set format */
if (mFormatNames != null) {
mSelectedFormat = Arrays.asList(mFormatNames).indexOf(criteria.format);
}
/* Set rarity */
if (criteria.rarity != null) {
ArrayList<Integer> rarityCheckedIndicesTmp = new ArrayList<>();
/* For each rarity */
for (int i = 0; i < mRarityNames.length; i++) {