Use Voyager on Category screen (#8472)

This commit is contained in:
Andreas
2022-11-08 04:13:14 +01:00
committed by GitHub
parent 9c9357639a
commit bf9edda04c
7 changed files with 253 additions and 204 deletions

View File

@@ -1,18 +1,17 @@
package eu.kanade.tachiyomi.ui.category
import androidx.compose.runtime.Composable
import eu.kanade.presentation.category.CategoryScreen
import eu.kanade.tachiyomi.ui.base.controller.FullComposeController
import androidx.compose.runtime.CompositionLocalProvider
import cafe.adriel.voyager.navigator.Navigator
import eu.kanade.presentation.util.LocalRouter
import eu.kanade.tachiyomi.ui.base.controller.BasicFullComposeController
class CategoryController : FullComposeController<CategoryPresenter>() {
override fun createPresenter() = CategoryPresenter()
class CategoryController : BasicFullComposeController() {
@Composable
override fun ComposeContent() {
CategoryScreen(
presenter = presenter,
navigateUp = router::popCurrentController,
)
CompositionLocalProvider(LocalRouter provides router) {
Navigator(screen = CategoryScreen())
}
}
}

View File

@@ -1,100 +0,0 @@
package eu.kanade.tachiyomi.ui.category
import android.os.Bundle
import eu.kanade.domain.category.interactor.CreateCategoryWithName
import eu.kanade.domain.category.interactor.DeleteCategory
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.interactor.RenameCategory
import eu.kanade.domain.category.interactor.ReorderCategory
import eu.kanade.domain.category.model.Category
import eu.kanade.presentation.category.CategoryState
import eu.kanade.presentation.category.CategoryStateImpl
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.util.lang.launchIO
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.consumeAsFlow
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class CategoryPresenter(
private val state: CategoryStateImpl = CategoryState() as CategoryStateImpl,
private val getCategories: GetCategories = Injekt.get(),
private val createCategoryWithName: CreateCategoryWithName = Injekt.get(),
private val renameCategory: RenameCategory = Injekt.get(),
private val reorderCategory: ReorderCategory = Injekt.get(),
private val deleteCategory: DeleteCategory = Injekt.get(),
) : BasePresenter<CategoryController>(), CategoryState by state {
private val _events: Channel<Event> = Channel(Int.MAX_VALUE)
val events = _events.consumeAsFlow()
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
presenterScope.launchIO {
getCategories.subscribe()
.collectLatest {
state.isLoading = false
state.categories = it.filterNot(Category::isSystemCategory)
}
}
}
fun createCategory(name: String) {
presenterScope.launchIO {
when (createCategoryWithName.await(name)) {
is CreateCategoryWithName.Result.NameAlreadyExistsError -> _events.send(Event.CategoryWithNameAlreadyExists)
is CreateCategoryWithName.Result.InternalError -> _events.send(Event.InternalError)
else -> {}
}
}
}
fun deleteCategory(category: Category) {
presenterScope.launchIO {
when (deleteCategory.await(category.id)) {
is DeleteCategory.Result.InternalError -> _events.send(Event.InternalError)
else -> {}
}
}
}
fun moveUp(category: Category) {
presenterScope.launchIO {
when (reorderCategory.await(category, category.order - 1)) {
is ReorderCategory.Result.InternalError -> _events.send(Event.InternalError)
else -> {}
}
}
}
fun moveDown(category: Category) {
presenterScope.launchIO {
when (reorderCategory.await(category, category.order + 1)) {
is ReorderCategory.Result.InternalError -> _events.send(Event.InternalError)
else -> {}
}
}
}
fun renameCategory(category: Category, name: String) {
presenterScope.launchIO {
when (renameCategory.await(category, name)) {
RenameCategory.Result.NameAlreadyExistsError -> _events.send(Event.CategoryWithNameAlreadyExists)
is RenameCategory.Result.InternalError -> _events.send(Event.InternalError)
else -> {}
}
}
}
sealed class Dialog {
object Create : Dialog()
data class Rename(val category: Category) : Dialog()
data class Delete(val category: Category) : Dialog()
}
sealed class Event {
object CategoryWithNameAlreadyExists : Event()
object InternalError : Event()
}
}

View File

@@ -0,0 +1,79 @@
package eu.kanade.tachiyomi.ui.category
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.presentation.category.CategoryScreen
import eu.kanade.presentation.category.components.CategoryCreateDialog
import eu.kanade.presentation.category.components.CategoryDeleteDialog
import eu.kanade.presentation.category.components.CategoryRenameDialog
import eu.kanade.presentation.components.LoadingScreen
import eu.kanade.presentation.util.LocalRouter
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.collectLatest
class CategoryScreen : Screen {
@Composable
override fun Content() {
val context = LocalContext.current
val router = LocalRouter.currentOrThrow
val screenModel = rememberScreenModel { CategoryScreenModel() }
val state by screenModel.state.collectAsState()
if (state is CategoryScreenState.Loading) {
LoadingScreen()
return
}
val successState = state as CategoryScreenState.Success
CategoryScreen(
state = successState,
onClickCreate = { screenModel.showDialog(CategoryDialog.Create) },
onClickRename = { screenModel.showDialog(CategoryDialog.Rename(it)) },
onClickDelete = { screenModel.showDialog(CategoryDialog.Delete(it)) },
onClickMoveUp = screenModel::moveUp,
onClickMoveDown = screenModel::moveDown,
navigateUp = router::popCurrentController,
)
when (val dialog = successState.dialog) {
null -> {}
CategoryDialog.Create -> {
CategoryCreateDialog(
onDismissRequest = screenModel::dismissDialog,
onCreate = { screenModel.createCategory(it) },
)
}
is CategoryDialog.Rename -> {
CategoryRenameDialog(
onDismissRequest = screenModel::dismissDialog,
onRename = { screenModel.renameCategory(dialog.category, it) },
category = dialog.category,
)
}
is CategoryDialog.Delete -> {
CategoryDeleteDialog(
onDismissRequest = screenModel::dismissDialog,
onDelete = { screenModel.deleteCategory(dialog.category.id) },
category = dialog.category,
)
}
}
LaunchedEffect(Unit) {
screenModel.events.collectLatest { event ->
if (event is CategoryEvent.LocalizedMessage) {
context.toast(event.stringRes)
}
}
}
}
}

View File

@@ -0,0 +1,140 @@
package eu.kanade.tachiyomi.ui.category
import androidx.annotation.StringRes
import androidx.compose.runtime.Immutable
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.coroutineScope
import eu.kanade.domain.category.interactor.CreateCategoryWithName
import eu.kanade.domain.category.interactor.DeleteCategory
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.interactor.RenameCategory
import eu.kanade.domain.category.interactor.ReorderCategory
import eu.kanade.domain.category.model.Category
import eu.kanade.tachiyomi.R
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class CategoryScreenModel(
private val getCategories: GetCategories = Injekt.get(),
private val createCategoryWithName: CreateCategoryWithName = Injekt.get(),
private val deleteCategory: DeleteCategory = Injekt.get(),
private val reorderCategory: ReorderCategory = Injekt.get(),
private val renameCategory: RenameCategory = Injekt.get(),
) : StateScreenModel<CategoryScreenState>(CategoryScreenState.Loading) {
private val _events: Channel<CategoryEvent> = Channel()
val events = _events.consumeAsFlow()
init {
coroutineScope.launch {
getCategories.subscribe()
.collectLatest { categories ->
mutableState.update {
CategoryScreenState.Success(
categories = categories.filterNot(Category::isSystemCategory),
)
}
}
}
}
fun createCategory(name: String) {
coroutineScope.launch {
when (createCategoryWithName.await(name)) {
is CreateCategoryWithName.Result.InternalError -> _events.send(CategoryEvent.InternalError)
CreateCategoryWithName.Result.NameAlreadyExistsError -> _events.send(CategoryEvent.CategoryWithNameAlreadyExists)
CreateCategoryWithName.Result.Success -> {}
}
}
}
fun deleteCategory(categoryId: Long) {
coroutineScope.launch {
when (deleteCategory.await(categoryId = categoryId)) {
is DeleteCategory.Result.InternalError -> _events.send(CategoryEvent.InternalError)
DeleteCategory.Result.Success -> {}
}
}
}
fun moveUp(category: Category) {
coroutineScope.launch {
when (reorderCategory.await(category, category.order - 1)) {
is ReorderCategory.Result.InternalError -> _events.send(CategoryEvent.InternalError)
ReorderCategory.Result.Success -> {}
ReorderCategory.Result.Unchanged -> {}
}
}
}
fun moveDown(category: Category) {
coroutineScope.launch {
when (reorderCategory.await(category, category.order + 1)) {
is ReorderCategory.Result.InternalError -> _events.send(CategoryEvent.InternalError)
ReorderCategory.Result.Success -> {}
ReorderCategory.Result.Unchanged -> {}
}
}
}
fun renameCategory(category: Category, name: String) {
coroutineScope.launch {
when (renameCategory.await(category, name)) {
is RenameCategory.Result.InternalError -> _events.send(CategoryEvent.InternalError)
RenameCategory.Result.NameAlreadyExistsError -> _events.send(CategoryEvent.CategoryWithNameAlreadyExists)
RenameCategory.Result.Success -> {}
}
}
}
fun showDialog(dialog: CategoryDialog) {
mutableState.update {
when (it) {
CategoryScreenState.Loading -> it
is CategoryScreenState.Success -> it.copy(dialog = dialog)
}
}
}
fun dismissDialog() {
mutableState.update {
when (it) {
CategoryScreenState.Loading -> it
is CategoryScreenState.Success -> it.copy(dialog = null)
}
}
}
}
sealed class CategoryDialog {
object Create : CategoryDialog()
data class Rename(val category: Category) : CategoryDialog()
data class Delete(val category: Category) : CategoryDialog()
}
sealed class CategoryEvent {
sealed class LocalizedMessage(@StringRes val stringRes: Int) : CategoryEvent()
object CategoryWithNameAlreadyExists : LocalizedMessage(R.string.error_category_exists)
object InternalError : LocalizedMessage(R.string.internal_error)
}
sealed class CategoryScreenState {
@Immutable
object Loading : CategoryScreenState()
@Immutable
data class Success(
val categories: List<Category>,
val dialog: CategoryDialog? = null,
) : CategoryScreenState() {
val isEmpty: Boolean
get() = categories.isEmpty()
}
}