click on button using Stateflow works itself without any click - android

I have merged my code from livedata to stateflow to make some actions. Now I'm using StateFlow to navigate to another fragment, the problem is when I click on the button it is not working and when I used livedata it was worked!.
Here's my code of viewmodel
private val _habitsUIState = MutableStateFlow(HabitsMainUIState())
val habitsUIState: StateFlow<HabitsMainUIState> get() = _habitsUIState
private val _addHabitClickedEvent = MutableStateFlow(false)
val addHabitClickedEvent: StateFlow<Boolean> get() = _addHabitClickedEvent
private val _editHabitLongClickedEvent = MutableStateFlow(HabitUIState())
val editHabitLongClickedEvent: StateFlow<HabitUIState> get() = _editHabitLongClickedEvent
override fun onEditHabitLongClicked(habit: HabitUIState): Boolean { _editHabitLongClickedEvent.value = habit return false
}
fun onAddHabitClicked() {
_addHabitClickedEvent.value = true
}
and here is my code in fragment
private fun observeEvents() {
lifecycleScope.launch {
viewModel.apply {
habitsUIState.collectLatest { habitsMainState ->
habitsAdapter.setItems(habitsMainState.habits)
}
addHabitClickedEvent.collectLatest {
navigateToAddHabitDialog()
}
editHabitLongClickedEvent.collectLatest { habitsUIState ->
navigateToHabitEditingDialog(habitsUIState)
}
}
}
}
My button xml "I know it is not needed here but it is ok"
<ImageButton
android:id="#+id/add_habit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="16dp"
android:layout_marginTop="16dp"
android:background="#drawable/circle_button_style"
android:onClick="#{() -> viewModel.onAddHabitClicked()}"
android:padding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_add" />
When the code is in the state above, only the data is loaded, but the rest of the code in the lifecycleScope never work as if it has frozen!
When I tried moving each function into its own lifecycleScope (see the code down), they all worked at the same time, but some of them should work when a certain button is pressed.
private fun observeEvents() {
lifecycleScope.launch {
viewModel.habitsUIState.collectLatest { habitsMainState ->
habitsAdapter.setItems(habitsMainState.habits)
}
}
lifecycleScope.launch {
viewModel.addHabitClickedEvent.collectLatest {
navigateToAddHabitDialog()
}
}
lifecycleScope.launch {
viewModel.editHabitLongClickedEvent.collectLatest { habitsUIState ->
navigateToHabitEditingDialog(habitsUIState)
}
}
}
What problem am I facing and how can I solve it? thank you
I have tried using livedata and it was worked!

Related

Jetpack compose lazy column not recomposing with list

