优化
This commit is contained in:
@@ -107,7 +107,11 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun setItems(items: List<ITEM>?, itemCallback: DiffUtil.ItemCallback<ITEM>) {
|
fun setItems(
|
||||||
|
items: List<ITEM>?,
|
||||||
|
itemCallback: DiffUtil.ItemCallback<ITEM>,
|
||||||
|
skipDiff: Boolean = false
|
||||||
|
) {
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
val oldItems = this.items.toList()
|
val oldItems = this.items.toList()
|
||||||
val callback = object : DiffUtil.Callback() {
|
val callback = object : DiffUtil.Callback() {
|
||||||
@@ -161,7 +165,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|||||||
onCurrentListChanged()
|
onCurrentListChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
handler.postDelayed(500) {
|
if (skipDiff) handler.postDelayed(500) {
|
||||||
if (diffJob?.isCompleted == false) {
|
if (diffJob?.isCompleted == false) {
|
||||||
diffJob?.cancel()
|
diffJob?.cancel()
|
||||||
setItems(items)
|
setItems(items)
|
||||||
|
|||||||
@@ -324,7 +324,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
|
|||||||
}.catch {
|
}.catch {
|
||||||
AppLog.put("书源界面更新书源出错", it)
|
AppLog.put("书源界面更新书源出错", it)
|
||||||
}.flowOn(IO).conflate().collect { data ->
|
}.flowOn(IO).conflate().collect { data ->
|
||||||
adapter.setItems(data, adapter.diffItemCallback)
|
adapter.setItems(data, adapter.diffItemCallback, !Debug.isChecking)
|
||||||
itemTouchCallback.isCanDrag = sort == BookSourceSort.Default
|
itemTouchCallback.isCanDrag = sort == BookSourceSort.Default
|
||||||
delay(500)
|
delay(500)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,13 +71,16 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
|
|||||||
private val fragmentMap = hashMapOf<Int, Fragment>()
|
private val fragmentMap = hashMapOf<Int, Fragment>()
|
||||||
private var bottomMenuCount = 4
|
private var bottomMenuCount = 4
|
||||||
private val realPositions = arrayOf(idBookshelf, idExplore, idRss, idMy)
|
private val realPositions = arrayOf(idBookshelf, idExplore, idRss, idMy)
|
||||||
|
private val adapter by lazy {
|
||||||
|
TabFragmentPageAdapter(supportFragmentManager)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||||
upBottomMenu()
|
upBottomMenu()
|
||||||
binding.run {
|
binding.run {
|
||||||
viewPagerMain.setEdgeEffectColor(primaryColor)
|
viewPagerMain.setEdgeEffectColor(primaryColor)
|
||||||
viewPagerMain.offscreenPageLimit = 3
|
viewPagerMain.offscreenPageLimit = 3
|
||||||
viewPagerMain.adapter = TabFragmentPageAdapter(supportFragmentManager)
|
viewPagerMain.adapter = adapter
|
||||||
viewPagerMain.addOnPageChangeListener(PageChangeCallback())
|
viewPagerMain.addOnPageChangeListener(PageChangeCallback())
|
||||||
bottomNavigationView.elevation = elevation
|
bottomNavigationView.elevation = elevation
|
||||||
bottomNavigationView.setOnNavigationItemSelectedListener(this@MainActivity)
|
bottomNavigationView.setOnNavigationItemSelectedListener(this@MainActivity)
|
||||||
@@ -350,9 +353,14 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
|
|||||||
private inner class PageChangeCallback : ViewPager.SimpleOnPageChangeListener() {
|
private inner class PageChangeCallback : ViewPager.SimpleOnPageChangeListener() {
|
||||||
|
|
||||||
override fun onPageSelected(position: Int) {
|
override fun onPageSelected(position: Int) {
|
||||||
|
val oldPosition = pagePosition
|
||||||
pagePosition = position
|
pagePosition = position
|
||||||
binding.bottomNavigationView.menu
|
binding.bottomNavigationView.menu
|
||||||
.getItem(realPositions[position]).isChecked = true
|
.getItem(realPositions[position]).isChecked = true
|
||||||
|
val callback1 = fragmentMap[getFragmentId(position)] as? Callback
|
||||||
|
val callback2 = fragmentMap[getFragmentId(oldPosition)] as? Callback
|
||||||
|
callback1?.onActive()
|
||||||
|
callback2?.onInactive()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -391,4 +399,10 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Callback {
|
||||||
|
fun onActive()
|
||||||
|
|
||||||
|
fun onInactive()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -23,6 +23,7 @@ import io.legado.app.lib.theme.primaryColor
|
|||||||
import io.legado.app.lib.theme.primaryTextColor
|
import io.legado.app.lib.theme.primaryTextColor
|
||||||
import io.legado.app.ui.book.explore.ExploreShowActivity
|
import io.legado.app.ui.book.explore.ExploreShowActivity
|
||||||
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
|
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
|
||||||
|
import io.legado.app.ui.main.MainActivity
|
||||||
import io.legado.app.utils.applyTint
|
import io.legado.app.utils.applyTint
|
||||||
import io.legado.app.utils.cnCompare
|
import io.legado.app.utils.cnCompare
|
||||||
import io.legado.app.utils.setEdgeEffectColor
|
import io.legado.app.utils.setEdgeEffectColor
|
||||||
@@ -31,6 +32,8 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.cancel
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.catch
|
import kotlinx.coroutines.flow.catch
|
||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
@@ -40,7 +43,7 @@ import kotlinx.coroutines.launch
|
|||||||
* 发现界面
|
* 发现界面
|
||||||
*/
|
*/
|
||||||
class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explore),
|
class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explore),
|
||||||
ExploreAdapter.CallBack {
|
ExploreAdapter.CallBack, MainActivity.Callback {
|
||||||
|
|
||||||
override val viewModel by viewModels<ExploreViewModel>()
|
override val viewModel by viewModels<ExploreViewModel>()
|
||||||
private val binding by viewBinding(FragmentExploreBinding::bind)
|
private val binding by viewBinding(FragmentExploreBinding::bind)
|
||||||
@@ -53,13 +56,14 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
|
|||||||
private val groups = linkedSetOf<String>()
|
private val groups = linkedSetOf<String>()
|
||||||
private var exploreFlowJob: Job? = null
|
private var exploreFlowJob: Job? = null
|
||||||
private var groupsMenu: SubMenu? = null
|
private var groupsMenu: SubMenu? = null
|
||||||
|
private var isActive = false
|
||||||
|
|
||||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
setSupportToolbar(binding.titleBar.toolbar)
|
setSupportToolbar(binding.titleBar.toolbar)
|
||||||
initSearchView()
|
initSearchView()
|
||||||
initRecyclerView()
|
initRecyclerView()
|
||||||
initGroupData()
|
initGroupData()
|
||||||
upExploreData()
|
upExploreData(once = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCompatCreateOptionsMenu(menu: Menu) {
|
override fun onCompatCreateOptionsMenu(menu: Menu) {
|
||||||
@@ -72,6 +76,7 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
|
|||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
searchView.clearFocus()
|
searchView.clearFocus()
|
||||||
|
exploreFlowJob?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initSearchView() {
|
private fun initSearchView() {
|
||||||
@@ -117,25 +122,29 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun upExploreData(searchKey: String? = null) {
|
private fun upExploreData(searchKey: String? = null, once: Boolean = false) {
|
||||||
exploreFlowJob?.cancel()
|
exploreFlowJob?.cancel()
|
||||||
exploreFlowJob = launch {
|
exploreFlowJob = launch {
|
||||||
when {
|
when {
|
||||||
searchKey.isNullOrBlank() -> {
|
searchKey.isNullOrBlank() -> {
|
||||||
appDb.bookSourceDao.flowExplore()
|
appDb.bookSourceDao.flowExplore()
|
||||||
}
|
}
|
||||||
|
|
||||||
searchKey.startsWith("group:") -> {
|
searchKey.startsWith("group:") -> {
|
||||||
val key = searchKey.substringAfter("group:")
|
val key = searchKey.substringAfter("group:")
|
||||||
appDb.bookSourceDao.flowGroupExplore(key)
|
appDb.bookSourceDao.flowGroupExplore(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
appDb.bookSourceDao.flowExplore(searchKey)
|
appDb.bookSourceDao.flowExplore(searchKey)
|
||||||
}
|
}
|
||||||
}.catch {
|
}.catch {
|
||||||
AppLog.put("发现界面更新数据出错", it)
|
AppLog.put("发现界面更新数据出错", it)
|
||||||
}.conflate().collect {
|
}.conflate().flowOn(IO).collect {
|
||||||
binding.tvEmptyMsg.isGone = it.isNotEmpty() || searchView.query.isNotEmpty()
|
binding.tvEmptyMsg.isGone = it.isNotEmpty() || searchView.query.isNotEmpty()
|
||||||
adapter.setItems(it, diffItemCallBack)
|
adapter.setItems(it, diffItemCallBack)
|
||||||
|
if (once) cancel()
|
||||||
|
delay(500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -202,4 +211,19 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
override fun onActive() {
|
||||||
|
isActive = true
|
||||||
|
upExploreData()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onInactive() {
|
||||||
|
isActive = false
|
||||||
|
exploreFlowJob?.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
if (isActive) upExploreData()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user