Skip to content

Commit

Permalink
Fix restoring custom fields in service button widget (home-assistant#…
Browse files Browse the repository at this point in the history
…3813)

- When reading service data from the database, make sure to also add custom fields.
 - Fix RecyclerView not setting fields to empty when no value is present, potentially showing input from other recycled fields if the field doesn't have any value.
 - Fix adding a new custom field destroying any input currently held in the views by using a specific insertion method instead of all changed, which will reset the values.
  • Loading branch information
jpelgrom authored Aug 24, 2023
1 parent fb71b9c commit cad1bfa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() {
.setView(fieldKeyInput)
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.setPositiveButton(android.R.string.ok) { _, _ ->
if (dynamicFields.any { it.field == binding.widgetTextConfigService.text.toString() }) return@setPositiveButton

val position = dynamicFields.size
dynamicFields.add(
position,
ServiceFieldBinder(
binding.widgetTextConfigService.text.toString(),
fieldKeyInput.text.toString()
)
)

dynamicFieldAdapter.notifyDataSetChanged()
dynamicFieldAdapter.notifyItemInserted(position)
}
.show()
}
Expand Down Expand Up @@ -134,6 +138,7 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() {
Log.d(TAG, "Fields applicable to this service: $fields")

val existingServiceData = mutableMapOf<String, Any?>()
val addedFields = mutableListOf<String>()
buttonWidgetDao.get(appWidgetId)?.let { buttonWidget ->
if (
buttonWidget.serverId != selectedServerId ||
Expand All @@ -146,6 +151,7 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() {
for (item in dbMap) {
val value = item.value.toString().replace("[", "").replace("]", "") + if (item.key == "entity_id") ", " else ""
existingServiceData[item.key] = value.ifEmpty { null }
addedFields.add(item.key)
}
}

Expand All @@ -165,6 +171,10 @@ class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity() {
dynamicFields.add(ServiceFieldBinder(serviceText, fieldKey, existingServiceData[fieldKey]))
}
}
addedFields.minus("entity_id").minus(fieldKeys).forEach { extraFieldKey ->
Log.d(TAG, "Creating a text input box for extra $extraFieldKey")
dynamicFields.add(ServiceFieldBinder(serviceText, extraFieldKey, existingServiceData[extraFieldKey]))
}

dynamicFieldAdapter.notifyDataSetChanged()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class WidgetDynamicFieldAdapter(
// Set text to empty string to prevent a recycled, incorrect value
autoCompleteTextView.setText("")
}
} else {
autoCompleteTextView.setText("")
}

// Have the text view store its text for later recall
Expand Down

0 comments on commit cad1bfa

Please sign in to comment.