Advertisement
Guest User

Untitled

a guest
May 13th, 2025
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.98 KB | None | 0 0
  1. import androidx.compose.foundation.background
  2. import androidx.compose.foundation.layout.Box
  3. import androidx.compose.foundation.layout.size
  4. import androidx.compose.runtime.Composable
  5. import androidx.compose.ui.Modifier
  6. import androidx.compose.ui.graphics.Color
  7. import androidx.compose.ui.tooling.preview.Preview
  8. import androidx.compose.ui.unit.dp
  9.  
  10. enum class MyCustomType {
  11.     Type1, Type2, Type3
  12. }
  13.  
  14. data class ComplexClass(val type: MyCustomType)
  15.  
  16. data class SimpleClass(val color: Color)
  17.  
  18. // need to test all types
  19. object ComplexDummyData {
  20.     val type1Data = ComplexClass(type = MyCustomType.Type1)
  21.     val type2Data = ComplexClass(type = MyCustomType.Type2)
  22.     val type3Data = ComplexClass(type = MyCustomType.Type3)
  23. }
  24.  
  25. // we only need to preview 1 colour value
  26. object SimpleDummyData {
  27.     val data = SimpleClass(color = Color.Yellow)
  28. }
  29.  
  30. @Composable
  31. fun ComplexComposable(modifier: Modifier = Modifier, data: ComplexClass) {
  32.  
  33.     // logic to figure out the background colour
  34.     val backgroundColour = when (data.type) {
  35.         MyCustomType.Type1 -> Color.Blue
  36.         MyCustomType.Type2 -> Color.Green
  37.         MyCustomType.Type3 -> Color.Red
  38.     }
  39.     Box(
  40.         modifier = modifier
  41.             .size(50.dp)
  42.             .background(color = backgroundColour)
  43.     )
  44. }
  45.  
  46. @Composable
  47. fun SimpleComposable(modifier: Modifier = Modifier, data: SimpleClass) {
  48.     Box(
  49.         modifier = modifier
  50.             .size(50.dp)
  51.             .background(color = data.color)
  52.     )
  53. }
  54.  
  55.  
  56. @Preview
  57. @Composable
  58. private fun PreviewComplexType1() {
  59.     ComplexComposable(data = ComplexDummyData.type1Data)
  60. }
  61.  
  62. @Preview
  63. @Composable
  64. private fun PreviewComplexType2() {
  65.     ComplexComposable(data = ComplexDummyData.type2Data)
  66. }
  67.  
  68. @Preview
  69. @Composable
  70. private fun PreviewComplexType3() {
  71.     ComplexComposable(data = ComplexDummyData.type3Data)
  72. }
  73.  
  74.  
  75. @Preview
  76. @Composable
  77. private fun SimpleComposablePreview() {
  78.     SimpleComposable(data = SimpleDummyData.data)
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement