LibraryContent.kt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package eu.kanade.presentation.library.components
  2. import androidx.compose.foundation.layout.Column
  3. import androidx.compose.foundation.layout.PaddingValues
  4. import androidx.compose.foundation.layout.padding
  5. import androidx.compose.runtime.Composable
  6. import androidx.compose.runtime.LaunchedEffect
  7. import androidx.compose.runtime.State
  8. import androidx.compose.ui.Modifier
  9. import androidx.compose.ui.platform.LocalUriHandler
  10. import com.google.accompanist.pager.rememberPagerState
  11. import com.google.accompanist.swiperefresh.SwipeRefresh
  12. import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
  13. import eu.kanade.core.prefs.PreferenceMutableState
  14. import eu.kanade.domain.category.model.Category
  15. import eu.kanade.presentation.components.EmptyScreen
  16. import eu.kanade.presentation.components.LoadingScreen
  17. import eu.kanade.presentation.components.SwipeRefreshIndicator
  18. import eu.kanade.presentation.library.LibraryState
  19. import eu.kanade.tachiyomi.R
  20. import eu.kanade.tachiyomi.data.database.models.LibraryManga
  21. import eu.kanade.tachiyomi.ui.library.LibraryItem
  22. import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
  23. import eu.kanade.tachiyomi.widget.EmptyView
  24. @Composable
  25. fun LibraryContent(
  26. state: LibraryState,
  27. contentPadding: PaddingValues,
  28. currentPage: Int,
  29. isLibraryEmpty: Boolean,
  30. isDownloadOnly: Boolean,
  31. isIncognitoMode: Boolean,
  32. showPageTabs: Boolean,
  33. showMangaCount: Boolean,
  34. onChangeCurrentPage: (Int) -> Unit,
  35. onMangaClicked: (Long) -> Unit,
  36. onToggleSelection: (LibraryManga) -> Unit,
  37. onRefresh: (Category?) -> Unit,
  38. onGlobalSearchClicked: () -> Unit,
  39. getNumberOfMangaForCategory: @Composable (Long) -> State<Int?>,
  40. getDisplayModeForPage: @Composable (Int) -> State<DisplayModeSetting>,
  41. getColumnsForOrientation: (Boolean) -> PreferenceMutableState<Int>,
  42. getLibraryForPage: @Composable (Int) -> State<List<LibraryItem>>,
  43. ) {
  44. val categories = state.categories
  45. val pagerState = rememberPagerState(currentPage.coerceAtMost(categories.lastIndex))
  46. if (categories.isEmpty()) {
  47. LoadingScreen()
  48. return
  49. }
  50. Column(
  51. modifier = Modifier.padding(contentPadding),
  52. ) {
  53. if (showPageTabs && categories.size > 1) {
  54. LibraryTabs(
  55. state = pagerState,
  56. categories = state.categories,
  57. showMangaCount = showMangaCount,
  58. getNumberOfMangaForCategory = getNumberOfMangaForCategory,
  59. isDownloadOnly = isDownloadOnly,
  60. isIncognitoMode = isIncognitoMode,
  61. )
  62. }
  63. val onClickManga = { manga: LibraryManga ->
  64. if (state.selectionMode.not()) {
  65. onMangaClicked(manga.id!!)
  66. } else {
  67. onToggleSelection(manga)
  68. }
  69. }
  70. val onLongClickManga = { manga: LibraryManga ->
  71. onToggleSelection(manga)
  72. }
  73. SwipeRefresh(
  74. state = rememberSwipeRefreshState(isRefreshing = false),
  75. onRefresh = { onRefresh(categories[currentPage]) },
  76. indicator = { s, trigger ->
  77. SwipeRefreshIndicator(
  78. state = s,
  79. refreshTriggerDistance = trigger,
  80. )
  81. },
  82. ) {
  83. if (state.searchQuery.isNullOrEmpty() && isLibraryEmpty) {
  84. val handler = LocalUriHandler.current
  85. EmptyScreen(
  86. R.string.information_empty_library,
  87. listOf(
  88. EmptyView.Action(R.string.getting_started_guide, R.drawable.ic_help_24dp) {
  89. handler.openUri("https://tachiyomi.org/help/guides/getting-started")
  90. },
  91. ),
  92. )
  93. return@SwipeRefresh
  94. }
  95. LibraryPager(
  96. state = pagerState,
  97. pageCount = categories.size,
  98. selectedManga = state.selection,
  99. getDisplayModeForPage = getDisplayModeForPage,
  100. getColumnsForOrientation = getColumnsForOrientation,
  101. getLibraryForPage = getLibraryForPage,
  102. onClickManga = onClickManga,
  103. onLongClickManga = onLongClickManga,
  104. onGlobalSearchClicked = onGlobalSearchClicked,
  105. searchQuery = state.searchQuery,
  106. )
  107. }
  108. LaunchedEffect(pagerState.currentPage) {
  109. onChangeCurrentPage(pagerState.currentPage)
  110. }
  111. }
  112. }