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

This commit is contained in:
HapeLee
2025-09-12 01:59:06 +08:00
parent e2dccabdd5
commit 2de4ced6bc
2 changed files with 71 additions and 65 deletions
@@ -18,10 +18,12 @@ import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.CheckBox
import android.widget.LinearLayout
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.content.ContextCompat
import androidx.core.graphics.scale
import androidx.core.view.doOnPreDraw
@@ -102,6 +104,10 @@ class BookInfoActivity :
ChangeCoverDialog.CallBack,
VariableDialog.Callback {
companion object {
private const val READ_BOOK_REQUEST_CODE = 1001
}
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
it?.let {
viewModel.getBook(false)?.let { book ->
@@ -112,7 +118,7 @@ class BookInfoActivity :
chapterChanged = it.third
appDb.bookDao.update(book)
}
startReadActivity(book)
startReadActivity(book, binding.btnRead)
}
}
} ?: let {
@@ -126,22 +132,6 @@ class BookInfoActivity :
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(
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 {
// if (ev.action == MotionEvent.ACTION_DOWN) {
// currentFocus?.let {
@@ -965,35 +972,44 @@ class BookInfoActivity :
book.addType(BookType.notShelf)
viewModel.saveBook(book) {
viewModel.saveChapterList {
startReadActivity(book)
startReadActivity(book, binding.btnRead)
}
}
} else {
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?
@@ -6,7 +6,6 @@ import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import android.transition.Transition
import android.view.Gravity
import android.view.HapticFeedbackConstants
import android.view.InputDevice
@@ -250,6 +249,16 @@ class ReadBookActivity : BaseReadBookActivity(),
private var justInitData: Boolean = false
private var syncDialog: AlertDialog? = null
private val delayMillis: Long = 500
private var hasLoadedBook = false
private fun ensureLoadBook() {
if (!hasLoadedBook) {
hasLoadedBook = true
loadBook()
}
}
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
@@ -258,40 +267,17 @@ class ReadBookActivity : BaseReadBookActivity(),
val transform = MaterialContainerTransform().apply {
addTarget(binding.rootView)
scrimColor = Color.TRANSPARENT
duration = 500
duration = delayMillis
}
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 =
MaterialSharedAxis(MaterialSharedAxis.X, true).apply {
duration = 300
duration = delayMillis
}
super.onCreate(savedInstanceState)
if (savedInstanceState != null && AppConfig.delayBookLoadEnable) {
loadBook()
}
if (AppConfig.sharedElementEnterTransitionEnable)
binding.rootView.transitionName = intent.getStringExtra("transitionName")
@@ -330,9 +316,13 @@ class ReadBookActivity : BaseReadBookActivity(),
override fun onPostCreate(savedInstanceState: Bundle?) {
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) {