I have a list which is stored inside a Viewmodel via Stateflow.
class FirstSettingViewModel : ViewModel() {
private val _mRoomList = MutableStateFlow<List<InitRoom>>(mutableListOf())
val mRoomList: StateFlow<List<InitRoom>> = _mRoomList
...
I observe the flow via collectAsState(). The LazyColumn consists of Boxes which can be clicked.
val roomList = mViewModel.mRoomList.collectAsState()
Dialog {
...
LazyColumn(...) {
items(roomList.value, key = { room -> room.room_seq}) { room ->
Box(Modifier.clickable {
**mViewModel.selectItem(room)**
}) {...}
}
}
}
When a click event occurs, the viewModel changes the 'isSelected' value via a copied list like this.
fun selectItem(room: InitRoom) = viewModelScope.launch(Dispatchers.IO) {
try {
val cpy = mutableListOf<InitRoom>()
mRoomList.value.forEach {
cpy.add(it.copy())
}
cpy.forEach {
it.isSelected = it.room_seq == room.room_seq
}
_mRoomList.emit(cpy)
} catch (e: Exception) {
ErrorController.showError(e)
}
}
When in an xml based view and a ListAdapter, this code will work well, but in the above compose code, it doesn't seem to recompose the LazyColumn at all. What can I do to re-compose the LazyColumn?
Use a SnapshotStateList instead of an ordinary List
change this,
private val _mRoomList = MutableStateFlow<List<InitRoom>>(mutableListOf())
val mRoomList: StateFlow<List<InitRoom>> = _mRoomList
to this
private val _mRoomList = MutableStateFlow<SnapshotStateList<InitRoom>>(mutableStateListOf())
val mRoomList: StateFlow<SnapshotStateList<InitRoom>> = _mRoomList

StateFlow collect does not change the data correctly

I'm using MVI architecture, coroutine and flow,
when I receive the data from the API, the status changed from LOADING to SUCCESS, and when I collect the stateFlow variable it submit the recyclerView successfully while when I try to hide the loading view (progressBar, lottie) the view freeze for a moment and it does not disappear.
I tried two ways
I tried to use the stateFlow in XML like this: android:visibility="#{viewModel.writersFlow.status == Results.Status.LOADING ? View.VISIBLE : View.GONE}", and of course I put lifecycleOwner = viewLifecycleOwner in onViewCreated function and I passed the viewModel to the XML
change the the visibility programmatically like bellow:
Repository:
override suspend fun getWriters(): Flow<Results<BaseModel<WriterModel>?>> =
resultFlowData(
networkCall = {
remoteDataSource.getResult {
endpoints.getWriters()
}
}
)
ViewModel:
private val _writersFlow: MutableStateFlow<Results<BaseModel<WriterModel>?>> =
MutableStateFlow(start())
val writersFlow: StateFlow<Results<BaseModel<WriterModel>?>>
get() = _writersFlow.asStateFlow()
private fun fetchWriters() {
viewModelScope.launch(Dispatchers.IO) {
writerRepository.getWriters().collect {
"writers: $it".log()
_writersFlow.emit(it)
}
}
}
Fragment: here in fragment you will see the binding.loading.visibility = View.GONE in both cases (SUCCESS, ERROR)
private fun gettingWriters() {
viewLifecycleOwner.lifecycleScope.launch {
viewModel.writersFlow.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED).collect {
when (it.status) {
Results.Status.SUCCESS -> {
writersAdapter.submitList(it.data?.response)
binding.loading.visibility = View.GONE
}
Results.Status.ERROR -> {
"$HOME_FRAGMENT: ${it.status}, ${it.code}, ${it.message}".log()
if (!it.message.isNullOrEmpty()) binding.root.snack(it.message ?: "") {}
binding.loading.visibility = View.GONE
}
Results.Status.LOADING -> {
binding.loading.visibility = View.VISIBLE
}
else -> {}
}
}
}
}
In the first way the loading view does not even appear but in the second way it appears but never disappear.

How to use MutableStateFlow to search item in list

