diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserDialog.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserDialog.java index b0609cbd72..6ba6c1cd1e 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserDialog.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserDialog.java @@ -173,7 +173,6 @@ public void onViewCreated(final View root, final @Nullable Bundle savedInstanceS _filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, activity); _recyclerList.setAdapter(_filesystemViewerAdapter); - _filesystemViewerAdapter.getFilter().filter(""); onFsViewerDoUiUpdate(_filesystemViewerAdapter); // Setup callbacks diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java index 58ef93e0d7..f0bb0a4f8c 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java @@ -124,7 +124,6 @@ public void onViewCreated(@NonNull View root, @Nullable Bundle savedInstanceStat _filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, context); _recyclerList.setAdapter(_filesystemViewerAdapter); - _filesystemViewerAdapter.getFilter().filter(""); onFsViewerDoUiUpdate(_filesystemViewerAdapter); _swipe.setOnRefreshListener(() -> { diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java index 039b4fddba..6cb0bd1769 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java @@ -597,7 +597,7 @@ public void showFile(final File file) { return; } - if (getFilePosition(file) < 0) { + if (_adapterDataFiltered.contains(file)) { final File dir = file.getParentFile(); if (dir != null) { loadFolder(dir, file); @@ -623,7 +623,7 @@ public void onLayoutChange(View v, int l, int t, int r, int b, int ol, int ot, i * @param file File to blink */ public boolean scrollToAndFlash(final File file) { - final int pos = getFilePosition(file); + final int pos = _adapterDataFiltered.indexOf(file); if (pos >= 0 && _layoutManager != null) { _layoutManager.scrollToPosition(pos); _recyclerView.post(() -> @@ -638,19 +638,6 @@ public boolean scrollToAndFlash(final File file) { return false; } - // Get the position of a file in the current view - // -1 if file is not a child of the current directory - public int getFilePosition(final File file) { - if (file != null) { - for (int i = 0; i < _adapterDataFiltered.size(); i++) { - if (_adapterDataFiltered.get(i).equals(file)) { - return i; - } - } - } - return -1; - } - private static final ExecutorService executorService = new ThreadPoolExecutor(0, 3, 60, TimeUnit.SECONDS, new SynchronousQueue<>()); private void loadFolder(final File folder, final File show) { @@ -751,12 +738,16 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl } if (folderChanged || modSumChanged || !newData.equals(_adapterData)) { + final ArrayList filteredData = new ArrayList<>(); + _filter._filter(newData, filteredData); + _recyclerView.post(() -> { // Modify all these values in the UI thread _adapterData.clear(); _adapterData.addAll(newData); + _adapterDataFiltered.clear(); + _adapterDataFiltered.addAll(filteredData); _currentSelection.retainAll(_adapterData); - _filter.filter(_filter._lastFilter); _currentFolder = folder; _prevModSum = modSum; @@ -775,7 +766,7 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl _recyclerView.post(() -> scrollToAndFlash(toShow)); }); - } else if (toShow != null && _adapterDataFiltered.contains(toShow)) { + } else { _recyclerView.post(() -> scrollToAndFlash(toShow)); } @@ -783,7 +774,7 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl _dopt.listener.onFsViewerDoUiUpdate(GsFileBrowserListAdapter.this); } }); - } else if (toShow != null && _adapterDataFiltered.contains(toShow)) { + } else { _recyclerView.post(() -> scrollToAndFlash(toShow)); } } @@ -828,7 +819,7 @@ private static class StringFilter extends Filter { private final GsFileBrowserListAdapter _adapter; private final List _originalList; private final List _filteredList; - public CharSequence _lastFilter = ""; + public String _lastFilter = ""; private StringFilter(GsFileBrowserListAdapter adapter, List adapterData) { super(); @@ -840,25 +831,28 @@ private StringFilter(GsFileBrowserListAdapter adapter, List adapterData) { @Override protected FilterResults performFiltering(CharSequence constraint) { final FilterResults results = new FilterResults(); - constraint = constraint.toString().toLowerCase(Locale.getDefault()).trim(); - _filteredList.clear(); - if (constraint.length() == 0) { - _filteredList.addAll(_originalList); - } else { - for (File file : _originalList) { - if (file.getName().toLowerCase(Locale.getDefault()).contains(constraint)) { - _filteredList.add(file); - } - } - } + _lastFilter = constraint.toString().toLowerCase().trim(); + _filter(_originalList, _filteredList); - _lastFilter = constraint; results.values = _filteredList; results.count = _filteredList.size(); return results; } + public void _filter(final List all, final List filtered) { + filtered.clear(); + if (_lastFilter.isEmpty()) { + filtered.addAll(all); + } else { + for (final File file : all) { + if (file.getName().toLowerCase().contains(_lastFilter)) { + filtered.add(file); + } + } + } + } + @Override @SuppressWarnings("unchecked") protected void publishResults(CharSequence constraint, FilterResults results) {