From 1bcf6f09c5e3ddfa1bdadfeaea80618d16373e95 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Fri, 1 Jul 2022 13:26:09 -0400 Subject: [PATCH] Use interactors for manga data calls --- .../jui/domain/manga/interactor/GetManga.kt | 33 +++++++++++++++++++ .../domain/manga/interactor/RefreshManga.kt | 33 +++++++++++++++++++ .../jui/ui/manga/MangaScreenViewModel.kt | 23 ++++++------- .../jui/ui/reader/ReaderMenuViewModel.kt | 6 ++-- 4 files changed, 81 insertions(+), 14 deletions(-) diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/GetManga.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/GetManga.kt index e69de29b..014384f1 100644 --- a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/GetManga.kt +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/GetManga.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.manga.interactor + +import ca.gosyer.jui.domain.manga.model.Manga +import ca.gosyer.jui.domain.manga.service.MangaRepository +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.singleOrNull +import me.tatarka.inject.annotations.Inject +import org.lighthousegames.logging.logging + +class GetManga @Inject constructor(private val mangaRepository: MangaRepository) { + + suspend fun await(mangaId: Long) = asFlow(mangaId) + .catch { log.warn(it) { "Failed to get manga $mangaId" } } + .singleOrNull() + + suspend fun await(manga: Manga) = asFlow(manga) + .catch { log.warn(it) { "Failed to get manga ${manga.title}(${manga.id})" } } + .singleOrNull() + + fun asFlow(mangaId: Long) = mangaRepository.getManga(mangaId) + + fun asFlow(manga: Manga) = mangaRepository.getManga(manga.id) + + companion object { + private val log = logging() + } +} diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/RefreshManga.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/RefreshManga.kt index e69de29b..eecd06ce 100644 --- a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/RefreshManga.kt +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/manga/interactor/RefreshManga.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.manga.interactor + +import ca.gosyer.jui.domain.manga.model.Manga +import ca.gosyer.jui.domain.manga.service.MangaRepository +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.singleOrNull +import me.tatarka.inject.annotations.Inject +import org.lighthousegames.logging.logging + +class RefreshManga @Inject constructor(private val mangaRepository: MangaRepository) { + + suspend fun await(mangaId: Long) = asFlow(mangaId) + .catch { log.warn(it) { "Failed to refresh manga $mangaId" } } + .singleOrNull() + + suspend fun await(manga: Manga) = asFlow(manga) + .catch { log.warn(it) { "Failed to refresh manga ${manga.title}(${manga.id})" } } + .singleOrNull() + + fun asFlow(mangaId: Long) = mangaRepository.getManga(mangaId, true) + + fun asFlow(manga: Manga) = mangaRepository.getManga(manga.id, true) + + companion object { + private val log = logging() + } +} diff --git a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/manga/MangaScreenViewModel.kt b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/manga/MangaScreenViewModel.kt index 8f954518..d4a1ebc5 100644 --- a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/manga/MangaScreenViewModel.kt +++ b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/manga/MangaScreenViewModel.kt @@ -9,7 +9,6 @@ package ca.gosyer.jui.ui.manga import ca.gosyer.jui.core.lang.withIOContext import ca.gosyer.jui.data.base.DateHandler import ca.gosyer.jui.data.chapter.ChapterRepositoryImpl -import ca.gosyer.jui.data.manga.MangaRepositoryImpl import ca.gosyer.jui.domain.category.interactor.AddMangaToCategory import ca.gosyer.jui.domain.category.interactor.GetCategories import ca.gosyer.jui.domain.category.interactor.GetMangaCategories @@ -19,6 +18,8 @@ import ca.gosyer.jui.domain.chapter.model.Chapter import ca.gosyer.jui.domain.download.service.DownloadService import ca.gosyer.jui.domain.library.interactor.AddMangaToLibrary import ca.gosyer.jui.domain.library.interactor.RemoveMangaFromLibrary +import ca.gosyer.jui.domain.manga.interactor.GetManga +import ca.gosyer.jui.domain.manga.interactor.RefreshManga import ca.gosyer.jui.domain.manga.model.Manga import ca.gosyer.jui.domain.ui.service.UiPreferences import ca.gosyer.jui.ui.base.chapter.ChapterDownloadItem @@ -34,7 +35,6 @@ import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapLatest -import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.single import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch @@ -43,7 +43,8 @@ import org.lighthousegames.logging.logging class MangaScreenViewModel @Inject constructor( private val dateHandler: DateHandler, - private val mangaHandler: MangaRepositoryImpl, + private val getManga: GetManga, + private val refreshManga: RefreshManga, private val chapterHandler: ChapterRepositoryImpl, private val getCategories: GetCategories, private val getMangaCategories: GetMangaCategories, @@ -130,14 +131,14 @@ class MangaScreenViewModel @Inject constructor( private suspend fun refreshMangaAsync(mangaId: Long, refresh: Boolean = false) = withIOContext { async { - mangaHandler.getManga(mangaId, refresh) - .onEach { - _manga.value = it - } - .catch { - log.warn(it) { "Error getting manga" } - } - .collect() + val manga = if (refresh) { + refreshManga.await(mangaId) + } else { + getManga.await(mangaId) + } + if (manga != null) { + _manga.value = manga + } getMangaCategories.await(mangaId) ?.let { _mangaCategories.value = it diff --git a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/reader/ReaderMenuViewModel.kt b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/reader/ReaderMenuViewModel.kt index 578eef23..61ce707a 100644 --- a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/reader/ReaderMenuViewModel.kt +++ b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/reader/ReaderMenuViewModel.kt @@ -9,9 +9,9 @@ package ca.gosyer.jui.ui.reader import ca.gosyer.jui.core.lang.launchDefault import ca.gosyer.jui.core.prefs.getAsFlow import ca.gosyer.jui.data.chapter.ChapterRepositoryImpl -import ca.gosyer.jui.data.manga.MangaRepositoryImpl import ca.gosyer.jui.domain.chapter.interactor.UpdateChapterMeta import ca.gosyer.jui.domain.chapter.model.Chapter +import ca.gosyer.jui.domain.manga.interactor.GetManga import ca.gosyer.jui.domain.manga.interactor.UpdateMangaMeta import ca.gosyer.jui.domain.manga.model.Manga import ca.gosyer.jui.domain.manga.model.MangaMeta @@ -50,7 +50,7 @@ import org.lighthousegames.logging.logging class ReaderMenuViewModel @Inject constructor( private val readerPreferences: ReaderPreferences, - private val mangaHandler: MangaRepositoryImpl, + private val getManga: GetManga, private val chapterHandler: ChapterRepositoryImpl, private val updateMangaMeta: UpdateMangaMeta, private val updateChapterMeta: UpdateChapterMeta, @@ -203,7 +203,7 @@ class ReaderMenuViewModel @Inject constructor( } private suspend fun initManga(mangaId: Long) { - mangaHandler.getManga(mangaId) + getManga.asFlow(mangaId) .onEach { _manga.value = it }