Created shortcut renaming functionality. Not finished.

This commit is contained in:
ottoptj 2024-11-02 07:08:26 +02:00
commit 902d7abac9
3 changed files with 76 additions and 26 deletions

View file

@ -25,6 +25,7 @@ import android.text.TextWatcher
import android.view.GestureDetector import android.view.GestureDetector
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
@ -212,7 +213,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
// Set the non-work profile drawable by default // Set the non-work profile drawable by default
textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null)
shortcutListeners(textView) shortcutListeners(textView, savedView)
if (savedView?.get(1) != "e") { if (savedView?.get(1) != "e") {
setShortcutSetup(textView, savedView) setShortcutSetup(textView, savedView)
@ -227,7 +228,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
private fun shortcutListeners(textView: TextView) { private fun shortcutListeners(textView: TextView, savedView: List<String>?) {
// Don't go to settings on long click, but keep other gestures functional // Don't go to settings on long click, but keep other gestures functional
textView.setOnTouchListener {_, event -> textView.setOnTouchListener {_, event ->
shortcutGestureDetector.onTouchEvent(event) shortcutGestureDetector.onTouchEvent(event)
@ -237,6 +238,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
ViewCompat.addAccessibilityAction(textView, getString(R.string.accessibility_set_shortcut)) { _, _ -> ViewCompat.addAccessibilityAction(textView, getString(R.string.accessibility_set_shortcut)) { _, _ ->
uiUtils.setMenuTitleAlignment(binding.menuTitle) uiUtils.setMenuTitleAlignment(binding.menuTitle)
uiUtils.setMenuTitleSize(binding.menuTitle) uiUtils.setMenuTitleSize(binding.menuTitle)
binding.menuTitle.hint = textView.text
binding.menuTitle.setText(textView.text)
binding.menuTitle.visibility = View.VISIBLE binding.menuTitle.visibility = View.VISIBLE
appAdapter?.shortcutTextView = textView appAdapter?.shortcutTextView = textView
@ -257,8 +260,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
textView.setOnLongClickListener { textView.setOnLongClickListener {
uiUtils.setMenuTitleAlignment(binding.menuTitle) uiUtils.setMenuTitleAlignment(binding.menuTitle)
uiUtils.setMenuTitleSize(binding.menuTitle) uiUtils.setMenuTitleSize(binding.menuTitle)
binding.menuTitle.hint = textView.text
binding.menuTitle.setText(textView.text)
binding.menuTitle.visibility = View.VISIBLE binding.menuTitle.visibility = View.VISIBLE
if (savedView != null) {
setRenameShortcutListener(textView, savedView)
}
appAdapter?.shortcutTextView = textView appAdapter?.shortcutTextView = textView
toAppMenu() toAppMenu()
searchSwitcher.visibility = View.GONE searchSwitcher.visibility = View.GONE
@ -267,6 +274,38 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
} }
private fun setRenameShortcutListener(textView: TextView, savedView: List<String>) {
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() { private fun toAppMenu() {
try { try {
// The menu opens from the top // The menu opens from the top
@ -853,6 +892,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
if (bottom - top > oldBottom - oldTop) { if (bottom - top > oldBottom - oldTop) {
// If keyboard is closed, remove cursor from the search bar // If keyboard is closed, remove cursor from the search bar
searchView.clearFocus() searchView.clearFocus()
binding.menuTitle.clearFocus()
} else if (bottom - top < oldBottom - oldTop && isInitialOpen) { } else if (bottom - top < oldBottom - oldTop && isInitialOpen) {
isInitialOpen = false isInitialOpen = false
appRecycler.scrollToPosition(0) appRecycler.scrollToPosition(0)

View file

@ -247,32 +247,32 @@ class UIUtils(private val context: Context) {
bottomSpace.layoutParams = bottomLayoutParams bottomSpace.layoutParams = bottomLayoutParams
} }
fun setDrawables(shortcut: TextView, alignment: String?) { fun setDrawables(textView: TextView, alignment: String?, alignments: Array<String> = arrayOf("left","center","right")) {
try { try {
when (alignment) { when (alignment) {
"left" -> { alignments[0] -> {
shortcut.setCompoundDrawablesWithIntrinsicBounds( textView.setCompoundDrawablesWithIntrinsicBounds(
shortcut.compoundDrawables.filterNotNull().first(), textView.compoundDrawables.filterNotNull().first(),
null, null,
null, null,
null null
) )
} }
"center" -> { alignments[1] -> {
shortcut.setCompoundDrawablesWithIntrinsicBounds( textView.setCompoundDrawablesWithIntrinsicBounds(
shortcut.compoundDrawables.filterNotNull().first(), textView.compoundDrawables.filterNotNull().first(),
null, null,
shortcut.compoundDrawables.filterNotNull().first(), textView.compoundDrawables.filterNotNull().first(),
null null
) )
} }
"right" -> { alignments[2] -> {
shortcut.setCompoundDrawablesWithIntrinsicBounds( textView.setCompoundDrawablesWithIntrinsicBounds(
null, null,
null, null,
shortcut.compoundDrawables.filterNotNull().first(), textView.compoundDrawables.filterNotNull().first(),
null null
) )
} }
@ -283,7 +283,7 @@ class UIUtils(private val context: Context) {
fun setAppAlignment( fun setAppAlignment(
textView: TextView, textView: TextView,
editText: TextView? = null, editText: TextView? = null,
regionText: TextView? = null regionText: TextView? = null,
) { ) {
val alignment = sharedPreferenceManager.getAppAlignment() val alignment = sharedPreferenceManager.getAppAlignment()
setTextGravity(textView, alignment) setTextGravity(textView, alignment)
@ -305,8 +305,9 @@ class UIUtils(private val context: Context) {
} }
fun setMenuTitleAlignment(menuTitle: TextView) { 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?) { private fun setTextAlignment(view: TextView, alignment: String?) {

View file

@ -21,20 +21,29 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="60dp" /> android:layout_height="60dp" />
<TextView <FrameLayout
android:id="@+id/menuTitle" android:id="@+id/menuTitleLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:clickable="false"
android:gravity="start"
android:paddingLeft="40dp" android:paddingLeft="40dp"
android:paddingTop="20dp" android:paddingTop="20dp"
android:paddingRight="40dp" android:paddingRight="40dp"
android:paddingBottom="20dp" android:paddingBottom="20dp">
android:text="@string/select_an_app"
android:textAppearance="@android:style/TextAppearance.DeviceDefault" <com.google.android.material.textfield.TextInputEditText
android:textColor="#C1F3F3F3" android:id="@+id/menuTitle"
android:textSize="36sp" /> android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:clickable="false"
android:gravity="start"
android:singleLine="true"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:textColor="#C1F3F3F3"
android:textSize="36sp"
app:drawableStartCompat="@drawable/edit_24px" />
</FrameLayout>
<ViewSwitcher <ViewSwitcher
android:id="@+id/menuView" android:id="@+id/menuView"