主页动画

This commit is contained in:
HapeLee
2025-11-14 22:32:49 +08:00
parent ba4f8e23a2
commit b498deb2d3
2 changed files with 74 additions and 77 deletions
@@ -203,9 +203,11 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
private fun upRecyclerData() { private fun upRecyclerData() {
booksFlowJob?.cancel() booksFlowJob?.cancel()
booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { booksFlowJob = viewLifecycleOwner.lifecycleScope.launch {
appDb.bookDao.flowByGroup(groupId).map { list ->
//排序
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
appDb.bookDao.flowByGroup(groupId)
.map { list ->
val isDescending = AppConfig.bookshelfSortOrder == 1 val isDescending = AppConfig.bookshelfSortOrder == 1
when (bookSort) { when (bookSort) {
@@ -220,33 +222,28 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
3 -> if (isDescending) list.sortedByDescending { it.order } 3 -> if (isDescending) list.sortedByDescending { it.order }
else list.sortedBy { it.order } else list.sortedBy { it.order }
4 -> if (isDescending) list.sortedByDescending { 4 -> if (isDescending)
max( list.sortedByDescending { max(it.latestChapterTime, it.durChapterTime) }
it.latestChapterTime, else
it.durChapterTime list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
)
}
else list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
5 -> if (isDescending) 5 -> if (isDescending)
list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) }
else else
list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) }
else -> if (isDescending) list.sortedByDescending { it.durChapterTime } else ->
if (isDescending) list.sortedByDescending { it.durChapterTime }
else list.sortedBy { it.durChapterTime } else list.sortedBy { it.durChapterTime }
} }
}.flowWithLifecycleAndDatabaseChangeFirst( }
viewLifecycleOwner.lifecycle, .flowOn(Dispatchers.Default)
Lifecycle.State.RESUMED, .catch { AppLog.put("书架更新出错", it) }
AppDatabase.BOOK_TABLE_NAME .collect { list ->
).catch {
AppLog.put("书架更新出错", it)
}.conflate().flowOn(Dispatchers.Default).collect { list ->
binding.emptyView.isGone = list.isNotEmpty() binding.emptyView.isGone = list.isNotEmpty()
binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty() binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty()
booksAdapter.setItems(list) booksAdapter.setItems(list)
delay(500) }
} }
} }
} }
@@ -14,6 +14,7 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@@ -246,16 +247,14 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
} }
private fun initBooksData() { private fun initBooksData() {
if (groupId == BookGroup.Companion.IdRoot) { if (groupId == BookGroup.IdRoot) {
if (isAdded) { if (isAdded) {
binding.collTopBar.title = getString(R.string.bookshelf) binding.collTopBar.title = getString(R.string.bookshelf)
binding.refreshLayout.isEnabled = true binding.refreshLayout.isEnabled = true
enableRefresh = true enableRefresh = true
} }
} else { } else {
bookGroups.firstOrNull { bookGroups.firstOrNull { groupId == it.groupId }?.let {
groupId == it.groupId
}?.let {
binding.collTopBar.title = it.groupName binding.collTopBar.title = it.groupName
binding.refreshLayout.isEnabled = it.enableRefresh binding.refreshLayout.isEnabled = it.enableRefresh
enableRefresh = it.enableRefresh enableRefresh = it.enableRefresh
@@ -263,11 +262,12 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
} }
booksFlowJob?.cancel() booksFlowJob?.cancel()
booksFlowJob = viewLifecycleOwner.lifecycleScope.launch { booksFlowJob = viewLifecycleOwner.lifecycleScope.launch {
appDb.bookDao.flowByGroup(groupId).map { list -> viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
//排序 appDb.bookDao.flowByGroup(groupId)
.map { list ->
val isDescending = AppConfig.bookshelfSortOrder == 1 val isDescending = AppConfig.bookshelfSortOrder == 1
val sortType = AppConfig.getBookSortByGroupId(groupId) val sortType = AppConfig.getBookSortByGroupId(groupId)
when (sortType) { when (sortType) {
1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime } 1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime }
else list.sortedBy { it.latestChapterTime } else list.sortedBy { it.latestChapterTime }
@@ -280,31 +280,31 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
3 -> if (isDescending) list.sortedByDescending { it.order } 3 -> if (isDescending) list.sortedByDescending { it.order }
else list.sortedBy { it.order } else list.sortedBy { it.order }
4 -> if (isDescending) list.sortedByDescending { 4 -> if (isDescending)
max(it.latestChapterTime, it.durChapterTime) list.sortedByDescending { max(it.latestChapterTime, it.durChapterTime) }
} else list.sortedBy { max(it.latestChapterTime, it.durChapterTime) } else
list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
5 -> if (isDescending) 5 -> if (isDescending)
list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) } list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) }
else else
list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) } list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) }
else -> if (isDescending) list.sortedByDescending { it.durChapterTime } else ->
if (isDescending) list.sortedByDescending { it.durChapterTime }
else list.sortedBy { it.durChapterTime } else list.sortedBy { it.durChapterTime }
} }
}
.flowOn(Dispatchers.Default)
.catch { AppLog.put("书架更新出错", it) }
.collect { list ->
}.flowWithLifecycleAndDatabaseChangeFirst(
viewLifecycleOwner.lifecycle,
Lifecycle.State.RESUMED,
AppDatabase.Companion.BOOK_TABLE_NAME
).catch {
AppLog.put("书架更新出错", it)
}.conflate().flowOn(Dispatchers.Default).collect { list ->
books = list books = list
booksAdapter.updateItems() booksAdapter.updateItems()
binding.emptyView.isGone = getItemCount() > 0 val empty = getItemCount() > 0
binding.refreshLayout.isEnabled = enableRefresh && getItemCount() > 0 binding.emptyView.isGone = empty
delay(500) binding.refreshLayout.isEnabled = enableRefresh && empty
}
} }
} }
} }