Reputation: 725
I have built an architecture based on Jetpack compose and I don't want to use Flows if possible. The data is in a tree structure; based on Requirements and Selections; which are rendered in a section-header based LazyList; where the Requirement is rendered as a section header and the Selections are list items. To avoid circular references I created another class called Conditions which is used to track the selection states and whether the section meets Requirements and is an valid/invalid state. I had created something like this
abstract class Selection(
val name: String,
val condition: Condition,
val selectionMinValue: Int,
val selectionMaxValue: Int
) : ComposeRender, Validatable by condition, Parcelable {
var selectionValidationState by mutableStateOf(ValidationState.CORRECT)
var selectionHeaderListPosition: Int = 0
@Composable
abstract override fun render()
override fun check(): ValidationState {
return if (condition.currentQuantity in selectionMinValue..selectionMaxValue) {
ValidationState.CORRECT
} else if (condition.currentQuantity < selectionMinValue) {
ValidationState.NOT_ENOUGH
} else {
ValidationState.TOO_MANY
}
}
}
Now I am concerned that this is leaky; and that anything that is being remembered; which is still in a Tree datastructure; should be in the ViewModel scope. My understanding is that this is ok; and that remembering a property that is in the ViewModel scope should not leak the Composable. Is this correct?
Would it be safe if when I created the Selection objects I used a viewModel.condition; where the property is in the viewModel? This viewModel would persist accross all of the screens; as the data is all interelated; as nested and capturing screens need access to data that is on different levels; going all the way through the Tree; for every screen. I don't mind if the whole data Tree is leaked as long as views aren't leaked.
Would the view that is generated by the Composable be leaked by the fact that is remembering a property of the outer class, and that outer class exists in the Tree structure; which persists across all screens? Is there a work around for this issue?
I want to use these ComposeRenders
as arguments for my NavHost
so just passing the values into the @Composable function isn't a great option. I am considering moving the Composable outside of the Selection object; and instead using a Utility class of some kind in the view model with a function like renderSelection
; and when renderSelection
is called it will do something like renderSelection(viewModel.selection);
and then the remember function will be in the @Composable
function scope.
Or I could use a static function for renderSelection
so again no property in the Tree is being referenced in the @Composable
is referenced by an object in the Tree.
Thank you & I hope this question helps other people dealing with the same scenario!
Upvotes: 0
Views: 136