From 902d7abac9e83abf18074c7c5dd4196495bb5f16 Mon Sep 17 00:00:00 2001 From: ottoptj Date: Sat, 2 Nov 2024 07:08:26 +0200 Subject: [PATCH] Created shortcut renaming functionality. Not finished. --- .../java/eu/ottop/yamlauncher/MainActivity.kt | 46 +++++++++++++++++-- .../eu/ottop/yamlauncher/utils/UIUtils.kt | 29 ++++++------ app/src/main/res/layout/activity_main.xml | 27 +++++++---- 3 files changed, 76 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 00ab8af..e2513b4 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -25,6 +25,7 @@ import android.text.TextWatcher import android.view.GestureDetector import android.view.MotionEvent import android.view.View +import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputMethodManager import android.widget.ImageView import android.widget.LinearLayout @@ -212,7 +213,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh // Set the non-work profile drawable by default textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) - shortcutListeners(textView) + shortcutListeners(textView, savedView) if (savedView?.get(1) != "e") { setShortcutSetup(textView, savedView) @@ -227,7 +228,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } @SuppressLint("ClickableViewAccessibility") - private fun shortcutListeners(textView: TextView) { + private fun shortcutListeners(textView: TextView, savedView: List?) { // Don't go to settings on long click, but keep other gestures functional textView.setOnTouchListener {_, event -> shortcutGestureDetector.onTouchEvent(event) @@ -237,6 +238,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh ViewCompat.addAccessibilityAction(textView, getString(R.string.accessibility_set_shortcut)) { _, _ -> uiUtils.setMenuTitleAlignment(binding.menuTitle) uiUtils.setMenuTitleSize(binding.menuTitle) + binding.menuTitle.hint = textView.text + binding.menuTitle.setText(textView.text) binding.menuTitle.visibility = View.VISIBLE appAdapter?.shortcutTextView = textView @@ -257,8 +260,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh textView.setOnLongClickListener { uiUtils.setMenuTitleAlignment(binding.menuTitle) uiUtils.setMenuTitleSize(binding.menuTitle) + binding.menuTitle.hint = textView.text + binding.menuTitle.setText(textView.text) binding.menuTitle.visibility = View.VISIBLE - + if (savedView != null) { + setRenameShortcutListener(textView, savedView) + } appAdapter?.shortcutTextView = textView toAppMenu() searchSwitcher.visibility = View.GONE @@ -267,6 +274,38 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } } + private fun setRenameShortcutListener(textView: TextView, savedView: List) { + binding.menuTitle.setOnEditorActionListener { _, actionId, _ -> + + if (actionId == EditorInfo.IME_ACTION_DONE) { + if (binding.menuTitle.text.isNullOrBlank()) { + Toast.makeText(this@MainActivity, getString(R.string.empty_rename), Toast.LENGTH_SHORT).show() + return@setOnEditorActionListener true + } + val imm = + getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + imm.hideSoftInputFromWindow(binding.menuTitle.windowToken, 0) + textView.text = binding.menuTitle.text + try { + sharedPreferenceManager.setShortcut( + textView, + savedView[0], + savedView[1].toInt() + ) + } catch (_: NumberFormatException) { + sharedPreferenceManager.setShortcut( + textView, + savedView[0], + 0 + ) + } + backToHome() + return@setOnEditorActionListener true + } + false + } + } + private fun toAppMenu() { try { // The menu opens from the top @@ -853,6 +892,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh if (bottom - top > oldBottom - oldTop) { // If keyboard is closed, remove cursor from the search bar searchView.clearFocus() + binding.menuTitle.clearFocus() } else if (bottom - top < oldBottom - oldTop && isInitialOpen) { isInitialOpen = false appRecycler.scrollToPosition(0) diff --git a/app/src/main/java/eu/ottop/yamlauncher/utils/UIUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/utils/UIUtils.kt index d3fee45..27bac48 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/utils/UIUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/utils/UIUtils.kt @@ -247,32 +247,32 @@ class UIUtils(private val context: Context) { bottomSpace.layoutParams = bottomLayoutParams } - fun setDrawables(shortcut: TextView, alignment: String?) { + fun setDrawables(textView: TextView, alignment: String?, alignments: Array = arrayOf("left","center","right")) { try { when (alignment) { - "left" -> { - shortcut.setCompoundDrawablesWithIntrinsicBounds( - shortcut.compoundDrawables.filterNotNull().first(), + alignments[0] -> { + textView.setCompoundDrawablesWithIntrinsicBounds( + textView.compoundDrawables.filterNotNull().first(), null, null, null ) } - "center" -> { - shortcut.setCompoundDrawablesWithIntrinsicBounds( - shortcut.compoundDrawables.filterNotNull().first(), + alignments[1] -> { + textView.setCompoundDrawablesWithIntrinsicBounds( + textView.compoundDrawables.filterNotNull().first(), null, - shortcut.compoundDrawables.filterNotNull().first(), + textView.compoundDrawables.filterNotNull().first(), null ) } - "right" -> { - shortcut.setCompoundDrawablesWithIntrinsicBounds( + alignments[2] -> { + textView.setCompoundDrawablesWithIntrinsicBounds( null, null, - shortcut.compoundDrawables.filterNotNull().first(), + textView.compoundDrawables.filterNotNull().first(), null ) } @@ -283,7 +283,7 @@ class UIUtils(private val context: Context) { fun setAppAlignment( textView: TextView, editText: TextView? = null, - regionText: TextView? = null + regionText: TextView? = null, ) { val alignment = sharedPreferenceManager.getAppAlignment() setTextGravity(textView, alignment) @@ -305,8 +305,9 @@ class UIUtils(private val context: Context) { } fun setMenuTitleAlignment(menuTitle: TextView) { - setTextGravity(menuTitle, sharedPreferenceManager.getAppAlignment()) - + val alignment = sharedPreferenceManager.getAppAlignment() + setTextGravity(menuTitle, alignment) + setDrawables(menuTitle, alignment, arrayOf("right","center","left")) } private fun setTextAlignment(view: TextView, alignment: String?) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f1d0c8a..2cdc754 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -21,20 +21,29 @@ android:layout_width="match_parent" android:layout_height="60dp" /> - + android:paddingBottom="20dp"> + + + +