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 androidx.compose.ui.text.intl.Locale
|
||||||
import ca.gosyer.jui.core.lang.displayName
|
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.model.Extension
|
||||||
import ca.gosyer.jui.domain.extension.service.ExtensionPreferences
|
import ca.gosyer.jui.domain.extension.service.ExtensionPreferences
|
||||||
import ca.gosyer.jui.i18n.MR
|
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.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.catch
|
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.filterNotNull
|
import kotlinx.coroutines.flow.filterNotNull
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import me.tatarka.inject.annotations.Inject
|
import me.tatarka.inject.annotations.Inject
|
||||||
import org.lighthousegames.logging.logging
|
import org.lighthousegames.logging.logging
|
||||||
|
|
||||||
class ExtensionsScreenViewModel @Inject constructor(
|
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,
|
extensionPreferences: ExtensionPreferences,
|
||||||
contextWrapper: ContextWrapper
|
contextWrapper: ContextWrapper
|
||||||
) : ViewModel(contextWrapper) {
|
) : ViewModel(contextWrapper) {
|
||||||
@@ -56,60 +60,38 @@ class ExtensionsScreenViewModel @Inject constructor(
|
|||||||
val isLoading = _isLoading.asStateFlow()
|
val isLoading = _isLoading.asStateFlow()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
getExtensions()
|
scope.launch {
|
||||||
|
getExtensions()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getExtensions() {
|
private suspend fun getExtensions() {
|
||||||
extensionHandler.getExtensionList()
|
extensionList.value = getExtensionList.await().orEmpty()
|
||||||
.onEach {
|
_isLoading.value = false
|
||||||
extensionList.value = it
|
|
||||||
_isLoading.value = false
|
|
||||||
}
|
|
||||||
.catch {
|
|
||||||
log.warn(it) { "Error getting extensions" }
|
|
||||||
emit(emptyList())
|
|
||||||
_isLoading.value = false
|
|
||||||
}
|
|
||||||
.launchIn(scope)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun install(extension: Extension) {
|
fun install(extension: Extension) {
|
||||||
log.info { "Install clicked" }
|
log.info { "Install clicked" }
|
||||||
extensionHandler.installExtension(extension)
|
scope.launch {
|
||||||
.onEach {
|
installExtension.await(extension)
|
||||||
getExtensions()
|
getExtensions()
|
||||||
}
|
}
|
||||||
.catch {
|
|
||||||
log.warn(it) { "Error installing extension ${extension.apkName}" }
|
|
||||||
getExtensions()
|
|
||||||
}
|
|
||||||
.launchIn(scope)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun update(extension: Extension) {
|
fun update(extension: Extension) {
|
||||||
log.info { "Update clicked" }
|
log.info { "Update clicked" }
|
||||||
extensionHandler.updateExtension(extension)
|
scope.launch {
|
||||||
.onEach {
|
updateExtension.await(extension)
|
||||||
getExtensions()
|
getExtensions()
|
||||||
}
|
}
|
||||||
.catch {
|
|
||||||
log.warn(it) { "Error updating extension ${extension.apkName}" }
|
|
||||||
getExtensions()
|
|
||||||
}
|
|
||||||
.launchIn(scope)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun uninstall(extension: Extension) {
|
fun uninstall(extension: Extension) {
|
||||||
log.info { "Uninstall clicked" }
|
log.info { "Uninstall clicked" }
|
||||||
extensionHandler.uninstallExtension(extension)
|
scope.launch {
|
||||||
.onEach {
|
uninstallExtension.await(extension)
|
||||||
getExtensions()
|
getExtensions()
|
||||||
}
|
}
|
||||||
.catch {
|
|
||||||
log.warn(it) { "Error uninstalling extension ${extension.apkName}" }
|
|
||||||
getExtensions()
|
|
||||||
}
|
|
||||||
.launchIn(scope)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setEnabledLanguages(langs: Set<String>) {
|
fun setEnabledLanguages(langs: Set<String>) {
|
||||||
|
|||||||
Reference in New Issue
Block a user