Android LiveData in Transformation map is null - android

I am facing and issue with Android LiveData and Transformation map. I am gonna explain the case:
I have a SingleLiveEvent and LiveData as follows (one for all items and another one for items to display in screen):
val documents: SingleLiveEvent<List<DocumentData>> = SingleLiveEvent()
val itemsToDisplay: LiveData<List<DocumentData>>
get() {
return Transformations.map(documents) { documents ->
return#map documents.filter { showOptionals || it.isMandatory }
}
}
In Fragment, after observing itemsToDisplay, if I am trying to get the value of itemsToDisplay LiveData (itemsToDisplay.value) is always null
itemsToDisplay.observe(this, Observer {
// Inside this method I need to get a calculated property from VM which uses
```itemsToDisplay.value``` and I would like to reuse across the app
loadData(it)
})
// View Model
val hasDocWithSign: Boolean
get() {
return **itemsToDisplay.value**?.any { it.isSignable } ?: false
}
Does anyone know if a LiveData does not hold the value if it is calculated using Transformation.map or it could be a potential bug?

When you call itemsToDisplay you get a new empty LiveData instance, because you declared it as a getter without a backing field.
val itemsToDisplay: LiveData<List<DocumentData>>
= Transformations.map(documents) { documents ->
documents.filter { showOptionals || it.isMandatory }
}
https://kotlinlang.org/docs/reference/properties.html#backing-fields

Related

How to retrieve values from Room, when creating the ViewModel instance

I have MVVM fragment with this simple requirement.
The fragment have a EntryText field, and I want to populate with a value calculated in the view model,
when the viewmodel is created I need to call a Room request and get a value, then that value should be present to the user, and the user can change that value.
Also I'm soring that value in the State Handle
var parcelaDesde = state.get<String>("parcelaDesde")?:"0"
set(value) {
field = value
state.set("parcelaDesde", value)
}
I resolved with:
*creating a public methos in viewmodel that retrieve info from Room and update local member field, also update a MutableLiveData that is observed in Fragment
fun updateParcelaDesde(default: Int) {
val num = parcelaDesde.toIntOrNull()
num?.let {
if (it == default) {
viewModelScope.launch(Dispatchers.IO) {
parcelaDesde = filialcruzaRepository.getMinNroParcela(filial.value?.id!!)?.toString()?:"0"
Log.d(TAG, "updateParcelaDesde: $parcelaDesde")
isLoadingDesde.postValue(false)
}
}
}
}
on the Fragment, I just observe the liveData and update the UI when te loading is completed.
viewModel.isLoadingDesde.observe(viewLifecycleOwner) {
binding.etParcelaDesde.setText( viewModel.parcelaDesde.trim() )
}
Is this the correct way ?
How to do this if I want to us dataBinding?
Best Regards

How to pass different value when switchMap is not executed? Android + Kotlin

I have a huge understanding problem here, I have a ecommerce app and I cannot properly calculate value of users cart.
The problem is, my solution works well to the point but I have an issue when there are no products in the cart. Obviously LiveData observer or switchMap will not get executed when it's value is empty.
It seems like something trivial, only thing I want to do here is handle the situation when user have no products in the cart. Is the livedata and switchMap a wrong approach here?
I get userCart from the repo -> I calculate its value in the viewModel and expose it to the view with dataBinding.
#HiltViewModel
class CartFragmentViewModel
#Inject
constructor(
private val repository: ProductRepository,
private val userRepository: UserRepository,
private val priceFormatter: PriceFormatter
) : ViewModel() {
private val user = userRepository.currentUser
val userCart = user.switchMap {
repository.getProductsFromCart(it.cart)
}
val cartValue = userCart.switchMap {
calculateCartValue(it)
}
private fun calculateCartValue(list: List<Product>?): LiveData<String> {
val cartVal = MutableLiveData<String>()
var cartValue = 0L
list?.let { prods ->
prods.forEach {
cartValue += it.price
}
cartVal.postValue(priceFormatter.formatPrice(cartValue))
} ?: cartVal.postValue(priceFormatter.formatPrice(0))
return cartVal
}
fun removeFromCart(product: Product) {
userRepository.removeFromCart(product)
getUserData()
}
private fun getUserData() {
userRepository.getUserData()
}
init {
getUserData()
}
}
Default value is to solve the "initial" empty cart.
Now if you need to trigger it when there's no data... (aka: after you remove items and the list is now empty), I'd use a sealed class to wrap the actual value.
(names and code are pseudo-code, so please don't copy-paste)
Something like this:
Your Repository should expose the cart, user, etc. wrapped in a sealed class:
sealed class UserCartState {
object Empty : UserCartState()
data class HasItems(items: List<things>)
object Error(t: Throwable) :UserCartState() //hypotetical state to signal problems
}
In your CartFragmentViewModel, you observe and use when (for example), to determine what did the repo responded with.
repo.cartState.observe(...) {
when (state) {
is Empty -> //deal with it
is HasItems -> // do what it takes to convert it, calculate it, etc.
is Error -> // handle it
}
}
When the user removes the last item in the cart, your repo should emit Empty.
The VM doesn't care how that happened, it simply reacts to the new state.
The UI cares even less. :)
You get the idea (I hope).
That's how I would look into it.
You can even use a flow of cart items, or the new "FlowState" thingy (see the latest Google I/O 21) to conserve resources when the lifecycle owner is not ready.
I suppose that this part of code creates the problem
list?.let { prods ->
prods.forEach {
cartValue += it.price
}
cartVal.postValue(priceFormatter.formatPrice(cartValue))
} ?: cartVal.postValue(priceFormatter.formatPrice(0))
Probably, list is not null but is empty. Please try this:
if (list.isNullOrEmpty) {
list.forEach {
cartValue += it.price
}
cartVal.postValue(priceFormatter.formatPrice(cartValue))
} else {
cartVal.postValue(priceFormatter.formatPrice(0))
}

