d

    dimsuz

    10 months ago
    How do I prevent
    PlotColors
    being recreated on each composable function call here? I can't put this in "remember", because
    MyTheme.colors.someColor
    is a
    Composable
    getter:
    fun MyComposable() {
      val colors = PlotColors(
        positiveColor = MyTheme.colors.indicatorContentGreen,
        negativeColor = MyTheme.colors.indicatorBackgroundRed,
      )
    }
    Oh. I suppose I could do
    val positiveColor = MyTheme.colors.indicatorContentGreen,
      val negativeColor = MyTheme.colors.indicatorContentGreen,
      val colors by remember { PlotColors(positiveColor, negativeColor) }
    but that would get very verbose in presence of many colors
    Csaba Kozák

    Csaba Kozák

    10 months ago
    Seems to be an unnecessary optimisation. You end up creating a lot more objects when using compose anyways. Compose will skip recomposition if your input does not change.
    d

    dimsuz

    10 months ago
    Thought so too, asked just in case I sometime will need something like this in a performance critical path with a lot of updates.
    Csaba Kozák

    Csaba Kozák

    10 months ago
    If you have expensive calculations, you can use
    derivedStateOf