MutableList becomes empty after navigation to another screen

73 views Asked by At

I am a new programmer, I am creating an application that simulates the football Euro. I created a mechanism to pass the best teams from each group to the knockout stage. To do this, I have created a variable currentTopTeams - a list to which it writes 3 teams from a given group, and then adds more teams from other groups to this list.

@Composable
fun GroupStageScreen(viewModel: EuroViewModel, navController: NavController) {
    val groups = listOf("A", "B", "C", "D", "E", "F")
    val (matchResult, setMatchResult) = remember { mutableStateOf(MutableList(6) { EuroMatchResult(0, 0) })  }
    val groupData by viewModel.groupData.collectAsState(emptyList())
    val (selectedGroup, setSelectedGroup) = remember { mutableStateOf(groups[0]) }

    var currentTopTeams by remember {
        mutableStateOf(mutableListOf<Team>())
    }
...
              Button(
                    onClick = {
                              val currentIndex = groups.indexOf(selectedGroup)
                        if (currentIndex < groups.size - 1){
                            val nextGroup = groups[currentIndex + 1]
                             val currentGroupTeams = groupData.find { it.group == selectedGroup }?.teams ?: emptyList()
                            val topTeams = currentGroupTeams.sortedByDescending { it.points }.take(3)

                            currentTopTeams.addAll(topTeams)
                            Log.d("GroupStageScreen", "Current Top Teams: $currentTopTeams")
                            viewModel.updateTopTeams(currentTopTeams)

                            setSelectedGroup(nextGroup)
                        } else {
                            navController.navigate(AppScreens.PlayoffScreen.name)
                        }
                    },

As I mentioned I would like to transfer this list of teams from GroupStageScreen to PlayoffScreen so I created a viewmodel:

class EuroViewModel : ViewModel() {

    private val groupService = retrofit.create(ApiService::class.java)

    private val _groupData = MutableStateFlow<List<GroupData>>(emptyList())
    val groupData: Flow<List<GroupData>> = _groupData.asStateFlow()

    private val _topTeams = MutableStateFlow<List<Team>>(emptyList())
    val topTeams: Flow<List<Team>> = _topTeams.asStateFlow()


    private val _currentTopTeams = MutableStateFlow<List<Team>>(emptyList())
    val currentTopTeams: Flow<List<Team>> = _currentTopTeams.asStateFlow()


    init {
        loadGroupData()
    }

    private fun loadGroupData(){
        viewModelScope.launch {
            try {
                val response = groupService.getTeamData()
                _groupData.value = response

                val topTeams = response.flatMap { it.teams.take(3) }
                _topTeams.value = topTeams
                Log.d("EuroVM", "topTeams: $topTeams")


            } catch (e: java.lang.Exception){
                Log.e("EuroViewModel", "${e.message}")
            }
        }
    }

    fun updateTopTeams(topTeams: List<Team>) {
            _currentTopTeams.value = topTeams
            Log.d("EuroViewModel", "CurrentTopTeams: $topTeams")
    }
}

However, although in GroupStageScreen and in EuroViewModel currentTopTeams is filled with the correct data, in PlayoffScreen this list is empty.

PlayoffScreen code:

@Composable
fun PlayoffScreen(viewModel: EuroViewModel) {

    val topTeams by viewModel.topTeams.collectAsState(emptyList())
    Log.d("topTeams", "$topTeams")

    val currentTopTeams by viewModel.currentTopTeams.collectAsState(emptyList())
    Log.d("currentTopTeams", "$currentTopTeams")

What am I doing wrong? Why is the currenTopTeams list in GroupStageScreen and Viewmodel full and already empty in PlayoffScreen? The topTeams variable, on the other hand, is full in PlayoffScreen. In what possible other way could I pass the currentTopTeams list to make it work?

In addition, here is the code for my Navigation and MainActivity: https://pastebin.mozilla.org/mFBpykEf

Below I insert logs from the apllication: Logs

0

There are 0 answers