From f2119770bc804d6c395986b646062dae17f0cffb Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Tue, 7 Jun 2022 18:15:17 +0200 Subject: [PATCH] migrate sorter classes to kotlin Signed-off-by: Andy Scherzinger --- .../nextcloud/talk/utils/DisplayUtils.java | 3 +- .../nextcloud/talk/utils/FileSortOrder.java | 107 ------------------ .../com/nextcloud/talk/utils/FileSortOrder.kt | 98 ++++++++++++++++ ...rderByDate.java => FileSortOrderByDate.kt} | 34 +++--- .../talk/utils/FileSortOrderByName.java | 63 ----------- .../talk/utils/FileSortOrderByName.kt | 59 ++++++++++ .../talk/utils/FileSortOrderBySize.java | 61 ---------- .../talk/utils/FileSortOrderBySize.kt | 58 ++++++++++ 8 files changed, 233 insertions(+), 250 deletions(-) delete mode 100644 app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.java create mode 100644 app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.kt rename app/src/main/java/com/nextcloud/talk/utils/{FileSortOrderByDate.java => FileSortOrderByDate.kt} (53%) delete mode 100644 app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.java create mode 100644 app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.kt delete mode 100644 app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.java create mode 100644 app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.kt diff --git a/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java b/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java index 8da1b07d7..c5baf6ffd 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java @@ -617,6 +617,7 @@ public class DisplayUtils { targetView.setController(newController); } + @Deprecated public static @StringRes int getSortOrderStringId(LegacyFileSortOrder sortOrder) { switch (sortOrder.name) { @@ -638,7 +639,7 @@ public class DisplayUtils { public static @StringRes int getSortOrderStringId(FileSortOrder sortOrder) { - switch (sortOrder.name) { + switch (sortOrder.getName()) { case sort_z_to_a_id: return R.string.menu_item_sort_by_name_z_a; case sort_new_to_old_id: diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.java b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.java deleted file mode 100644 index 53b443693..000000000 --- a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Nextcloud Talk application - * - * @author Sven R. Kunze - * @author Andy Scherzinger - * Copyright (C) 2021 Andy Scherzinger - * Copyright (C) 2017 Sven R. Kunze - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.nextcloud.talk.utils; - -import android.text.TextUtils; - -import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import androidx.annotation.Nullable; - -/** - * Sort order - */ -public class FileSortOrder { - public static final String sort_a_to_z_id = "sort_a_to_z"; - public static final String sort_z_to_a_id = "sort_z_to_a"; - public static final String sort_old_to_new_id = "sort_old_to_new"; - public static final String sort_new_to_old_id = "sort_new_to_old"; - public static final String sort_small_to_big_id = "sort_small_to_big"; - public static final String sort_big_to_small_id = "sort_big_to_small"; - - public static final FileSortOrder sort_a_to_z = new FileSortOrderByName(sort_a_to_z_id, true); - public static final FileSortOrder sort_z_to_a = new FileSortOrderByName(sort_z_to_a_id, false); - public static final FileSortOrder sort_old_to_new = new FileSortOrderByDate(sort_old_to_new_id, true); - public static final FileSortOrder sort_new_to_old = new FileSortOrderByDate(sort_new_to_old_id, false); - public static final FileSortOrder sort_small_to_big = new FileSortOrderBySize(sort_small_to_big_id, true); - public static final FileSortOrder sort_big_to_small = new FileSortOrderBySize(sort_big_to_small_id, false); - - public static final Map sortOrders; - - static { - HashMap temp = new HashMap<>(); - temp.put(sort_a_to_z.name, sort_a_to_z); - temp.put(sort_z_to_a.name, sort_z_to_a); - temp.put(sort_old_to_new.name, sort_old_to_new); - temp.put(sort_new_to_old.name, sort_new_to_old); - temp.put(sort_small_to_big.name, sort_small_to_big); - temp.put(sort_big_to_small.name, sort_big_to_small); - - sortOrders = Collections.unmodifiableMap(temp); - } - - public String name; - public boolean isAscending; - - public FileSortOrder(String name, boolean ascending) { - this.name = name; - isAscending = ascending; - } - - public static FileSortOrder getFileSortOrder(@Nullable String key) { - if (TextUtils.isEmpty(key) || !sortOrders.containsKey(key)) { - return sort_a_to_z; - } else { - return sortOrders.get(key); - } - } - - public List sortCloudFiles(List files) { - return sortCloudFilesByFavourite(files); - } - - /** - * Sorts list by Favourites. - * - * @param files files to sort - */ - public static List sortCloudFilesByFavourite(List files) { - Collections.sort(files, (o1, o2) -> { - if (o1.isFavorite() && o2.isFavorite()) { - return 0; - } else if (o1.isFavorite()) { - return -1; - } else if (o2.isFavorite()) { - return 1; - } - return 0; - }); - - return files; - } -} diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.kt b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.kt new file mode 100644 index 000000000..de4935457 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.kt @@ -0,0 +1,98 @@ +/* + * Nextcloud Talk application + * + * @author Sven R. Kunze + * @author Andy Scherzinger + * Copyright (C) 2021-2022 Andy Scherzinger + * Copyright (C) 2017 Sven R. Kunze + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.utils + +import android.text.TextUtils +import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem +import java.util.Collections + +/** + * Sort order + */ +open class FileSortOrder(var name: String, var isAscending: Boolean) { + companion object { + const val sort_a_to_z_id = "sort_a_to_z" + const val sort_z_to_a_id = "sort_z_to_a" + const val sort_old_to_new_id = "sort_old_to_new" + const val sort_new_to_old_id = "sort_new_to_old" + const val sort_small_to_big_id = "sort_small_to_big" + const val sort_big_to_small_id = "sort_big_to_small" + + val sort_a_to_z: FileSortOrder = FileSortOrderByName(sort_a_to_z_id, true) + val sort_z_to_a: FileSortOrder = FileSortOrderByName(sort_z_to_a_id, false) + val sort_old_to_new: FileSortOrder = FileSortOrderByDate(sort_old_to_new_id, true) + val sort_new_to_old: FileSortOrder = FileSortOrderByDate(sort_new_to_old_id, false) + val sort_small_to_big: FileSortOrder = FileSortOrderBySize(sort_small_to_big_id, true) + val sort_big_to_small: FileSortOrder = FileSortOrderBySize(sort_big_to_small_id, false) + + val sortOrders: Map = mapOf( + sort_a_to_z.name to sort_a_to_z, + sort_z_to_a.name to sort_z_to_a, + sort_old_to_new.name to sort_old_to_new, + sort_new_to_old.name to sort_new_to_old, + sort_small_to_big.name to sort_small_to_big, + sort_big_to_small.name to sort_big_to_small, + ) + + fun getFileSortOrder(key: String?): FileSortOrder { + return if (TextUtils.isEmpty(key) || !sortOrders.containsKey(key)) { + sort_a_to_z + } else { + sortOrders[key]!! + } + } + + /** + * Sorts list by Favourites. + * + * @param files files to sort + */ + fun sortCloudFilesByFavourite(files: List): List { + Collections.sort(files, RemoteFileBrowserItemFavoriteComparator()) + return files + } + } + + open fun sortCloudFiles(files: List): List { + return sortCloudFilesByFavourite(files) + } + + val multiplier : Int + get() = if (isAscending) 1 else -1 + + /** + * Comparator for RemoteFileBrowserItems, sorts favorite state. + */ + class RemoteFileBrowserItemFavoriteComparator : Comparator { + override fun compare(left: RemoteFileBrowserItem, right: RemoteFileBrowserItem): Int { + return if (left.isFavorite && right.isFavorite) { + 0 + } else if (left.isFavorite) { + -1 + } else if (right.isFavorite) { + 1 + } else { + 0 + } + } + } +} diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.java b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.kt similarity index 53% rename from app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.java rename to app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.kt index 48f661019..c6d66d6ed 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.java +++ b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.kt @@ -3,7 +3,7 @@ * * @author Sven R. Kunze * @author Andy Scherzinger - * Copyright (C) 2021 Andy Scherzinger + * Copyright (C) 2021-2022 Andy Scherzinger * Copyright (C) 2017 Sven R. Kunze * * This program is free software: you can redistribute it and/or modify @@ -19,34 +19,32 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +package com.nextcloud.talk.utils -package com.nextcloud.talk.utils; - -import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem; - -import java.util.Collections; -import java.util.List; +import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem +import java.util.Collections /** * Created by srkunze on 28.08.17. */ -public class FileSortOrderByDate extends FileSortOrder { - - FileSortOrderByDate(String name, boolean ascending) { - super(name, ascending); - } - +class FileSortOrderByDate internal constructor(name: String, ascending: Boolean) : FileSortOrder(name, ascending) { /** * Sorts list by Date. * * @param files list of files to sort */ - public List sortCloudFiles(List files) { - final int multiplier = isAscending ? 1 : -1; + override fun sortCloudFiles(files: List): List { + Collections.sort(files, RemoteFileBrowserItemDateComparator(multiplier)) + return super.sortCloudFiles(files) + } - Collections.sort(files, (o1, o2) -> - multiplier * Long.compare(o1.getModifiedTimestamp(), o2.getModifiedTimestamp())); + /** + * Comparator for RemoteFileBrowserItems, sorts by modified timestamp. + */ + class RemoteFileBrowserItemDateComparator(private val multiplier: Int) : Comparator { - return super.sortCloudFiles(files); + override fun compare(left: RemoteFileBrowserItem, right: RemoteFileBrowserItem): Int { + return multiplier * left.modifiedTimestamp.compareTo(right.modifiedTimestamp) + } } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.java b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.java deleted file mode 100644 index a2cfdbf4c..000000000 --- a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Nextcloud Talk application - * - * @author Sven R. Kunze - * @author Andy Scherzinger - * Copyright (C) 2021 Andy Scherzinger - * Copyright (C) 2017 Sven R. Kunze - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.nextcloud.talk.utils; - -import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem; - -import java.util.Collections; -import java.util.List; - -import third_parties.daveKoeller.AlphanumComparator; - -/** - * Created by srkunze on 28.08.17. - */ -public class FileSortOrderByName extends FileSortOrder { - - FileSortOrderByName(String name, boolean ascending) { - super(name, ascending); - } - - /** - * Sorts list by Name. - * - * @param files files to sort - */ - @SuppressWarnings("Bx") - public List sortCloudFiles(List files) { - final int multiplier = isAscending ? 1 : -1; - - Collections.sort(files, (o1, o2) -> { - if (!o1.isFile() && !o2.isFile()) { - return multiplier * new AlphanumComparator().compare(o1, o2); - } else if (!o1.isFile()) { - return -1; - } else if (!o2.isFile()) { - return 1; - } - return multiplier * new AlphanumComparator().compare(o1, o2); - }); - - return super.sortCloudFiles(files); - } -} diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.kt b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.kt new file mode 100644 index 000000000..8062b8a8d --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.kt @@ -0,0 +1,59 @@ +/* + * Nextcloud Talk application + * + * @author Sven R. Kunze + * @author Andy Scherzinger + * Copyright (C) 2021-2022 Andy Scherzinger + * Copyright (C) 2017 Sven R. Kunze + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.utils + +import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem +import third_parties.daveKoeller.AlphanumComparator +import java.util.Collections + +/** + * Created by srkunze on 28.08.17. + */ +class FileSortOrderByName internal constructor(name: String, ascending: Boolean) : FileSortOrder(name, ascending) { + /** + * Sorts list by Name. + * + * @param files files to sort + */ + override fun sortCloudFiles(files: List): List { + Collections.sort(files, RemoteFileBrowserItemNameComparator(multiplier)) + return super.sortCloudFiles(files) + } + + /** + * Comparator for RemoteFileBrowserItems, sorts by name. + */ + class RemoteFileBrowserItemNameComparator(private val multiplier: Int) : Comparator { + + override fun compare(left: RemoteFileBrowserItem, right: RemoteFileBrowserItem): Int { + return if (!left.isFile && !right.isFile) { + return multiplier * AlphanumComparator().compare(left, right) + } else if (!left.isFile) { + -1 + } else if (!right.isFile) { + 1 + } else { + multiplier * AlphanumComparator().compare(left, right) + } + } + } +} diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.java b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.java deleted file mode 100644 index 8ecbdd2b9..000000000 --- a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Nextcloud Talk application - * - * @author Sven R. Kunze - * @author Andy Scherzinger - * Copyright (C) 2021 Andy Scherzinger - * Copyright (C) 2017 Sven R. Kunze - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.nextcloud.talk.utils; - -import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem; - -import java.util.Collections; -import java.util.List; - -/** - * Sorts files by sizes - */ -public class FileSortOrderBySize extends FileSortOrder { - - FileSortOrderBySize(String name, boolean ascending) { - super(name, ascending); - } - - /** - * Sorts list by Size. - * - * @param files list of files to sort - */ - public List sortCloudFiles(List files) { - final int multiplier = isAscending ? 1 : -1; - - Collections.sort(files, (o1, o2) -> { - if (!o1.isFile() && !o2.isFile()) { - return multiplier * Long.compare(o1.getSize(), o2.getSize()); - } else if (!o1.isFile()) { - return -1; - } else if (!o2.isFile()) { - return 1; - } else { - return multiplier * Long.compare(o1.getSize(), o2.getSize()); - } - }); - - return super.sortCloudFiles(files); - } -} diff --git a/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.kt b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.kt new file mode 100644 index 000000000..d0390251d --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.kt @@ -0,0 +1,58 @@ +/* + * Nextcloud Talk application + * + * @author Sven R. Kunze + * @author Andy Scherzinger + * Copyright (C) 2021-2022 Andy Scherzinger + * Copyright (C) 2017 Sven R. Kunze + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.utils + +import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem +import java.util.Collections + +/** + * Sorts files by sizes + */ +class FileSortOrderBySize internal constructor(name: String, ascending: Boolean) : FileSortOrder(name, ascending) { + /** + * Sorts list by Size. + * + * @param files list of files to sort + */ + override fun sortCloudFiles(files: List): List { + Collections.sort(files, RemoteFileBrowserItemSizeComparator(multiplier)) + return super.sortCloudFiles(files) + } + + /** + * Comparator for RemoteFileBrowserItems, sorts by name. + */ + class RemoteFileBrowserItemSizeComparator(private val multiplier: Int) : Comparator { + + override fun compare(left: RemoteFileBrowserItem, right: RemoteFileBrowserItem): Int { + return if (!left.isFile && !right.isFile) { + return multiplier * left.size.compareTo(right.size) + } else if (!left.isFile) { + -1 + } else if (!right.isFile) { + 1 + } else { + multiplier * left.size.compareTo(right.size) + } + } + } +}