Skip to content

Add ModelsViewModel and ModelService for fetching AnyLogic model metadata#501

Open
kofi-k wants to merge 1 commit intomainfrom
models-service
Open

Add ModelsViewModel and ModelService for fetching AnyLogic model metadata#501
kofi-k wants to merge 1 commit intomainfrom
models-service

Conversation

@kofi-k
Copy link
Collaborator

@kofi-k kofi-k commented Mar 4, 2026

No description provided.

@kofi-k kofi-k requested a review from Erikvv March 4, 2026 00:00

object AnyLogicModelsRepository {
private val service = ModelService()
var models by mutableStateOf<List<AnyLogicModel>>(emptyList())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found in the LanguageManager that this is does not trigger a re-render. Probably because it is not in the Composable tree.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to explain this but if it is read inside a composable, it will trigger a recomposition.

and i know the basic rule is that when a composable reads a State<T> during composition and that state changes, compose re-renders.
If you never read it in composition, nothing happens.

suspend fun getAllModels(): List<AnyLogicModel>
}

class ModelService(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually the "Repository" is simple and the "Service" more complex. But here it is the other way around

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the confusion comes from mixing backend naming conventions with frontend/Kobweb architecture? 🧐 I don't see anything wrong with the setup itself, hmm

@Erikvv
Copy link
Member

Erikvv commented Mar 4, 2026

I wonder if we can have a more pure implementation of the ViewModel pattern like in https://github.com/google-developer-training/basic-android-kotlin-compose-training-unscramble/tree/main/app/src/main/java/com/example/unscramble/ui

Put the state in compositionLocal so it can be accessed from all the ModelCard components. Like I do with the LanguageProvider:

@Composable
fun LanguageProvider(content: @Composable () -> Unit) {
val language = LanguageManager.language.collectAsState()
CompositionLocalProvider(LocalLanguage provides language.value) {
content()
}
}

@Erikvv
Copy link
Member

Erikvv commented Mar 4, 2026

Please let me know if we're kind of on the same page.

@Erikvv
Copy link
Member

Erikvv commented Mar 4, 2026

I wonder if we should enrich the "TwinModel" objects before rendering, or if the logic should be put in the card components (and in individual model pages)

@kofi-k
Copy link
Collaborator Author

kofi-k commented Mar 4, 2026

Im inclined to agree with having the state in compositionLocal, while i worked this, co-pilot suggested this approach and i thought it was a bit too much.

I wonder if we should enrich the "TwinModel" objects before rendering, or if the logic should be put in the card components (and in individual model pages)

I think enriching TwinModel objects should work well and makes it easier to update model pages via the component wrappers. I will try something

@Erikvv
Copy link
Member

Erikvv commented Mar 4, 2026

I think enriching TwinModel objects should work well and makes it easier to update model pages via the component wrappers. I will try something

Ok I mostly wanted to know what you'll build on top of this. Will approve this PR.

@kofi-k kofi-k changed the title Add cached AnyLogic models repository and service implementation Add ModelsViewModel and ModelService for fetching AnyLogic model metadata Mar 12, 2026
@kofi-k
Copy link
Collaborator Author

kofi-k commented Mar 12, 2026

New changes aligns with ViewModel pattern, i envision how we can also enrich TwinModelCard objects from here before collecting it in composables.

@kofi-k kofi-k requested a review from Erikvv March 13, 2026 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants