在函数内处理 LiveData 和 Observable 到 return 发出的值

Handle LiveData and Observable to return inside a function a value emmited

我开始学习 MVVM 架构和 Kotlin 反应式编程。 如何使用 kotlin LiveData, Coroutines and/or ObservableoptionSelected() 函数中执行 return 仅在用户单击 par1 或 par2 按钮后 return 内容到 [=14] 的第二个=]函数?

class PlaceholderFragment : Fragment()  {
    var par1:Button? = null
    var par2:Button? = null
    var parTextLive: LiveData<String> = MutableLiveData<String>()

    var arr = arrayOf("homework", "chores", "shopping")
    var i = -1
    var j: Int = 0
    var tmp: String = ""
    var clicked: Boolean = false
    var parText: String = ""
    var len: Int = arr.size

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_main, container, false)
        par1 = root!!.findViewById(R.id.par1) as Button
        par2 = root!!.findViewById(R.id.par2) as Button

        lifecycleScope.launch {
            insertionSort()
        }
        par1!!.setOnClickListener {
            lifecycleScope.launch {

                //how to get in LiveData the String value?

                //parTextLive = (par1!!.text.toString())
                parText = (par1!!.text.toString())
            }
        }
        par2!!.setOnClickListener {
            lifecycleScope.launch {

                //how to get in LiveData the String value?

                //parTextLive = (par2!!.text.toString())
                parText = (par2!!.text.toString())
            }
        }
        return root
    }

    suspend fun optionSelected(str1: String, str2: String): String {
        return withContext(Dispatchers.Main) {
            println("opções setadas: $str1 or $str2")
            par1!!.text = str1
            par2!!.text = str2

            // I setted the buttons pair1 and pair2 with texts of str1 and str2. When the user click in one of them, I want to return the text of clicked button 

            delay(5000) //substitute the delay by the user click event

            return@withContext str1  //returning the variable parTextLive with the refreshed value
        }
    }

    suspend fun insertionSort(): Array<String> {
        return withContext(Dispatchers.Default) {
            while(len-- != 0) {
                tmp = arr[++i];
                j = i
                while (j-- != 0 && (optionSelected(arr[j], tmp) == arr[j])) {
                    arr[j + 1] = arr[j];
                }
                arr[j + 1] = tmp
            }
            return@withContext arr.apply { reverse() }
        }
    }
}

我使用 parTextLive.value 为 MutableLiveData 赋值 为了等待用户点击接收到 LiveData 值,我使用了 parTextLive.asFlow().first(),它一直等到 parTextLive 收到一些值

class PlaceholderFragment : Fragment()  {
    var par1:Button? = null
    var par2:Button? = null
    var parTextLive: LiveData<String> = MutableLiveData<String>()

    var arr = arrayOf("homework", "chores", "shopping")

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_main, container, false)
        par1 = root!!.findViewById(R.id.par1) as Button
        par2 = root!!.findViewById(R.id.par2) as Button

        lifecycleScope.launch {
            insertionSort(arr)
        }
        par1!!.setOnClickListener {
            lifecycleScope.launch {    
                parTextLive.value = (par1!!.text.toString())
            }
        }
        par2!!.setOnClickListener {
            lifecycleScope.launch {    
                parTextLive.value = (par2!!.text.toString())
            }
        }
        return root
    }

    suspend fun optionSelected(str1: String, str2: String): String {
        return withContext(Dispatchers.Main) {
            println("opções setadas: $str1 or $str2")
            par1!!.text = str1
            par2!!.text = str2

            val parText = (parTextLive.asFlow().first())
            parTextLive = MutableLiveData<String>()
            return@withContext parText
        }
    }

    suspend fun insertionSort(): Array<String> {
        return withContext(Dispatchers.Default) {
            while(len-- != 0) {
                tmp = arr[++i];
                j = i
                while (j-- != 0 && (optionSelected(arr[j], tmp) == arr[j])) {
                    arr[j + 1] = arr[j];
                }
                arr[j + 1] = tmp
            }
            return@withContext arr.apply { reverse() }
        }
    }
}