0

So this getLocalizedTitle() function I have for this 3 type of classes, should I have as utils and pass titles to this function or there can be nicer solution with extensions?

data class Carousel(val id: String, val titles: List<Title>) : ContentItem()
 data class HeroBanner(val titles: List<Title>,
                        val descriptions: List<Description>,
                        val images: List<Image>, val id: String) : ContentItem() 
}


data class MenuItem(val type: String, val titles: List<Title>, val actionType: String, val pageComponents: List<PageComponent>) :



    fun getLocalizedTitle(locale: String): Title? {
      val titles = titles.filter { it.locale == locale }
      return if (titles.isNotEmpty()) {
        titles[0]
      } else {
        null
      }
    }

Artur A
  • 257
  • 3
  • 20

1 Answers1

1

You could create an extension function:

fun List<Title>.localized(locale: String): Title? =
     firstOrNull { it.locale == locale }
Francesc
  • 25,014
  • 10
  • 66
  • 84