From 8a10313b9a9a6ae2c6c2837330eaf27b8dccfd3b Mon Sep 17 00:00:00 2001 From: ottoptj Date: Thu, 4 Jul 2024 16:52:28 +0300 Subject: [PATCH] Changed menu updates back to less efficient entire refresh due to bugs. Fixed a bug with menu position after back from search. --- .../eu/ottop/yamlauncher/AppActionMenu.kt | 18 +-- .../eu/ottop/yamlauncher/AppMenuAdapter.kt | 19 +-- .../yamlauncher/AppMenuLinearLayoutManager.kt | 2 +- .../java/eu/ottop/yamlauncher/MainActivity.kt | 152 +++++------------- 4 files changed, 45 insertions(+), 146 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt index 58bfe6e..e6e4169 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt @@ -108,14 +108,7 @@ class AppActionMenu { editText.text.toString() ) CoroutineScope(Dispatchers.Default).launch { - val newPosition = appUtils.getInstalledApps(activity) - .indexOfFirst { it.first.applicationInfo.packageName == appInfo.packageName && it.second.second == workProfile } - activity.appUpdate = false - withContext(Dispatchers.Main) { - activity.updateItem(position, app) - activity.moveItem(position, newPosition) - } - activity.updateInstalledApps() + activity.refreshAppMenu() } @@ -135,14 +128,9 @@ class AppActionMenu { ) CoroutineScope(Dispatchers.Default).launch { - val newPosition = appUtils.getInstalledApps(activity) - .indexOfFirst { it.first.applicationInfo.packageName == appInfo.packageName && it.second.second == workProfile } - activity.appUpdate = false - withContext(Dispatchers.Main) { - activity.updateItem(position, app) - activity.moveItem(position, newPosition) + CoroutineScope(Dispatchers.Default).launch { + activity.refreshAppMenu() } - activity.updateInstalledApps() } } } diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt index f74d855..d8f2d54 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt @@ -123,25 +123,8 @@ class AppMenuAdapter( return apps.size } - fun addApp(position: Int, app: Pair>) { - apps.add(position, app) - } - - fun removeApp(position: Int) { - apps.removeAt(position) - } - - fun updateApp(position: Int, app: Pair>) { - apps[position] = app - } - - fun moveApp(position: Int, newPosition: Int) { - val app = apps.removeAt(position) - apps.add(newPosition, app) - - } - fun updateApps(newApps: List>>) { apps = newApps.toMutableList() + notifyDataSetChanged() } } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt index 58a2f15..000ab90 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt @@ -17,7 +17,7 @@ class AppMenuLinearLayoutManager(private val activity: MainActivity) : LinearLay val scrollRange = super.scrollVerticallyBy(dy, recycler, state) val overscroll: Int = dy - scrollRange - if (overscroll < 0 && firstVisibleItemPosition == 0 && scrollStarted && activity.appUpdate) { + if (overscroll < 0 && firstVisibleItemPosition == 0 && scrollStarted && activity.isJobActive()) { activity.backToHome() } diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 433afca..cc427a2 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -68,8 +68,6 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap private val swipeThreshold = 100 private val swipeVelocityThreshold = 100 - var appUpdate = true - @SuppressLint("ClickableViewAccessibility") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -144,17 +142,14 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap // Detect swipe up if (deltaY < -swipeThreshold && abs(velocityY) > swipeVelocityThreshold) { openAppMenuActivity() - weatherSystem.getTemp(this@MainActivity) } // Detect swipe down else if (deltaY > swipeThreshold && abs(velocityY) > swipeVelocityThreshold) { - val statusBarService = getSystemService(Context.STATUS_BAR_SERVICE) val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager") val expandMethod: Method = statusBarManager.getMethod("expandNotificationsPanel") expandMethod.invoke(statusBarService) - weatherSystem.getWeatherForCurrentLocation(this@MainActivity) } // Detect swipe left @@ -296,12 +291,10 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap if (searchView.text.isNullOrEmpty()) { startTask() - appUpdate = true } } else if (bottom - top < oldBottom - oldTop) { job?.cancel() - appUpdate = false } } @@ -311,12 +304,13 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - CoroutineScope(Dispatchers.Default).launch { - filterItems(searchView.text.toString()) - } + } override fun afterTextChanged(s: Editable?) { + CoroutineScope(Dispatchers.Default).launch { + filterItems(searchView.text.toString()) + } } }) } @@ -348,10 +342,11 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap } private suspend fun applySearch(newFilteredApps: MutableList>>) { - val changes = detectChanges(installedApps, newFilteredApps) - installedApps = newFilteredApps - withContext(Dispatchers.Main) { - applyChanges(changes, installedApps) + if (!listsEqual(installedApps, newFilteredApps)) { + withContext(Dispatchers.Main) { + updateMenu(newFilteredApps) + } + installedApps = newFilteredApps } } @@ -390,16 +385,19 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap } }, 100) handler.postDelayed({ + CoroutineScope(Dispatchers.Default).launch { + refreshAppMenu() + try { - recyclerView.scrollToPosition(0) + withContext(Dispatchers.Main) { + recyclerView.scrollToPosition(0) + } } catch (_: UninitializedPropertyAccessException) { } - CoroutineScope(Dispatchers.Default).launch { - refreshAppMenu() - } - }, 150) + }}, 150) + } private fun toAppMenu() { @@ -476,10 +474,12 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap suspend fun refreshAppMenu() { try { val updatedApps = appUtils.getInstalledApps(this@MainActivity) - val changes = detectChanges(installedApps, updatedApps) - installedApps = updatedApps - withContext(Dispatchers.Main) { - applyChanges(changes, installedApps) + println("update running") + if (!listsEqual(installedApps, updatedApps)) { + withContext(Dispatchers.Main) { + updateMenu(updatedApps) + } + installedApps = updatedApps } } catch (_: UninitializedPropertyAccessException) { @@ -493,103 +493,31 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap imm.hideSoftInputFromWindow(binding.root.windowToken, 0) } - private fun detectChanges(oldList: List>>, newList: List>>): List { - val changes = mutableListOf() - val removalChanges = mutableListOf() - val oldSet = oldList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet() - val newSet = newList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet() - // Detect removals - oldList.forEachIndexed { index, oldItem -> - if (!newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) { - removalChanges.add(Change(ChangeType.REMOVE, index)) - } - } - - // Detect insertions - newList.forEachIndexed { index, newItem -> - if (!oldSet.contains(Pair(newItem.first.applicationInfo.packageName, newItem.second.second))) { - changes.add(Change(ChangeType.INSERT, index)) - } - } - - oldList.forEachIndexed { index, oldItem -> - if (newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) { - val newIndex = newList.indexOfFirst { it.first.applicationInfo.packageName == oldItem.first.applicationInfo.packageName && it.second.second == oldItem.second.second } - if (oldItem.first.componentName != newList[newIndex].first.componentName) { - changes.add(Change(ChangeType.UPDATE, index)) - } - if (index != newIndex) { - if (appUpdate) { - changes.add(Change(ChangeType.MOVE, index)) - appUpdate = false - } - } - - } - } - - changes.addAll(removalChanges.reversed()) - - return changes - } @SuppressLint("NotifyDataSetChanged") - private fun applyChanges(changes: List, updatedApps: List>>) { - changes.forEach { change -> - when (change.type) { - ChangeType.INSERT -> { - insertItem(change.position, updatedApps[change.position]) - } - ChangeType.REMOVE -> { - try { - removeItem(change.position) - } - catch (_: IndexOutOfBoundsException) { - } - } - ChangeType.UPDATE -> { - updateItem(change.position, updatedApps[change.position]) - } + private fun updateMenu(updatedApps : List>>) { + adapter.updateApps(updatedApps) + println("moved") + } - ChangeType.MOVE -> { - adapter.updateApps(updatedApps) - adapter.notifyDataSetChanged() - println("moved") - appUpdate = true - } + private fun listsEqual(list1: List>>, list2: List>>): Boolean { + if (list1.size != list2.size) return false + for (i in list1.indices) { + if (list1[i].first.componentName != list2[i].first.componentName || list1[i].second.first != list2[i].second.first) { + return false } } + + return true } - private fun insertItem(position: Int, app: Pair>) { - adapter.addApp(position, app) - adapter.notifyItemInserted(position) + fun isJobActive(): Boolean { + return if (job != null) { + job!!.isActive + } else { + false + } } - private fun removeItem(position: Int) { - adapter.removeApp(position) - adapter.notifyItemRemoved(position) - } - - fun updateItem(position: Int, app: Pair>) { - adapter.updateApp(position, app) - adapter.notifyItemChanged(position) - } - - fun moveItem(position: Int, newPosition: Int) { - adapter.moveApp(position, newPosition) - adapter.notifyItemMoved(position, newPosition) - } - - fun updateInstalledApps() { - installedApps = appUtils.getInstalledApps(this@MainActivity) - appUpdate = true - } -} - -data class Change(val type: ChangeType, val position: Int, val newPosition: Int = 0) - -enum class ChangeType { - INSERT, REMOVE, UPDATE, MOVE } \ No newline at end of file