mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-10 06:42:05 +01:00
Use interactors for manga data calls
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user