Kaynağa Gözat

为主页面添加预测性返回手势支持

HapeLee 1 yıl önce
ebeveyn
işleme
fcfd8bd4db

+ 32 - 24
app/src/main/java/io/legato/kazusa/ui/main/MainActivity.kt

@@ -11,6 +11,7 @@ import android.view.MenuItem
 import android.view.MotionEvent
 import android.view.MotionEvent
 import android.widget.FrameLayout
 import android.widget.FrameLayout
 import android.widget.ImageView
 import android.widget.ImageView
+import androidx.activity.OnBackPressedCallback
 import androidx.activity.addCallback
 import androidx.activity.addCallback
 import androidx.activity.viewModels
 import androidx.activity.viewModels
 import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
 import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
@@ -97,7 +98,7 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
     private val adapter by lazy {
     private val adapter by lazy {
         TabFragmentPageAdapter(this)
         TabFragmentPageAdapter(this)
     }
     }
-
+    private lateinit var backCallback: OnBackPressedCallback
     private val badge by lazy {
     private val badge by lazy {
         getNavigationBarView().getOrCreateBadge(R.id.menu_bookshelf)
         getNavigationBarView().getOrCreateBadge(R.id.menu_bookshelf)
     }
     }
@@ -107,6 +108,23 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
         recreate()
         recreate()
     }
     }
 
 
+    private fun setupBackCallback() {
+        backCallback = object : OnBackPressedCallback(false) {
+            override fun handleOnBackPressed() {
+                if (pagePosition != 0) {
+                    binding.viewPagerMain.currentItem = 0
+                    return
+                }
+
+                val fragment = fragmentMap[getFragmentId(0)] as? BookshelfFragment2
+                if (fragment?.back() == true) {
+                    return
+                }
+            }
+        }
+        onBackPressedDispatcher.addCallback(this, backCallback)
+    }
+
     override fun onCreate(savedInstanceState: Bundle?) {
     override fun onCreate(savedInstanceState: Bundle?) {
         installSplashScreen()
         installSplashScreen()
         super.onCreate(savedInstanceState)
         super.onCreate(savedInstanceState)
@@ -118,29 +136,8 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
             pagePosition = savedInstanceState.getInt("currentPagePosition", 0)
             pagePosition = savedInstanceState.getInt("currentPagePosition", 0)
         }
         }
 
 
-        onBackPressedDispatcher.addCallback(this) {
-            if (pagePosition != 0) {
-                binding.viewPagerMain.currentItem = 0
-                return@addCallback
-            }
-
-            (fragmentMap[getFragmentId(0)] as? BookshelfFragment2)?.let {
-                if (it.back()) return@addCallback
-            }
-
-            if (System.currentTimeMillis() - exitTime > 2000) {
-                toastOnUi(R.string.double_click_exit)
-                exitTime = System.currentTimeMillis()
-            } else {
-                if (BaseReadAloudService.pause) {
-                    finish()
-                } else {
-                    moveTaskToBack(true)
-                }
-            }
-        }
-
         // 其他初始化逻辑
         // 其他初始化逻辑
+        setupBackCallback()
         upBottomMenu()
         upBottomMenu()
         initView()
         initView()
         upHomePage()
         upHomePage()
@@ -511,9 +508,14 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
                 idMy -> R.id.menu_my_config
                 idMy -> R.id.menu_my_config
                 else -> R.id.menu_bookshelf
                 else -> R.id.menu_bookshelf
             }
             }
+            updateBackCallbackState()
         }
         }
     }
     }
 
 
+    private fun updateBackCallbackState() {
+        val fragment = fragmentMap[getFragmentId(0)] as? BookshelfFragment2
+        backCallback.isEnabled = (pagePosition != 0) || (fragment?.canHandleBack() == true)
+    }
 
 
     private inner class TabFragmentPageAdapter(
     private inner class TabFragmentPageAdapter(
         activity: FragmentActivity
         activity: FragmentActivity
@@ -524,7 +526,13 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
         override fun createFragment(position: Int): Fragment {
         override fun createFragment(position: Int): Fragment {
             val fragment = when (getFragmentId(position)) {
             val fragment = when (getFragmentId(position)) {
                 idBookshelf1 -> BookshelfFragment1(position)
                 idBookshelf1 -> BookshelfFragment1(position)
-                idBookshelf2 -> BookshelfFragment2(position)
+                idBookshelf2 -> {
+                    val bookshelfFragment = BookshelfFragment2(position)
+                    bookshelfFragment.setGroupIdChangeListener {
+                        updateBackCallbackState()
+                    }
+                    bookshelfFragment
+                }
                 idBookshelf3 -> BookshelfFragment3(position)
                 idBookshelf3 -> BookshelfFragment3(position)
                 idExplore -> ExploreFragment(position)
                 idExplore -> ExploreFragment(position)
                 idRss -> RssFragment(position)
                 idRss -> RssFragment(position)

+ 16 - 0
app/src/main/java/io/legato/kazusa/ui/main/bookshelf/books/BookshelfFragment2.kt

@@ -60,6 +60,10 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
     SearchView.OnQueryTextListener,
     SearchView.OnQueryTextListener,
     BaseBooksAdapter.CallBack {
     BaseBooksAdapter.CallBack {
 
 
+    fun interface OnGroupIdChangeListener {
+        fun onGroupIdChanged()
+    }
+
     constructor(position: Int) : this() {
     constructor(position: Int) : this() {
         val bundle = Bundle()
         val bundle = Bundle()
         bundle.putInt("position", position)
         bundle.putInt("position", position)
@@ -72,6 +76,12 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
 
 
     private val bookshelfLayoutGrid by lazy { requireContext().bookshelfLayoutGrid }
     private val bookshelfLayoutGrid by lazy { requireContext().bookshelfLayoutGrid }
 
 
+    private var groupIdChangeListener: OnGroupIdChangeListener? = null
+
+    fun setGroupIdChangeListener(listener: OnGroupIdChangeListener) {
+        this.groupIdChangeListener = listener
+    }
+
     private val booksAdapter: BaseBooksAdapter<*> by lazy {
     private val booksAdapter: BaseBooksAdapter<*> by lazy {
         when (bookshelfLayoutMode) {
         when (bookshelfLayoutMode) {
             0 -> {
             0 -> {
@@ -251,6 +261,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
         if (groupId != BookGroup.Companion.IdRoot) {
         if (groupId != BookGroup.Companion.IdRoot) {
             groupId = BookGroup.Companion.IdRoot
             groupId = BookGroup.Companion.IdRoot
             initBooksData()
             initBooksData()
+            groupIdChangeListener?.onGroupIdChanged()
             return true
             return true
         }
         }
         return false
         return false
@@ -302,6 +313,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
             is BookGroup -> {
             is BookGroup -> {
                 groupId = item.groupId
                 groupId = item.groupId
                 initBooksData()
                 initBooksData()
+                groupIdChangeListener?.onGroupIdChanged()
             }
             }
         }
         }
     }
     }
@@ -342,6 +354,10 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
         }
         }
     }
     }
 
 
+    fun canHandleBack(): Boolean {
+        return groupId != BookGroup.Companion.IdRoot
+    }
+
     override fun getItems(): List<Any> {
     override fun getItems(): List<Any> {
         return if (groupId == BookGroup.Companion.IdRoot) {
         return if (groupId == BookGroup.Companion.IdRoot) {
             bookGroups + books
             bookGroups + books