I have this in my HomeViewModel
class HomeViewModel(
private val preferenceRepository: PreferenceRepository,
private val currencyApiService: CurrencyApiService,
private val mongoRepository: MongoRepository
) : ScreenModel {
var allCurrencies = mutableStateListOf<CurrencyModel>()
private set
init {
mongoRepository.readCurrencyData().onEach { listOfCurrencies ->
println("HomeViewModel DB Local observed ${listOfCurrencies.getSuccessData()?.count()}")
allCurrencies.clear()
if(listOfCurrencies.getSuccessData()?.isNotEmpty() == true) {
listOfCurrencies.getSuccessData()?.let { currencies ->
allCurrencies.addAll(currencies)
}
}
}.launchIn(screenModelScope)
}
So when items are inserted into MongoDB the code above populates the allCurrencies
However, in my HomeScreenRoot I get the allCurrencies from the HomeViewModel.
object HomeScreenRoot : Screen {
@Composable
override fun Content() {
val homeViewModel = getScreenModel<HomeViewModel>()
val allCurrencies = homeViewModel.allCurrencies
HomeScreen(
onHomeEvents = { event ->
homeViewModel.homeEvents(event)
},
allCurrencies = allCurrencies
)
}
}
But inside my HomeScreen it doesn’t get the currencies and it is always empty. As I am using SnapshotStateList I thought that adding items to it will trigger a recompose when the state changes. So compose know where this is being used.
fun HomeScreen(
allCurrencies: SnapshotStateList<CurrencyModel>,
onHomeEvents: (HomeEvents) -> Unit,
) {
CurrencyPickerDialog(
listOfCurrency = allCurrencies,
currencyType = selectedCurrencyType,
onPositiveClicked = {
isDialogOpened = false
},
onDismiss = {
isDialogOpened = false
}
)