refactor(library/compose): StateCoroutinePresenter

This commit is contained in:
Ahmad Ansori Palembani 2025-01-07 18:59:08 +07:00
parent 7d9c0faf86
commit 48c2ad9b33
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
8 changed files with 116 additions and 54 deletions

View file

@ -0,0 +1,15 @@
package eu.kanade.tachiyomi.ui.base.presenter
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
/**
* Presenter that mimic [cafe.adriel.voyager.core.model.StateScreenModel] for easier migration.
* Temporary class while we're migrating to Compose.
*/
abstract class StateCoroutinePresenter<S, C>(initialState: S) : BaseCoroutinePresenter<C>() {
protected val mutableState: MutableStateFlow<S> = MutableStateFlow(initialState)
val state: StateFlow<S> = mutableState.asStateFlow()
}

View file

@ -1,30 +0,0 @@
package eu.kanade.tachiyomi.ui.library
import android.os.Bundle
import androidx.compose.runtime.Composable
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.transitions.CrossfadeTransition
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.base.controller.BaseComposeController
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import yokai.domain.ui.UiPreferences
import yokai.presentation.library.LibraryScreen
class LibraryComposeController(
bundle: Bundle? = null,
val uiPreferences: UiPreferences = Injekt.get(),
val preferences: PreferencesHelper = Injekt.get(),
) : BaseComposeController(bundle) {
override val shouldHideLegacyAppBar = false
@Composable
override fun ScreenContent() {
Navigator(
screen = LibraryScreen(),
content = {
CrossfadeTransition(navigator = it)
},
)
}
}

View file

@ -0,0 +1,49 @@
package eu.kanade.tachiyomi.ui.library.compose
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.core.view.isGone
import androidx.core.view.isVisible
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.LibraryControllerBinding
import eu.kanade.tachiyomi.ui.base.controller.BaseCoroutineController
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import yokai.domain.ui.UiPreferences
import yokai.presentation.library.LibraryContent
import yokai.presentation.theme.YokaiTheme
class LibraryComposeController(
bundle: Bundle? = null,
val uiPreferences: UiPreferences = Injekt.get(),
val preferences: PreferencesHelper = Injekt.get(),
) : BaseCoroutineController<LibraryControllerBinding, LibraryComposePresenter>(bundle) {
override val presenter = LibraryComposePresenter()
override fun createBinding(inflater: LayoutInflater) = LibraryControllerBinding.inflate(inflater)
override fun onViewCreated(view: View) {
super.onViewCreated(view)
binding.composeView.isVisible = true
binding.swipeRefresh.isGone = true
binding.composeView.setContent {
YokaiTheme {
ScreenContent()
}
}
}
@Composable
fun ScreenContent() {
val state by presenter.state.collectAsState()
LibraryContent(
columns = 3,
)
}
}

View file

@ -0,0 +1,11 @@
package eu.kanade.tachiyomi.ui.library.compose
import eu.kanade.tachiyomi.ui.base.presenter.StateCoroutinePresenter
class LibraryComposePresenter :
StateCoroutinePresenter<LibraryComposePresenter.State, LibraryComposeController>(State.Loading) {
sealed interface State {
data object Loading : State
}
}

View file

@ -85,8 +85,8 @@ import eu.kanade.tachiyomi.ui.base.SmallToolbarInterface
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.ui.base.controller.BaseLegacyController import eu.kanade.tachiyomi.ui.base.controller.BaseLegacyController
import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.library.LibraryComposeController
import eu.kanade.tachiyomi.ui.library.LibraryController import eu.kanade.tachiyomi.ui.library.LibraryController
import eu.kanade.tachiyomi.ui.library.compose.LibraryComposeController
import eu.kanade.tachiyomi.ui.manga.MangaDetailsController import eu.kanade.tachiyomi.ui.manga.MangaDetailsController
import eu.kanade.tachiyomi.ui.more.AboutController import eu.kanade.tachiyomi.ui.more.AboutController
import eu.kanade.tachiyomi.ui.more.OverflowDialog import eu.kanade.tachiyomi.ui.more.OverflowDialog

View file

@ -0,0 +1,31 @@
package yokai.presentation.library
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import yokai.presentation.AppBarType
import yokai.presentation.YokaiScaffold
import yokai.presentation.library.components.LazyLibraryGrid
@Composable
fun LibraryContent(
columns: Int,
) {
val items = (0..100).toList()
YokaiScaffold(
onNavigationIconClicked = {},
appBarType = AppBarType.NONE,
) { contentPadding ->
LazyLibraryGrid(
columns = columns,
contentPadding = contentPadding,
) {
items(
items = items,
contentType = { "library_grid_item" }
) {
Text("Hello world! ($it)")
}
}
}
}

View file

@ -1,33 +1,13 @@
package yokai.presentation.library package yokai.presentation.library
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import yokai.presentation.AppBarType
import yokai.presentation.YokaiScaffold
import yokai.presentation.library.components.LazyLibraryGrid
import yokai.util.Screen import yokai.util.Screen
class LibraryScreen : Screen() { class LibraryScreen : Screen() {
@Composable @Composable
override fun Content() { override fun Content() {
val columns = 3 // FIXME: Retrieve from preferences LibraryContent(
val items = (0..100).toList() columns = 3, // FIXME: Retrieve from preferences
YokaiScaffold( )
onNavigationIconClicked = {},
appBarType = AppBarType.NONE,
) { contentPadding ->
LazyLibraryGrid(
columns = columns,
contentPadding = contentPadding,
) {
items(
items = items,
contentType = { "library_grid_item" }
) {
Text("Hello world! ($it)")
}
}
}
} }
} }

View file

@ -15,6 +15,12 @@
android:layout_height="75dp" android:layout_height="75dp"
android:layout_gravity="center" /> android:layout_gravity="center" />
<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh" android:id="@+id/swipe_refresh"
android:layout_width="match_parent" android:layout_width="match_parent"