From 229a8afd8b6c9d2ca89fbe5bf69a47097049ad3f Mon Sep 17 00:00:00 2001 From: Kala30 Date: Wed, 18 Jul 2018 14:47:32 -0700 Subject: [PATCH] Added sort functionality in MainActivity --- .idea/assetWizardSettings.xml | 5 +- .../cogentworks/overwidget/MainActivity.java | 80 ++++++++++++++----- app/src/main/res/drawable/ic_sort.xml | 5 ++ app/src/main/res/drawable/ic_sort_black.xml | 9 +++ app/src/main/res/menu/menu.xml | 6 +- app/src/main/res/values/strings.xml | 5 ++ 6 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 app/src/main/res/drawable/ic_sort.xml create mode 100644 app/src/main/res/drawable/ic_sort_black.xml diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml index fc3f0c6..7c50db5 100644 --- a/.idea/assetWizardSettings.xml +++ b/.idea/assetWizardSettings.xml @@ -67,7 +67,7 @@ @@ -77,8 +77,7 @@ diff --git a/app/src/main/java/com/cogentworks/overwidget/MainActivity.java b/app/src/main/java/com/cogentworks/overwidget/MainActivity.java index a3600b0..cbf67bf 100644 --- a/app/src/main/java/com/cogentworks/overwidget/MainActivity.java +++ b/app/src/main/java/com/cogentworks/overwidget/MainActivity.java @@ -29,6 +29,8 @@ import com.woxthebox.draglistview.swipe.ListSwipeItem; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; public class MainActivity extends AppCompatActivity { @@ -251,6 +253,10 @@ public boolean onPrepareOptionsMenu(Menu menu) { .getBoolean(SettingsActivity.PREF_DARK_THEME, false); checkable.setChecked(isDark); + MenuItem sort = menu.findItem(R.id.sort); + if (isDark) + sort.setIcon(R.drawable.ic_sort); + return true; } @@ -275,44 +281,76 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.settings: startActivityForResult(new Intent(this, SettingsActivity.class), REQUEST_EXIT); return true; - /*case R.id.sort: - ItemAdapter listAdapter = new ItemAdapter(mItemArray, R.layout.list_item, R.id.layout_main, true); - mDragListView.setAdapter(listAdapter, true); - return true;*/ + case R.id.sort: + sortItemsDialog(); + return true; default: return false; } } - /*private void sortItemsDialog() { + private static int lastSortItem; + + private void sortItemsDialog() { if (!isBusy) { AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("Sort by") - .setSingleChoiceItems(R.) - .setPositiveButton("Add", new DialogInterface.OnClickListener() { + .setSingleChoiceItems(R.array.sort_array, lastSortItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - EditText editText = dialogView.findViewById(R.id.config_battletag); - String battleTag = String.valueOf(editText.getText()); - Spinner platformSpinner = dialogView.findViewById(R.id.platform_spinner); - String platform = platformSpinner.getSelectedItem().toString(); - Spinner regionSpinner = dialogView.findViewById(R.id.region_spinner); - String region = regionSpinner.getSelectedItem().toString(); - - if (!dbHelper.getList("BattleTag").contains(battleTag)) { - Toast.makeText(findViewById(R.id.swiperefresh).getContext(), "Adding player...", Toast.LENGTH_SHORT).show(); - AddProfileTask addTask = new AddProfileTask(context, battleTag, platform, region); - addTask.execute(); - } else { - Snackbar.make(findViewById(R.id.layout_main), "Player already added to list", Snackbar.LENGTH_SHORT).show(); + ArrayList itemList = dbHelper.getList(); + Comparator comparator; + switch (which) { + case 0: // Name + comparator = new Comparator() { + @Override + public int compare(Profile o1, Profile o2) { + return o1.BattleTag.compareTo(o2.BattleTag); + } + }; + break; + case 1: // SR + comparator = new Comparator() { + @Override + public int compare(Profile o1, Profile o2) { + int compRank1 = 0; + if (!o1.CompRank.equals("")) + compRank1 = Integer.parseInt(o1.CompRank); + int compRank2 = 0; + if (!o2.CompRank.equals("")) + compRank2 = Integer.parseInt(o2.CompRank); + return compRank2 - compRank1; + } + }; + break; + case 2: // Level + comparator = new Comparator() { + @Override + public int compare(Profile o1, Profile o2) { + int level1 = Integer.parseInt(o1.Level) + Integer.parseInt(o1.Prestige) * 100; + int level2 = Integer.parseInt(o2.Level) + Integer.parseInt(o2.Prestige) * 100; + return level2 - level1; + } + }; + break; + default: + return; } + Collections.sort(itemList, comparator); + + ItemAdapter listAdapter = new ItemAdapter(itemList, R.layout.list_item, R.id.layout_main, true); + mDragListView.setAdapter(listAdapter, true); + + dbHelper.setList(itemList); + lastSortItem = which; + dialog.dismiss(); } }) .setNegativeButton("Cancel", null) .create(); dialog.show(); } - }*/ + } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { diff --git a/app/src/main/res/drawable/ic_sort.xml b/app/src/main/res/drawable/ic_sort.xml new file mode 100644 index 0000000..b3e29ab --- /dev/null +++ b/app/src/main/res/drawable/ic_sort.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_sort_black.xml b/app/src/main/res/drawable/ic_sort_black.xml new file mode 100644 index 0000000..fd4c56f --- /dev/null +++ b/app/src/main/res/drawable/ic_sort_black.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index 935db46..b026525 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -11,7 +11,9 @@ android:title="Blackwatch" android:checkable="true" /> - + android:title="Sort" + android:icon="@drawable/ic_sort_black" + app:showAsAction="always"/> \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5c9b6bd..69da49f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -29,6 +29,11 @@ Overbuff Master Overwatch + + Name + SR + Level + OverWidget Settings