Skip to content

Commit

Permalink
Fix FileListSorter "None on top" sort mode
Browse files Browse the repository at this point in the history
When the option is "None on top", I think there should be no file and/or folder header labels displayed.

Fixes #942.
  • Loading branch information
TranceLove committed Oct 24, 2023
1 parent 0b0e9e6 commit dbd7502
Show file tree
Hide file tree
Showing 5 changed files with 765 additions and 507 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.amaze.filemanager.adapters;

import static com.amaze.filemanager.filesystem.compressed.CompressedHelper.*;
import static com.amaze.filemanager.filesystem.files.FileListSorter.SORT_NONE_ON_TOP;
import static com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_COLORIZE_ICONS;
import static com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_SHOW_FILE_SIZE;
import static com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_SHOW_GOBACK_BUTTON;
Expand Down Expand Up @@ -609,34 +610,43 @@ private void setItems(
}

public void createHeaders(boolean invalidate, List<IconDataParcelable> uris) {
boolean[] headers = new boolean[] {false, false};
if ((mainFragment.getMainFragmentViewModel() != null
&& mainFragment.getMainFragmentViewModel().getDsort() == SORT_NONE_ON_TOP)
|| getItemsDigested() == null
|| getItemsDigested().isEmpty()) {
return;
} else {
boolean[] headers = new boolean[] {false, false};

for (int i = 0; i < getItemsDigested().size(); i++) {
for (int i = 0; i < getItemsDigested().size(); i++) {

if (getItemsDigested().get(i).layoutElementParcelable != null) {
LayoutElementParcelable nextItem = getItemsDigested().get(i).layoutElementParcelable;
if (getItemsDigested().get(i).layoutElementParcelable != null) {
LayoutElementParcelable nextItem = getItemsDigested().get(i).layoutElementParcelable;

if (!headers[0] && nextItem.isDirectory) {
headers[0] = true;
getItemsDigested().add(i, new ListItem(TYPE_HEADER_FOLDERS));
uris.add(i, null);
continue;
}
if (nextItem != null) {
if (!headers[0] && nextItem.isDirectory) {
headers[0] = true;
getItemsDigested().add(i, new ListItem(TYPE_HEADER_FOLDERS));
uris.add(i, null);
continue;
}

if (!headers[1]
&& !nextItem.isDirectory
&& !nextItem.title.equals(".")
&& !nextItem.title.equals("..")) {
headers[1] = true;
getItemsDigested().add(i, new ListItem(TYPE_HEADER_FILES));
uris.add(i, null);
continue; // leave this continue for symmetry
if (!headers[1]
&& !nextItem.isDirectory
&& !nextItem.title.equals(".")
&& !nextItem.title.equals("..")) {
headers[1] = true;
getItemsDigested().add(i, new ListItem(TYPE_HEADER_FILES));
uris.add(i, null);
continue; // leave this continue for symmetry
}
}
}
}
}

if (invalidate) {
notifyDataSetChanged();
if (invalidate) {
notifyDataSetChanged();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.Q;
import static com.amaze.filemanager.filesystem.files.FileListSorter.SORT_ASC;
import static com.amaze.filemanager.filesystem.files.FileListSorter.SORT_DSC;

import java.io.File;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -258,9 +260,9 @@ private void postListCustomPathProcess(

if (sortType <= 3) {
sortBy = sortType;
isAscending = 1;
isAscending = SORT_ASC;
} else {
isAscending = -1;
isAscending = SORT_DSC;
sortBy = sortType - 4;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,34 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.amaze.filemanager.filesystem.files;
package com.amaze.filemanager.filesystem.files

import java.util.Comparator;
import androidx.annotation.IntDef
import com.amaze.filemanager.adapters.data.LayoutElementParcelable
import java.util.Locale

import com.amaze.filemanager.adapters.data.LayoutElementParcelable;

public class FileListSorter implements Comparator<LayoutElementParcelable> {

private int dirsOnTop = 0;
private int asc = 1;
private int sort = 0;

public FileListSorter(int dir, int sort, int asc) {
this.dirsOnTop = dir;
this.asc = asc;
this.sort = sort;
}

private boolean isDirectory(LayoutElementParcelable path) {
return path.isDirectory;
}

/**
* Compares two elements and return negative, zero and positive integer if first argument is less
* than, equal to or greater than second
*/
@Override
public int compare(LayoutElementParcelable file1, LayoutElementParcelable file2) {
/**
* [Comparator] implementation to sort [LayoutElementParcelable]s.
*/
class FileListSorter(
@DirSortMode dirArg: Int,
@SortBy sortArg: Int,
@SortOrder ascArg: Int
) : Comparator<LayoutElementParcelable> {
private var dirsOnTop = dirArg
private val asc = ascArg
private val sort = sortArg

private fun isDirectory(path: LayoutElementParcelable): Boolean {
return path.isDirectory
}

/*File f1;
/**
* Compares two elements and return negative, zero and positive integer if first argument is less
* than, equal to or greater than second
*/
override fun compare(file1: LayoutElementParcelable, file2: LayoutElementParcelable): Int {
/*File f1;
if(!file1.hasSymlink()) {
Expand All @@ -64,63 +62,84 @@ public class FileListSorter implements Comparator<LayoutElementParcelable> {
} else {
f2=new File(file1.getSymlink());
}*/
if (dirsOnTop == SORT_DIR_ON_TOP) {
if (isDirectory(file1) && !isDirectory(file2)) {
return -1
} else if (isDirectory(file2) && !isDirectory(file1)) {
return 1
}
} else if (dirsOnTop == SORT_FILE_ON_TOP) {
if (isDirectory(file1) && !isDirectory(file2)) {
return 1
} else if (isDirectory(file2) && !isDirectory(file1)) {
return -1
}
}

if (dirsOnTop == 0) {
if (isDirectory(file1) && !isDirectory(file2)) {
return -1;

} else if (isDirectory(file2) && !isDirectory(file1)) {
return 1;
}
} else if (dirsOnTop == 1) {
if (isDirectory(file1) && !isDirectory(file2)) {

return 1;
} else if (isDirectory(file2) && !isDirectory(file1)) {
return -1;
}
when (sort) {
SORT_BY_NAME -> {
// sort by name
return asc * file1.title.compareTo(file2.title, ignoreCase = true)
}
SORT_BY_LAST_MODIFIED -> {
// sort by last modified
return asc * java.lang.Long.valueOf(file1.date).compareTo(file2.date)
}
SORT_BY_SIZE -> {
// sort by size
return if (!file1.isDirectory && !file2.isDirectory) {
asc * java.lang.Long.valueOf(file1.longSize).compareTo(file2.longSize)
} else {
file1.title.compareTo(file2.title, ignoreCase = true)
}
}
SORT_BY_TYPE -> {
// sort by type
return if (!file1.isDirectory && !file2.isDirectory) {
val ext_a = getExtension(file1.title)
val ext_b = getExtension(file2.title)
val res = asc * ext_a.compareTo(ext_b)
if (res == 0) {
asc * file1.title.compareTo(file2.title, ignoreCase = true)
} else {
res
}
} else {
file1.title.compareTo(file2.title, ignoreCase = true)
}
}
else -> return 0
}
}

if (sort == 0) {
companion object {

// sort by name
return asc * file1.title.compareToIgnoreCase(file2.title);
} else if (sort == 1) {
const val SORT_BY_NAME = 0
const val SORT_BY_LAST_MODIFIED = 1
const val SORT_BY_SIZE = 2
const val SORT_BY_TYPE = 3

// sort by last modified
return asc * Long.valueOf(file1.date).compareTo(file2.date);
} else if (sort == 2) {
const val SORT_DIR_ON_TOP = 0
const val SORT_FILE_ON_TOP = 1
const val SORT_NONE_ON_TOP = 2

// sort by size
if (!file1.isDirectory && !file2.isDirectory) {
const val SORT_ASC = 1
const val SORT_DSC = -1

return asc * Long.valueOf(file1.longSize).compareTo(file2.longSize);
} else {
@Retention(AnnotationRetention.SOURCE)
@IntDef(SORT_BY_NAME, SORT_BY_LAST_MODIFIED, SORT_BY_SIZE, SORT_BY_TYPE)
annotation class SortBy

return file1.title.compareToIgnoreCase(file2.title);
}
@Retention(AnnotationRetention.SOURCE)
@IntDef(SORT_DIR_ON_TOP, SORT_FILE_ON_TOP, SORT_NONE_ON_TOP)
annotation class DirSortMode

} else if (sort == 3) {
@Retention(AnnotationRetention.SOURCE)
@IntDef(SORT_ASC, SORT_DSC)
annotation class SortOrder

// sort by type
if (!file1.isDirectory && !file2.isDirectory) {

final String ext_a = getExtension(file1.title);
final String ext_b = getExtension(file2.title);

final int res = asc * ext_a.compareTo(ext_b);
if (res == 0) {
return asc * file1.title.compareToIgnoreCase(file2.title);
fun getExtension(a: String): String {
return a.substring(a.lastIndexOf(".") + 1).lowercase(Locale.getDefault())
}
return res;
} else {
return file1.title.compareToIgnoreCase(file2.title);
}
}
return 0;
}

public static String getExtension(String a) {
return a.substring(a.lastIndexOf(".") + 1).toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ import com.amaze.filemanager.adapters.data.LayoutElementParcelable
import com.amaze.filemanager.database.CloudHandler
import com.amaze.filemanager.fileoperations.filesystem.OpenMode
import com.amaze.filemanager.filesystem.HybridFileParcelable
import com.amaze.filemanager.filesystem.files.FileListSorter.Companion.DirSortMode
import com.amaze.filemanager.filesystem.files.FileListSorter.Companion.SortBy
import com.amaze.filemanager.filesystem.files.FileListSorter.Companion.SortOrder
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_GRID_COLUMNS
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_GRID_COLUMNS_DEFAULT
import com.amaze.filemanager.utils.DataUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*
import java.util.Objects

class MainFragmentViewModel : ViewModel() {

Expand All @@ -56,11 +59,19 @@ class MainFragmentViewModel : ViewModel() {
var searchHelper = ArrayList<HybridFileParcelable>()
var no = 0

@SortBy
var sortby = 0

@DirSortMode
var dsort = 0

@SortOrder
var asc = 0

var home: String? = null

var results: Boolean = false

lateinit var openMode: OpenMode

// defines the current visible tab, default either 0 or 1
Expand Down
Loading

0 comments on commit dbd7502

Please sign in to comment.