View:
val viewModel = hiltViewModel<ActivityViewModel>()
Text("STATE: ${viewModel.state.activity?.inspectionInvitation?.state?.title}")
ViewModel:
var state by mutableStateOf(ActivityScreenState())
private set
....
suspend fun fetchActivity(id: String) {
val resource = repository.fetchActivity(id)
val activity = resource.data
resource.errorMessage?.let {
Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
}
state = state.copy(
isLoading = false,
activity = activity
)
}
suspend fun accept(invitation: Invitation) {
val tempActivity = state.activity
tempActivity?.invitation?.state = InvitationState.ACCEPTED
state = state.copy(
activity = tempActivity
)
}
ActivityScreenState:
data class ActivityScreenState(
val isLoading: Boolean = true,
val activity: Activity? = null,
)
data class Activity(
val id: String,
@Json(name = "inspection_invitation")
val invitation: Invitation?,
)
data class Invitation(
val id: String,
var state: InvitationState
)
enum class ActivityState(val value: String) {
@Json(name = "accepted")
ACCEPTED("accepted"),
@Json(name = "declined")
DECLINED("declined");
...
}
I have a viewMode, ActivityScreenState data class that contains Activity data class. When I update Activity inside the ActivityScreenState it doesn’t recompose my composable view but I know it changes if I Log.d it..
I’ve tried searching but couldn’t find what I’m doing wrong. Also found out it only recomposes if I nullify the activity inside ActivityState…
Am I doing something wrong or is this a bug? I’m new to android & jetpack compose so I have no idea what to do here…
9