Quellcode durchsuchen

修复书籍的加载问题,这次真的修了孩子们

HapeLee vor 1 Jahr
Ursprung
Commit
2de4ced6bc

+ 53 - 37
app/src/main/java/io/legato/kazusa/ui/book/info/BookInfoActivity.kt

@@ -18,10 +18,12 @@ import android.os.Build
 import android.os.Bundle
 import android.os.Bundle
 import android.view.Menu
 import android.view.Menu
 import android.view.MenuItem
 import android.view.MenuItem
+import android.view.View
 import android.widget.CheckBox
 import android.widget.CheckBox
 import android.widget.LinearLayout
 import android.widget.LinearLayout
-import androidx.activity.result.contract.ActivityResultContracts
 import androidx.activity.viewModels
 import androidx.activity.viewModels
+import androidx.core.app.ActivityCompat
+import androidx.core.app.ActivityOptionsCompat
 import androidx.core.content.ContextCompat
 import androidx.core.content.ContextCompat
 import androidx.core.graphics.scale
 import androidx.core.graphics.scale
 import androidx.core.view.doOnPreDraw
 import androidx.core.view.doOnPreDraw
@@ -102,6 +104,10 @@ class BookInfoActivity :
     ChangeCoverDialog.CallBack,
     ChangeCoverDialog.CallBack,
     VariableDialog.Callback {
     VariableDialog.Callback {
 
 
+    companion object {
+        private const val READ_BOOK_REQUEST_CODE = 1001
+    }
+
     private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
     private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
         it?.let {
         it?.let {
             viewModel.getBook(false)?.let { book ->
             viewModel.getBook(false)?.let { book ->
@@ -112,7 +118,7 @@ class BookInfoActivity :
                         chapterChanged = it.third
                         chapterChanged = it.third
                         appDb.bookDao.update(book)
                         appDb.bookDao.update(book)
                     }
                     }
-                    startReadActivity(book)
+                    startReadActivity(book, binding.btnRead)
                 }
                 }
             }
             }
         } ?: let {
         } ?: let {
@@ -126,22 +132,6 @@ class BookInfoActivity :
             AppConfig.defaultBookTreeUri = treeUri.toString()
             AppConfig.defaultBookTreeUri = treeUri.toString()
         }
         }
     }
     }
-    private val readBookResult = registerForActivityResult(
-        ActivityResultContracts.StartActivityForResult()
-    ) {
-        viewModel.upBook(intent)
-        when (it.resultCode) {
-            RESULT_OK -> {
-                viewModel.inBookshelf = true
-                upTvBookshelf()
-            }
-
-            RESULT_DELETED -> {
-                setResult(RESULT_OK)
-                finish()
-            }
-        }
-    }
     private val infoEditResult = registerForActivityResult(
     private val infoEditResult = registerForActivityResult(
         StartActivityContract(BookInfoEditActivity::class.java)
         StartActivityContract(BookInfoEditActivity::class.java)
     ) {
     ) {
@@ -358,6 +348,23 @@ class BookInfoActivity :
         }
         }
     }
     }
 
 
+    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
+        super.onActivityResult(requestCode, resultCode, data)
+        if (requestCode == READ_BOOK_REQUEST_CODE) {
+            when (resultCode) {
+                RESULT_OK -> {
+                    viewModel.inBookshelf = true
+                    upTvBookshelf()
+                }
+
+                RESULT_DELETED -> {
+                    setResult(RESULT_OK)
+                    finish()
+                }
+            }
+        }
+    }
+
 //    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
 //    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
 //        if (ev.action == MotionEvent.ACTION_DOWN) {
 //        if (ev.action == MotionEvent.ACTION_DOWN) {
 //            currentFocus?.let {
 //            currentFocus?.let {
@@ -965,35 +972,44 @@ class BookInfoActivity :
             book.addType(BookType.notShelf)
             book.addType(BookType.notShelf)
             viewModel.saveBook(book) {
             viewModel.saveBook(book) {
                 viewModel.saveChapterList {
                 viewModel.saveChapterList {
-                    startReadActivity(book)
+                    startReadActivity(book, binding.btnRead)
                 }
                 }
             }
             }
         } else {
         } else {
             viewModel.saveBook(book) {
             viewModel.saveBook(book) {
-                startReadActivity(book)
+                startReadActivity(book, binding.btnRead)
             }
             }
         }
         }
     }
     }
 
 
