Merge pull request #605 from alexbakker/highlight-on-add

After adding a new entry, scroll to it and highlight it
This commit is contained in:
Michael Schättgen 2020-12-03 21:48:48 +01:00 committed by GitHub
commit f15a0018ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 17 deletions

View file

@ -285,7 +285,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
if (_loaded) { if (_loaded) {
UUID entryUUID = (UUID) data.getSerializableExtra("entryUUID"); UUID entryUUID = (UUID) data.getSerializableExtra("entryUUID");
VaultEntry entry = _vault.getEntryByUUID(entryUUID); VaultEntry entry = _vault.getEntryByUUID(entryUUID);
_entryListView.addEntry(entry); _entryListView.addEntry(entry, true);
} }
} }

View file

@ -37,6 +37,7 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
private boolean _showAccountName; private boolean _showAccountName;
private boolean _searchAccountName; private boolean _searchAccountName;
private boolean _highlightEntry; private boolean _highlightEntry;
private boolean _tempHighlightEntry;
private boolean _tapToReveal; private boolean _tapToReveal;
private int _tapToRevealTime; private int _tapToRevealTime;
private boolean _copyOnTap; private boolean _copyOnTap;
@ -92,6 +93,10 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
_highlightEntry = highlightEntry; _highlightEntry = highlightEntry;
} }
public void setTempHighlightEntry(boolean highlightEntry) {
_tempHighlightEntry = highlightEntry;
}
public void setIsCopyOnTapEnabled(boolean enabled) { public void setIsCopyOnTapEnabled(boolean enabled) {
_copyOnTap = enabled; _copyOnTap = enabled;
} }
@ -100,13 +105,13 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
return _shownEntries.get(position); return _shownEntries.get(position);
} }
public void addEntry(VaultEntry entry) { public int addEntry(VaultEntry entry) {
_entries.add(entry); _entries.add(entry);
if (isEntryFiltered(entry)) { if (isEntryFiltered(entry)) {
return; return -1;
} }
boolean added = false; int position = -1;
Comparator<VaultEntry> comparator = _sortCategory.getComparator(); Comparator<VaultEntry> comparator = _sortCategory.getComparator();
if (comparator != null) { if (comparator != null) {
// insert the entry in the correct order // insert the entry in the correct order
@ -115,16 +120,16 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
if (comparator.compare(_shownEntries.get(i), entry) > 0) { if (comparator.compare(_shownEntries.get(i), entry) > 0) {
_shownEntries.add(i, entry); _shownEntries.add(i, entry);
notifyItemInserted(i); notifyItemInserted(i);
added = true; position = i;
break; break;
} }
} }
} }
if (!added){ if (position < 0){
_shownEntries.add(entry); _shownEntries.add(entry);
int position = getItemCount() - 1; position = getItemCount() - 1;
if (position == 0) { if (position == 0) {
notifyDataSetChanged(); notifyDataSetChanged();
} else { } else {
@ -134,6 +139,7 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
_view.onListChange(); _view.onListChange();
checkPeriodUniformity(); checkPeriodUniformity();
return position;
} }
public void addEntries(Collection<VaultEntry> entries) { public void addEntries(Collection<VaultEntry> entries) {
@ -341,7 +347,7 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
VaultEntry entry = _shownEntries.get(position); VaultEntry entry = _shownEntries.get(position);
boolean hidden = _tapToReveal && entry != _focusedEntry; boolean hidden = _tapToReveal && entry != _focusedEntry;
boolean dimmed = _highlightEntry && _focusedEntry != null && _focusedEntry != entry; boolean dimmed = (_highlightEntry || _tempHighlightEntry) && _focusedEntry != null && _focusedEntry != entry;
boolean showProgress = entry.getInfo() instanceof TotpInfo && ((TotpInfo) entry.getInfo()).getPeriod() != getMostFrequentPeriod(); boolean showProgress = entry.getInfo() instanceof TotpInfo && ((TotpInfo) entry.getInfo()).getPeriod() != getMostFrequentPeriod();
holder.setData(entry, _codeGroupSize, _showAccountName, showProgress, hidden, dimmed); holder.setData(entry, _codeGroupSize, _showAccountName, showProgress, hidden, dimmed);
holder.setFocused(_selectedEntries.contains(entry)); holder.setFocused(_selectedEntries.contains(entry));
@ -358,12 +364,12 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
holder.animateCopyText(); holder.animateCopyText();
} }
if (_highlightEntry || _tapToReveal) { if (_highlightEntry || _tempHighlightEntry || _tapToReveal) {
if (_focusedEntry == entry) { if (_focusedEntry == entry) {
resetFocus(); resetFocus();
handled = true; handled = true;
} else { } else {
focusEntry(entry); focusEntry(entry, _tapToRevealTime);
} }
} }
} else { } else {
@ -497,20 +503,20 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
return maxValue > 1 ? maxKey : -1; return maxValue > 1 ? maxKey : -1;
} }
private void focusEntry(VaultEntry entry) { public void focusEntry(VaultEntry entry, int secondsToFocus) {
_focusedEntry = entry; _focusedEntry = entry;
_dimHandler.removeCallbacksAndMessages(null); _dimHandler.removeCallbacksAndMessages(null);
for (EntryHolder holder : _holders) { for (EntryHolder holder : _holders) {
if (holder.getEntry() != _focusedEntry) { if (holder.getEntry() != _focusedEntry) {
if (_highlightEntry) { if (_highlightEntry || _tempHighlightEntry) {
holder.dim(); holder.dim();
} }
if (_tapToReveal) { if (_tapToReveal) {
holder.hideCode(); holder.hideCode();
} }
} else { } else {
if (_highlightEntry) { if (_highlightEntry || _tempHighlightEntry) {
holder.highlight(); holder.highlight();
} }
if (_tapToReveal) { if (_tapToReveal) {
@ -519,12 +525,12 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
} }
} }
_dimHandler.postDelayed(this::resetFocus, _tapToRevealTime * 1000); _dimHandler.postDelayed(this::resetFocus, secondsToFocus * 1000);
} }
private void resetFocus() { private void resetFocus() {
for (EntryHolder holder : _holders) { for (EntryHolder holder : _holders) {
if (_highlightEntry) { if (_focusedEntry != null) {
holder.highlight(); holder.highlight();
} }
if (_tapToReveal) { if (_tapToReveal) {
@ -533,6 +539,7 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
} }
_focusedEntry = null; _focusedEntry = null;
_tempHighlightEntry = false;
} }
private void updateDraggableStatus() { private void updateDraggableStatus() {
@ -565,7 +572,7 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
} }
public void addSelectedEntry(VaultEntry entry) { public void addSelectedEntry(VaultEntry entry) {
if (_highlightEntry) { if (_focusedEntry != null) {
resetFocus(); resetFocus();
} }

View file

@ -1,9 +1,11 @@
package com.beemdevelopment.aegis.ui.views; package com.beemdevelopment.aegis.ui.views;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.AnimationUtils; import android.view.animation.AnimationUtils;
@ -273,8 +275,39 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
} }
public void addEntry(VaultEntry entry) { public void addEntry(VaultEntry entry) {
_adapter.addEntry(entry); addEntry(entry, false);
}
@SuppressLint("ClickableViewAccessibility")
public void addEntry(VaultEntry entry, boolean focusEntry) {
int position = _adapter.addEntry(entry);
updateEmptyState(); updateEmptyState();
if (focusEntry && position >= 0) {
RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
_recyclerView.removeOnScrollListener(this);
_adapter.setTempHighlightEntry(true);
final int secondsToFocus = 3;
_adapter.focusEntry(entry, secondsToFocus);
}
}
};
_recyclerView.addOnScrollListener(scrollListener);
_recyclerView.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_recyclerView.removeOnScrollListener(scrollListener);
_recyclerView.stopScroll();
_recyclerView.setOnTouchListener(null);
}
return false;
});
_recyclerView.smoothScrollToPosition(position);
}
} }
public void addEntries(Collection<VaultEntry> entries) { public void addEntries(Collection<VaultEntry> entries) {