Replace LiveData with StateFlow

This commit is contained in:
Koitharu
2023-05-27 12:25:49 +03:00
parent 47f346b42c
commit 5a0c54e00f
147 changed files with 1047 additions and 1039 deletions

View File

@@ -14,7 +14,7 @@ import kotlinx.coroutines.runBlocking
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.parser.MangaIntent
import org.koitharu.kotatsu.core.util.ext.getDrawableOrThrow
import org.koitharu.kotatsu.history.domain.HistoryRepository
import org.koitharu.kotatsu.history.data.HistoryRepository
import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.util.replaceWith

View File

@@ -4,7 +4,7 @@ import android.content.Intent
import android.widget.RemoteViewsService
import coil.ImageLoader
import dagger.hilt.android.AndroidEntryPoint
import org.koitharu.kotatsu.history.domain.HistoryRepository
import org.koitharu.kotatsu.history.data.HistoryRepository
import javax.inject.Inject
@AndroidEntryPoint

View File

@@ -17,6 +17,8 @@ import org.koitharu.kotatsu.core.exceptions.resolve.SnackbarErrorObserver
import org.koitharu.kotatsu.core.prefs.AppWidgetConfig
import org.koitharu.kotatsu.core.ui.BaseActivity
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
import org.koitharu.kotatsu.core.util.ext.observe
import org.koitharu.kotatsu.core.util.ext.observeEvent
import org.koitharu.kotatsu.databinding.ActivityCategoriesBinding
import org.koitharu.kotatsu.widget.shelf.adapter.CategorySelectAdapter
import org.koitharu.kotatsu.widget.shelf.model.CategoryItem
@@ -57,7 +59,7 @@ class ShelfConfigActivity :
viewModel.checkedId = config.categoryId
viewModel.content.observe(this, this::onContentChanged)
viewModel.onError.observe(this, SnackbarErrorObserver(viewBinding.recyclerView, null))
viewModel.onError.observeEvent(this, SnackbarErrorObserver(viewBinding.recyclerView, null))
}
override fun onClick(v: View) {

View File

@@ -1,13 +1,15 @@
package org.koitharu.kotatsu.widget.shelf
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.plus
import org.koitharu.kotatsu.core.ui.BaseViewModel
import org.koitharu.kotatsu.core.util.asFlowLiveData
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
import org.koitharu.kotatsu.widget.shelf.model.CategoryItem
import javax.inject.Inject
@@ -19,7 +21,7 @@ class ShelfConfigViewModel @Inject constructor(
private val selectedCategoryId = MutableStateFlow(0L)
val content: LiveData<List<CategoryItem>> = combine(
val content: StateFlow<List<CategoryItem>> = combine(
favouritesRepository.observeCategories(),
selectedCategoryId,
) { categories, selectedId ->
@@ -29,7 +31,11 @@ class ShelfConfigViewModel @Inject constructor(
CategoryItem(it.id, it.title, selectedId == it.id)
}
list
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, emptyList())
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, emptyList())
var checkedId: Long by selectedCategoryId::value
var checkedId: Long
get() = selectedCategoryId.value
set(value) {
selectedCategoryId.value = value
}
}