From e7d5eba6a4a14e482381b4f4a6615b40571a3ca2 Mon Sep 17 00:00:00 2001 From: "eric.marchand" Date: Wed, 24 Jan 2024 15:14:10 +0100 Subject: [PATCH] Use currentEntity formatting also in List and Detail fragment like action parameters --- .../actionparameters/ActionParametersFragment.kt | 11 +---------- .../detail/EntityDetailFragmentObserver.kt | 10 ++++++++++ .../qmobile/qmobileui/list/EntityListAdapter.kt | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt index abd23bab..5d3ff3d6 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt @@ -135,19 +135,10 @@ class ActionParametersFragment : BaseFragment(), ActionProvider, MenuProvider { const val INPUT_CONTROL_DISPLAY_TEXT_INJECT_KEY = "input_control_display_text_inject" const val INPUT_CONTROL_FIELD_VALUE_INJECT_KEY = "input_control_field_value_inject" - @Suppress("UNCHECKED_CAST") fun checkChoiceList(actionParameter : JSONObject, immutableEntity: RoomEntity) { val inputControlName = actionParameter.getSafeString("source")?.removePrefix("/") ?: return val fieldMapping = BaseApp.runtimeDataHolder.inputControls.find { it.name == inputControlName } ?: return - fieldMapping.choiceListComputed = null - - val dataSource = ((fieldMapping.choiceList as? Map)?.get("dataSource")) as? Map ?: return - val currentEntity = dataSource.get("currentEntity") as? Boolean ?: false - if (!currentEntity) return - - val fieldName = (dataSource.get("field") as? String) ?: return - val choiceList = (ReflectionUtils.getInstancePropertyForInputControl(immutableEntity, fieldName) as? JSONObject) ?: return - fieldMapping.choiceListComputed = choiceList.toStringMap() + fieldMapping.checkChoiceList(immutableEntity) } } diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/detail/EntityDetailFragmentObserver.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/detail/EntityDetailFragmentObserver.kt index f097eaac..f73c6b5e 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/detail/EntityDetailFragmentObserver.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/detail/EntityDetailFragmentObserver.kt @@ -8,6 +8,7 @@ package com.qmobile.qmobileui.detail import com.qmobile.qmobileapi.model.entity.EntityModel import com.qmobile.qmobileapi.utils.parseToString +import com.qmobile.qmobiledatastore.data.RoomEntity import com.qmobile.qmobiledatasync.app.BaseApp import com.qmobile.qmobiledatasync.relation.RelationHelper import com.qmobile.qmobiledatasync.viewmodel.EntityViewModel @@ -42,6 +43,14 @@ class EntityDetailFragmentObserver( } } + private fun checkChoiceList(tableName : String, immutableEntity: RoomEntity) { + BaseApp.runtimeDataHolder.customFormatters[tableName]?.let { fieldMappings -> + for ((name, fieldMapping) in fieldMappings) { + if (fieldMapping.binding == "localizedText") fieldMapping.checkChoiceList(immutableEntity) + } + } + } + // Observe entity private fun observeEntity() { entityViewModel.entity.observe( @@ -50,6 +59,7 @@ class EntityDetailFragmentObserver( Timber.d("Observed entity from Room, json = ${BaseApp.mapper.parseToString(entity)}") entity?.let { fragment.binding.setVariable(BR.entityData, entity) + checkChoiceList(entityViewModel.getAssociatedTableName(), it) fragment.binding.executePendingBindings() RelationHelper.setupRelationNavigation(fragment.tableName, fragment.binding, entity) diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/list/EntityListAdapter.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/list/EntityListAdapter.kt index 9b4a9894..c838a65d 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/list/EntityListAdapter.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/list/EntityListAdapter.kt @@ -16,6 +16,7 @@ import androidx.recyclerview.widget.DiffUtil import com.qmobile.qmobileapi.model.entity.EntityModel import com.qmobile.qmobiledatastore.data.RoomEntity import com.qmobile.qmobiledatasync.app.BaseApp +import com.qmobile.qmobiledatasync.utils.FieldMapping import com.qmobile.qmobileui.formatters.FormatterUtils import com.qmobile.qmobileui.utils.ReflectionUtils import com.qmobile.qmobileui.utils.ResourcesHelper @@ -58,8 +59,21 @@ class EntityListAdapter internal constructor( return ListItemViewHolder(dataBinding, tableName, onItemClick, onItemLongClick) } + private fun checkChoiceList(immutableEntity: RoomEntity) { + BaseApp.runtimeDataHolder.customFormatters[tableName]?.let { fieldMappings -> + for ((name, fieldMapping) in fieldMappings) { + if (fieldMapping.binding == "localizedText") { + fieldMapping.checkChoiceList(immutableEntity) + } + } + } + } override fun onBindViewHolder(holder: ListItemViewHolder, position: Int) { - holder.bind(getItem(position)) + val roomEntity: RoomEntity? = getItem(position) + roomEntity?.let { + checkChoiceList(it) + } + holder.bind(roomEntity) } @Suppress("TooGenericExceptionCaught")