Skip to content

Commit

Permalink
Synchronized filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Nov 7, 2024
1 parent 4b68177 commit bc817e8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(() ->
Expand All @@ -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) {
Expand Down Expand Up @@ -751,12 +738,16 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl
}

if (folderChanged || modSumChanged || !newData.equals(_adapterData)) {
final ArrayList<File> 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;

Expand All @@ -775,15 +766,15 @@ 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));
}

if (_dopt.listener != null) {
_dopt.listener.onFsViewerDoUiUpdate(GsFileBrowserListAdapter.this);
}
});
} else if (toShow != null && _adapterDataFiltered.contains(toShow)) {
} else {
_recyclerView.post(() -> scrollToAndFlash(toShow));
}
}
Expand Down Expand Up @@ -828,7 +819,7 @@ private static class StringFilter extends Filter {
private final GsFileBrowserListAdapter _adapter;
private final List<File> _originalList;
private final List<File> _filteredList;
public CharSequence _lastFilter = "";
public String _lastFilter = "";

private StringFilter(GsFileBrowserListAdapter adapter, List<File> adapterData) {
super();
Expand All @@ -840,25 +831,28 @@ private StringFilter(GsFileBrowserListAdapter adapter, List<File> 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<File> all, final List<File> 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) {
Expand Down

0 comments on commit bc817e8

Please sign in to comment.