瀏覽代碼

migrate sorter classes to kotlin

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 2 年之前
父節點
當前提交
f2119770bc

+ 2 - 1
app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java

@@ -617,6 +617,7 @@ public class DisplayUtils {
         targetView.setController(newController);
         targetView.setController(newController);
     }
     }
 
 
+    @Deprecated
     public static @StringRes
     public static @StringRes
     int getSortOrderStringId(LegacyFileSortOrder sortOrder) {
     int getSortOrderStringId(LegacyFileSortOrder sortOrder) {
         switch (sortOrder.name) {
         switch (sortOrder.name) {
@@ -638,7 +639,7 @@ public class DisplayUtils {
 
 
     public static @StringRes
     public static @StringRes
     int getSortOrderStringId(FileSortOrder sortOrder) {
     int getSortOrderStringId(FileSortOrder sortOrder) {
-        switch (sortOrder.name) {
+        switch (sortOrder.getName()) {
             case sort_z_to_a_id:
             case sort_z_to_a_id:
                 return R.string.menu_item_sort_by_name_z_a;
                 return R.string.menu_item_sort_by_name_z_a;
             case sort_new_to_old_id:
             case sort_new_to_old_id:

+ 0 - 107
app/src/main/java/com/nextcloud/talk/utils/FileSortOrder.java

@@ -1,107 +0,0 @@
-/*
- * Nextcloud Talk application
- *
- * @author Sven R. Kunze
- * @author Andy Scherzinger
- * Copyright (C) 2021 Andy Scherzinger <info@andy-scherzinger.de>
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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<String, FileSortOrder> sortOrders;
-
-    static {
-        HashMap<String, FileSortOrder> 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<RemoteFileBrowserItem> sortCloudFiles(List<RemoteFileBrowserItem> files) {
-        return sortCloudFilesByFavourite(files);
-    }
-
-    /**
-     * Sorts list by Favourites.
-     *
-     * @param files files to sort
-     */
-    public static List<RemoteFileBrowserItem> sortCloudFilesByFavourite(List<RemoteFileBrowserItem> 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;
-    }
-}

+ 98 - 0
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 <info@andy-scherzinger.de>
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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<String, FileSortOrder> = 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<RemoteFileBrowserItem>): List<RemoteFileBrowserItem> {
+            Collections.sort(files, RemoteFileBrowserItemFavoriteComparator())
+            return files
+        }
+    }
+
+    open fun sortCloudFiles(files: List<RemoteFileBrowserItem>): List<RemoteFileBrowserItem> {
+        return sortCloudFilesByFavourite(files)
+    }
+
+    val multiplier : Int
+        get() = if (isAscending) 1 else -1
+
+    /**
+     * Comparator for RemoteFileBrowserItems, sorts favorite state.
+     */
+    class RemoteFileBrowserItemFavoriteComparator : Comparator<RemoteFileBrowserItem> {
+        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
+            }
+        }
+    }
+}

+ 16 - 18
app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.java → app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByDate.kt

@@ -3,7 +3,7 @@
  *
  *
  * @author Sven R. Kunze
  * @author Sven R. Kunze
  * @author Andy Scherzinger
  * @author Andy Scherzinger
- * Copyright (C) 2021 Andy Scherzinger <info@andy-scherzinger.de>
+ * Copyright (C) 2021-2022 Andy Scherzinger <info@andy-scherzinger.de>
  * Copyright (C) 2017 Sven R. Kunze
  * Copyright (C) 2017 Sven R. Kunze
  *
  *
  * This program is free software: you can redistribute it and/or modify
  * 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
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
  */
+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.
  * 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.
      * Sorts list by Date.
      *
      *
      * @param files list of files to sort
      * @param files list of files to sort
      */
      */
-    public List<RemoteFileBrowserItem> sortCloudFiles(List<RemoteFileBrowserItem> files) {
-        final int multiplier = isAscending ? 1 : -1;
+    override fun sortCloudFiles(files: List<RemoteFileBrowserItem>): List<RemoteFileBrowserItem> {
+        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<RemoteFileBrowserItem> {
 
 
-        return super.sortCloudFiles(files);
+        override fun compare(left: RemoteFileBrowserItem, right: RemoteFileBrowserItem): Int {
+            return multiplier * left.modifiedTimestamp.compareTo(right.modifiedTimestamp)
+        }
     }
     }
 }
 }

+ 0 - 63
app/src/main/java/com/nextcloud/talk/utils/FileSortOrderByName.java

@@ -1,63 +0,0 @@
-/*
- * Nextcloud Talk application
- *
- * @author Sven R. Kunze
- * @author Andy Scherzinger
- * Copyright (C) 2021 Andy Scherzinger <info@andy-scherzinger.de>
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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<RemoteFileBrowserItem> sortCloudFiles(List<RemoteFileBrowserItem> 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);
-    }
-}

+ 59 - 0
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 <info@andy-scherzinger.de>
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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<RemoteFileBrowserItem>): List<RemoteFileBrowserItem> {
+        Collections.sort(files, RemoteFileBrowserItemNameComparator(multiplier))
+        return super.sortCloudFiles(files)
+    }
+
+    /**
+     * Comparator for RemoteFileBrowserItems, sorts by name.
+     */
+    class RemoteFileBrowserItemNameComparator(private val multiplier: Int) : Comparator<RemoteFileBrowserItem> {
+
+        override fun compare(left: RemoteFileBrowserItem, right: RemoteFileBrowserItem): Int {
+            return if (!left.isFile && !right.isFile) {
+                return multiplier * AlphanumComparator<Any?>().compare(left, right)
+            } else if (!left.isFile) {
+                -1
+            } else if (!right.isFile) {
+                1
+            } else {
+                multiplier * AlphanumComparator<Any?>().compare(left, right)
+            }
+        }
+    }
+}

+ 0 - 61
app/src/main/java/com/nextcloud/talk/utils/FileSortOrderBySize.java

@@ -1,61 +0,0 @@
-/*
- * Nextcloud Talk application
- *
- * @author Sven R. Kunze
- * @author Andy Scherzinger
- * Copyright (C) 2021 Andy Scherzinger <info@andy-scherzinger.de>
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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<RemoteFileBrowserItem> sortCloudFiles(List<RemoteFileBrowserItem> 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);
-    }
-}

+ 58 - 0
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 <info@andy-scherzinger.de>
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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<RemoteFileBrowserItem>): List<RemoteFileBrowserItem> {
+        Collections.sort(files, RemoteFileBrowserItemSizeComparator(multiplier))
+        return super.sortCloudFiles(files)
+    }
+
+    /**
+     * Comparator for RemoteFileBrowserItems, sorts by name.
+     */
+    class RemoteFileBrowserItemSizeComparator(private val multiplier: Int) : Comparator<RemoteFileBrowserItem> {
+
+        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)
+            }
+        }
+    }
+}