简体   繁体   English

填充并发出 StateFlow 列表

[英]Populate and emit StateFlow List

I want to use StateFlow.我想使用 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.我面临一个问题:首先,我有一个 singleton 包含一个字符串列表,我想要一些“容易”理解的东西,即使它不是现在的目标。 The purpose is to populate and emit the list with strings (it will be a complex object later).目的是用字符串填充和发出列表(稍后它将是一个复杂的 object)。

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?有没有办法在不使用 tempList 的情况下实现这一点?

Thanks you谢谢

If you don't want to take temporary variable in-order to add new item to mutable list you can use plus (+) operator function .如果您不想使用临时变量以将新项目添加到可变列表,您可以使用加号 (+) 运算符 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所以伪代码变成了这样: val newList = oldMutableList + newItem

Similarly you can remove item from list like val newList = oldMutableList - itemToRemove同样,您可以从列表中删除项目,例如val newList = oldMutableList - itemToRemove

Read more about operator function on kotlin collection here !此处阅读有关 kotlin 集合上的运算符 function 的更多信息!

There are 3 stages Im doing this within;我在其中进行了 3 个阶段; the repo, the view model and the compose function.回购,视图 model 和撰写 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.最后在你的 compose 方法中。 any compose that is handed the mutablestate from your viewmodel will react to list changes任何从视图模型传递可变状态的 compose 都会对列表更改做出反应

val chatList = viewModel.easyChatList.value


ChatListContent(items = chatList, action = navToChat)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM