-
- All Implemented Interfaces:
-
java.lang.AutoCloseable
public final class ViewModelInstance implements AutoCloseable
A view model instance for data binding which has properties that can be set and observed.
The instance must be bound to a state machine for its values to take effect. This is done by passing it to Rive.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classViewModelInstance.Companion
-
Field Summary
Fields Modifier and Type Field Description private final ViewModelInstanceHandleinstanceHandle
-
Method Summary
Modifier and Type Method Description final ViewModelInstanceHandlegetInstanceHandle()final StringgetViewModelName()Gets the name of the view model that defines this instance. final StringgetName()Gets the editor-assigned name of this view model instance. final Flow<Float>getNumberFlow(String propertyPath)Creates or retrieves from cache a number property, represented as a cold Flow. final Flow<String>getStringFlow(String propertyPath)Creates or retrieves from cache a string property, represented as a cold Flow. final Flow<Boolean>getBooleanFlow(String propertyPath)Creates or retrieves from cache a boolean property, represented as a cold Flow. final Flow<String>getEnumFlow(String propertyPath)Creates or retrieves from cache an enum property, represented as a cold Flow. final Flow<Integer>getColorFlow(String propertyPath)Creates or retrieves from cache a color property, represented as a cold Flow. final Flow<Unit>getTriggerFlow(String propertyPath)Creates or retrieves from cache a trigger property, represented as a cold Flow. final UnitsetNumber(String propertyPath, Float value)Sets a number property on this view model instance. final UnitsetString(String propertyPath, String value)Sets a string property on this view model instance. final UnitsetBoolean(String propertyPath, Boolean value)Sets a boolean property on this view model instance. final UnitsetEnum(String propertyPath, String value)Sets an enum property on this view model instance. final UnitsetColor(String propertyPath, @ColorInt() Integer value)Sets a color property on this view model instance. final UnitfireTrigger(String propertyPath)Fires a trigger on this view model instance. final UnitsetImage(String propertyPath, ImageAsset image)Assigns the given image to the image property on this view model instance, or clears the property if image is null. final UnitsetArtboard(String propertyPath, Artboard artboard)Assigns the given artboard to the bindable artboard property on this view model instance, or clears the property if artboard is null. final UnitsetViewModelInstance(String propertyPath, ViewModelInstance instance)Assigns the given view model instance to the nested view model property on this view model instance. final IntegergetListSize(String propertyPath)final UnitinsertToListAtIndex(String propertyPath, Integer index, ViewModelInstance item)Inserts an item into a list property at the specified index. final UnitappendToList(String propertyPath, ViewModelInstance item)Appends an item to the end of a list property. final UnitremoveFromListAtIndex(String propertyPath, Integer index)Removes an item from a list property at the specified index. final UnitremoveFromList(String propertyPath, ViewModelInstance item)Removes an item from a list property. final UnitswapListItems(String propertyPath, Integer indexA, Integer indexB)Swaps two items in a list property by their indices. -
-
Method Detail
-
getInstanceHandle
final ViewModelInstanceHandle getInstanceHandle()
-
getViewModelName
final String getViewModelName()
Gets the name of the view model that defines this instance.
Unlike getName, which returns the editor-assigned name of this specific instance, this returns the name of its view model definition. Multiple instances can therefore return the same view model name while having different instance names.
-
getName
final String getName()
Gets the editor-assigned name of this view model instance.
This works for all creation sources, including names the caller may not know upfront, such as the name of the instance marked "Default" in the Rive file when created with ViewModelInstanceSource.Default, or the name of a list item obtained with ViewModelInstanceSource.ReferenceListItem.
-
getNumberFlow
final Flow<Float> getNumberFlow(String propertyPath)
Creates or retrieves from cache a number property, represented as a cold Flow.
The flow is subscribed to updates from the Rive worker while it is being collected.
This flow emits every distinct value (up to the backing buffer limit). If you process the flow slowly, consider applying conflate if you only need the latest value to skip intermediate values. Alternatively, if you need to process every value, consider using a buffer operator with an appropriate buffer size to handle bursts.
Collection of the flow may cause an exception:
RuntimeException: If this class has been closed, if the property does not exist on this view model instance, or is is of a different type.
IllegalStateException: If the backing Rive worker has been released.
- Parameters:
propertyPath- The path to the property from this view model instance.
-
getStringFlow
final Flow<String> getStringFlow(String propertyPath)
Creates or retrieves from cache a string property, represented as a cold Flow.
The collection of the flow may cause an exception. See getNumberFlow for details.
- Parameters:
propertyPath- The path to the property from this view model instance.
-
getBooleanFlow
final Flow<Boolean> getBooleanFlow(String propertyPath)
Creates or retrieves from cache a boolean property, represented as a cold Flow.
The collection of the flow may cause an exception. See getNumberFlow for details.
- Parameters:
propertyPath- The path to the property from this view model instance.
-
getEnumFlow
final Flow<String> getEnumFlow(String propertyPath)
Creates or retrieves from cache an enum property, represented as a cold Flow. Enums are represented as strings, and this flow will emit the string value of the enum.
The collection of the flow may cause an exception. See getNumberFlow for details.
- Parameters:
propertyPath- The path to the property from this view model instance.
-
getColorFlow
final Flow<Integer> getColorFlow(String propertyPath)
Creates or retrieves from cache a color property, represented as a cold Flow. Colors are represented as AARRGGBB integers, and this flow will emit the integer value of the color.
The collection of the flow may cause an exception. See getNumberFlow for details.
- Parameters:
propertyPath- The path to the property from this view model instance.
-
getTriggerFlow
final Flow<Unit> getTriggerFlow(String propertyPath)
Creates or retrieves from cache a trigger property, represented as a cold Flow. Triggers emit Unit as the value, which simply indicates that the trigger has been fired.
The collection of the flow may cause an exception. See getNumberFlow for details.
- Parameters:
propertyPath- The path to the trigger property from this view model instance.
-
setNumber
final Unit setNumber(String propertyPath, Float value)
Sets a number property on this view model instance.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the property from this view model instance.value- The value to set the property to.
-
setString
final Unit setString(String propertyPath, String value)
Sets a string property on this view model instance.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the property from this view model instance.value- The value to set the property to.
-
setBoolean
final Unit setBoolean(String propertyPath, Boolean value)
Sets a boolean property on this view model instance.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the property from this view model instance.value- The value to set the property to.
-
setEnum
final Unit setEnum(String propertyPath, String value)
Sets an enum property on this view model instance. Enums are represented as strings.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the property from this view model instance.value- The string value of the enum to set the property to.
-
setColor
final Unit setColor(String propertyPath, @ColorInt() Integer value)
Sets a color property on this view model instance. Colors are represented as AARRGGBB integers.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the property from this view model instance.value- The integer value of the color to set the property to.
-
fireTrigger
final Unit fireTrigger(String propertyPath)
Fires a trigger on this view model instance.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the trigger property from this view model instance.
-
setImage
final Unit setImage(String propertyPath, ImageAsset image)
Assigns the given image to the image property on this view model instance, or clears the property if image is null.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the property from this view model instance.image- The image to assign to the property, or null to clear the property.
-
setArtboard
final Unit setArtboard(String propertyPath, Artboard artboard)
Assigns the given artboard to the bindable artboard property on this view model instance, or clears the property if artboard is null.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the property from this view model instance.artboard- The artboard to assign to the property, or null to clear the property.
-
setViewModelInstance
final Unit setViewModelInstance(String propertyPath, ViewModelInstance instance)
Assigns the given view model instance to the nested view model property on this view model instance.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
Once the nested view model instance is added to the view model property, you do not need to keep your reference to it. The parent view model instance maintains its own native reference to the nested instance.
If you created the view model instance manually (for example via ViewModelInstance.fromFile), you may close it to release your reference once you no longer need that view model instance elsewhere.
If you used rememberViewModelInstance, do not close it manually. It is closed automatically when the Composable leaves composition.
- Parameters:
propertyPath- The path to the view model property from this view model instance.instance- The view model instance to assign to the property.
-
getListSize
final Integer getListSize(String propertyPath)
-
insertToListAtIndex
final Unit insertToListAtIndex(String propertyPath, Integer index, ViewModelInstance item)
Inserts an item into a list property at the specified index.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
Once the item is added to the list, you do not need to hold a reference to its instance. The list will also maintain a reference to the item.
- Parameters:
propertyPath- The path to the list property from this view model instance.index- The index at which to insert the item.item- The view model instance to insert into the list.
-
appendToList
final Unit appendToList(String propertyPath, ViewModelInstance item)
Appends an item to the end of a list property.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
Once the item is added to the list, you do not need to hold a reference to its instance. The list will also maintain a reference to the item.
- Parameters:
propertyPath- The path to the list property from this view model instance.item- The view model instance to append to the list.
-
removeFromListAtIndex
final Unit removeFromListAtIndex(String propertyPath, Integer index)
Removes an item from a list property at the specified index.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the list property from this view model instance.index- The index of the item to remove.
-
removeFromList
final Unit removeFromList(String propertyPath, ViewModelInstance item)
Removes an item from a list property.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the list property from this view model instance.item- The view model instance to remove from the list.
-
swapListItems
final Unit swapListItems(String propertyPath, Integer indexA, Integer indexB)
Swaps two items in a list property by their indices.
ℹ️ Changes to bound Rive elements will not be reflected until the next state machine advance.
- Parameters:
propertyPath- The path to the list property from this view model instance.indexA- The index of the first item to swap.indexB- The index of the second item to swap.
-
-
-
-