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.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<String>?) {
// 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<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() {
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)

View file

@ -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<String> = 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?) {

View file

@ -21,20 +21,29 @@
android:layout_width="match_parent"
android:layout_height="60dp" />
<TextView
android:id="@+id/menuTitle"
<FrameLayout
android:id="@+id/menuTitleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="start"
android:paddingLeft="40dp"
android:paddingTop="20dp"
android:paddingRight="40dp"
android:paddingBottom="20dp"
android:text="@string/select_an_app"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:textColor="#C1F3F3F3"
android:textSize="36sp" />
android:paddingBottom="20dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/menuTitle"
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
android:id="@+id/menuView"