I am learning kotlin flow in android. I want to basically instant search in my list and filter to show in reyclerview. I searched in google and found this amazing medium post. This post is basically search from google. I want to search item in list and show in reyclerview. Can someone guide me how can I start this. I am explanning in more detail
Suppose I have one SearchBox and one Reyclerview which one item abc one, abc two, xyz one, xyz two... etc.
main image when all data is combine
Scenario 1
when I start typing in SearchBox and enter small a or capital A I want to show only two item matching in recyclerview, look like this
Scenario 2
when I enter any wrong text in SearchBox I want to basically show a text message that not found, look like this
Any guidance would be great. Thanks
I am adding my piece of code
ExploreViewModel.kt
class ExploreViewModel(private var list: ArrayList<Category>) : BaseViewModel() {
val filteredTopics = MutableStateFlow<List<opics>>(emptyList())
var topicSelected: TopicsArea? = TopicsArea.ALL
set(value) {
field = value
handleTopicSelection(field ?: TopicsArea.ALL)
}
private fun handleTopicSelection(value: TopicsArea) {
if (value == TopicsArea.ALL) {
filterAllCategories(true)
} else {
filteredTopics.value = list.firstOrNull { it.topics != null && it.title == value.title }
?.topics?.sortedBy { topic -> topic.title }.orEmpty()
}
}
fun filterAllCategories(isAllCategory: Boolean) {
if (isAllCategory && topicSelected == TopicsArea.ALL && !isFirstItemIsAllCategory()) {
list.add(0, code = TopicsArea.ALL.categoryCode))
} else if (isFirstItemIsAllCategory()) {
list.removeAt(0)
}
filteredTopics.value = list.flatMap { it.topics!! }.distinctBy { topic -> topic.title }.sortedBy { topic -> topic.title }
}
private fun isFirstItemIsAllCategory() = list.firstOrNull()?.code == TopicsArea.ALL
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
app:closeIcon="#drawable/ic_cancel"
app:layout_constraintBottom_toTopOf="#+id/exploreScroll"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed" />
<HorizontalScrollView
android:id="#+id/exploreScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchView">
<com.google.android.material.chip.ChipGroup
android:id="#+id/exploreChips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipSpacingHorizontal="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:singleLine="true"
app:singleSelection="true" />
</HorizontalScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/exploreList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:paddingTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/exploreScroll" />
</androidx.constraintlayout.widget.ConstraintLayout>
Category.kt
#Parcelize
data class Category(
val id: String? = null,
val title: String? = null,
val code: String? = null,
val topics: List<Topics>? = null,
) : Parcelable
Topics.kt
#Parcelize
data class Topics(
val id: String? = null,
val title: String? = null
) : Parcelable
Dummy data and coming from server
fun categoriesList() = listOf(
Categories("21", "physical", listOf(Topics("1", "Abc one"), Topics("2", "Abc Two"))),
Categories("2211", "mind", listOf(Topics("1", "xyz one"), Topics("2", "xyz two"))),
Categories("22131", "motorized", listOf(Topics("1", "xyz three"), Topics("2", "xyz four"))),
)
In my view model list is holding above dummy data. And In my recyclerview I am passing the whole object and I am doing flatMap to combine all data into list. Make sure In recyclerview is using Topic and using title property. In Image Abc one, Abc two is holding in Topic. Thanks
After #Tenfour04 suggestion I will go to A2 suggestion because I have already data which converted into flow and passing in my adapter. I am adding my activity code as well.
ExploreActivity.kt
class ExploreActivity : AppCompatActivity() {
private val binding by lazy { ExploreLayoutBinding.inflate(layoutInflater) }
val viewModel by viewModel<ExploreViewModel> {
val list = intent?.getParcelableArrayListExtra(LIST_KEY) ?: emptyList<Category>()
parametersOf(list)
}
var exploreAdapter = ExploreAdapter { topic -> handleNextActivity(topic) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
setupView()
}
fun setupView() {
setupSearchView()
setupFilteredTopic()
setupExploreAdapter()
}
private fun setupFilteredTopic() {
lifecycleScope.launchWhenCreated {
repeatOnLifecycle(Lifecycle.State.CREATED) {
viewModel.filteredTopics.collect { filteredTopicsList ->
exploreAdapter.submitList(filteredTopicsList)
}
}
}
}
fun setupSearchView() {
binding.searchView.apply {
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?) = false
override fun onQueryTextChange(newText: String?): Boolean {
return true
}
})
}
}
fun setupExploreAdapter() {
with(binding.exploreList) {
adapter = exploreAdapter
}
}
}
UPDATE 2
ExploreViewModel.kt
val filteredCategories = query
.debounce(200) // low debounce because we are just filtering local data
.distinctUntilChanged()
.combine(filteredTopics) { queryText, categoriesList ->
val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
return#combine filteredTopics
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
}
I am getting error when I set in adapter
fixed
filteredTopics.value
The tutorial you linked has a Flow produced by the SearchView. If you want to keep the search functionality in your ViewModel, you can put a MutableStateFlow in your ViewModel that will be updated by the SearchView indirectly. You can expose a property for updating the query.
There are two different ways this could be done, depending on whether you (A) already have a complete list of your data that you want to query quickly or (B) you want to query a server or your database every time your query text changes.
And then even (A) can be broken up into: (A1) you have a static plain old List, or (A2) your source List comes from a Flow, such as a returned Room flow that is not based on query parameters.
All code below is in the ViewModel class.
A1:
private val allCategories = categoriesList()
private val query = MutableStateFlow("")
// You should add an OnQueryTextListener on your SearchView that
// sets this property in the ViewModel
var queryText: String
get() = query.value
set(value) { query.value = value }
// This is the flow that should be observed for the updated list that
// can be passed to the RecyclerView.Adapter.
val filteredCategories = query
.debounce(200) // low debounce because we are just filtering local data
.distinctUntilChanged()
.map {
val criteria = it.lowercase()
allCategories.filter { category -> criteria in category.title.lowercase }
}
A2:
In this example I put a simple placeholder flow for the upstream server query. This could be any flow.
private val allCategories = flow {
categoriesList()
}
private val query = MutableStateFlow("")
// You should add an OnQueryTextListener on your SearchView that
// sets this property in the ViewModel
var queryText: String
get() = query.value
set(value) { query.value = value }
// This is the flow that should be observed for the updated list that
// can be passed to the RecyclerView.Adapter.
val filteredCategories = query
.debounce(200) // low debounce because we are just filtering local data
.distinctUntilChanged()
.combine(allCategories) { queryText, categoriesList ->
val criteria = queryText.lowercase()
categoriesList.filter { category -> criteria in category.title.lowercase }
}
B
private val query = MutableStateFlow("")
// You should add an OnQueryTextListener on your SearchView that
// sets this property in the ViewModel
var queryText: String
get() = query.value
set(value) { query.value = value }
// This is the flow that should be observed for the updated list that
// can be passed to the RecyclerView.Adapter.
val filteredCategories = query
.debounce(500) // maybe bigger to avoid too many queries
.distinctUntilChanged()
.map {
val criteria = it.lowercase()
categoriesList(criteria) // up to you to implement this depending on source
}

