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

I’m starting in MVVM architecture and Kotlin reactive programming.
How can I use kotlin LiveData, Coroutines and/or Observable to do in optionSelected() function a return only after the user click in par1 or par2 buttons to return the content to second while of insertionSort() function?

class PlaceholderFragment : Fragment()  {
    var par1:Button? = null
    var par2:Button? = null
    var parTextLive: MutableLiveData<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 {

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

                parTextLive.value = (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 some emitter event of user click

            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() }
        }
    }
}

Perhaps you are looking for something like:

parTextLive.asFlow().first()

This will wait for parTextLive to have a value.

1 Like

@nickallendev I’ve imported kotlinx.coroutines.flow.asFlow in file, but the asFlow function appears as Unresolved reference

I have to import another library?

The extension method is in package androidx.lifecycle in library “androidx.lifecycle:lifecycle-livedata-ktx:2.2.0”

You can check out List of KTX extensions when having trouble finding where extension methods are defined.

1 Like

It works fine now, many thanks @nickallendev