Horis 3 лет назад
Родитель
Сommit
4844685bf5

+ 6 - 2
app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt

@@ -107,7 +107,11 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
     }
 
     @Synchronized
-    fun setItems(items: List<ITEM>?, itemCallback: DiffUtil.ItemCallback<ITEM>) {
+    fun setItems(
+        items: List<ITEM>?,
+        itemCallback: DiffUtil.ItemCallback<ITEM>,
+        skipDiff: Boolean = false
+    ) {
         kotlin.runCatching {
             val oldItems = this.items.toList()
             val callback = object : DiffUtil.Callback() {
@@ -161,7 +165,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
                     onCurrentListChanged()
                 }
             }
-            handler.postDelayed(500) {
+            if (skipDiff) handler.postDelayed(500) {
                 if (diffJob?.isCompleted == false) {
                     diffJob?.cancel()
                     setItems(items)

+ 1 - 1
app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt

@@ -324,7 +324,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
             }.catch {
                 AppLog.put("书源界面更新书源出错", it)
             }.flowOn(IO).conflate().collect { data ->
-                adapter.setItems(data, adapter.diffItemCallback)
+                adapter.setItems(data, adapter.diffItemCallback, !Debug.isChecking)
                 itemTouchCallback.isCanDrag = sort == BookSourceSort.Default
                 delay(500)
             }

+ 15 - 1
app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@@ -71,13 +71,16 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
     private val fragmentMap = hashMapOf<Int, Fragment>()
     private var bottomMenuCount = 4
     private val realPositions = arrayOf(idBookshelf, idExplore, idRss, idMy)
+    private val adapter by lazy {
+        TabFragmentPageAdapter(supportFragmentManager)
+    }
 
     override fun onActivityCreated(savedInstanceState: Bundle?) {
         upBottomMenu()
         binding.run {
             viewPagerMain.setEdgeEffectColor(primaryColor)
             viewPagerMain.offscreenPageLimit = 3
-            viewPagerMain.adapter = TabFragmentPageAdapter(supportFragmentManager)
+            viewPagerMain.adapter = adapter
             viewPagerMain.addOnPageChangeListener(PageChangeCallback())
             bottomNavigationView.elevation = elevation
             bottomNavigationView.setOnNavigationItemSelectedListener(this@MainActivity)
@@ -350,9 +353,14 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
     private inner class PageChangeCallback : ViewPager.SimpleOnPageChangeListener() {
 
         override fun onPageSelected(position: Int) {
+            val oldPosition = pagePosition
             pagePosition = position
             binding.bottomNavigationView.menu
                 .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()
+    }
+
 }

+ 29 - 5
app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt

@@ -23,6 +23,7 @@ import io.legado.app.lib.theme.primaryColor
 import io.legado.app.lib.theme.primaryTextColor
 import io.legado.app.ui.book.explore.ExploreShowActivity
 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.cnCompare
 import io.legado.app.utils.setEdgeEffectColor
@@ -31,6 +32,8 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.Job
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.delay
 import kotlinx.coroutines.flow.catch
 import kotlinx.coroutines.flow.conflate
 import kotlinx.coroutines.flow.flowOn
@@ -40,7 +43,7 @@ import kotlinx.coroutines.launch
  * 发现界面
  */
 class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explore),
-    ExploreAdapter.CallBack {
+    ExploreAdapter.CallBack, MainActivity.Callback {
 
     override val viewModel by viewModels<ExploreViewModel>()
     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 var exploreFlowJob: Job? = null
     private var groupsMenu: SubMenu? = null
+    private var isActive = false
 
     override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
         setSupportToolbar(binding.titleBar.toolbar)
         initSearchView()
         initRecyclerView()
         initGroupData()
-        upExploreData()
+        upExploreData(once = true)
     }
 
     override fun onCompatCreateOptionsMenu(menu: Menu) {
@@ -72,6 +76,7 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
     override fun onPause() {
         super.onPause()
         searchView.clearFocus()
+        exploreFlowJob?.cancel()
     }
 
     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 = launch {
             when {
                 searchKey.isNullOrBlank() -> {
                     appDb.bookSourceDao.flowExplore()
                 }
+
                 searchKey.startsWith("group:") -> {
                     val key = searchKey.substringAfter("group:")
                     appDb.bookSourceDao.flowGroupExplore(key)
                 }
+
                 else -> {
                     appDb.bookSourceDao.flowExplore(searchKey)
                 }
             }.catch {
                 AppLog.put("发现界面更新数据出错", it)
-            }.conflate().collect {
+            }.conflate().flowOn(IO).collect {
                 binding.tvEmptyMsg.isGone = it.isNotEmpty() || searchView.query.isNotEmpty()
                 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()
+    }
+
+}