Pablo
08/01/2024, 11:22 AMopen class Screen(val id: Int, val components: List<Component>)
open class Component(val id: Int)
class ImageComponent(id: Int, val imageSrc: String) : Component(id)
class TextComponent(id: Int, val text: String) : Component(id)
Now imagine a Screen that contains two texts and one image:
Screen(1, listOf(
TextComponent(1, "hello"),
TextComponent(2, "World"),
ImageComponent(3, "hello_world_image_route")
))
Previously on java I had a generateView() method on each screen and component, and those methods where overrided on subclasses and I simply needed to call generateView to get the views for each screen and set them to the content of the current activity.
Now I'm trying to migrate this to Compose and MVVM. Now each view must be a Composable, but for obvious reasons, Composables can only be called from other composables and I need to separate the model from the composables in the process to move to MVVM. So I can't have a @Composable method on those classes. Once I have my Screen object, how can I generate composables from it? I can't put a "generateComposable" method on it. I know how to write a @Composable function that paints a Text or an Image, but I don't know where should I put the code to generate that composable for each kind of screen or element when I have the model in memory. Which strategy can I follow?Pablo
08/01/2024, 11:28 AM@Composable
open fun Composable(modifier: Modifier) {}
on the classes of the model, and each class of the model overrides this to call the correct Composable function which is stored on the UI layer, for example, for Text:
@Composable
override fun Composable(modifier: Modifier) {
TextComposable(
component = this,
modifier = modifier
)
}
Proably this is not the best approach, I'm trying to understand which strategy is better. Any help please?Robert C
08/01/2024, 11:51 AMwhen(type){
type is TextType -> TextComposable(type.data)
type is ImageType -> ImageComposable(type.data)
}
also instead of making everything open you can just make it sealed class something like UiComponent idk
you can just iterate through given from BE components and based on type generate a composableChrimaeon
08/01/2024, 12:10 PMPablo
08/01/2024, 1:30 PMPablo
08/01/2024, 1:31 PMPablo
08/01/2024, 1:32 PMPablo
08/01/2024, 1:32 PMPablo
08/01/2024, 1:33 PMChrimaeon
08/01/2024, 1:34 PMRobert C
08/01/2024, 1:35 PMRobert C
08/01/2024, 1:36 PMChrimaeon
08/01/2024, 1:36 PMPablo
08/01/2024, 1:37 PMRobert C
08/01/2024, 1:37 PMPablo
08/01/2024, 1:37 PMPablo
08/01/2024, 1:37 PMPablo
08/01/2024, 1:37 PMChrimaeon
08/01/2024, 1:38 PMChrimaeon
08/01/2024, 1:39 PMPablo
08/01/2024, 1:39 PMChrimaeon
08/01/2024, 1:41 PMRobert C
08/01/2024, 1:43 PMRobert C
08/01/2024, 1:43 PMPablo
08/01/2024, 1:44 PM{
"section1": {
"title": "example glossary",
"components": {
"text": "S",
"image": {
"source": "imagesource"
"action": "open_section_2"
}
}
}
}
}
Pablo
08/01/2024, 1:44 PMPablo
08/01/2024, 1:45 PMChrimaeon
08/01/2024, 1:45 PMPablo
08/01/2024, 1:46 PMPablo
08/01/2024, 1:47 PMRobert C
08/01/2024, 1:47 PMChrimaeon
08/01/2024, 1:58 PMStylianos Gakis
08/01/2024, 2:14 PMPablo
08/01/2024, 2:24 PMPablo
08/01/2024, 2:24 PMPablo
08/01/2024, 2:25 PMPablo
08/01/2024, 2:25 PMMarcin Wisniowski
08/20/2024, 11:18 AM