From 3e4b416fe772192c668e9f68b2ece24a861d89d9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 13 Feb 2022 23:08:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/source/manage/BookSourceActivity.kt | 116 +++++++++--------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 057baa03f..e21aaaa78 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -42,7 +42,9 @@ import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.* -import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.flow class BookSourceActivity : VMBaseActivity(), PopupMenu.OnMenuItemClickListener, @@ -208,67 +210,65 @@ class BookSourceActivity : VMBaseActivity { - appDb.bookSourceDao.flowAll() - } - searchKey == getString(R.string.enabled) -> { - appDb.bookSourceDao.flowEnabled() - } - searchKey == getString(R.string.disabled) -> { - appDb.bookSourceDao.flowDisabled() - } - searchKey == getString(R.string.need_login) -> { - appDb.bookSourceDao.flowLogin() - } - searchKey.startsWith("group:") -> { - val key = searchKey.substringAfter("group:") - appDb.bookSourceDao.flowGroupSearch("%$key%") - } - else -> { - appDb.bookSourceDao.flowSearch("%$searchKey%") - } - }.collect { data -> - val sourceList = - if (sortAscending) when (sort) { - Sort.Weight -> data.sortedBy { it.weight } - Sort.Name -> data.sortedWith { o1, o2 -> - o1.bookSourceName.cnCompare(o2.bookSourceName) - } - Sort.Url -> data.sortedBy { it.bookSourceUrl } - Sort.Update -> data.sortedByDescending { it.lastUpdateTime } - Sort.Respond -> data.sortedBy { it.respondTime } - Sort.Enable -> data.sortedWith { o1, o2 -> - var sort = -o1.enabled.compareTo(o2.enabled) - if (sort == 0) { - sort = o1.bookSourceName.cnCompare(o2.bookSourceName) - } - sort - } - else -> data + when { + searchKey.isNullOrEmpty() -> { + appDb.bookSourceDao.flowAll() + } + searchKey == getString(R.string.enabled) -> { + appDb.bookSourceDao.flowEnabled() + } + searchKey == getString(R.string.disabled) -> { + appDb.bookSourceDao.flowDisabled() + } + searchKey == getString(R.string.need_login) -> { + appDb.bookSourceDao.flowLogin() + } + searchKey.startsWith("group:") -> { + val key = searchKey.substringAfter("group:") + appDb.bookSourceDao.flowGroupSearch("%$key%") + } + else -> { + appDb.bookSourceDao.flowSearch("%$searchKey%") + } + }.catch { + AppLog.put("书源界面更新书源出错", it) + }.collectLatest { data -> + val sourceList = + if (sortAscending) when (sort) { + Sort.Weight -> data.sortedBy { it.weight } + Sort.Name -> data.sortedWith { o1, o2 -> + o1.bookSourceName.cnCompare(o2.bookSourceName) } - else when (sort) { - Sort.Weight -> data.sortedByDescending { it.weight } - Sort.Name -> data.sortedWith { o1, o2 -> - o2.bookSourceName.cnCompare(o1.bookSourceName) + Sort.Url -> data.sortedBy { it.bookSourceUrl } + Sort.Update -> data.sortedByDescending { it.lastUpdateTime } + Sort.Respond -> data.sortedBy { it.respondTime } + Sort.Enable -> data.sortedWith { o1, o2 -> + var sort = -o1.enabled.compareTo(o2.enabled) + if (sort == 0) { + sort = o1.bookSourceName.cnCompare(o2.bookSourceName) } - Sort.Url -> data.sortedByDescending { it.bookSourceUrl } - Sort.Update -> data.sortedBy { it.lastUpdateTime } - Sort.Respond -> data.sortedByDescending { it.respondTime } - Sort.Enable -> data.sortedWith { o1, o2 -> - var sort = o1.enabled.compareTo(o2.enabled) - if (sort == 0) { - sort = o1.bookSourceName.cnCompare(o2.bookSourceName) - } - sort + sort + } + else -> data + } + else when (sort) { + Sort.Weight -> data.sortedByDescending { it.weight } + Sort.Name -> data.sortedWith { o1, o2 -> + o2.bookSourceName.cnCompare(o1.bookSourceName) + } + Sort.Url -> data.sortedByDescending { it.bookSourceUrl } + Sort.Update -> data.sortedBy { it.lastUpdateTime } + Sort.Respond -> data.sortedByDescending { it.respondTime } + Sort.Enable -> data.sortedWith { o1, o2 -> + var sort = o1.enabled.compareTo(o2.enabled) + if (sort == 0) { + sort = o1.bookSourceName.cnCompare(o2.bookSourceName) } - else -> data.reversed() + sort } - adapter.setItems(sourceList, adapter.diffItemCallback) - } - }.onFailure { - AppLog.put("更新书源出错", it) + else -> data.reversed() + } + adapter.setItems(sourceList, adapter.diffItemCallback) } } }