masteramyx
10/02/2023, 9:59 PMval customerState: MutableState<CustomerData?> = remember { mutableStateOf(Manager.getInstance().customer) }
key(customerState.value?.id) {
// all the compose stuff
}
The manager class is in java if that makes any difference...
Is this incorrect way to use key()
? It appears to only work on the initial composition when the fragment is created and I've verified that observed value is being updated. But the updates to the customer field are not triggering the block inside key()
Zach Klippenstein (he/him) [MOD]
10/02/2023, 10:16 PMmasteramyx
10/02/2023, 10:18 PMZach Klippenstein (he/him) [MOD]
10/02/2023, 10:18 PMkey
at all for thatid
should automatically refresh without key
masteramyx
10/02/2023, 10:21 PMZach Klippenstein (he/him) [MOD]
10/02/2023, 10:22 PMManager
is your singleton, and you’re only reading customer
inside the remember
.masteramyx
10/02/2023, 10:22 PMZach Klippenstein (he/him) [MOD]
10/02/2023, 10:22 PMcustomerState
needs to be a `MutableState`—are you ever updating that property?Manager.customer
backed by a MutableState
?masteramyx
10/02/2023, 10:24 PMZach Klippenstein (he/him) [MOD]
10/02/2023, 10:28 PMcustomerState
a MutableState
won’t magically make that customer
observable or anything. It means you can now manually set it to a new CustomerData
value, and that change will be observable. But you’d need to wire that up manually.mutableStateOf
from java, i believe, but if this is in a large codebase legacy module, you might not want to add the compose dependency, idkManager.customer
property changes?masteramyx
10/02/2023, 10:46 PMcustomerChanged()
which given the old architecture, we could invoke just some refresh()
type method and update the xml.Zach Klippenstein (he/him) [MOD]
10/02/2023, 10:50 PM@Composable fun Customer(manager: Manager)
var customerState: CustomerState? by remember(manager) { mutableStateOf(manager.customer) }
DisposableEffect(manager) {
val listener = fun(newCustomer: CustomerData?) { customerState = it }
manager.addCustomerChangedListener(listener)
onDispose {
manager.removeCustomerChangedListener(listener)
}
}
// …rest of customer view
}
masteramyx
10/02/2023, 11:10 PMkey()
?
DisposableEffect
simple defines a side effect for each new unique value of `key`(argument not function). Which in this case is only 1 value, the singleton. And the side-effect is setting the listener and defining the behavior for `onDispose`; removing the listener.
Inside the side-effect we're able to update that mutable state when the listener is triggered and as a result key()
is able to observe the changes to the mutableState.
This is super helpful!Zach Klippenstein (he/him) [MOD]
10/02/2023, 11:19 PMkey()
function for this at allmanager
as keys to remember
and DisposableEffect
for correctness – in production, the value of that parameter would never change if it’s a singleton, so their lifetimes would match that of the composable.Yessimple defines a side effect for each new unique value of `key`(argument not function). Which in this case is only 1 value, the singleton. And the side-effect is setting the listener and defining the behavior for `onDispose`; removing the listener.DisposableEffect
Inside the side-effect we’re able to update that mutable state when the listener is triggeredYes
as a resultYes, although there’s nothing special about theis able to observe the changes to the mutableStatekey()
key()
function, and anything in the composable that reads the customerState
value should automatically update when it changes.@Composable fun Customer(manager: Manager)
var customerState: CustomerState? by remember(manager) { mutableStateOf(manager.customer) }
DisposableEffect(manager) {
val listener = fun(newCustomer: CustomerData?) { customerState = it }
manager.addCustomerChangedListener(listener)
onDispose {
manager.removeCustomerChangedListener(listener)
}
}
Column {
CustomerName(customerState.name)
CustomerAddress(customerState.address)
CustomerPaymentInfo(customerState.paymentInfo)
…
}
}
and those composables would get the new details whenever the custom state changesmasteramyx
10/02/2023, 11:35 PMYes, although there’s nothing special about theI see, simply using remember & mutableState enables us to update that remembered value and observe the updated value. 👍 Would a more common use offunction, and anything in the composable that reads thekey()
value should automatically update when it changes.customerState
key()
be for example....
If I had some sub-composables inside this larger Customer(manager: Manager
and I want to have finer control over that execution block even though the outer is undergoing re/composition?Zach Klippenstein (he/him) [MOD]
10/03/2023, 12:43 AM