|
|
|
@ -7,6 +7,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.flow.catch
|
|
|
|
import kotlinx.coroutines.flow.catch
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.debounce
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
|
|
|
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
|
|
|
import org.koitharu.kotatsu.base.ui.util.ReversibleAction
|
|
|
|
import org.koitharu.kotatsu.base.ui.util.ReversibleAction
|
|
|
|
@ -24,8 +25,8 @@ import org.koitharu.kotatsu.list.ui.model.LoadingState
|
|
|
|
import org.koitharu.kotatsu.list.ui.model.toErrorState
|
|
|
|
import org.koitharu.kotatsu.list.ui.model.toErrorState
|
|
|
|
import org.koitharu.kotatsu.list.ui.model.toGridModel
|
|
|
|
import org.koitharu.kotatsu.list.ui.model.toGridModel
|
|
|
|
import org.koitharu.kotatsu.list.ui.model.toUi
|
|
|
|
import org.koitharu.kotatsu.list.ui.model.toUi
|
|
|
|
import org.koitharu.kotatsu.local.domain.LocalMangaRepository
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.parsers.model.Manga
|
|
|
|
import org.koitharu.kotatsu.parsers.model.Manga
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.parsers.model.SortOrder
|
|
|
|
import org.koitharu.kotatsu.shelf.domain.ShelfRepository
|
|
|
|
import org.koitharu.kotatsu.shelf.domain.ShelfRepository
|
|
|
|
import org.koitharu.kotatsu.shelf.ui.model.ShelfSectionModel
|
|
|
|
import org.koitharu.kotatsu.shelf.ui.model.ShelfSectionModel
|
|
|
|
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
|
|
|
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
|
|
|
@ -35,11 +36,10 @@ import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
|
|
@HiltViewModel
|
|
|
|
@HiltViewModel
|
|
|
|
class ShelfViewModel @Inject constructor(
|
|
|
|
class ShelfViewModel @Inject constructor(
|
|
|
|
repository: ShelfRepository,
|
|
|
|
private val repository: ShelfRepository,
|
|
|
|
private val historyRepository: HistoryRepository,
|
|
|
|
private val historyRepository: HistoryRepository,
|
|
|
|
private val favouritesRepository: FavouritesRepository,
|
|
|
|
private val favouritesRepository: FavouritesRepository,
|
|
|
|
private val trackingRepository: TrackingRepository,
|
|
|
|
private val trackingRepository: TrackingRepository,
|
|
|
|
private val localMangaRepository: LocalMangaRepository,
|
|
|
|
|
|
|
|
private val settings: AppSettings,
|
|
|
|
private val settings: AppSettings,
|
|
|
|
) : BaseViewModel(), ListExtraProvider {
|
|
|
|
) : BaseViewModel(), ListExtraProvider {
|
|
|
|
|
|
|
|
|
|
|
|
@ -47,13 +47,15 @@ class ShelfViewModel @Inject constructor(
|
|
|
|
|
|
|
|
|
|
|
|
val content: LiveData<List<ListModel>> = combine(
|
|
|
|
val content: LiveData<List<ListModel>> = combine(
|
|
|
|
historyRepository.observeAllWithHistory(),
|
|
|
|
historyRepository.observeAllWithHistory(),
|
|
|
|
|
|
|
|
repository.observeLocalManga(SortOrder.UPDATED),
|
|
|
|
repository.observeFavourites(),
|
|
|
|
repository.observeFavourites(),
|
|
|
|
trackingRepository.observeUpdatedManga(),
|
|
|
|
trackingRepository.observeUpdatedManga(),
|
|
|
|
) { history, favourites, updated ->
|
|
|
|
) { history, local, favourites, updated ->
|
|
|
|
mapList(history, favourites, updated)
|
|
|
|
mapList(history, favourites, updated, local)
|
|
|
|
}.catch { e ->
|
|
|
|
}.debounce(500)
|
|
|
|
emit(listOf(e.toErrorState(canRetry = false)))
|
|
|
|
.catch { e ->
|
|
|
|
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, listOf(LoadingState))
|
|
|
|
emit(listOf(e.toErrorState(canRetry = false)))
|
|
|
|
|
|
|
|
}.asFlowLiveData(viewModelScope.coroutineContext + Dispatchers.Default, listOf(LoadingState))
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun getCounter(mangaId: Long): Int {
|
|
|
|
override suspend fun getCounter(mangaId: Long): Int {
|
|
|
|
return trackingRepository.getNewChaptersCount(mangaId)
|
|
|
|
return trackingRepository.getNewChaptersCount(mangaId)
|
|
|
|
@ -87,6 +89,13 @@ class ShelfViewModel @Inject constructor(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun deleteLocal(ids: Set<Long>) {
|
|
|
|
|
|
|
|
launchLoadingJob(Dispatchers.Default) {
|
|
|
|
|
|
|
|
repository.deleteLocalManga(ids)
|
|
|
|
|
|
|
|
onActionDone.postCall(ReversibleAction(R.string.removal_completed, null))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun clearHistory(minDate: Long) {
|
|
|
|
fun clearHistory(minDate: Long) {
|
|
|
|
launchJob(Dispatchers.Default) {
|
|
|
|
launchJob(Dispatchers.Default) {
|
|
|
|
val stringRes = if (minDate <= 0) {
|
|
|
|
val stringRes = if (minDate <= 0) {
|
|
|
|
@ -123,11 +132,15 @@ class ShelfViewModel @Inject constructor(
|
|
|
|
history: List<MangaWithHistory>,
|
|
|
|
history: List<MangaWithHistory>,
|
|
|
|
favourites: Map<FavouriteCategory, List<Manga>>,
|
|
|
|
favourites: Map<FavouriteCategory, List<Manga>>,
|
|
|
|
updated: Map<Manga, Int>,
|
|
|
|
updated: Map<Manga, Int>,
|
|
|
|
|
|
|
|
local: List<Manga>,
|
|
|
|
): List<ListModel> {
|
|
|
|
): List<ListModel> {
|
|
|
|
val result = ArrayList<ListModel>(favourites.keys.size + 2)
|
|
|
|
val result = ArrayList<ListModel>(favourites.keys.size + 2)
|
|
|
|
if (history.isNotEmpty()) {
|
|
|
|
if (history.isNotEmpty()) {
|
|
|
|
mapHistory(result, history)
|
|
|
|
mapHistory(result, history)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local.isNotEmpty()) {
|
|
|
|
|
|
|
|
mapLocal(result, local)
|
|
|
|
|
|
|
|
}
|
|
|
|
if (updated.isNotEmpty()) {
|
|
|
|
if (updated.isNotEmpty()) {
|
|
|
|
mapUpdated(result, updated)
|
|
|
|
mapUpdated(result, updated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -174,6 +187,16 @@ class ShelfViewModel @Inject constructor(
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun mapLocal(
|
|
|
|
|
|
|
|
destination: MutableList<in ShelfSectionModel.Local>,
|
|
|
|
|
|
|
|
local: List<Manga>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
destination += ShelfSectionModel.Local(
|
|
|
|
|
|
|
|
items = local.toUi(ListMode.GRID, this),
|
|
|
|
|
|
|
|
showAllButtonText = R.string.show_all,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun mapFavourites(
|
|
|
|
private suspend fun mapFavourites(
|
|
|
|
destination: MutableList<in ShelfSectionModel.Favourites>,
|
|
|
|
destination: MutableList<in ShelfSectionModel.Favourites>,
|
|
|
|
favourites: Map<FavouriteCategory, List<Manga>>,
|
|
|
|
favourites: Map<FavouriteCategory, List<Manga>>,
|
|
|
|
|