save launcher order at reordering

This commit is contained in:
tibbi 2020-11-08 09:58:46 +01:00
parent 6a014936a9
commit 4da1259e58
3 changed files with 23 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beInvisibleIf import com.simplemobiletools.commons.extensions.beInvisibleIf
import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.helpers.SORT_BY_CUSTOM
import com.simplemobiletools.commons.interfaces.ItemMoveCallback import com.simplemobiletools.commons.interfaces.ItemMoveCallback
import com.simplemobiletools.commons.interfaces.ItemTouchHelperContract import com.simplemobiletools.commons.interfaces.ItemTouchHelperContract
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
@ -94,6 +95,14 @@ class LaunchersAdapter(activity: SimpleActivity, val launchers: ArrayList<AppLau
override fun onActionModeDestroyed() { override fun onActionModeDestroyed() {
isChangingOrder = false isChangingOrder = false
notifyDataSetChanged() notifyDataSetChanged()
launchers.forEachIndexed { index, appLauncher ->
appLauncher.order = index + 1
}
launchers.forEach {
activity.dbHelper.updateLauncherOrder(it.id, it.order)
}
} }
private fun changeOrder() { private fun changeOrder() {
@ -180,7 +189,9 @@ class LaunchersAdapter(activity: SimpleActivity, val launchers: ArrayList<AppLau
Collections.swap(launchers, i, i - 1) Collections.swap(launchers, i, i - 1)
} }
} }
notifyItemMoved(fromPosition, toPosition) notifyItemMoved(fromPosition, toPosition)
activity.config.sorting = SORT_BY_CUSTOM
} }
override fun onRowClear(myViewHolder: ViewHolder?) { override fun onRowClear(myViewHolder: ViewHolder?) {

View File

@ -138,6 +138,16 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) == 1 return mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) == 1
} }
fun updateLauncherOrder(id: Int, order: Int) {
val values = ContentValues().apply {
put(COL_APP_ORDER, order)
}
val selection = "$COL_ID = ?"
val selectionArgs = Array(1) { id.toString() }
mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs)
}
fun getLaunchers(): ArrayList<AppLauncher> { fun getLaunchers(): ArrayList<AppLauncher> {
val resources = context.resources val resources = context.resources
val packageManager = context.packageManager val packageManager = context.packageManager

View File

@ -4,7 +4,7 @@ import android.graphics.drawable.Drawable
import com.simplemobiletools.commons.helpers.SORT_BY_TITLE import com.simplemobiletools.commons.helpers.SORT_BY_TITLE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.commons.helpers.SORT_DESCENDING
data class AppLauncher(val id: Int, var title: String, val packageName: String, val order: Int, val drawable: Drawable? = null) : Comparable<AppLauncher> { data class AppLauncher(val id: Int, var title: String, val packageName: String, var order: Int, val drawable: Drawable? = null) : Comparable<AppLauncher> {
companion object { companion object {
var sorting = 0 var sorting = 0
} }
@ -16,7 +16,7 @@ data class AppLauncher(val id: Int, var title: String, val packageName: String,
override fun compareTo(other: AppLauncher): Int { override fun compareTo(other: AppLauncher): Int {
var result = when { var result = when {
sorting and SORT_BY_TITLE != 0 -> title.toLowerCase().compareTo(other.title.toLowerCase()) sorting and SORT_BY_TITLE != 0 -> title.toLowerCase().compareTo(other.title.toLowerCase())
else -> 1 else -> order.compareTo(other.order)
} }
if (sorting and SORT_DESCENDING != 0) { if (sorting and SORT_DESCENDING != 0) {