View System

1. Add in a xml that you want to use

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</androidx.constraintlayout.widget.ConstraintLayout>

2. Add in your ViewModel the interface from the Dynamic

class YourViewModel(
    val craft: CraftDView,
    val repository: DynamicRepository 
) : ViewModel() {
 //Stuffs 
}

3. In your Activity/Fragment connect the Dynamic to the adapter in xml

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDynamicComponentBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.recycler.adapter = vm.craft as CraftDViewAdapter
        //--- Stuffs
}

4. Create object Properties of according with your components for example:

@JsonIgnoreProperties(ignoreUnknown = true)
data class TextProperties(
    @JsonProperty("xxx") val myProperties1: String,
    @JsonProperty("xxx") val myProperties2: String,
)

5. Create object ViewRenders of according with your components for example:

class MyComponentRender(override var onClickListener: CraftDViewListener?)
    : ViewRenderer<MyComponentRender.MyHolder>("Your Key", "Your Identifier") {

    inner class MyHolder(val anyView: AnyView) : RecyclerView.ViewHolder(anyView)

    override fun bindView(model: SimpleProperties, holder: MyHolder, position: Int) {
        val anyProperties = model.value.convertToVO<AnyProperties>()

        holder.any.text = anyProperties.text
        anyProperties.textColorHex?.let { textColorHex ->
            //Stuff
        }
        anyProperties.actionProperties?.let { actionProperties ->
          //Stuff
        }
    }

    override fun createViewHolder(parent: ViewGroup): MyHolder {
        return MyHolder(AnyView(parent.context))
    }
}

6. Configure your ViewModel to accept for example:

class DynamicViewModel(
    val craft: CraftD,
    val repository: SampleCraftDRepository
) : ViewModel() {
  
    fun onResume() {
        viewModelScope.launch {
            repository.getDynamic()
                .catch {
                    it.printStackTrace()
                }
                .collect {
                    setupDynamicRender(it)
                    craft.setViewObjectDiff(it)
                }
        }
    }

    private fun setupDynamicRender(list: List<SimpleProperties>) {
        craft.registerRenderers(
            CraftDBuilderManager.getBuilderRenders(
                simpleProperties = list,
                customDynamicBuilderList = customListViewRender // Can you pass your custom list from ViewRender
            ) { action ->
                listener.invoke(action)
            })
    }

    private val listener = object : CraftDViewListener {
        override fun invoke(actionProperties: ActionProperties) {
            actionProperties.analytics?.let {
               //Stuff
            }
            actionProperties.deeplink?.let {
                //Stuff
            }
        }
    }
}

7. Enjoy and Have fun to create a json that you need

Your json must to have at least two parameters key and value that are respective of your object for example:

{
  "data": [
    {
      "key": "MyDynamicView",
      "value": {
        "text": "Any",
        "textColor": "Any"
      }
    }
  ]
}

Last updated