2022-08-31

How to trigger action on UI on adapter and reverse situation from adapter to UI?

Is it possbile to stop the flow after first collecting the data?

When I debug it the first click make the order like:

fromFragment

    private val mutableStateAdapterFlow = MutableStateFlow(-1)


[...]
                vm.updateGoalData(value.id, updatedData) // method to updateMyGoal
                setGoalsAdapter.notifyDataSetChanged()

  • I also tried without notifyDataSetChanged(), beacause of the flow it is triggered anyway.

then in adapter methods in coroutine are triggered multiplte times and it changes the value to +1 and then it shows in UI previous value i.e. daysLeft where 5 then I clicked to add 1 day it goes for 6 value but this coroutine is triggered multiple times and come back to 5.

                addDay.setOnClickListener {
                    onPlusButtonClickedListener(
                        CustomSetGoalsDialogData(
                            item.id,
                            item.goal,
                            item.timeGoal
                        )
                    )
                    mutableAddMinusDayStateFlow.value = item.id
                }

                CoroutineScope(Dispatchers.Main).launch {
                    mutableAddMinusDayStateFlow.collectLatest { it ->
                        idTvItemTimeGoal.text = "$daysLeft days left"
                    }
                mutableAddMinusDayStateFlow.value = -1
            }

IMO the problem is it collectLatest, but I'm not sure what I could use. The weird thing is it works, but after second click it is refreshed. When I click and open any other fragment then come back the data is changed.



No comments:

Post a Comment