ElawsDev
03/06/2024, 10:24 AMElawsDev
03/06/2024, 10:35 AMkotlin
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 :
kotlin
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 (apparently it has something to do with ViewModel outliving activities and context may be outdated).
Could you please help me learn the proper way to achieve this trivial usecase ?
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.Akram Raza
03/06/2024, 12:55 PMElawsDev
03/06/2024, 1:27 PMAkram Raza
03/06/2024, 1:59 PMElawsDev
03/06/2024, 3:20 PMAkram Raza
03/06/2024, 4:15 PMAkram Raza
03/06/2024, 4:16 PMElawsDev
03/06/2024, 4:31 PMfor a in listA
for b in listB
listC.add("$a: $b")
I only need a single instance of MyData class : it is the class which holds all the data of my app.ElawsDev
03/06/2024, 4:51 PMAkram Raza
03/06/2024, 5:03 PMElawsDev
03/06/2024, 5:04 PMChrimaeon
03/06/2024, 5:19 PMElawsDev
03/06/2024, 5:21 PMChrimaeon
03/06/2024, 5:21 PMclass ScreenViewModel : ViewModel() {
var listC: List<Int> by muableStateOf(emptyList())
private set
init () {
listC = <calucalte list content>
}
}
ElawsDev
03/06/2024, 5:22 PMChrimaeon
03/06/2024, 5:22 PMElawsDev
03/06/2024, 5:25 PMAkram Raza
03/07/2024, 5:13 AMElawsDev
03/07/2024, 10:26 AMval listA = resources.getStringArray...
val listB = ...
val myViewModel = MyViewModel(listA, listB)
How does this compare to your solution ?
2. So I understand that I'm passing my ListA and ListB to the viewModel. But then my viewModel will be dependant on Context, since ListA and ListB depend on Context ? If user changes language, my view model will have outdated listA and listB values, right ? So is it really better practice than using resources IDs in the view model ?
3. Also, isn't data flowing from UI to viewModel in opposition to UDF principle ?
Thank you for your helpElawsDev
03/07/2024, 10:42 AMAkram Raza
03/07/2024, 11:00 AMElawsDev
03/07/2024, 11:11 AMAkram Raza
03/07/2024, 11:15 AMElawsDev
03/07/2024, 10:25 PMAkram Raza
03/08/2024, 6:49 AMElawsDev
03/08/2024, 10:02 AM