Move FavouriteScene to paging3 (#1238)

* Initial move favScene to paging3

* Refine

* Refine

* Refine

* Refine

* Implement goto bottom

* Refine

* Implement Jumpto

* Refine

* Refine

* Refine

* Refine

* Refine

* Cleanup code

* Cleanup code

* Refine

* Refine

* Do load in IOContext
This commit is contained in:
tarsin authored and GitHub committed 2023-07-10 14:00:45 +08:00
1 parent 740fff2178
commit d4d6fe56a9
11 files changed
+434 -871

No files matched your search

+2 -21
View File
@@ -96,12 +96,6 @@ object EhDB {
dao.deleteByKey(gid)
}
@Synchronized
fun clearDownloadDirname() {
val dao = db.downloadDirnameDao()
dao.deleteAll()
}
@get:Synchronized
val allDownloadLabelList: List<DownloadLabel>
get() = db.downloadLabelDao().list()
@@ -158,21 +152,6 @@ object EhDB {
dao.delete(raw!!)
}
@get:Synchronized
val allLocalFavorites: List<GalleryInfo>
get() {
val dao = db.localFavoritesDao()
val list = dao.list()
return ArrayList<GalleryInfo>(list)
}
@Synchronized
fun searchLocalFavorites(query: String): List<GalleryInfo> {
val dao = db.localFavoritesDao()
val list = dao.list("%$query%")
return ArrayList<GalleryInfo>(list)
}
@Synchronized
fun removeLocalFavorites(gid: Long) {
db.localFavoritesDao().deleteByKey(gid)
@@ -274,6 +253,8 @@ object EhDB {
val localFavLazyList: PagingSource<Int, LocalFavoriteInfo>
get() = db.localFavoritesDao().listLazy()
fun searchLocalFav(keyword: String) = db.localFavoritesDao().listLazy("%$keyword%")
@Synchronized
fun putHistoryInfo(galleryInfo: GalleryInfo?) {
val dao = db.historyDao()
@@ -23,8 +23,8 @@ import kotlinx.parcelize.Parcelize
@Parcelize
class FavListUrlBuilder(
private var mPrev: String? = null,
private var mNext: String? = null,
var mPrev: String? = null,
var mNext: String? = null,
var jumpTo: String? = null,
var keyword: String? = null,
var favCat: Int = FAV_CAT_ALL,
@@ -17,6 +17,9 @@ interface LocalFavoritesDao : BasicDao<LocalFavoriteInfo> {
@Query("SELECT * FROM LOCAL_FAVORITES WHERE TITLE LIKE :title ORDER BY TIME DESC")
fun list(title: String): List<LocalFavoriteInfo>
@Query("SELECT * FROM LOCAL_FAVORITES WHERE TITLE LIKE :title ORDER BY TIME DESC")
fun listLazy(title: String): PagingSource<Int, LocalFavoriteInfo>
@Query("SELECT * FROM LOCAL_FAVORITES WHERE GID = :gid")
fun load(gid: Long): LocalFavoriteInfo?
@@ -124,7 +124,7 @@ class AddDeleteDrawable(context: Context, color: Int) : Drawable() {
setShape(true, duration)
}
fun setShape(delete: Boolean, duration: Long) {
private fun setShape(delete: Boolean, duration: Long) {
if (!(!delete && mProgress == 0f || delete && mProgress == 1f)) {
val endProgress = if (delete) 1f else 0f
if (duration <= 0) {
@@ -414,7 +414,7 @@ class FabLayout @JvmOverloads constructor(
}
}
interface OnExpandListener {
fun interface OnExpandListener {
fun onExpand(expanded: Boolean)
}
@@ -75,8 +75,7 @@ abstract class GalleryInfoContentHelper : ContentHelper() {
}
fun goTo(time: Long, isNext: Boolean) {
val formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd", Locale.US).withZone(ZoneOffset.UTC)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.US).withZone(ZoneOffset.UTC)
jumpTo = formatter.format(Instant.ofEpochMilli(time))
if (isNext) {
goTo(mNext ?: "2", true)
@@ -1,209 +0,0 @@
package com.hippo.ehviewer.ui.scene
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SearchBar
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.currentRecomposeScope
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.os.bundleOf
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import androidx.paging.PagingState
import androidx.paging.cachedIn
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemContentType
import androidx.paging.compose.itemKey
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.client.data.FavListUrlBuilder
import com.hippo.ehviewer.client.data.FavListUrlBuilder.Companion.FAV_CAT_LOCAL
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.ui.main.GalleryInfoListItem
import com.hippo.ehviewer.ui.setMD3Content
import eu.kanade.tachiyomi.util.system.pxToDp
class ComposeFavScene : BaseScene() {
private var curFav by mutableIntStateOf(Settings.recentFavCat)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
) = ComposeView(inflater.context).apply {
setMD3Content {
Scaffold(
topBar = {
var active by remember { mutableStateOf(false) }
val favCatName: String = when (curFav) {
in 0..9 -> Settings.favCat[curFav]
FAV_CAT_LOCAL -> stringResource(id = R.string.local_favorites)
else -> stringResource(id = R.string.cloud_favorites)
}
SearchBar(
query = favCatName,
onQueryChange = { },
onSearch = { },
active = active,
onActiveChange = { active = it },
leadingIcon = {
IconButton(onClick = { toggleDrawer(GravityCompat.START) }) {
Icon(imageVector = Icons.Default.Menu, contentDescription = null)
}
},
modifier = Modifier.padding(horizontal = 18.dp),
) {
}
},
) {
val favBuilder = rememberSaveable(curFav) { FavListUrlBuilder(favCat = curFav) }
val localFavData = remember {
Pager(PagingConfig(25)) {
EhDB.localFavLazyList
}.flow.cachedIn(lifecycleScope)
}.collectAsLazyPagingItems()
val cloudData = remember(curFav) {
Pager(PagingConfig(25)) {
object : PagingSource<String, GalleryInfo>() {
override fun getRefreshKey(state: PagingState<String, GalleryInfo>): String? = null
override suspend fun load(params: LoadParams<String>): LoadResult<String, GalleryInfo> {
when (params) {
is LoadParams.Prepend -> {
favBuilder.setIndex(params.key, isNext = false)
}
is LoadParams.Append -> {
favBuilder.setIndex(params.key, isNext = true)
}
is LoadParams.Refresh -> {
}
}
val r = EhEngine.getFavorites(favBuilder.build())
Settings.favCat = r.catArray
Settings.favCount = r.countArray
Settings.favCloudCount = r.countArray.sum()
return LoadResult.Page(r.galleryInfoList, r.prev, r.next)
}
}
}.flow.cachedIn(lifecycleScope)
}.collectAsLazyPagingItems()
val data = if (curFav == FAV_CAT_LOCAL) localFavData else cloudData
val height = remember { (3 * Settings.listThumbSize * 3).pxToDp.dp }
LazyColumn(
modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen.gallery_list_margin_h), vertical = dimensionResource(id = R.dimen.gallery_list_margin_v)),
verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.gallery_list_interval)),
contentPadding = it,
) {
if (data.itemCount == 0) {
item {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
) {
CircularProgressIndicator()
}
}
}
items(
count = data.itemCount,
key = data.itemKey(key = { item -> item.gid }),
contentType = data.itemContentType(),
) { index ->
val item = data[index]
if (item != null) {
GalleryInfoListItem(
onClick = {
navAnimated(
R.id.galleryDetailScene,
bundleOf(
GalleryDetailScene.KEY_ACTION to GalleryDetailScene.ACTION_GALLERY_INFO,
GalleryDetailScene.KEY_GALLERY_INFO to item,
),
)
},
onLongClick = {},
info = item,
modifier = Modifier.height(height),
isInFavScene = true,
showPages = Settings.showGalleryPages,
)
}
}
}
}
}
}
override fun onCreateDrawerView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
return ComposeView(inflater.context).apply {
setMD3Content {
ElevatedCard {
val scope = currentRecomposeScope
LaunchedEffect(Unit) {
Settings.favChangesFlow.collect {
scope.invalidate()
}
}
val faves = arrayOf(
stringResource(id = R.string.local_favorites) to Settings.favLocalCount,
stringResource(id = R.string.cloud_favorites) to Settings.favCloudCount,
*Settings.favCat.zip(Settings.favCount.toTypedArray()).toTypedArray(),
)
TopAppBar(title = { Text(text = stringResource(id = R.string.collections)) })
LazyColumn {
itemsIndexed(faves) { index, (name, count) ->
ListItem(
headlineContent = { Text(text = name) },
trailingContent = { Text(text = count.toString(), style = MaterialTheme.typography.bodyLarge) },
modifier = Modifier.clickable {
curFav = index - 2
toggleDrawer(GravityCompat.END)
},
)
}
}
}
}
}
}
}
File diff suppressed because it is too large. Load diff
@@ -130,7 +130,6 @@ abstract class GalleryAdapter(
}
init {
recyclerView.adapter = this
recyclerView.layoutManager = layoutManager
this.type = type
adjustPaddings()
@@ -324,6 +324,7 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, OnClickFabList
binding.contentLayout.recyclerView,
Settings.listMode,
)
binding.contentLayout.recyclerView.adapter = mAdapter
binding.contentLayout.recyclerView.clipToPadding = false
binding.contentLayout.recyclerView.clipChildren = false
binding.contentLayout.recyclerView.addOnScrollListener(mOnScrollListener)