¿Cuál es la sintaxis cuando se utiliza el número de comparations en cuando instrucción

0

Pregunta

Tengo este código:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            320 -> StatisticsSettings.SMALL_PHONE
            480 -> StatisticsSettings.LARGE_PHONE
            600 -> StatisticsSettings.SMALL_TABLET
            720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }

y me preguntaba si podía hacer de casos de la when declaración con un comparador en lugar de un número entero. Algo como esto:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 320 -> StatisticsSettings.SMALL_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 480 -> StatisticsSettings.LARGE_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 600 -> StatisticsSettings.SMALL_TABLET
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }
kotlin
2021-11-23 23:05:51
1

Mejor respuesta

4

Utilizar rangos:

val statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
  in 0..320 -> StatisticsSettings.SMALL_PHONE
  in 321..480 -> StatisticsSettings.LARGE_PHONE
  in 481..600 -> StatisticsSettings.SMALL_TABLET
  in 601..720 -> StatisticsSettings.LARGE_TABLET
  else -> throw IllegalArgumentException("Cannot compute dp")
}

O usted podría utilizar constantes enum:

enum class StatisticsSettings(val intRange: IntRange) {
  SMALL_PHONE(0..320),
  LARGE_PHONE(321..480),
  SMALL_TABLET(481..600),
  LARGE_TABLET(601..720)
}

val intRange = ScreenHandler.convertPixelsToDp(width, context).toInt()

val statisticsSettings = StatisticsSettings.values().find { intRange in it.intRange }

Esto tiene la ventaja de que los rangos son "obligados" a la enumeración de sí mismo. Si alguna vez cambiar estos valores, usted no tiene que cambiar en el, posiblemente, varias ubicaciones en el código.

Edit: cambiado de filtro para encontrar (gracias a @ArpitShukla, ver comentario abajo)

2021-11-24 08:43:33

Puede reemplazar filter con find. Que tendría más sentido aquí.
Arpit Shukla

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................