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 :
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.