mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-10 23:02:04 +01:00
Use interactors for downloader data calls
This commit is contained in:
@@ -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.download.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.download.service.DownloadRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class ClearDownloadQueue @Inject constructor(private val downloadRepository: DownloadRepository) {
|
||||
|
||||
suspend fun await() = asFlow()
|
||||
.catch { log.warn(it) { "Failed to clear download queue" } }
|
||||
.collect()
|
||||
|
||||
fun asFlow() = downloadRepository.clearDownloadQueue()
|
||||
|
||||
companion object {
|
||||
private val log = logging()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.download.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.download.service.DownloadRepository
|
||||
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 StartDownloading @Inject constructor(private val downloadRepository: DownloadRepository) {
|
||||
|
||||
suspend fun await() = asFlow()
|
||||
.catch { log.warn(it) { "Failed to start downloader" } }
|
||||
.collect()
|
||||
|
||||
fun asFlow() = downloadRepository.startDownloading()
|
||||
|
||||
companion object {
|
||||
private val log = logging()
|
||||
}
|
||||
}
|
||||
@@ -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.download.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.download.service.DownloadRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class StopDownloading @Inject constructor(private val downloadRepository: DownloadRepository) {
|
||||
|
||||
suspend fun await() = asFlow()
|
||||
.catch { log.warn(it) { "Failed to stop downloader" } }
|
||||
.collect()
|
||||
|
||||
fun asFlow() = downloadRepository.stopDownloading()
|
||||
|
||||
companion object {
|
||||
private val log = logging()
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,11 @@
|
||||
package ca.gosyer.jui.ui.downloads
|
||||
|
||||
import ca.gosyer.jui.data.chapter.ChapterRepositoryImpl
|
||||
import ca.gosyer.jui.data.download.DownloadRepositoryImpl
|
||||
import ca.gosyer.jui.domain.base.WebsocketService.Actions
|
||||
import ca.gosyer.jui.domain.chapter.model.Chapter
|
||||
import ca.gosyer.jui.domain.download.interactor.ClearDownloadQueue
|
||||
import ca.gosyer.jui.domain.download.interactor.StartDownloading
|
||||
import ca.gosyer.jui.domain.download.interactor.StopDownloading
|
||||
import ca.gosyer.jui.domain.download.service.DownloadService
|
||||
import ca.gosyer.jui.uicore.vm.ContextWrapper
|
||||
import ca.gosyer.jui.uicore.vm.ViewModel
|
||||
@@ -21,12 +23,15 @@ import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class DownloadsScreenViewModel @Inject constructor(
|
||||
private val downloadService: DownloadService,
|
||||
private val downloadsHandler: DownloadRepositoryImpl,
|
||||
private val startDownloading: StartDownloading,
|
||||
private val stopDownloading: StopDownloading,
|
||||
private val clearDownloadQueue: ClearDownloadQueue,
|
||||
private val chapterHandler: ChapterRepositoryImpl,
|
||||
private val contextWrapper: ContextWrapper,
|
||||
standalone: Boolean
|
||||
@@ -43,27 +48,15 @@ class DownloadsScreenViewModel @Inject constructor(
|
||||
val downloadQueue get() = DownloadService.downloadQueue.asStateFlow()
|
||||
|
||||
fun start() {
|
||||
downloadsHandler.startDownloading()
|
||||
.catch {
|
||||
log.warn(it) { "Error starting download" }
|
||||
}
|
||||
.launchIn(scope)
|
||||
scope.launch { startDownloading.await() }
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
downloadsHandler.stopDownloading()
|
||||
.catch {
|
||||
log.warn(it) { "Error stopping download" }
|
||||
}
|
||||
.launchIn(scope)
|
||||
scope.launch { stopDownloading.await() }
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
downloadsHandler.clearDownloadQueue()
|
||||
.catch {
|
||||
log.warn(it) { "Error clearing download" }
|
||||
}
|
||||
.launchIn(scope)
|
||||
scope.launch { clearDownloadQueue.await() }
|
||||
}
|
||||
|
||||
fun stopDownload(chapter: Chapter) {
|
||||
|
||||
Reference in New Issue
Block a user