1

I am new to jetpack compose,

I am showing a dataset to Lazycolumn that works fine. When I try to fliter i.e. replace the original dataset with different dataset, my Lazycolumn initially shows it the replaced one but in a flash it goes back to original dataset again.

Here some snippets to what I have done, I suspect my compose logic and could not able to find out

// The main composeable where I am observing the changes and calling ShowList to populate

@Composeable
fun SomeScreen(viewModel : TestviewModel){
    val stateCountryCodeMap = remember { mutableStateOf(mapOf<String?, List<CountryInfo>>()) }
    // observe and retrieve the dataset.
    testViewModel.stateMapCountryInfo.collectAsState().value.let {
        stateCountryCodeMap.value = it
    }

     // Some Test to buttom to load a different data set
     someRandomeButtom.click{
       viewModel. filterCountryList()
       }

     // request to load original data set
     testViewModel.fetchCountryList()

     
     ShowList(
            currentSelected = stateCountryCodeSelectedIndex.value,
            groupedCountryInfo = stateCountryCodeMap.value,
            testViewModel = testViewModel
        )
}

// The ShowList function to display

@Composable
private fun ShowList(
    currentSelected: Pair<String, Int>,
    groupedCountryInfo: Map<String?, List<CountryInfo>>,
    testViewModel: TestViewModel
) {
   // LazyColumn stuff to render itmems from map dataset
}

// and TestviewModel

    val stateMapCountryInfo = MutableStateFlow(mapOf<String?, List<CountryInfo>>())
    val stateSortedCountryInfo = MutableStateFlow(listOf<CountryInfo>())
    fun fetchCountryList() {
               // some IO operation which gives result
                when (val result = getCountryListRepo.invoke()) {
                    is Result.Success -> {
                        val countryInfoResultList = result.data
                        // sort the list by country name and store
                        stateSortedCountryInfo.value  = countryInfoResultList.sortedBy { it.countryName }
                        // save it to map
                        stateMapCountryInfo.value = stateSortedCountryInfo.value.groupBy { it.countryName?.get(Constants.ZERO).toString() }
                   
        }
    }




val stateFilteredCountryInfo = MutableStateFlow(listOf<CountryInfo>())

fun filterCountryList() {
             // some IO operation 
            // filter on the sorted array, // results 2 items -  India, Indonesia
            val filteredList = stateSortedCountryInfo.value.filter {
                it.countryName?.contains("Ind") == true  
            }
            
            // store the filtered result
            stateFilteredCountryInfo.value = filteredList
     
     
           // now assing it to map
            stateMapCountryInfo.value  = stateFilteredCountryInfo.value.groupBy { it.countryName?.get(Constants.ZERO).toString() }

        }
    }
}

Till this point, it is a straight forward display of items in ShowList method.

Now, back to SomeScreenMethod(..),

now If I click on that random button, which gives me a different/filtered list as expected and LazyColumn updates it but then again goes back to original state.

Can you pinpoint where it went wrong?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Vineet
  • 53
  • 5

1 Answers1

1

it seems when recomposition is happening, getting triggered multiple times.

testViewModel.fetchCountryList()

can you try this and check

 LaunchedEffect(Unit) {
        testViewModel.fetchCountryList()
    }
hemen
  • 1,460
  • 2
  • 16
  • 35