ViewModel and use of context to fetch a 50x50 list of localized strings ressources?

61 Views Asked by At

Please find here a simplified usecase for my Kotlin - Jetpack Compose project :

  • I need to build a list of 50 x 50 strings (let's call it listC). This list is built from 2 lists of 50 strings : listA and listB. Each string is localized, hence it has to be retrieved from string resources.

  • This is why I created a MyData class as follows (simplified code syntax) :

class MyData(val context: Context) {
  var listA : MutableList<String> = mutableListOf()
  var listB : MutableList<String> = mutableListOf()
  var listC : MutableList<String> = mutableListOf()

init{
   initializeData()
}

fun initializeData() {
   listA.apply {
     add(context.resources.getString(R.string.valueA1)
     ...
     add(context.resources.getString(R.string.valueA50)
   }

   listB.apply {
     add(context.resources.getString(R.string.valueB1)
     ...
     add(context.resources.getString(R.string.valueB50)
   }

   for a in listA
     for b in listB
         listC.add("$a: $b")
}
  • listC items have the syntax "$a: $b" for simplification, but the string that will be displayed can be either "$a: $b", "$b: $a", "$a" or "$b".

  • One of these 50 x 50 strings will be shown to user based on business logic defined in ScreenViewModel, therefore data should be retrieved in ScreenViewModel as follows :

class ScreenViewModel : ViewModel() {

   val context = LocalContext.current
   var myData = MyData(context) 

}

Apparently this is very bad practice but I have absolutely no idea why nor how to do better.

Could you please help me learn the proper way to achieve this ?

This is a quite difficult topic to grasp for a beginner : which resource should I study to have a better understanding of this issue ?

Thank you very much.

0

There are 0 best solutions below