miqbaldc
04/02/2021, 5:50 AMdata class State(title: String, description: String)
fun State.toSessionExpired(): State {
val state = this
return with(state) {
// when token expired
if (httpCode == 401) {
copy(
title = getString(R.string.error_expired_title),
description = getString(R.string.error_expired_description),
)
} else this
}
}
arekolek
04/02/2021, 8:22 AMdata class State(val title: String, val description: String)
fun State.toSessionExpired(): State {
return takeUnless { httpCode == 401 }
?: State(
title = getString(R.string.error_expired_title),
description = getString(R.string.error_expired_description),
)
}
miqbaldc
04/02/2021, 4:54 PMelizarov
04/02/2021, 8:48 PMtherealbluepandabear
04/06/2021, 1:49 AMmiqbaldc
04/06/2021, 11:46 AMtherealbluepandabear
04/07/2021, 2:36 AMarekolek
04/07/2021, 6:42 AMif
with `takeUnless`:
• val state = this
followed by with (state)
was just redundant
• copy
with all arguments is just equivalent to calling the constructor, so why not do that, after all you don't need anything from the original, so it's not really a copy
Elvis operators - in my opinion - make things hard to understand in certain scenariosthat's probably true, I'm surprised you think this is one of those scenarios though
miqbaldc
04/07/2021, 8:36 AMdata class OnScreenState(
@DrawableRes
val logo: Int = R.drawable.ic_illustration_astronaut,
val title: String?,
val description: String?,
val buttonOkText: String?,
val buttonNoText: String?,
)
private fun OnScreenState.withDefault(context: Context): OnScreenState =
with(this) {
takeUnless { it.description == TOKEN_EXPIRED }
?: copy(
title = context.getString(R.string.error_expired_title),
description = context.getString(R.string.error_expired_description),
)
}.copy(buttonOkText = buttonOkText ?: context.getString(R.string.all_ok_understand))
arekolek
04/07/2021, 9:03 AMprivate fun OnScreenState.withDefault(context: Context): OnScreenState =
copy(
title = if (description != TOKEN_EXPIRED) title else context.getString(R.string.error_expired_title),
description = if (description != TOKEN_EXPIRED) description else context.getString(R.string.error_expired_description),
buttonOkText = buttonOkText ?: context.getString(R.string.all_ok_understand),
)
And I'd say @therealbluepandabear has a point then, because I'd prefer if-else
if you needed the two copy
calls, so you can avoid the with(this)
(which is used instead of parentheses I suppose):
private fun OnScreenState.withDefault(context: Context): OnScreenState =
if (it.description == TOKEN_EXPIRED) {
copy(
title = context.getString(R.string.error_expired_title),
description = context.getString(R.string.error_expired_description),
)
} else {
this
}.copy(buttonOkText = buttonOkText ?: context.getString(R.string.all_ok_understand))
miqbaldc
04/08/2021, 4:38 AMprivate fun OnScreenState.withDefault(context: Context): OnScreenState =
copy(
title = if (description != TOKEN_EXPIRED) title else context.getString(R.string.error_expired_title),
description = if (description != TOKEN_EXPIRED) description else context.getString(R.string.error_expired_description),
buttonOkText = buttonOkText ?: context.getString(R.string.all_ok_understand),
)This is more readable than our approach, thanks @arekolek @therealbluepandabear :kotlin-intensifies: IDK why previously we go to such length over-complicated things that should be a simple
if-else
😞therealbluepandabear
04/08/2021, 4:43 AM