Skip to content

Commit

Permalink
use ListFilesCommand#listFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Oct 13, 2024
1 parent 6304fdc commit cd1c20d
Showing 1 changed file with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
return null;
}

final MainFragment mainFragment = mainFragmentReference.get();
MainFragmentViewModel viewModel = mainFragment.getMainFragmentViewModel();

List<LayoutElementParcelable> recentFiles = new ArrayList<>(40);

Cursor cursor = getRecentFilesCursor(c);
Expand All @@ -552,10 +555,10 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
File f = new File(filePath);
if (f.isDirectory()) {
List<File> files = getFilesFromDirectory(f);
List<File> files = getFilesFromDirectory(mainFragment, filePath);
for (File file : files)
compareFileAndAddToList(recentFiles, file);
} else compareFileAndAddToList(recentFiles, f);
compareFileAndAddToList(viewModel, recentFiles, file);
} else compareFileAndAddToList(viewModel, recentFiles, f);
} while (cursor.moveToNext());
}
cursor.close();
Expand Down Expand Up @@ -597,7 +600,7 @@ private Cursor getRecentFilesCursor(Context c) {
return cursor;
}

private void compareFileAndAddToList(List<LayoutElementParcelable> recentFiles, File file) {
private void compareFileAndAddToList(MainFragmentViewModel viewModel, List<LayoutElementParcelable> recentFiles, File file) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 2);
Date d = c.getTime();
Expand All @@ -608,6 +611,7 @@ private void compareFileAndAddToList(List<LayoutElementParcelable> recentFiles,
LayoutElementParcelable parcelable = createListParcelables(strings);
if (parcelable != null) {
recentFiles.add(parcelable);
viewModel.incrementFileCount();
}
}
}
Expand All @@ -616,24 +620,25 @@ private void compareFileAndAddToList(List<LayoutElementParcelable> recentFiles,
/**
* Recursively fetches the files from directory tree and adds all the files in a list
*
* @param f: File
* @param mainFragment: the main fragment reference
* @param path: the file path
* @return List of files in directory tree.
*/
private List<File> getFilesFromDirectory(File f) {
List<File> allFilesInDir = new ArrayList<>();
try {
File[] files = f.listFiles();
for (File file : files) {
if (file.isDirectory()) {
getFilesFromDirectory(file);
} else {
allFilesInDir.add(file);
}
}
} catch (Exception exception) {
LOG.error(exception.getLocalizedMessage());
}
return allFilesInDir;
private List<File> getFilesFromDirectory(MainFragment mainFragment, String path) {

List<File> files = new ArrayList<>();

ListFilesCommand.INSTANCE.listFiles(
path,
mainFragment.requireMainActivity().isRootExplorer(),
showHiddenFiles,
mode -> { return null; },
hybridFileParcelable -> {
files.add(hybridFileParcelable.getFile());
return null;
});

return files;
}

private @Nullable List<LayoutElementParcelable> listTrashBinFiles() {
Expand Down

0 comments on commit cd1c20d

Please sign in to comment.