Skip to content

Commit f76aaf9

Browse files
Refactor(rename): Simplify rename logic and UI
This commit refactors the app renaming functionality in the `AppDrawerAdapter` for a simpler user experience and cleaner code. The rename button has been changed from a `TextView` to an `ImageView`, and a cancel `ImageView` has been added. The logic for handling rename actions is now more direct: - Clicking the save button with an empty text field resets the app name to its default. - Clicking save with a non-empty field applies the new name. - A new cancel button allows the user to exit the rename mode without making changes. This removes the complex `TextWatcher` that previously updated the save button's text (`Rename`/`Reset`/`Cancel`) based on the input, simplifying the view holder's binding logic.
1 parent d3d30f1 commit f76aaf9

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

app/src/main/java/com/github/codeworkscreativehub/mlauncher/ui/adapter/AppDrawerAdapter.kt

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import android.widget.EditText
1919
import android.widget.Filter
2020
import android.widget.Filterable
2121
import android.widget.FrameLayout
22+
import android.widget.ImageView
2223
import android.widget.LinearLayout
2324
import android.widget.TextView
2425
import androidx.appcompat.content.res.AppCompatResources
@@ -160,27 +161,29 @@ class AppDrawerAdapter(
160161
}
161162

162163
holder.appSaveRename.setOnClickListener {
163-
when (holder.appSaveRename.text) {
164-
getLocalizedString(R.string.rename) -> {
165-
val name = holder.appRenameEdit.text.toString().trim()
166-
AppLogger.d("AppListDebug", "✏️ Renaming ${appModel.activityPackage} to $name")
167-
notifyItemChanged(holder.absoluteAdapterPosition)
168-
AppLogger.d("AppListDebug", "🔁 notifyItemChanged at ${holder.absoluteAdapterPosition}")
169-
appRenameListener(appModel.activityPackage, name)
170-
}
164+
val currentText = holder.appRenameEdit.text.toString().trim()
171165

172-
getLocalizedString(R.string.reset) -> {
166+
when {
167+
currentText.isEmpty() -> { // Reset state
173168
AppLogger.d("AppListDebug", "✏️ Resetting ${appModel.activityPackage} to default")
174-
notifyItemChanged(holder.absoluteAdapterPosition)
175-
AppLogger.d("AppListDebug", "🔁 notifyItemChanged at ${holder.absoluteAdapterPosition}")
176-
appRenameListener(appModel.activityPackage, emptyString()) // empty string signals default
169+
appRenameListener(appModel.activityPackage, emptyString()) // empty string = default
177170
}
178171

179-
else -> {
180-
notifyItemChanged(holder.absoluteAdapterPosition)
172+
currentText == appModel.activityLabel -> { // Rename state
173+
AppLogger.d("AppListDebug", "✏️ Renaming ${appModel.activityPackage} to $currentText")
174+
appRenameListener(appModel.activityPackage, currentText)
181175
}
182176
}
183177

178+
notifyItemChanged(holder.absoluteAdapterPosition)
179+
AppLogger.d("AppListDebug", "🔁 notifyItemChanged at ${holder.absoluteAdapterPosition}")
180+
}
181+
182+
holder.appSaveCancel.setOnClickListener {
183+
AppLogger.d("AppListDebug", "✏️ Cancel rename for ${appModel.activityPackage}")
184+
185+
notifyItemChanged(holder.absoluteAdapterPosition)
186+
AppLogger.d("AppListDebug", "🔁 notifyItemChanged at ${holder.absoluteAdapterPosition}")
184187
}
185188

186189
holder.appSaveTag.setOnClickListener {
@@ -321,7 +324,8 @@ class AppDrawerAdapter(
321324
val appHide: TextView = itemView.appHide
322325
val appLock: TextView = itemView.appLock
323326
val appRenameEdit: EditText = itemView.appRenameEdit
324-
val appSaveRename: TextView = itemView.appSaveRename
327+
val appSaveRename: ImageView = itemView.appSaveRename
328+
val appSaveCancel: ImageView = itemView.appSaveCancel
325329
val appTagEdit: EditText = itemView.appTagEdit
326330
val appSaveTag: TextView = itemView.appSaveTag
327331

@@ -426,20 +430,10 @@ class AppDrawerAdapter(
426430
}
427431

428432
appRenameEdit.apply {
429-
val activityLabel = prefs.getAppAlias(appListItem.activityPackage).takeIf { it.isNotBlank() } ?: appListItem.activityLabel
433+
val activityLabel = prefs.getAppAlias(appListItem.activityPackage).takeIf { it.isNotBlank() }
434+
?: appListItem.activityLabel
435+
430436
text = Editable.Factory.getInstance().newEditable(activityLabel)
431-
appSaveRename.text = getLocalizedString(R.string.cancel)
432-
addTextChangedListener(object : TextWatcher {
433-
override fun afterTextChanged(s: Editable) {}
434-
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
435-
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
436-
appSaveRename.text = when {
437-
text.isEmpty() -> getLocalizedString(R.string.reset)
438-
text.toString() == activityLabel -> getLocalizedString(R.string.cancel)
439-
else -> getLocalizedString(R.string.rename)
440-
}
441-
}
442-
})
443437
}
444438

445439
appTagEdit.apply {

0 commit comments

Comments
 (0)