Skip to content

Commit

Permalink
Use currentEntity formatting also in List and Detail fragment like ac…
Browse files Browse the repository at this point in the history
…tion parameters
  • Loading branch information
e-marchand committed Jan 24, 2024
1 parent e6fdfc8 commit e7d5eba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any>)?.get("dataSource")) as? Map<String, Any> ?: 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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit e7d5eba

Please sign in to comment.