Skip to content

Commit f950e05

Browse files
committed
dont change sorting logic
Signed-off-by: alperozturk <[email protected]>
1 parent 9e2ae86 commit f950e05

File tree

6 files changed

+48
-318
lines changed

6 files changed

+48
-318
lines changed

app/src/androidTest/java/com/nextcloud/utils/FileSortOrderTests.kt

Lines changed: 0 additions & 278 deletions
This file was deleted.

app/src/main/java/com/owncloud/android/ui/adapter/LocalFileListAdapter.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Locale;
4242
import java.util.Set;
43+
import java.util.concurrent.ExecutorService;
4344
import java.util.concurrent.Executors;
4445

4546
import androidx.annotation.NonNull;
@@ -64,6 +65,7 @@ public class LocalFileListAdapter extends RecyclerView.Adapter<RecyclerView.View
6465
private Set<File> checkedFiles;
6566
private ViewThemeUtils viewThemeUtils;
6667
private boolean isWithinEncryptedFolder;
68+
private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
6769

6870
private static final int VIEWTYPE_ITEM = 0;
6971
private static final int VIEWTYPE_FOOTER = 1;
@@ -313,7 +315,7 @@ public void swapDirectory(final File directory) {
313315
mFiles.clear();
314316
mFilesAll.clear();
315317

316-
Executors.newSingleThreadExecutor().execute(() -> {
318+
singleThreadExecutor.execute(() -> {
317319
// Load first page of folders
318320
List<File> firstPage = FileHelper.INSTANCE.listDirectoryEntries(directory, currentOffset, PAGE_SIZE, true);
319321

@@ -383,15 +385,25 @@ private void notifyItemRange(List<File> updatedList) {
383385
});
384386
}
385387

388+
private final Object filesLock = new Object();
389+
386390
@SuppressLint("NotifyDataSetChanged")
387391
public void setSortOrder(FileSortOrder sortOrder) {
388392
localFileListFragmentInterface.setLoading(true);
389-
final Handler uiHandler = new Handler(Looper.getMainLooper());
390-
Executors.newSingleThreadExecutor().execute(() -> {
391-
preferences.setSortOrder(FileSortOrder.Type.localFileListView, sortOrder);
392-
mFiles = sortOrder.sortLocalFiles(mFiles);
393+
singleThreadExecutor.execute(() -> {
394+
List<File> sortedCopy;
395+
synchronized (filesLock) {
396+
sortedCopy = new ArrayList<>(mFiles);
397+
}
398+
399+
sortedCopy = sortOrder.sortLocalFiles(sortedCopy);
393400

394-
uiHandler.post(() -> {
401+
List<File> finalSortedCopy = sortedCopy;
402+
new Handler(Looper.getMainLooper()).post(() -> {
403+
synchronized (filesLock) {
404+
mFiles = finalSortedCopy;
405+
mFilesAll = new ArrayList<>(finalSortedCopy);
406+
}
395407
notifyDataSetChanged();
396408
localFileListFragmentInterface.setLoading(false);
397409
});
@@ -532,4 +544,8 @@ public void setFiles(List<File> newFiles) {
532544
notifyDataSetChanged();
533545
localFileListFragmentInterface.setLoading(false);
534546
}
547+
548+
public void cleanup() {
549+
singleThreadExecutor.shutdown();
550+
}
535551
}

app/src/main/java/com/owncloud/android/ui/fragment/LocalFileListFragment.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,5 +420,9 @@ public interface ContainerActivity {
420420
boolean isWithinEncryptedFolder();
421421
}
422422

423-
423+
@Override
424+
public void onDestroyView() {
425+
mAdapter.cleanup();
426+
super.onDestroyView();
427+
}
424428
}

app/src/main/java/com/owncloud/android/utils/FileSortOrderByDate.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ class FileSortOrderByDate(name: String, ascending: Boolean) : FileSortOrder(name
2626
favoritesFirst: Boolean
2727
): MutableList<OCFile> {
2828
val multiplier = if (isAscending) 1 else -1
29-
val copy = files.toMutableList()
30-
copy.sortWith { o1: OCFile, o2: OCFile ->
29+
files.sortWith { o1: OCFile, o2: OCFile ->
3130
multiplier * o1.modificationTimestamp.compareTo(o2.modificationTimestamp)
3231
}
33-
return super.sortCloudFiles(copy, foldersBeforeFiles, favoritesFirst)
32+
return super.sortCloudFiles(files, foldersBeforeFiles, favoritesFirst)
3433
}
3534

3635
/**
@@ -40,11 +39,10 @@ class FileSortOrderByDate(name: String, ascending: Boolean) : FileSortOrder(name
4039
*/
4140
override fun sortTrashbinFiles(files: MutableList<TrashbinFile>): List<TrashbinFile> {
4241
val multiplier = if (isAscending) 1 else -1
43-
val copy = files.toMutableList()
44-
copy.sortWith { o1: TrashbinFile, o2: TrashbinFile ->
42+
files.sortWith { o1: TrashbinFile, o2: TrashbinFile ->
4543
multiplier * o1.deletionTimestamp.compareTo(o2.deletionTimestamp)
4644
}
47-
return super.sortTrashbinFiles(copy)
45+
return super.sortTrashbinFiles(files)
4846
}
4947

5048
/**
@@ -54,10 +52,9 @@ class FileSortOrderByDate(name: String, ascending: Boolean) : FileSortOrder(name
5452
*/
5553
override fun sortLocalFiles(files: MutableList<File>): List<File> {
5654
val multiplier = if (isAscending) 1 else -1
57-
val copy = files.toMutableList()
58-
copy.sortWith { o1: File, o2: File ->
55+
files.sortWith { o1: File, o2: File ->
5956
multiplier * o1.lastModified().compareTo(o2.lastModified())
6057
}
61-
return copy
58+
return files
6259
}
6360
}

0 commit comments

Comments
 (0)