Android: get data inside an obsever in mvvm and add it to a list

I'm using the following code to get data from the viewmodel, i want to return the list after getting the data inside the observer but it returns empty
private fun showFirstTodo(): ArrayList<hitsobj> {
var listRecipe: ArrayList<hitsobj> = ArrayList()
var obj: MutableList<hitsobj?>? = null
viewModel.getFirstTodo().observe(this, Observer {
obj = it.hit
for (receta in obj!!) {
listRecipe.add(receta!!)
//Log.e(TAG, listRecipe.toString())
}
})
return listRecipe
}
You are always returning an empty list in showFirstTodo() function, you need to perform whatever you want to do with the List value of the LiveData returned by getFirstTodo() in the Observer object you are registering, the code would be like this:
private fun registerObserver(){
viewModel.getFirstTodo().observe(this, Observer {
val theListOfObjects = it.hit
//todo: here you utilize the list value of the LiveData in your view model whenever this list changes
})
}

How can I perform Transformations on MutableLiveData?

The docs show how you can perform Transformations on a LiveData object? How can I perform a transformation like map() and switchMap() on a MutableLiveData object instead?
MutableLiveData is just a subclass of LiveData. Any API that accepts a LiveData will also accept a MutableLiveData, and it will still behave the way you expect.
Exactly the same way:
fun viewModelFun() = Transformations.map(mutableLiveData) {
//do somethinf with it
}
Perhaps your problem is you dont know how does yor mutable live data fit on this.
In the recent update mutable live data can start with a default value
private val form = MutableLiveData(Form.emptyForm())
That should trigger the transformation as soon as an observer is attached, because it will have a value to dispatch.
Of maybe you need to trigger it once the observer is attached
fun viewModelFun(selection: String) = liveData {
mutableLiveData.value = selection.toUpperCase
val source = Transformations.map(mutableLiveData) {
//do somethinf with it
}
emitSource(source)
}
And if you want the switch map is usually like this:
private val name = MutableLiveData<String>()
fun observeNames() = Transformations.switchMap(name) {
dbLiveData.search(name) //a list with the names
}
fun queryName(likeName: String) {
name.value = likeName
}
And in the view you would set a listener to the edit text of the search
searchEt.doAfterTextChange {...
viewModel.queryName(text)
}

How to chain transformations in Android when using live data?

Given the following setup:
I have 2 repositories: Repository A and Repository B both of them return live data.
I have a ViewModel that uses both of these repositories.
I want to extract something from Repository A and depending on the result I want to grab something from Repository B and then transform the result before returning to UI.
For this I have been looking at the LiveData Transformation classes. The examples show a single transformation of the result however I want something along the lines of chaining two transformations. How can I accomplish this?
I have tried setting something up like this but get a type mismatch on the second transformation block:
internal val launchStatus: LiveData<String> = Transformations
.map(respositoryA.getData(), { data ->
if (data.isValid){
"stringA"
} else {
//This gives a type mismatch for the entire block
Transformations.map(repositoryB.getData(), {
result -> result.toString()
})
}
})
(Also please let me know if there is an alternate/recommended approach for grabbing something for chaining these call i.e. grab something from A and then grab something from B depending on result of A and so on)
Your lambda sometimes returns the String "stringA", and sometimes returns the LiveData<String> given by:
Transformations.map(repositoryB.getData(), {
result -> result.toString()
})
This means that your lambda doesn't make sense - it returns different things in different branches.
As others have mentioned, you could write your own MediatorLiveData instead of using the one given by Transformations. However, I think it's easier to do the following:
internal val launchStatus: LiveData<String> = Transformations
.switchMap(respositoryA.getData(), { data ->
if (data.isValid) {
MutableLiveData().apply { setValue("stringA") }
} else {
Transformations.map(repositoryB.getData(), {
result -> result.toString()
})
}
})
All I've done is made the first code branch also return a LiveData<String>, so now your lambda makes sense - it's a (String) -> LiveData<String>. I had to make one other change: use switchMap instead of map. This is because map takes a lambda (X) -> Y, but switchMap takes a lambda (X) -> LiveData<Y>.
I used MediatorLiveData to solve this problem.
MediatorLiveData can observer other LiveData objects and react to them.
Instead of observing either of the repositories. I created myData (instance of MediatorLiveData) in my ViewModel class and have my view observe this object. Then I add Repository A as the initial source and observe that and only add Repository B if the result of A requires it. This allows me to keep the transformations associated with the live data of each of the repo and still process each result in the correct order. See below for implementation:
internal val myData: MediatorLiveData<String> = MediatorLiveData()
private val repoA: LiveData<String> = Transformations.map(
respositoryA.getData(), { data ->
if (data.isValid) "stringA" else ""
})
private val repoB: LiveData<String> = Transformations.map(
repositoryB.getData(), { data -> "stringB"
})
fun start() {
myData.addSource(repoA, {
if (it == "stringA") {
myData.value = it
} else {
myData.addSource(repoB, {
myData.value = it
})
}
})
}
Note: The solution does not cover the case where repoB might be added multiple times but it should be simple enough to handle.
I would try using switchMap instead of map:
Similar to map(), applies a function to the value stored in the LiveData object and unwraps and dispatches the result downstream. The function passed to switchMap() must return a LiveData object.
You can nest transformations.
val finalLiveData = Transformations.switchMap(liveData1){
val search = it
Transformations.switchMap(liveData2) {
db(context).dao().all(search, it)
}
}
You can transform the data by using switchmap. Here's an example documentation.

Categories

Resources