Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Optimize this long nested If statement in Kotlin

Please help me to optimize this long If statement in Kotlin. I want to make it shorter as much as possible.

`if (locationType == 1) {
        if (isPostDayOfWeeks) {
            if (isPostLocation) {
                Log.d("RefreshStatus", "PostLocation: " + isPostLocation)
                settingRepo.setLocationStatusText(true, endTime.toString())
            } else {
                if(isPostNextDayOfWeek()){
                    settingRepo.setLocationStatusText(false, startTime.toString())
                }else{
                    settingRepo.setLocationStatusText(false, null)

                }
                Log.d("RefreshStatus", "PostLocation: $isPostLocation")
            }
        } else {
            settingRepo.setLocationStatusText(false, null)
        }
    } else {
        if (autoLocationSwitch) {
            settingRepo.setLocationStatusText(true, null)
        } else {
            settingRepo.setLocationStatusText(false, null)
        }
    }`

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does this help?

val (X, Y) = when {
    locationType != 1 -> autoLocationSwitch to null
    !isPostDayOfWeeks -> false to null
    isPostLocation -> true to endTime.toString()
    isPostNextDayOfWeek() -> false to startTime.toString()
    else -> false to null
}
settingRepo.setLocationStatusText(X, Y)

Replace X and Y with proper names.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading