Skip to content

Instantly share code, notes, and snippets.

@jacobmoncur
Created January 15, 2016 04:59
Show Gist options
  • Save jacobmoncur/210cf03adbc7baf1533b to your computer and use it in GitHub Desktop.
Save jacobmoncur/210cf03adbc7baf1533b to your computer and use it in GitHub Desktop.

When

when(currentButtonState){
    ButtonState.GONE -> setupGoneButton(duration)
    ButtonState.BEGIN -> setupStartButton(duration)
    ButtonState.PROGRESS -> setupProgress()
    ButtonState.SUCCESS -> setupSuccessButton()
}
when(currentButtonState){
    ButtonState.GONE -> setupGoneButton(duration)
    ButtonState.BEGIN -> setupStartButton(duration)
    ButtonState.PROGRESS -> setupProgress()
    ButtonState.SUCCESS -> setupSuccessButton()
}
val iconId = when {
    link.contains("twitter", true) -> R.drawable.twitter
    link.contains("facebook", true) -> R.drawable.facebook
    link.contains("linkedin", true) -> R.drawable.linkedin
    link.contains("pinterest", true) -> R.drawable.pinterest
    link.contains("skype", true) -> R.drawable.skype
    else -> R.drawable.blank
}
when (x) {
  in 1..10 -> print("x is in the range")
  in validNumbers -> print("x is valid")
  !in 10..20 -> print("x is outside the range")
  else -> print("none of the above")
}
fun transform(color: String): Int = when (color) {
    "Red" -> Color.RED
    "Green" -> Color.GREEN
    "Blue" -> Color.BLUE
    else -> Color.TRANSPARENT
}
// Instance type checking
val item = viewModels.get(position)
when(item){
    is HeaderViewModel -> return HeaderViewModel.viewType
    is RequestButtonViewModel -> return RequestButtonViewModel.viewType
    is MoreViewModel -> return MoreViewModel.viewType
    is EmptyViewModel -> return EmptyViewModel.viewType
    is WhosOutItemViewModel -> return WhosOutItemViewModel.viewType
    is FooterViewModel -> return FooterViewModel.viewType
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment