Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Fixes #3321 Fixes #3320 Fixes #3317 Downloads list improvements #3326

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
public final static boolean BYPASS_CACHE_ON_RELOAD = false;
public final static boolean MULTI_E10S = false;
public final static int DOWNLOADS_STORAGE_DEFAULT = INTERNAL;
public final static int DOWNLOADS_SORTING_ORDER_DEFAULT = SortingContextMenuWidget.SORT_FILENAME_AZ;
public final static int DOWNLOADS_SORTING_ORDER_DEFAULT = SortingContextMenuWidget.SORT_DATE_ASC;

// Enable telemetry by default (opt-out).
public final static boolean CRASH_REPORTING_DEFAULT = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected void initialize() {
ViewModelProvider.AndroidViewModelFactory.getInstance(((VRBrowserActivity) getContext()).getApplication()))
.get(DownloadsViewModel.class);

mSortingComparator = mAZFileNameComparator;
mSortingComparator = getSorting(SettingsStore.getInstance(getContext()).getDownloadsSortingOrder());

updateUI();
}
Expand Down Expand Up @@ -319,27 +319,9 @@ protected void showSortingContextMenu(@NonNull View view) {

SortingContextMenuWidget menu = new SortingContextMenuWidget(getContext());
menu.setItemDelegate(item -> {
switch (item) {
case SortingContextMenuWidget.SORT_FILENAME_AZ:
mSortingComparator = mAZFileNameComparator;
break;
case SortingContextMenuWidget.SORT_FILENAME_ZA:
mSortingComparator = mZAFilenameComparator;
break;
case SortingContextMenuWidget.SORT_DATE_ASC:
mSortingComparator = mDownloadDateAscComparator;
break;
case SortingContextMenuWidget.SORT_DATE_DESC:
mSortingComparator = mDownloadDateDescComparator;
break;
case SortingContextMenuWidget.SORT_SIZE_ASC:
mSortingComparator = mDownloadSizeAscComparator;
break;
case SortingContextMenuWidget.SORT_SIZE_DESC:
mSortingComparator = mDownloadSizeDescComparator;
break;
}
mSortingComparator = getSorting(item);
onDownloadsUpdate(mDownloadsManager.getDownloads());
mBinding.downloadsList.scrollToPosition(0);
});
menu.getPlacement().parentHandle = window.getHandle();

Expand All @@ -352,14 +334,67 @@ protected void showSortingContextMenu(@NonNull View view) {
menu.show(UIWidget.REQUEST_FOCUS);
}

private Comparator<Download> getSorting(@SortingContextMenuWidget.Order int order) {
switch (order) {
case SortingContextMenuWidget.SORT_FILENAME_AZ:
return mAZFileNameComparator;
case SortingContextMenuWidget.SORT_FILENAME_ZA:
return mZAFilenameComparator;
case SortingContextMenuWidget.SORT_DATE_ASC:
return mDownloadDateAscComparator;
case SortingContextMenuWidget.SORT_DATE_DESC:
return mDownloadDateDescComparator;
case SortingContextMenuWidget.SORT_SIZE_ASC:
return mDownloadSizeAscComparator;
case SortingContextMenuWidget.SORT_SIZE_DESC:
return mDownloadSizeDescComparator;
}

return mDownloadIdComparator;
}

// DownloadsManager.DownloadsListener

private Comparator<Download> mAZFileNameComparator = (o1, o2) -> o1.getFilename().compareTo(o2.getFilename());
private Comparator<Download> mZAFilenameComparator = (o1, o2) -> o2.getFilename().compareTo(o1.getFilename());
private Comparator<Download> mDownloadDateAscComparator = (o1, o2) -> (int)(o1.getLastModified() - o2.getLastModified());
private Comparator<Download> mDownloadDateDescComparator = (o1, o2) -> (int)(o2.getLastModified() - o1.getLastModified());
private Comparator<Download> mDownloadSizeAscComparator = (o1, o2) -> (int)(o1.getSizeBytes() - o2.getSizeBytes());
private Comparator<Download> mDownloadSizeDescComparator = (o1, o2) -> (int)(o2.getSizeBytes() - o1.getSizeBytes());
private Comparator<Download> mDownloadIdComparator = (o1, o2) -> (int)(o1.getId() - o2.getId());

private Comparator<Download> mAZFileNameComparator = (o1, o2) -> {
int nameDiff = o1.getFilename().compareTo(o2.getFilename());
if (nameDiff == 0) {
return mDownloadIdComparator.compare(o1, o2);

} else {
return nameDiff;
}
};
private Comparator<Download> mZAFilenameComparator = (o1, o2) -> {
Comment on lines +360 to +369
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: mAZFileNameComparator vs mZAFilenameComparator Name is only capitalized in one of the names. Can address in a follow up.

int nameDiff = o2.getFilename().compareTo(o1.getFilename());
if (nameDiff == 0) {
return mDownloadIdComparator.compare(o1, o2);

} else {
return nameDiff;
}
};
private Comparator<Download> mDownloadDateAscComparator = (o1, o2) -> mDownloadIdComparator.compare(o1, o2);
private Comparator<Download> mDownloadDateDescComparator = (o1, o2) -> mDownloadIdComparator.compare(o2, o1);
private Comparator<Download> mDownloadSizeAscComparator = (o1, o2) -> {
int sizeDiff = (int)(o1.getSizeBytes() - o2.getSizeBytes());
if (sizeDiff == 0) {
return mDownloadIdComparator.compare(o1, o2);

} else {
return sizeDiff;
}
};
private Comparator<Download> mDownloadSizeDescComparator = (o1, o2) -> {
int sizeDiff = (int)(o2.getSizeBytes() - o1.getSizeBytes());
if (sizeDiff == 0) {
return mDownloadIdComparator.compare(o1, o2);

} else {
return sizeDiff;
}
};

@Override
public void onDownloadsUpdate(@NonNull List<Download> downloads) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class SortingContextMenuWidget extends MenuWidget {

@IntDef(value = {SORT_FILENAME_AZ, SORT_FILENAME_ZA, SORT_DATE_ASC, SORT_DATE_DESC})
@IntDef(value = {SORT_FILENAME_AZ, SORT_FILENAME_ZA, SORT_DATE_ASC, SORT_DATE_DESC, SORT_SIZE_ASC, SORT_SIZE_DESC})
public @interface Order {}
public static final int SORT_FILENAME_AZ = 0;
public static final int SORT_FILENAME_ZA = 1;
Expand Down