Use interactors for updates data calls

This commit is contained in:
Syer10
2022-07-01 12:37:59 -04:00
parent ca2e3813bb
commit 94011e55b4
5 changed files with 95 additions and 14 deletions

View File

@@ -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()
}
}

View File

@@ -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()
}
}

View File

@@ -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()
}
}

View File

@@ -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 {

View File

@@ -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
}