ternary operator for drawable in layout expression

This is my fragment_setting.xml.
<ToggleButton
android:layout_width="47dp"
android:layout_height="27dp"
android:background="#{settingVm.isNotiOn ? #drawable/btn_on_mid : #drawable/btn_off_mid}"
android:onClick="#{()->settingVm.changeBtnStatus()}"
android:text="#string/on"
android:textOff="on"
android:textOn="on"
android:textSize="11sp"
android:textStyle="bold" />
<ToggleButton
android:layout_width="47dp"
android:layout_height="27dp"
android:background="#{settingVm.isNotiOn ? #drawable/btn_off_mid : #drawable/btn_on_mid}"
android:onClick="#{()-> settingVm.changeBtnStatus()}"
android:text="#string/off"
android:textOff="off"
android:textOn="off"
android:textSize="11sp"
android:textStyle="bold" />
This is my SettingViewModel
class SettingViewModel(handler: SettingHandler) : ViewModel() {
var handler = handler
var isNotiOn: Boolean? = true
var visibility = View.VISIBLE
init {
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
Timber.d("start")
}
}
fun onBackBtnPressed() {
Timber.d("onBackBtnPressed()")
handler.onBackBtnPressed()
}
fun showLogoutDialogue() {
Timber.d("showLogoutDialogue()")
handler.showLogoutDialogue()
}
fun changeBtnStatus(){
Timber.d("changeBtnStatus()")
handler.changeBtnStatus()
}
}
And this is my SettingFragment
...
val spUtil = SharedPreferenceUtil(activity!!)
when (spUtil.isNotificationOn) {
false -> {
binding!!.settingVm!!.isNotiOn = false
}
else -> {
binding!!.settingVm!!.isNotiOn = true
}
}
...
override fun changeBtnStatus() {
// TODO: Set real notification setting.
val spUtil = SharedPreferenceUtil(activity!!)
when (binding!!.settingVm!!.isNotiOn) {
true -> {
binding!!.settingVm!!.isNotiOn = false
spUtil.isNotificationOn = false
}
else -> {
binding!!.settingVm!!.isNotiOn = true
spUtil.isNotificationOn = true
}
}
}
What's the problem??? I am not using two way binding and the ternary operator like #={}. But I reckon I should use two way binding because it is not a constant value. And I have two images correctly.
Someone says I should not use is prefix because it might generate getter and setter. So, I even tried removing it and define has prefix or just NotiOn but didn't work.
Please try this code once ## Your XML should be in the code is look like this
<ToggleButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:button="#{settingVm.isNotiOn ? #drawable/btn_on_mid : #drawable/btn_off_mid}"/>
and the following java code you can reduce and get the result in proper form. please use this code on fragment class.
val spUtil = SharedPreferenceUtil(activity!!) tbutton.setOnCheckedChangeListener { buttonView, isChecked -> spUtil.isNotificationOn = isChecked binding!!.settingVm!!.isNotiOn = isChecked }
========================================================================
You can use a MutableLiveData in order to change the status and observe that inside the fragment to get live update and you can change the drawable values according to this LiveData
ViewModel.kt
class GuestViewModel #Inject constructor(val repo: GuestRepository) : ViewModel() {
val guest = MutableLiveData<Guest>()
val guestLoading = MutableLiveData<Boolean>()
}
Fragment.kt
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final TestViewModel viewModel = ViewModelProviders.of(getActivity()).get(TestViewModel.class);
viewModel.guestLoading.observe(this, it -> {
//Do something
});
}

