From 94011e55b4b37eafdfdb0fcfb4de405f472486d9 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Fri, 1 Jul 2022 12:37:59 -0400 Subject: [PATCH] Use interactors for updates data calls --- .../updates/interactor/GetRecentUpdates.kt | 26 +++++++++++++++ .../updates/interactor/UpdateCategory.kt | 33 +++++++++++++++++++ .../updates/interactor/UpdateLibrary.kt | 26 +++++++++++++++ .../jui/ui/library/LibraryScreenViewModel.kt | 18 ++++------ .../jui/ui/updates/UpdatesScreenViewModel.kt | 6 ++-- 5 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/GetRecentUpdates.kt create mode 100644 domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateCategory.kt create mode 100644 domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateLibrary.kt diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/GetRecentUpdates.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/GetRecentUpdates.kt new file mode 100644 index 00000000..3b477028 --- /dev/null +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/GetRecentUpdates.kt @@ -0,0 +1,26 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain.updates.interactor + +import ca.gosyer.jui.domain.updates.service.UpdatesRepository +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.singleOrNull +import me.tatarka.inject.annotations.Inject +import org.lighthousegames.logging.logging + +class GetRecentUpdates @Inject constructor(private val updatesRepository: UpdatesRepository) { + + suspend fun await(pageNum: Int) = asFlow(pageNum) + .catch { log.warn(it) { "Failed to get updates for page $pageNum" } } + .singleOrNull() + + fun asFlow(pageNum: Int) = updatesRepository.getRecentUpdates(pageNum) + + companion object { + private val log = logging() + } +} diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateCategory.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateCategory.kt new file mode 100644 index 00000000..b2e742b7 --- /dev/null +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateCategory.kt @@ -0,0 +1,33 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain.updates.interactor + +import ca.gosyer.jui.domain.category.model.Category +import ca.gosyer.jui.domain.updates.service.UpdatesRepository +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect +import me.tatarka.inject.annotations.Inject +import org.lighthousegames.logging.logging + +class UpdateCategory @Inject constructor(private val updatesRepository: UpdatesRepository) { + + suspend fun await(categoryId: Long) = asFlow(categoryId) + .catch { log.warn(it) { "Failed to update category $categoryId" } } + .collect() + + suspend fun await(category: Category) = asFlow(category) + .catch { log.warn(it) { "Failed to update category ${category.name}(${category.id})" } } + .collect() + + fun asFlow(categoryId: Long) = updatesRepository.updateCategory(categoryId) + + fun asFlow(category: Category) = updatesRepository.updateCategory(category.id) + + companion object { + private val log = logging() + } +} diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateLibrary.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateLibrary.kt new file mode 100644 index 00000000..694cfe32 --- /dev/null +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/updates/interactor/UpdateLibrary.kt @@ -0,0 +1,26 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain.updates.interactor + +import ca.gosyer.jui.domain.updates.service.UpdatesRepository +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect +import me.tatarka.inject.annotations.Inject +import org.lighthousegames.logging.logging + +class UpdateLibrary @Inject constructor(private val updatesRepository: UpdatesRepository) { + + suspend fun await() = asFlow() + .catch { log.warn(it) { "Failed to update library" } } + .collect() + + fun asFlow() = updatesRepository.updateLibrary() + + companion object { + private val log = logging() + } +} diff --git a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/library/LibraryScreenViewModel.kt b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/library/LibraryScreenViewModel.kt index 94fad002..00a4ebed 100644 --- a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/library/LibraryScreenViewModel.kt +++ b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/library/LibraryScreenViewModel.kt @@ -10,7 +10,6 @@ import androidx.compose.ui.text.intl.Locale import androidx.compose.ui.text.toLowerCase import ca.gosyer.jui.core.lang.withDefaultContext import ca.gosyer.jui.core.prefs.getAsFlow -import ca.gosyer.jui.data.updates.UpdatesRepositoryImpl import ca.gosyer.jui.domain.category.interactor.GetCategories import ca.gosyer.jui.domain.category.interactor.GetMangaListFromCategory import ca.gosyer.jui.domain.category.model.Category @@ -20,6 +19,8 @@ import ca.gosyer.jui.domain.library.model.Sort import ca.gosyer.jui.domain.library.service.LibraryPreferences import ca.gosyer.jui.domain.manga.model.Manga import ca.gosyer.jui.domain.manga.model.MangaStatus +import ca.gosyer.jui.domain.updates.interactor.UpdateCategory +import ca.gosyer.jui.domain.updates.interactor.UpdateLibrary import ca.gosyer.jui.i18n.MR import ca.gosyer.jui.ui.util.lang.Collator import ca.gosyer.jui.uicore.vm.ContextWrapper @@ -79,7 +80,8 @@ class LibraryScreenViewModel @Inject constructor( private val getCategories: GetCategories, private val getMangaListFromCategory: GetMangaListFromCategory, private val removeMangaFromLibrary: RemoveMangaFromLibrary, - private val updatesHandler: UpdatesRepositoryImpl, + private val updateLibrary: UpdateLibrary, + private val updateCategory: UpdateCategory, libraryPreferences: LibraryPreferences, contextWrapper: ContextWrapper ) : ViewModel(contextWrapper) { @@ -277,19 +279,11 @@ class LibraryScreenViewModel @Inject constructor( } fun updateLibrary() { - updatesHandler.updateLibrary() - .catch { - log.warn(it) { "Error updating library" } - } - .launchIn(scope) + scope.launch { updateLibrary.await() } } fun updateCategory(category: Category) { - updatesHandler.updateCategory(category.id) - .catch { - log.warn(it) { "Error updating category" } - } - .launchIn(scope) + scope.launch { updateCategory.await(category) } } private companion object { diff --git a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/updates/UpdatesScreenViewModel.kt b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/updates/UpdatesScreenViewModel.kt index 0971109c..be7e4e56 100644 --- a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/updates/UpdatesScreenViewModel.kt +++ b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/updates/UpdatesScreenViewModel.kt @@ -14,6 +14,7 @@ import ca.gosyer.jui.data.chapter.ChapterRepositoryImpl import ca.gosyer.jui.data.updates.UpdatesRepositoryImpl import ca.gosyer.jui.domain.chapter.model.Chapter import ca.gosyer.jui.domain.download.service.DownloadService +import ca.gosyer.jui.domain.updates.interactor.GetRecentUpdates import ca.gosyer.jui.ui.base.chapter.ChapterDownloadItem import ca.gosyer.jui.uicore.vm.ContextWrapper import ca.gosyer.jui.uicore.vm.ViewModel @@ -39,6 +40,7 @@ import org.lighthousegames.logging.logging class UpdatesScreenViewModel @Inject constructor( private val chapterHandler: ChapterRepositoryImpl, private val updatesHandler: UpdatesRepositoryImpl, + private val getRecentUpdates: GetRecentUpdates, contextWrapper: ContextWrapper ) : ViewModel(contextWrapper) { @@ -72,7 +74,7 @@ class UpdatesScreenViewModel @Inject constructor( } private suspend fun getUpdates(page: Int) { - updatesHandler.getRecentUpdates(page) + getRecentUpdates.asFlow(page) .onEach { updates -> updates.page .map { @@ -107,7 +109,7 @@ class UpdatesScreenViewModel @Inject constructor( _isLoading.value = false } .catch { - log.warn(it) { "Error getting updates" } + log.warn(it) { "Failed to get updates for page $page" } if (page > 1) { currentPage.value = page - 1 }