|
|
|
@ -7,6 +7,7 @@ import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.cancelAndJoin
|
|
|
|
import kotlinx.coroutines.cancelAndJoin
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
import kotlinx.coroutines.flow.SharingStarted
|
|
|
|
import kotlinx.coroutines.flow.SharingStarted
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
import kotlinx.coroutines.flow.mapLatest
|
|
|
|
import kotlinx.coroutines.flow.mapLatest
|
|
|
|
@ -34,13 +35,15 @@ class FavouritesCategoriesViewModel @Inject constructor(
|
|
|
|
) : BaseViewModel() {
|
|
|
|
) : BaseViewModel() {
|
|
|
|
|
|
|
|
|
|
|
|
private var commitJob: Job? = null
|
|
|
|
private var commitJob: Job? = null
|
|
|
|
|
|
|
|
private val isActionsEnabled = MutableStateFlow(true)
|
|
|
|
|
|
|
|
|
|
|
|
val content = combine(
|
|
|
|
val content = combine(
|
|
|
|
repository.observeCategoriesWithCovers(),
|
|
|
|
repository.observeCategoriesWithCovers(),
|
|
|
|
observeAllCategories(),
|
|
|
|
observeAllCategories(),
|
|
|
|
settings.observeAsFlow(AppSettings.KEY_ALL_FAVOURITES_VISIBLE) { isAllFavouritesVisible },
|
|
|
|
settings.observeAsFlow(AppSettings.KEY_ALL_FAVOURITES_VISIBLE) { isAllFavouritesVisible },
|
|
|
|
) { cats, all, showAll ->
|
|
|
|
isActionsEnabled,
|
|
|
|
cats.toUiList(all, showAll)
|
|
|
|
) { cats, all, showAll, hasActions ->
|
|
|
|
|
|
|
|
cats.toUiList(all, showAll, hasActions)
|
|
|
|
}.withErrorHandling()
|
|
|
|
}.withErrorHandling()
|
|
|
|
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, listOf(LoadingState))
|
|
|
|
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, listOf(LoadingState))
|
|
|
|
|
|
|
|
|
|
|
|
@ -77,6 +80,10 @@ class FavouritesCategoriesViewModel @Inject constructor(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun setActionsEnabled(value: Boolean) {
|
|
|
|
|
|
|
|
isActionsEnabled.value = value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun getCategories(ids: LongSet): ArrayList<FavouriteCategory> {
|
|
|
|
fun getCategories(ids: LongSet): ArrayList<FavouriteCategory> {
|
|
|
|
val items = content.requireValue()
|
|
|
|
val items = content.requireValue()
|
|
|
|
return items.mapNotNullTo(ArrayList(ids.size)) { item ->
|
|
|
|
return items.mapNotNullTo(ArrayList(ids.size)) { item ->
|
|
|
|
@ -86,7 +93,8 @@ class FavouritesCategoriesViewModel @Inject constructor(
|
|
|
|
|
|
|
|
|
|
|
|
private fun Map<FavouriteCategory, List<Cover>>.toUiList(
|
|
|
|
private fun Map<FavouriteCategory, List<Cover>>.toUiList(
|
|
|
|
allFavorites: Pair<Int, List<Cover>>,
|
|
|
|
allFavorites: Pair<Int, List<Cover>>,
|
|
|
|
showAll: Boolean
|
|
|
|
showAll: Boolean,
|
|
|
|
|
|
|
|
hasActions: Boolean,
|
|
|
|
): List<ListModel> {
|
|
|
|
): List<ListModel> {
|
|
|
|
if (isEmpty()) {
|
|
|
|
if (isEmpty()) {
|
|
|
|
return listOf(
|
|
|
|
return listOf(
|
|
|
|
@ -104,6 +112,7 @@ class FavouritesCategoriesViewModel @Inject constructor(
|
|
|
|
mangaCount = allFavorites.first,
|
|
|
|
mangaCount = allFavorites.first,
|
|
|
|
covers = allFavorites.second,
|
|
|
|
covers = allFavorites.second,
|
|
|
|
isVisible = showAll,
|
|
|
|
isVisible = showAll,
|
|
|
|
|
|
|
|
isActionsEnabled = hasActions,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
mapTo(result) { (category, covers) ->
|
|
|
|
mapTo(result) { (category, covers) ->
|
|
|
|
@ -111,6 +120,7 @@ class FavouritesCategoriesViewModel @Inject constructor(
|
|
|
|
mangaCount = covers.size,
|
|
|
|
mangaCount = covers.size,
|
|
|
|
covers = covers.take(3),
|
|
|
|
covers = covers.take(3),
|
|
|
|
category = category,
|
|
|
|
category = category,
|
|
|
|
|
|
|
|
isActionsEnabled = hasActions,
|
|
|
|
isTrackerEnabled = settings.isTrackerEnabled && AppSettings.TRACK_FAVOURITES in settings.trackSources,
|
|
|
|
isTrackerEnabled = settings.isTrackerEnabled && AppSettings.TRACK_FAVOURITES in settings.trackSources,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|