This library uses Server Driven Ui concepts to help developer to create screens on the Android platform
If you want to know more details about this library see about The Weapon Master Warrior - DynamicView a explation about the use.
If you want to improve the use from the DynamicView see about D&D - Perfect Party to your Application Android
https://jitpack.io/#jitpack/android-example
Add it to your settings.gradle with:
dependencyResolutionManagement {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
and:
dependencies {
implementation 'com.github.rviannaoliveira:DynamicView:{latest version}'
}
<?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>
class DynamicViewModel(
val dynamic: Dynamic,
val repository: DynamicRepository
) : ViewModel() {
//Stuffs
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDynamicComponentBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.recycler.adapter = vm.dynamic as DynamicAdapter
//--- Stuffs
}
enum class DynamicComponent(val key: String) {
TEXT_VIEW_COMPONENT("${DYNAMIC}TextView"),
}
fun List<SimpleProperties>.toRenders(listener : DynamicViewListener): List<ViewRenderer<*>> {
return this
.distinctBy { it.key }
.mapNotNull { simpleProperties ->
val key = simpleProperties.key
when (DynamicComponent.values().find { it.key == key }) {
DynamicComponent.TEXT_VIEW_COMPONENT -> TextViewComponentRender(listener)
else -> null
}
}
}
internal const val DYNAMIC = "Dynamic"
@JsonIgnoreProperties(ignoreUnknown = true)
data class TextProperties(
@JsonProperty("xxx") val myProperties1: String,
@JsonProperty("xxx") val myProperties2: String,
)
class MyComponentRender(override var onClickListener: DynamicViewListener?)
: ViewRenderer<MyComponentRender.MyHolder>(DynamicComponent.MY_COMPONENT.key, DynamicComponent.MY_COMPONENT.ordinal) {
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))
}
}
class DynamicViewModel(
val dynamic: Dynamic,
val repository: DynamicRepository
) : ViewModel() {
fun onResume() {
viewModelScope.launch {
repository.getDynamic()
.catch {
it.printStackTrace()
}
.collect {
setupDynamicRender(it)
dynamic.setViewObjectDiff(it)
}
}
}
private fun setupDynamicRender(list : List<SimpleProperties>) {
dynamic.registerRenderers(list.toRenders {
listener.invoke(it)
})
}
private val listener = object : DynamicViewListener {
override fun invoke(actionProperties: DynamicActionProperties) {
actionProperties.analytics?.let {
//Stuff
}
actionProperties.deeplink?.let {
//Stuff
}
}
}
}
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"
}
}
]
}
The DynamicView library was made inspired by these repositories:
https://github.com/vivchar/RendererRecyclerViewAdapter
https://github.com/GustavoHSSantorio/Dynamic-Adapter (We worked in the initial solution)
Thanks to these repositories that made I think in new ideas to generate a new library version
Copyright 2021 Rodrigo Vianna
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.