7

I want to use StateFlow. But for now, I don't find any lecture which could help me.

I'm facing an issue : To start, I have a singleton which hold a list of String, I want something "easy" to understand even if it isn't the goal purpose for now. The purpose is to populate and emit the list with strings (it will be a complex object later).

class CustomStateFlow() {
    private val _custom = MutableStateFlow(emptyList<String>())
    val custom: StateFlow<List<String>> = _custom

    fun addString(string: String) {
        val tempList = _custom.value.toMutableList()
        tempList.add(string)
        _custom.value = tempList
}

This seems to work, but I don't like the temp list... without, I can't trigger the "collect" of custom in my fragment.

Is there a way to achieve this without using a tempList ?

Thanks you

beigirad
  • 4,986
  • 2
  • 29
  • 52
JohnDoe
  • 83
  • 1
  • 6
  • 1
    You can try this syntax: `_custom.value = _custom.value.toMutableList() + string` – Jeel Vankhede Nov 25 '20 at 09:28
  • It seems to works! Thanks you! – JohnDoe Nov 25 '20 at 09:32
  • @JeelVankhede, thanks again for your answer, I'm facing a new issue. I this possible to trigger the update with an update of a complex field object? For example: ClassA(var temp: String, val id: String), my custom is now an emptyList of ClassA, and I want to update the temp value of one element Thanks – JohnDoe Nov 25 '20 at 11:28
  • I didn't follow your question correctly – Jeel Vankhede Nov 25 '20 at 11:48
  • @JeelVankhede In my initial question, the model was simple, I use a list of strings. Now, I try with a complex object, so sometimes, the object already exist and I just want to update a field. To do so, I retrieve the first object matching with the desired id and I update the field. But, because I don't add or remove any object in the list, the StateFlow doesn't trigger the update – JohnDoe Nov 25 '20 at 13:25
  • In my opinion, you should get list and then use https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/set.html method to update the element and then update your stateflow by resubmitting the list again. That's the solution I can suggest unless someone else suggests better. – Jeel Vankhede Nov 25 '20 at 15:04

2 Answers2

3

If you don't want to take temporary variable in-order to add new item to mutable list you can use plus (+) operator function. By doing so returns you new list (immutable) with added value that you can use further.

So the pseudo-code becomes something like this: val newList = oldMutableList + newItem

Similarly you can remove item from list like val newList = oldMutableList - itemToRemove

Read more about operator function on kotlin collection here!

Noah
  • 2,718
  • 3
  • 17
  • 23
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
0

There are 3 stages Im doing this within; the repo, the view model and the compose function.

Repository where the data manipulated

    //This object is the list you will edit
private val _easyChatList =  mutableListOf<ChatDescriptionEntity>()
//This object is a wrapper. if you pass it a new object it will call emit
private val _easyChatListHolder = MutableStateFlow(listOf<ChatDescriptionEntity>())
//this object sends out the immutable list
override val easyChatList = _easyChatListHolder.asStateFlow()

//-- add() or remove or sort or shuffle etc --//

   fun sortUpdatedChatLists()
{
    _easyChatList.sort()


    val nl = _easyChatList.toList() // extract a new list to force the emit

    _easyChatListHolder.value = nl


}

viewmodel for your fragment or compose where the list is sent

  var easyChatList: MutableState<List<ChatDescriptionEntity>> = mutableStateOf(listOf())

init {

    repositoryInterface.getChatsList()

    viewModelScope.launch {

        repositoryInterface.easyChatList.collect{
                i -> easyChatList.value = i
        }
    }


}

Lastly in you compose method. any compose that is handed the mutablestate from your viewmodel will react to list changes

val chatList = viewModel.easyChatList.value


ChatListContent(items = chatList, action = navToChat)
cagney
  • 492
  • 3
  • 11
  • another solution that seems better https://stackoverflow.com/questions/69718059/android-jetpack-compose-mutablestatelistof-not-doing-recomposition – cagney Aug 12 '22 at 22:18