Turning listeners into kotlin coroutine channels

I have several functions that I want to use to do pipelines with Channels. The main one is globalLayouts, where I create a Channel from the framework listener:
fun View.globalLayouts(): ReceiveChannel<View> =
Channel<View>().apply {
val view = this#globalLayouts
val listener = ViewTreeObserver.OnGlobalLayoutListener {
offer(view)
}
invokeOnClose {
viewTreeObserver.removeOnGlobalLayoutListener(listener)
}
viewTreeObserver.addOnGlobalLayoutListener(listener)
}
#UseExperimental(InternalCoroutinesApi::class)
fun <E> ReceiveChannel<E>.distinctUntilChanged(context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel<E> =
GlobalScope.produce(context, onCompletion = consumes()) {
var last: Any? = Any()
consumeEach {
if (it != last) {
send(it)
last = it
}
}
}
fun View.keyboardVisibility(): ReceiveChannel<KeyboardVisibility> {
val rect = Rect()
return globalLayouts()
.map {
getWindowVisibleDisplayFrame(rect)
when (rect.height()) {
height -> KeyboardVisibility.HIDDEN
else -> KeyboardVisibility.SHOWN
}
}
.distinctUntilChanged()
}
I have a CoroutineScope called alive:
val ControllerLifecycle.alive: CoroutineScope
get() {
val scope = MainScope()
addLifecycleListener(object : Controller.LifecycleListener() {
override fun preDestroyView(controller: Controller, view: View) {
removeLifecycleListener(this)
scope.cancel()
}
})
return scope
}
then I do:
alive.launch {
root.keyboardVisibility().consumeEach {
appbar.setExpanded(it == KeyboardVisibility.HIDDEN)
}
}
This code starts working just fine, but I get
kotlinx.coroutines.JobCancellationException: Job was cancelled; job=JobImpl{Cancelled}#811031f
once my alive scope is destroyed. Right after invokeOnClose is called in globalLayouts. What am I doing wrong and how do I debug this?
Figured it out - the code works fine, but
viewTreeObserver.removeOnGlobalLayoutListener(listener)
is bugged for CoordinatorLayout.

Categories

Resources