Update tag drawer when ever tags may have been added/removed

This commit is contained in:
RichyHBM 2017-11-11 18:59:56 +00:00
parent 79b9ee3aa9
commit 37d75491f4
3 changed files with 75 additions and 3 deletions

View file

@ -92,6 +92,7 @@ public class MainActivity extends BaseActivity
private Handler handler; private Handler handler;
private Runnable handlerTask; private Runnable handlerTask;
private ListView tagsDrawerListView;
private TagsAdapter tagsDrawerAdapter; private TagsAdapter tagsDrawerAdapter;
private ActionBarDrawerToggle tagsToggle; private ActionBarDrawerToggle tagsToggle;
@ -175,6 +176,8 @@ public class MainActivity extends BaseActivity
e.updateOTP(); e.updateOTP();
adapter.addEntry(e); adapter.addEntry(e);
adapter.saveEntries(); adapter.saveEntries();
refreshTags();
} }
} }
}) })
@ -409,13 +412,16 @@ public class MainActivity extends BaseActivity
e.updateOTP(); e.updateOTP();
adapter.addEntry(e); adapter.addEntry(e);
adapter.saveEntries(); adapter.saveEntries();
refreshTags();
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(this, R.string.toast_invalid_qr_code, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.toast_invalid_qr_code, Toast.LENGTH_LONG).show();
} }
} }
} else if (requestCode == INTENT_INTERNAL_BACKUP && resultCode == RESULT_OK) { } else if (requestCode == INTENT_INTERNAL_BACKUP && resultCode == RESULT_OK) {
if (intent.getBooleanExtra("reload", false)) if (intent.getBooleanExtra("reload", false)) {
adapter.loadEntries(); adapter.loadEntries();
refreshTags();
}
} else if (requestCode == INTENT_INTERNAL_AUTHENTICATE) { } else if (requestCode == INTENT_INTERNAL_AUTHENTICATE) {
if (resultCode != RESULT_OK) { if (resultCode != RESULT_OK) {
Toast.makeText(getBaseContext(), R.string.toast_auth_failed, Toast.LENGTH_LONG).show(); Toast.makeText(getBaseContext(), R.string.toast_auth_failed, Toast.LENGTH_LONG).show();
@ -527,7 +533,7 @@ public class MainActivity extends BaseActivity
} }
private void setupDrawer() { 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().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
@ -566,6 +572,12 @@ public class MainActivity extends BaseActivity
CheckedTextView childCheckBox = (CheckedTextView)tagsDrawerListView.getChildAt(i); CheckedTextView childCheckBox = (CheckedTextView)tagsDrawerListView.getChildAt(i);
childCheckBox.setChecked(checkedTextView.isChecked()); childCheckBox.setChecked(checkedTextView.isChecked());
} }
if(checkedTextView.isChecked()) {
adapter.filterByTags(adapter.getTags());
} else {
adapter.filterByTags(new ArrayList<String>());
}
} }
}); });
allTagsButton.setChecked(settings.getAllTagsToggle()); allTagsButton.setChecked(settings.getAllTagsToggle());
@ -582,7 +594,33 @@ public class MainActivity extends BaseActivity
checkedTextView.setChecked(!checkedTextView.isChecked()); checkedTextView.setChecked(!checkedTextView.isChecked());
settings.setTagToggle(checkedTextView.getText().toString(), checkedTextView.isChecked()); settings.setTagToggle(checkedTextView.getText().toString(), checkedTextView.isChecked());
List<String> 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<String> getCheckedTags() {
List<String> checkedTags = new ArrayList<>();
for(String tag : adapter.getTags()) {
if(settings.getTagToggle(tag)) {
checkedTags.add(tag);
}
}
return checkedTags;
} }
} }

View file

@ -63,6 +63,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
private ArrayList<Entry> entries; private ArrayList<Entry> entries;
private ArrayList<Entry> displayedEntries; private ArrayList<Entry> displayedEntries;
private Callback callback; private Callback callback;
private List<String> tagsFilter = new ArrayList<>();
private SortMode sortMode = SortMode.UNSORTED; private SortMode sortMode = SortMode.UNSORTED;
@ -93,6 +94,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
public void entriesChanged() { public void entriesChanged() {
displayedEntries = sortEntries(entries); displayedEntries = sortEntries(entries);
filterByTags(tagsFilter);
notifyDataSetChanged(); notifyDataSetChanged();
} }
@ -105,6 +107,29 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
entriesChanged(); entriesChanged();
} }
public void filterByTags(List<String> tags) {
tagsFilter = tags;
List<Entry> 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() { public void updateTokens() {
boolean change = false; boolean change = false;
@ -293,7 +318,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
return this.sortMode; return this.sortMode;
} }
private ArrayList<Entry> sortEntries(ArrayList<Entry> unsorted) { private ArrayList<Entry> sortEntries(List<Entry> unsorted) {
ArrayList<Entry> sorted = new ArrayList<>(unsorted); ArrayList<Entry> sorted = new ArrayList<>(unsorted);
if (sortMode == SortMode.LABEL) { if (sortMode == SortMode.LABEL) {

View file

@ -9,6 +9,7 @@ import android.widget.CheckedTextView;
import org.shadowice.flocke.andotp.Utilities.Settings; import org.shadowice.flocke.andotp.Utilities.Settings;
import java.util.Collections;
import java.util.List; import java.util.List;
public class TagsAdapter extends ArrayAdapter<String> { public class TagsAdapter extends ArrayAdapter<String> {
@ -36,4 +37,12 @@ public class TagsAdapter extends ArrayAdapter<String> {
checkedTextView.setChecked(settings.getTagToggle(tags.get(i))); checkedTextView.setChecked(settings.getTagToggle(tags.get(i)));
return checkedTextView; return checkedTextView;
} }
public void setTags(List<String> tags) {
this.tags = tags;
Collections.sort(this.tags);
this.clear();
this.addAll(this.tags);
notifyDataSetChanged();
}
} }