For a while, I want to convert the entered currencies to each other and show them on the other screen, but when I check the view model with println, the result I can see is NaN when I make viewmodel.result in the ui. What is the reason for this and how can I solve it?
my ui
If the user presses oncofirm on the button, the operations in the view model are performed.
if (viewModel.isDialogShown) {
AlertDialog(
onDismiss = {
viewModel.onDismissClick()
},
onConfirm = {
viewModel.getConversionRateByCurrency()
viewModel.onDismissClick()
//viewModel.calculate()
println(viewModel.resultState)
With println(viewModel.resultState)
comes 0.0
but when I press the button for the second time and say confirm, then the correct result comes.
my view model
@HiltViewModel
class ExchangeMainViewModel @Inject constructor(
private val exchangeInsertUseCase: InsertExchangeUseCase,
private val exchangeGetAllUseCase: GetAllExchangeUseCase,
private val getConversionRateByCurrencyUseCase: GetConversionRateByCurrencyUseCase
) : ViewModel() {
var isDialogShown by mutableStateOf(false)
private set
var dropDownMenuItem1 by mutableStateOf("")
var dropDownMenuItem2 by mutableStateOf("")
var outLineTxtFieldValue by mutableStateOf(TextFieldValue(""))
var firstConversionRate by mutableStateOf(0.0)
var secondConversionRate by mutableStateOf(0.0)
var resultState by mutableStateOf(0.0)
fun onConfirmClick() {
isDialogShown = true
}
fun onDismissClick() {
isDialogShown = false
}
fun check(context: Context): Boolean {
if (outLineTxtFieldValue.text.isNullOrEmpty() || dropDownMenuItem1 == "" || dropDownMenuItem2 == "") {
Toast.makeText(context, "please select a value and currency ", Toast.LENGTH_LONG).show()
return false
}
return true
}
fun getConversionRateByCurrency() {
viewModelScope.launch {
val firstRate = async {
getConversionRateByCurrencyUseCase.getConversionRateByCurrency(dropDownMenuItem1)
}
val secondRate = async {
getConversionRateByCurrencyUseCase.getConversionRateByCurrency(dropDownMenuItem2)
}
firstConversionRate = firstRate.await()
secondConversionRate = secondRate.await()
delay(200L)
val result = async {
calculate()
}
resultState = result.await()
}
}
private fun calculate(): Double {
if (!firstConversionRate.equals(0.0) && !secondConversionRate.equals(0.0)) {
val amount = outLineTxtFieldValue.text.toInt()
val resultOfCalculate = (amount / firstConversionRate) * secondConversionRate
return resultOfCalculate
}
return 1.1
}
}
I can see the value in the view model but not the ui. Also, I do a lot of checking with if and 0.0 because I couldn't get out of it, so I followed a method like this because I couldn't solve the problem. Anyone have a better idea?
CodePudding user response:
It looks like the issue is that you are not updating the resultState
value until after the getConversionRateByCurrency
function has completed. Because of this, when you try to display the resultState
in your UI, it has not yet been updated and so it displays NaN
.
One way to fix this would be to update the resultState
value immediately after calling the getConversionRateByCurrency
function, like this:
fun onConfirmClick() {
isDialogShown = true
getConversionRateByCurrency()
resultState = calculate()
}
This will ensure that the resultState
value is updated before it is displayed in the UI. You can then remove the delay
and async
calls in the getConversionRateByCurrency
function, as they are no longer needed.
Additionally, you may want to update your calculate
function to handle the case where the conversion rates are not yet available (i.e., if they are still 0.0). You could do this by returning a default value (e.g., 0.0) if the conversion rates are not yet available, like this:
private fun calculate(): Double {
if (firstConversionRate != 0.0 && secondConversionRate != 0.0) {
val amount = out