mirror of
https://github.com/home-assistant/android
synced 2024-07-22 10:54:12 +00:00
Fix restoring custom fields in service button widget (#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.
This commit is contained in:
parent
fb71b9c788
commit
cad1bfa47e
|
@ -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()
|
||||
}
|
||||
|
@ -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 ||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue