From 4765b2383130ef903f4d847bdbf73bdd32b28c1a Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 7 Nov 2020 21:58:19 +0100 Subject: [PATCH] implementing sorting by title --- .../applauncher/activities/MainActivity.kt | 16 ++++++++++---- .../applauncher/models/AppLauncher.kt | 21 ++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt index 8938204..7d9ea11 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt @@ -117,8 +117,14 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { displayedLaunchers = dbHelper.getLaunchers() checkInvalidApps() initZoomListener() + setupAdapter(displayedLaunchers) + } - LaunchersAdapter(this, displayedLaunchers, this, launchers_grid, launchers_fastscroller) { + private fun setupAdapter(launchers: ArrayList) { + AppLauncher.sorting = config.sorting + launchers.sort() + + LaunchersAdapter(this, launchers, this, launchers_grid, launchers_fastscroller) { val launchIntent = packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName) if (launchIntent != null) { try { @@ -142,17 +148,18 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { } launchers_fastscroller.setViews(launchers_grid) { - launchers_fastscroller.updateBubbleText(displayedLaunchers.getOrNull(it)?.getBubbleText() ?: "") + val item = (launchers_grid.adapter as LaunchersAdapter).launchers.getOrNull(it) + launchers_fastscroller.updateBubbleText(item?.getBubbleText() ?: "") } ensureBackgroundThread { - notDisplayedLaunchers = getNotDisplayedLaunchers(displayedLaunchers) + notDisplayedLaunchers = getNotDisplayedLaunchers(launchers) } } private fun showSortingDialog() { ChangeSortingDialog(this) { - + setupAdapter(displayedLaunchers) } } @@ -214,6 +221,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { invalidIds.add(id.toString()) } } + dbHelper.deleteLaunchers(invalidIds) displayedLaunchers = displayedLaunchers.filter { !invalidIds.contains(it.id.toString()) } as ArrayList } diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt index b6d76b0..c9d53ea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/models/AppLauncher.kt @@ -1,9 +1,28 @@ package com.simplemobiletools.applauncher.models import android.graphics.drawable.Drawable +import com.simplemobiletools.commons.helpers.SORT_BY_TITLE +import com.simplemobiletools.commons.helpers.SORT_DESCENDING + +data class AppLauncher(val id: Int, var title: String, val packageName: String, val drawable: Drawable? = null) : Comparable { + companion object { + var sorting = 0 + } -data class AppLauncher(val id: Int, var title: String, val packageName: String, val drawable: Drawable? = null) { override fun equals(other: Any?) = packageName.equals((other as AppLauncher).packageName, true) fun getBubbleText() = title + + override fun compareTo(other: AppLauncher): Int { + var result = when { + sorting and SORT_BY_TITLE != 0 -> title.compareTo(other.title) + else -> 1 + } + + if (sorting and SORT_DESCENDING != 0) { + result *= -1 + } + + return result + } }