mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-10 06:42:05 +01:00
Use interactors for extension 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.extension.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.extension.service.ExtensionRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.singleOrNull
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class GetExtensionList @Inject constructor(private val extensionRepository: ExtensionRepository) {
|
||||
|
||||
suspend fun await() = asFlow()
|
||||
.catch { log.warn(it) { "Failed to get extension list" } }
|
||||
.singleOrNull()
|
||||
|
||||
fun asFlow() = extensionRepository.getExtensionList()
|
||||
|
||||
companion object {
|
||||
private val log = logging()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.extension.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.extension.model.Extension
|
||||
import ca.gosyer.jui.domain.extension.service.ExtensionRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.singleOrNull
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class InstallExtension @Inject constructor(private val extensionRepository: ExtensionRepository) {
|
||||
|
||||
suspend fun await(extension: Extension) = asFlow(extension)
|
||||
.catch { log.warn(it) { "Failed to install extension ${extension.apkName}" } }
|
||||
.collect()
|
||||
|
||||
fun asFlow(extension: Extension) = extensionRepository.installExtension(extension)
|
||||
|
||||
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.extension.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.extension.model.Extension
|
||||
import ca.gosyer.jui.domain.extension.service.ExtensionRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class UninstallExtension @Inject constructor(private val extensionRepository: ExtensionRepository) {
|
||||
|
||||
suspend fun await(extension: Extension) = asFlow(extension)
|
||||
.catch { log.warn(it) { "Failed to uninstall extension ${extension.apkName}" } }
|
||||
.collect()
|
||||
|
||||
fun asFlow(extension: Extension) = extensionRepository.uninstallExtension(extension)
|
||||
|
||||
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.extension.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.extension.model.Extension
|
||||
import ca.gosyer.jui.domain.extension.service.ExtensionRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class UpdateExtension @Inject constructor(private val extensionRepository: ExtensionRepository) {
|
||||
|
||||
suspend fun await(extension: Extension) = asFlow(extension)
|
||||
.catch { log.warn(it) { "Failed to update extension ${extension.apkName}" } }
|
||||
.collect()
|
||||
|
||||
fun asFlow(extension: Extension) = extensionRepository.updateExtension(extension)
|
||||
|
||||
companion object {
|
||||
private val log = logging()
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,10 @@ package ca.gosyer.jui.ui.extensions
|
||||
|
||||
import androidx.compose.ui.text.intl.Locale
|
||||
import ca.gosyer.jui.core.lang.displayName
|
||||
import ca.gosyer.jui.data.extension.ExtensionRepositoryImpl
|
||||
import ca.gosyer.jui.domain.extension.interactor.GetExtensionList
|
||||
import ca.gosyer.jui.domain.extension.interactor.InstallExtension
|
||||
import ca.gosyer.jui.domain.extension.interactor.UninstallExtension
|
||||
import ca.gosyer.jui.domain.extension.interactor.UpdateExtension
|
||||
import ca.gosyer.jui.domain.extension.model.Extension
|
||||
import ca.gosyer.jui.domain.extension.service.ExtensionPreferences
|
||||
import ca.gosyer.jui.i18n.MR
|
||||
@@ -17,18 +20,19 @@ import ca.gosyer.jui.uicore.vm.ViewModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class ExtensionsScreenViewModel @Inject constructor(
|
||||
private val extensionHandler: ExtensionRepositoryImpl,
|
||||
private val getExtensionList: GetExtensionList,
|
||||
private val installExtension: InstallExtension,
|
||||
private val updateExtension: UpdateExtension,
|
||||
private val uninstallExtension: UninstallExtension,
|
||||
extensionPreferences: ExtensionPreferences,
|
||||
contextWrapper: ContextWrapper
|
||||
) : ViewModel(contextWrapper) {
|
||||
@@ -56,60 +60,38 @@ class ExtensionsScreenViewModel @Inject constructor(
|
||||
val isLoading = _isLoading.asStateFlow()
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
getExtensions()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getExtensions() {
|
||||
extensionHandler.getExtensionList()
|
||||
.onEach {
|
||||
extensionList.value = it
|
||||
private suspend fun getExtensions() {
|
||||
extensionList.value = getExtensionList.await().orEmpty()
|
||||
_isLoading.value = false
|
||||
}
|
||||
.catch {
|
||||
log.warn(it) { "Error getting extensions" }
|
||||
emit(emptyList())
|
||||
_isLoading.value = false
|
||||
}
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
fun install(extension: Extension) {
|
||||
log.info { "Install clicked" }
|
||||
extensionHandler.installExtension(extension)
|
||||
.onEach {
|
||||
scope.launch {
|
||||
installExtension.await(extension)
|
||||
getExtensions()
|
||||
}
|
||||
.catch {
|
||||
log.warn(it) { "Error installing extension ${extension.apkName}" }
|
||||
getExtensions()
|
||||
}
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
fun update(extension: Extension) {
|
||||
log.info { "Update clicked" }
|
||||
extensionHandler.updateExtension(extension)
|
||||
.onEach {
|
||||
scope.launch {
|
||||
updateExtension.await(extension)
|
||||
getExtensions()
|
||||
}
|
||||
.catch {
|
||||
log.warn(it) { "Error updating extension ${extension.apkName}" }
|
||||
getExtensions()
|
||||
}
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
fun uninstall(extension: Extension) {
|
||||
log.info { "Uninstall clicked" }
|
||||
extensionHandler.uninstallExtension(extension)
|
||||
.onEach {
|
||||
scope.launch {
|
||||
uninstallExtension.await(extension)
|
||||
getExtensions()
|
||||
}
|
||||
.catch {
|
||||
log.warn(it) { "Error uninstalling extension ${extension.apkName}" }
|
||||
getExtensions()
|
||||
}
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
fun setEnabledLanguages(langs: Set<String>) {
|
||||
|
||||
Reference in New Issue
Block a user