Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

android - Kotlin Elvis Statement with Live Data

We have a common flow pattern in our code base in Android Java, but I am trying to learn Kotlin so I want to try and get the best statement to be used throughout the code. The follow pattern is usually that a user hits a button gets a dialog and we have to build up the data. The line below is what I want to try to make more efficient.

 viewModel.getLiveData().value
                        ?.let {  viewModel.setTarget(viewModel.getLiveData().value!!.copy(targetNumber = result)) }
                        ?: viewModel.setTarget(Target(result, null))

Here is my basic fragment

class DemoFragment : Fragment(R.layout.fragment_demo_layout) {
private val viewModel: DemoViewModel by viewModels()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    btn_toast.setOnClickListener {
        Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
    }

    btn_toast.setOnLongClickListener {
        Toast.makeText(context, "Hello long click", Toast.LENGTH_SHORT).show()
        true
    }

    viewModel.getLiveData().observe(viewLifecycleOwner) { target -> updateUI(target) }

    btnTargetNumber.setOnClickListener {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_text_entry, null)

        AlertDialog.Builder(context)
            .setTitle("Enter Target Number")
            .setView(dialogView)
            .setPositiveButton("OK") { _, _ ->
                run {
                    val result =
                        dialogView.findViewById<EditText>(R.id.targetNumberInput).editableText.toString()
                    viewModel.getLiveData().value
                        ?.let {  viewModel.setTarget(viewModel.getLiveData().value!!.copy(targetNumber = result)) }
                        ?: viewModel.setTarget(Target(result, null))
                }

            }.show()
    }
}

private fun updateUI(target: Target?) {
    btnTargetNumber.text = target?.targetNumber
}

}

Here is the simple view model

class DemoViewModel(private val handle : SavedStateHandle) : ViewModel() {
 private val targetData : MutableLiveData<Target> = MutableLiveData()

fun setTarget (target: Target){
    targetData.value = target
}

fun  getLiveData() : LiveData<Target>
{
    return targetData
}

}

question from:https://stackoverflow.com/questions/65829065/kotlin-elvis-statement-with-live-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It could be simplified to a single call to setTarget and inside the call ?: will do this with simplification.

viewModel.setTarget(viewModel.getLiveData().value?.copy(targetNumber = result)?:Target(result, null))

In above viewModel.getLiveData().value?.copy(targetNumber = result) this will either execute if valuenot null else the part after ?: i.e. Target(result, null). So you can acheive the same result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...