From 228dd2966bba64c4c82d0f00c7955871f7b0746a Mon Sep 17 00:00:00 2001 From: ottoptj Date: Mon, 27 May 2024 18:40:44 +0300 Subject: [PATCH] Some refactoring and improved returning to home screen from app menu. --- .../ottop/yamlauncher/AppMenuEdgeFactory.kt | 7 +- .../yamlauncher/AppMenuLinearLayoutManager.kt | 23 ----- .../java/eu/ottop/yamlauncher/MainActivity.kt | 97 ++++++++++--------- app/src/main/res/layout/activity_main.xml | 1 + 4 files changed, 53 insertions(+), 75 deletions(-) delete mode 100644 app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt index f927570..98ddd51 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt @@ -9,7 +9,7 @@ class AppMenuEdgeFactory(private val activity: MainActivity) : RecyclerView.Edge return AppMenuEdgeEffect(activity) } } -class AppMenuEdgeEffect(activity: MainActivity) : EdgeEffect(activity) { +class AppMenuEdgeEffect(private val activity: MainActivity) : EdgeEffect(activity) { // Adjust the speed here private val animationSpeedFactor = 0.5f @@ -19,10 +19,7 @@ class AppMenuEdgeEffect(activity: MainActivity) : EdgeEffect(activity) { override fun onPull(deltaDistance: Float, displacement: Float) { super.onPull(deltaDistance * animationSpeedFactor, displacement) - } - - override fun onRelease() { - super.onRelease() + activity.showHome() } override fun onPullDistance(deltaDistance: Float, displacement: Float): Float { diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt deleted file mode 100644 index 5d58c95..0000000 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt +++ /dev/null @@ -1,23 +0,0 @@ -package eu.ottop.yamlauncher - -import android.content.Context -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView - -class AppMenuLinearLayoutManager(private val activity: MainActivity) : LinearLayoutManager(activity) { - - private var scrollState = RecyclerView.SCROLL_STATE_IDLE - fun setScrollState(state: Int) { - scrollState = state - } - - override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int { - val scrollRange = super.scrollVerticallyBy(dy, recycler, state) - val overscroll: Int = dy - scrollRange - if (overscroll < 0 && scrollState == RecyclerView.SCROLL_STATE_DRAGGING) { - activity.showHome() - } - return scrollRange - } - - } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index f1bc512..36460ac 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -59,7 +59,6 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap private var appActionMenu = AppActionMenu() private val sharedPreferenceManager = SharedPreferenceManager() private val appUtils = AppUtils() - private val appMenuLinearLayoutManager = AppMenuLinearLayoutManager(this@MainActivity) private val appMenuEdgeFactory = AppMenuEdgeFactory(this@MainActivity) override fun onCreate(savedInstanceState: Bundle?) { @@ -103,53 +102,52 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap adapter = AppMenuAdapter(this@MainActivity, newApps, this@MainActivity, this@MainActivity, this@MainActivity) withContext(Dispatchers.Main) { recyclerView = findViewById(R.id.recycler_view) - recyclerView.layoutManager = appMenuLinearLayoutManager recyclerView.edgeEffectFactory = appMenuEdgeFactory recyclerView.adapter = adapter recyclerView.scrollToPosition(0) } - - recyclerView.addOnScrollListener(object: OnScrollListener() { - override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { - super.onScrollStateChanged(recyclerView, newState) - appMenuLinearLayoutManager.setScrollState(newState) - } - }) } - @SuppressLint("ClickableViewAccessibility") private fun handleListItems() { for (i in findViewById(R.id.shortcuts).children) { val textView = i as TextView - textView.setOnTouchListener() {_, event -> - gestureDetector.onTouchEvent(event) - super.onTouchEvent(event) - } + unselectedSetup(textView) val savedView = sharedPreferenceManager.getShortcut(this, textView) - textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) - - textView.compoundDrawablePadding = 0 - unselectedListeners(textView) - if (savedView?.get(1) != "e") { - - if (savedView?.get(1) != "0") { - textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_work_app, null),null,null,null) - } - else { - textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) - } - textView.text = savedView?.get(2) - selectedListeners(textView, savedView) + selectedSetup(textView, savedView) } } } + @SuppressLint("ClickableViewAccessibility") + private fun unselectedSetup(textView: TextView) { + textView.setOnTouchListener() {_, event -> + gestureDetector.onTouchEvent(event) + super.onTouchEvent(event) + } + + textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) + + textView.compoundDrawablePadding = 0 + unselectedListeners(textView) + } + + private fun selectedSetup(textView: TextView, savedView: List?) { + if (savedView?.get(1) != "0") { + textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_work_app, null),null,null,null) + } + else { + textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) + } + textView.text = savedView?.get(2) + selectedListeners(textView, savedView) + } + private fun unselectedListeners(textView: TextView) { textView.setOnClickListener { Toast.makeText(this, "Long click to select an app", Toast.LENGTH_SHORT).show() @@ -159,7 +157,6 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap adapter.shortcutTextView = textView showApps() - return@setOnLongClickListener true } } @@ -200,7 +197,6 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap override fun afterTextChanged(s: Editable?) { } - }) } @@ -210,27 +206,35 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap val newFilteredApps = mutableListOf>>() val updatedApps = appUtils.getInstalledApps(this@MainActivity) - if (cleanQuery.isNullOrEmpty()) { - manualRefresh() - newFilteredApps.addAll(installedApps) - } else { - updatedApps.forEach { - val cleanItemText = sharedPreferenceManager.getAppName(this@MainActivity, it.first.applicationInfo.packageName, it.second.second, it.first.applicationInfo.loadLabel(packageManager)).toString().clean() - if (cleanItemText.contains(cleanQuery, ignoreCase = true)) { - newFilteredApps.add(it) - } - } - } + getFilteredApps(cleanQuery, newFilteredApps, updatedApps) - val changes = detectChanges(installedApps, newFilteredApps) - installedApps = newFilteredApps - withContext(Dispatchers.Main) { - applyChanges(changes, installedApps) - } + applySearch(newFilteredApps) } } + private fun getFilteredApps(cleanQuery: String?, newFilteredApps: MutableList>>, updatedApps: List>>) { + if (cleanQuery.isNullOrEmpty()) { + manualRefresh() + newFilteredApps.addAll(installedApps) + } else { + updatedApps.forEach { + val cleanItemText = sharedPreferenceManager.getAppName(this@MainActivity, it.first.applicationInfo.packageName, it.second.second, it.first.applicationInfo.loadLabel(packageManager)).toString().clean() + if (cleanItemText.contains(cleanQuery, ignoreCase = true)) { + newFilteredApps.add(it) + } + } + } + } + + private suspend fun applySearch(newFilteredApps: MutableList>>) { + val changes = detectChanges(installedApps, newFilteredApps) + installedApps = newFilteredApps + withContext(Dispatchers.Main) { + applyChanges(changes, installedApps) + } + } + private fun String.clean(): String { return this.replace("[^a-zA-Z0-9]".toRegex(), "") } @@ -594,7 +598,6 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap } fun moveItem(position: Int, newPosition: Int) { - Log.d("Movestatus","MOVED") adapter.moveApp(position, newPosition) adapter.notifyItemMoved(position, newPosition) } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a1b9344..49b81a0 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -48,6 +48,7 @@ android:padding="0dp" android:requiresFadingEdge="vertical" android:scrollbars="none" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:stackFromEnd="true">