From 37d75491f4a2d1fa92d8a9bdcc088842aaeee4cc Mon Sep 17 00:00:00 2001 From: RichyHBM Date: Sat, 11 Nov 2017 18:59:56 +0000 Subject: [PATCH] Update tag drawer when ever tags may have been added/removed --- .../andotp/Activities/MainActivity.java | 42 ++++++++++++++++++- .../andotp/View/EntriesCardAdapter.java | 27 +++++++++++- .../flocke/andotp/View/TagsAdapter.java | 9 ++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java index 031a9620..f67a60a0 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java @@ -92,6 +92,7 @@ public class MainActivity extends BaseActivity private Handler handler; private Runnable handlerTask; + private ListView tagsDrawerListView; private TagsAdapter tagsDrawerAdapter; private ActionBarDrawerToggle tagsToggle; @@ -175,6 +176,8 @@ public class MainActivity extends BaseActivity e.updateOTP(); adapter.addEntry(e); adapter.saveEntries(); + + refreshTags(); } } }) @@ -409,13 +412,16 @@ public class MainActivity extends BaseActivity e.updateOTP(); adapter.addEntry(e); adapter.saveEntries(); + refreshTags(); } catch (Exception e) { Toast.makeText(this, R.string.toast_invalid_qr_code, Toast.LENGTH_LONG).show(); } } } else if (requestCode == INTENT_INTERNAL_BACKUP && resultCode == RESULT_OK) { - if (intent.getBooleanExtra("reload", false)) + if (intent.getBooleanExtra("reload", false)) { adapter.loadEntries(); + refreshTags(); + } } else if (requestCode == INTENT_INTERNAL_AUTHENTICATE) { if (resultCode != RESULT_OK) { Toast.makeText(getBaseContext(), R.string.toast_auth_failed, Toast.LENGTH_LONG).show(); @@ -527,7 +533,7 @@ public class MainActivity extends BaseActivity } private void setupDrawer() { - final ListView tagsDrawerListView = (ListView)findViewById(R.id.tags_list_in_drawer); + tagsDrawerListView = (ListView)findViewById(R.id.tags_list_in_drawer); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); @@ -566,6 +572,12 @@ public class MainActivity extends BaseActivity CheckedTextView childCheckBox = (CheckedTextView)tagsDrawerListView.getChildAt(i); childCheckBox.setChecked(checkedTextView.isChecked()); } + + if(checkedTextView.isChecked()) { + adapter.filterByTags(adapter.getTags()); + } else { + adapter.filterByTags(new ArrayList()); + } } }); allTagsButton.setChecked(settings.getAllTagsToggle()); @@ -582,7 +594,33 @@ public class MainActivity extends BaseActivity checkedTextView.setChecked(!checkedTextView.isChecked()); settings.setTagToggle(checkedTextView.getText().toString(), checkedTextView.isChecked()); + + List checkedTags = new ArrayList<>(); + for(int i = 0; i < tagsDrawerListView.getChildCount(); i++) { + CheckedTextView childCheckBox = (CheckedTextView)tagsDrawerListView.getChildAt(i); + if(childCheckBox.isChecked()) { + checkedTags.add(childCheckBox.getText().toString()); + } + } + adapter.filterByTags(checkedTags); } }); + + adapter.filterByTags(getCheckedTags()); + } + + void refreshTags() { + tagsDrawerAdapter.setTags(adapter.getTags()); + adapter.filterByTags(getCheckedTags()); + } + + List getCheckedTags() { + List checkedTags = new ArrayList<>(); + for(String tag : adapter.getTags()) { + if(settings.getTagToggle(tag)) { + checkedTags.add(tag); + } + } + return checkedTags; } } \ No newline at end of file diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java index b48bce19..c2a4bd22 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java @@ -63,6 +63,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter private ArrayList entries; private ArrayList displayedEntries; private Callback callback; + private List tagsFilter = new ArrayList<>(); private SortMode sortMode = SortMode.UNSORTED; @@ -93,6 +94,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter public void entriesChanged() { displayedEntries = sortEntries(entries); + filterByTags(tagsFilter); notifyDataSetChanged(); } @@ -105,6 +107,29 @@ public class EntriesCardAdapter extends RecyclerView.Adapter entriesChanged(); } + public void filterByTags(List tags) { + tagsFilter = tags; + List matchingEntries = new ArrayList<>(); + + for(Entry e : entries) { + //Entries with no tags will always be shown + Boolean foundMatchingTag = e.getTags().isEmpty(); + + for(String tag : tags) { + if(e.getTags().contains(tag)) { + foundMatchingTag = true; + } + } + + if(foundMatchingTag) { + matchingEntries.add(e); + } + } + + displayedEntries = sortEntries(matchingEntries); + notifyDataSetChanged(); + } + public void updateTokens() { boolean change = false; @@ -293,7 +318,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter return this.sortMode; } - private ArrayList sortEntries(ArrayList unsorted) { + private ArrayList sortEntries(List unsorted) { ArrayList sorted = new ArrayList<>(unsorted); if (sortMode == SortMode.LABEL) { diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/TagsAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/View/TagsAdapter.java index ecb18627..09884282 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/TagsAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/TagsAdapter.java @@ -9,6 +9,7 @@ import android.widget.CheckedTextView; import org.shadowice.flocke.andotp.Utilities.Settings; +import java.util.Collections; import java.util.List; public class TagsAdapter extends ArrayAdapter { @@ -36,4 +37,12 @@ public class TagsAdapter extends ArrayAdapter { checkedTextView.setChecked(settings.getTagToggle(tags.get(i))); return checkedTextView; } + + public void setTags(List tags) { + this.tags = tags; + Collections.sort(this.tags); + this.clear(); + this.addAll(this.tags); + notifyDataSetChanged(); + } }