-    private fun startReadActivity(book: Book) {
-        when {
-            book.isAudio -> readBookResult.launch(
-                Intent(this, AudioPlayActivity::class.java)
-                    .putExtra("bookUrl", book.bookUrl)
-                    .putExtra("inBookshelf", viewModel.inBookshelf)
-            )
+    private fun startReadActivity(book: Book, sharedView: View) {
+        val transitionName = "book_${book.bookUrl}_${System.currentTimeMillis()}"
+        sharedView.transitionName = transitionName
 
 
-            else -> readBookResult.launch(
-                Intent(
-                    this,
-                    if (!book.isLocal && book.isImage && AppConfig.showMangaUi) ReadMangaActivity::class.java
-                    else ReadBookActivity::class.java
-                )
-                    .putExtra("bookUrl", book.bookUrl)
-                    .putExtra("inBookshelf", viewModel.inBookshelf)
-                    .putExtra("chapterChanged", chapterChanged)
-            )
+        val cls = when {
+            book.isAudio -> AudioPlayActivity::class.java
+            !book.isLocal && book.isImage && AppConfig.showMangaUi -> ReadMangaActivity::class.java
+            else -> ReadBookActivity::class.java
         }
         }
+
+        val intent = Intent(this, cls).apply {
+            putExtra("bookUrl", book.bookUrl)
+            putExtra("inBookshelf", viewModel.inBookshelf)
+            putExtra("chapterChanged", chapterChanged)
+            putExtra("transitionName", transitionName)
+        }
+
+        val options = ActivityOptionsCompat.makeSceneTransitionAnimation(
+            this,
+            sharedView,
+            transitionName
+        )
+        ActivityCompat.startActivityForResult(
+            this,
+            intent,
+            READ_BOOK_REQUEST_CODE,
+            options.toBundle()
+        )
     }
     }
 
 
     override val oldBook: Book?
     override val oldBook: Book?

+ 18 - 28
app/src/main/java/io/legato/kazusa/ui/book/read/ReadBookActivity.kt

@@ -6,7 +6,6 @@ import android.content.Intent
 import android.content.res.Configuration
 import android.content.res.Configuration
 import android.graphics.Color
 import android.graphics.Color
 import android.os.Bundle
 import android.os.Bundle
-import android.transition.Transition
 import android.view.Gravity
 import android.view.Gravity
 import android.view.HapticFeedbackConstants
 import android.view.HapticFeedbackConstants
 import android.view.InputDevice
 import android.view.InputDevice
@@ -250,6 +249,16 @@ class ReadBookActivity : BaseReadBookActivity(),
     private var justInitData: Boolean = false
     private var justInitData: Boolean = false
     private var syncDialog: AlertDialog? = null
     private var syncDialog: AlertDialog? = null
 
 
+    private val delayMillis: Long = 500
+    private var hasLoadedBook = false
+
+    private fun ensureLoadBook() {
+        if (!hasLoadedBook) {
+            hasLoadedBook = true
+            loadBook()
+        }
+    }
+
     @SuppressLint("ClickableViewAccessibility")
     @SuppressLint("ClickableViewAccessibility")
     override fun onCreate(savedInstanceState: Bundle?) {
     override fun onCreate(savedInstanceState: Bundle?) {
 
 
@@ -258,40 +267,17 @@ class ReadBookActivity : BaseReadBookActivity(),
             val transform = MaterialContainerTransform().apply {
             val transform = MaterialContainerTransform().apply {
                 addTarget(binding.rootView)
                 addTarget(binding.rootView)
                 scrimColor = Color.TRANSPARENT
                 scrimColor = Color.TRANSPARENT
-                duration = 500
+                duration = delayMillis
             }
             }
             window.sharedElementEnterTransition = transform
             window.sharedElementEnterTransition = transform
 
 
-            if (AppConfig.delayBookLoadEnable) {
-                window.sharedElementEnterTransition?.addListener(object :
-                    Transition.TransitionListener {
-                    override fun onTransitionStart(transition: Transition) {}
-                    override fun onTransitionEnd(transition: Transition) {
-                        loadBook()
-                        transition.removeListener(this)
-                    }
-
-                    override fun onTransitionCancel(transition: Transition) {
-                        loadBook()
-                        transition.removeListener(this)
-                    }
-
-                    override fun onTransitionPause(transition: Transition) {}
-                    override fun onTransitionResume(transition: Transition) {}
-                })
-            }
-
         }
         }
         window.sharedElementReturnTransition =
         window.sharedElementReturnTransition =
             MaterialSharedAxis(MaterialSharedAxis.X, true).apply {
             MaterialSharedAxis(MaterialSharedAxis.X, true).apply {
-                duration = 300
+                duration = delayMillis
             }
             }
         super.onCreate(savedInstanceState)
         super.onCreate(savedInstanceState)
 
 
-        if (savedInstanceState != null && AppConfig.delayBookLoadEnable) {
-            loadBook()
-        }
-
         if (AppConfig.sharedElementEnterTransitionEnable)
         if (AppConfig.sharedElementEnterTransitionEnable)
             binding.rootView.transitionName = intent.getStringExtra("transitionName")
             binding.rootView.transitionName = intent.getStringExtra("transitionName")
 
 
@@ -330,9 +316,13 @@ class ReadBookActivity : BaseReadBookActivity(),
 
 
     override fun onPostCreate(savedInstanceState: Bundle?) {
     override fun onPostCreate(savedInstanceState: Bundle?) {
         super.onPostCreate(savedInstanceState)
         super.onPostCreate(savedInstanceState)
-        if (!AppConfig.delayBookLoadEnable || !AppConfig.sharedElementEnterTransitionEnable) {
-            loadBook()
+        if (!AppConfig.sharedElementEnterTransitionEnable || !AppConfig.delayBookLoadEnable) {
+            ensureLoadBook()
         }
         }
+        //虽然很扯但有用
+        binding.rootView.postDelayed({
+            ensureLoadBook()
+        }, delayMillis)
     }
     }
 
 
     override fun onNewIntent(intent: Intent) {
     override fun onNewIntent(intent: Intent) {