-
Notifications
You must be signed in to change notification settings - Fork 47
feat(composeui): graphql interactions #4276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tbaile
wants to merge
14
commits into
AlchemistSimulator:master
Choose a base branch
from
Tbaile:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ac74c1f
build(graphql): add wasmJS target
Tbaile 76443c7
feat(composeui): scaffold first interactions with graphql
Tbaile dd69fbf
feat: wip subscription management
Tbaile 27a4173
feat: inital material3 ui, added canvas
Tbaile 63f2e7d
feat(graphql-surrogate): add step to graphql simulation surrogate
AngeloFilaseta 382bc81
chore(build): actualize the `yarn.lock` file
DanySK b4473de
feat: added alert in case of error
Tbaile a303301
feat: added simulation and node subscription
Tbaile b86809f
feat: vertical scrolling
Tbaile a62762e
chore(build): update the javadoc.io cache
DanySK 36ffb65
chore(build): update the javadoc.io cache
DanySK 0144671
chore(build): update the javadoc.io cache
DanySK 5e6e6f6
chore(build): update the javadoc.io cache
DanySK 08586aa
fix: styling fix and review
Tbaile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...emist-composeui/src/commonMain/kotlin/it/unibo/alchemist/boundary/composeui/NodeDrawer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2010-2025, Danilo Pianini and contributors | ||
* listed, for each module, in the respective subproject's build.gradle.kts file. | ||
* | ||
* This file is part of Alchemist, and is distributed under the terms of the | ||
* GNU General Public License, with a linking exception, | ||
* as described in the file LICENSE in the Alchemist distribution's top directory. | ||
*/ | ||
|
||
package it.unibo.alchemist.boundary.composeui | ||
|
||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import it.unibo.alchemist.boundary.composeui.viewmodels.NodeViewModel | ||
|
||
/** | ||
* Display the information of a node, subscribing to its own channel for data. | ||
*/ | ||
Tbaile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Composable | ||
fun NodeDrawer(nodeId: Int) { | ||
val nodeModel: NodeViewModel = viewModel(key = "node-$nodeId") { NodeViewModel(nodeId) } | ||
val nodeInfo by nodeModel.nodeInfo.collectAsStateWithLifecycle() | ||
Text("Node $nodeId: $nodeInfo") | ||
} |
71 changes: 71 additions & 0 deletions
71
...i/src/commonMain/kotlin/it/unibo/alchemist/boundary/composeui/viewmodels/NodeViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (C) 2010-2025, Danilo Pianini and contributors | ||
* listed, for each module, in the respective subproject's build.gradle.kts file. | ||
* | ||
* This file is part of Alchemist, and is distributed under the terms of the | ||
* GNU General Public License, with a linking exception, | ||
* as described in the file LICENSE in the Alchemist distribution's top directory. | ||
*/ | ||
|
||
package it.unibo.alchemist.boundary.composeui.viewmodels | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.apollographql.apollo3.api.Error | ||
import it.unibo.alchemist.boundary.graphql.client.GraphQLClientFactory | ||
import it.unibo.alchemist.boundary.graphql.client.NodeInfoSubscription | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
data class Molecule(val name: String) | ||
|
||
data class MoleculeConcentration(val concentration: String, val molecule: Molecule) | ||
|
||
data class NodeInfo( | ||
val id: Int, | ||
val moleculeCount: Int, | ||
val properties: List<String>, | ||
val contents: List<MoleculeConcentration>, | ||
) | ||
|
||
class NodeViewModel(private val nodeId: Int) : ViewModel() { | ||
private val _nodeInfo = MutableStateFlow<NodeInfo?>(null) | ||
val nodeInfo = _nodeInfo.asStateFlow() | ||
|
||
private val _errors = MutableStateFlow<List<Error>>(emptyList()) | ||
val errors = _errors.asStateFlow() | ||
|
||
// TODO: parameterize the host and port and separate client in different file | ||
private val client = GraphQLClientFactory.subscriptionClient( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to add URL and port as configuration/parameter of NodeViewModel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, I've actually left this as is for a couple of reasons:
|
||
"127.0.0.1", | ||
3000, | ||
) | ||
|
||
private fun load() { | ||
_errors.value = emptyList() | ||
viewModelScope.launch { | ||
client.subscription(NodeInfoSubscription(nodeId)) | ||
.toFlow() | ||
.collect { response -> | ||
response.data?.let { data -> | ||
_nodeInfo.value = NodeInfo( | ||
data.environment.nodeById.id, | ||
data.environment.nodeById.moleculeCount, | ||
data.environment.nodeById.properties, | ||
data.environment.nodeById.contents.entries.map { | ||
MoleculeConcentration( | ||
it.concentration, | ||
Molecule(it.molecule.name), | ||
) | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
init { | ||
load() | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...commonMain/kotlin/it/unibo/alchemist/boundary/composeui/viewmodels/SimulationViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (C) 2010-2025, Danilo Pianini and contributors | ||
* listed, for each module, in the respective subproject's build.gradle.kts file. | ||
* | ||
* This file is part of Alchemist, and is distributed under the terms of the | ||
* GNU General Public License, with a linking exception, | ||
* as described in the file LICENSE in the Alchemist distribution's top directory. | ||
*/ | ||
|
||
package it.unibo.alchemist.boundary.composeui.viewmodels | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.apollographql.apollo3.api.Error | ||
import it.unibo.alchemist.boundary.graphql.client.GraphQLClientFactory | ||
import it.unibo.alchemist.boundary.graphql.client.PauseSimulationMutation | ||
import it.unibo.alchemist.boundary.graphql.client.PlaySimulationMutation | ||
import it.unibo.alchemist.boundary.graphql.client.SimulationSubscription | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
enum class SimulationStatus { | ||
Init, | ||
Ready, | ||
Paused, | ||
Running, | ||
Terminated, | ||
} | ||
|
||
data class Node(val id: Int, val coordinates: List<Double>) | ||
|
||
class SimulationViewModel : ViewModel() { | ||
private val _nodes = MutableStateFlow<List<Node>>(emptyList()) | ||
private val _status = MutableStateFlow(SimulationStatus.Init) | ||
private val _errors = MutableStateFlow<List<Error>>(emptyList()) | ||
|
||
val nodes = _nodes.asStateFlow() | ||
val status = _status.asStateFlow() | ||
val errors = _errors.asStateFlow() | ||
|
||
// TODO: parameterize the host and port and separate client in different file | ||
private val client = GraphQLClientFactory.subscriptionClient( | ||
"127.0.0.1", | ||
3000, | ||
) | ||
|
||
fun pause() { | ||
viewModelScope.launch { | ||
client.mutation(PauseSimulationMutation()).execute() | ||
} | ||
} | ||
|
||
fun play() { | ||
viewModelScope.launch { | ||
client.mutation(PlaySimulationMutation()).execute() | ||
} | ||
} | ||
|
||
fun fetch() { | ||
_errors.value = emptyList() | ||
viewModelScope.launch { | ||
client.subscription(SimulationSubscription()) | ||
.toFlow() | ||
.collect { response -> | ||
if (response.hasErrors()) { | ||
response.errors?.let { errors -> | ||
_errors.update { errors } | ||
} | ||
} | ||
response.data?.let { data -> | ||
_status.update { | ||
when (data.simulation.status) { | ||
"READY" -> SimulationStatus.Ready | ||
"PAUSED" -> SimulationStatus.Paused | ||
"RUNNING" -> SimulationStatus.Running | ||
"TERMINATED" -> SimulationStatus.Terminated | ||
else -> SimulationStatus.Init | ||
} | ||
} | ||
_nodes.value = data.simulation.environment.nodeToPos.entries.map { | ||
Node( | ||
id = it.id, | ||
coordinates = it.position.coordinates, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
init { | ||
fetch() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.