pull/1625/head
kunfei 3 years ago
parent a0d6c9942d
commit 3e4b416fe7
  1. 116
      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.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.* 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<ActivityBookSourceBinding, BookSourceViewModel>(), class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceViewModel>(),
PopupMenu.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener,
@ -208,67 +210,65 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
private fun upBookSource(searchKey: String? = null) { private fun upBookSource(searchKey: String? = null) {
sourceFlowJob?.cancel() sourceFlowJob?.cancel()
sourceFlowJob = launch { sourceFlowJob = launch {
runCatching { when {
when { searchKey.isNullOrEmpty() -> {
searchKey.isNullOrEmpty() -> { appDb.bookSourceDao.flowAll()
appDb.bookSourceDao.flowAll() }
} searchKey == getString(R.string.enabled) -> {
searchKey == getString(R.string.enabled) -> { appDb.bookSourceDao.flowEnabled()
appDb.bookSourceDao.flowEnabled() }
} searchKey == getString(R.string.disabled) -> {
searchKey == getString(R.string.disabled) -> { appDb.bookSourceDao.flowDisabled()
appDb.bookSourceDao.flowDisabled() }
} searchKey == getString(R.string.need_login) -> {
searchKey == getString(R.string.need_login) -> { appDb.bookSourceDao.flowLogin()
appDb.bookSourceDao.flowLogin() }
} searchKey.startsWith("group:") -> {
searchKey.startsWith("group:") -> { val key = searchKey.substringAfter("group:")
val key = searchKey.substringAfter("group:") appDb.bookSourceDao.flowGroupSearch("%$key%")
appDb.bookSourceDao.flowGroupSearch("%$key%") }
} else -> {
else -> { appDb.bookSourceDao.flowSearch("%$searchKey%")
appDb.bookSourceDao.flowSearch("%$searchKey%") }
} }.catch {
}.collect { data -> AppLog.put("书源界面更新书源出错", it)
val sourceList = }.collectLatest { data ->
if (sortAscending) when (sort) { val sourceList =
Sort.Weight -> data.sortedBy { it.weight } if (sortAscending) when (sort) {
Sort.Name -> data.sortedWith { o1, o2 -> Sort.Weight -> data.sortedBy { it.weight }
o1.bookSourceName.cnCompare(o2.bookSourceName) 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
} }
else when (sort) { Sort.Url -> data.sortedBy { it.bookSourceUrl }
Sort.Weight -> data.sortedByDescending { it.weight } Sort.Update -> data.sortedByDescending { it.lastUpdateTime }
Sort.Name -> data.sortedWith { o1, o2 -> Sort.Respond -> data.sortedBy { it.respondTime }
o2.bookSourceName.cnCompare(o1.bookSourceName) 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
Sort.Update -> data.sortedBy { it.lastUpdateTime } }
Sort.Respond -> data.sortedByDescending { it.respondTime } else -> data
Sort.Enable -> data.sortedWith { o1, o2 -> }
var sort = o1.enabled.compareTo(o2.enabled) else when (sort) {
if (sort == 0) { Sort.Weight -> data.sortedByDescending { it.weight }
sort = o1.bookSourceName.cnCompare(o2.bookSourceName) Sort.Name -> data.sortedWith { o1, o2 ->
} o2.bookSourceName.cnCompare(o1.bookSourceName)
sort }
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) else -> data.reversed()
} }
}.onFailure { adapter.setItems(sourceList, adapter.diffItemCallback)
AppLog.put("更新书源出错", it)
} }
} }
} }

Loading…
Cancel
Save