Search code examples
androidkotlin-stateflow

StateFlow.collect callback not firing when my adapter changes


I have this fragment that has a recycler view with it's adapter, and I have this methods

private fun observeItemsStateAndSetButtonStyle(type: String) {
    when(type) {
        TaxonomyType.PROVINCE.value -> checkIfEmptyAndSetButtonState(viewModel.selectedProvinces)
        TaxonomyType.AREA.value -> checkIfEmptyAndSetButtonState(viewModel.selectedAreas)
        TaxonomyType.SECTOR.value -> checkIfEmptyAndSetButtonState(viewModel.selectedSectors)
    }
}

private fun checkIfEmptyAndSetButtonState(taxonomies: StateFlow<MutableSet<Taxonomy>>) {
    lifecycleScope.launch {
        taxonomies.collect {
            if (it.isEmpty()) {
                setButtonEnabledState(false)
            } else {
                setButtonEnabledState(true)
            }
        }
    }
}

these viewmodel fields are declared as MutableStateFlow<MutableSet>

and in the adapter when I click inside the holder i add or remove elements from this mutableSet calling this variable.value.remove or variable.value.add methods.

the problem is that when I add a value or remove it it's not firing the collect callback

holder.binding.root.onClick {
        if (isChecked(holder, taxonomyItem.type)) {
            isChecked.value = false
            holder.binding.rowCountryCheck.hide()
            when(taxonomyItem.type) {
                TaxonomyType.PROVINCE.value -> {
                    val t = viewModel.selectedProvinces.value.first { it.id == taxonomyItem.id }
                    viewModel.selectedProvinces.value.remove(t)
                }
                TaxonomyType.AREA.value -> {
                    val t = viewModel.selectedAreas.value.first { it.id == taxonomyItem.id }
                    viewModel.selectedAreas.value.remove(t)
                }
                TaxonomyType.SECTOR.value -> {
                    val t = viewModel.selectedSectors.value.first { it.id == taxonomyItem.id }
                    viewModel.selectedSectors.value.remove(t)
                }
            }
            setLabelStyle(holder.binding.rowCountryTextTitle, R.attr.typography_text_list_item_16_unselected)
            notifyItemChanged(index)
        } else {
            isChecked.value = true
            holder.binding.rowCountryCheck.show()
            when(taxonomyItem.type) {
                TaxonomyType.PROVINCE.value -> viewModel.selectedProvinces.value.add(taxonomyItem)
                TaxonomyType.AREA.value -> viewModel.selectedAreas.value.add(taxonomyItem)
                TaxonomyType.SECTOR.value -> viewModel.selectedSectors.value.add(taxonomyItem)
            }
            setLabelStyle(holder.binding.rowCountryTextTitle, R.attr.typography_text_list_item_16_selected)
            notifyItemChanged(index)
        }
    }

Solution

  • I finally got it changing to MutableLiveData library, that has the possibility to set the value property, unless the MutableStateFlow class