Fix reader not loading under certain circumstances (#128)

* Revert "Fix reader bug on certain orientation settings"

This reverts commit 6e59583dea.

* Revert "Skip `buildProvider` if the activity will restart"

This reverts commit a880f801fb.

* Preserve gallery provider in view model

(cherry picked from commit e2f024ae29)
This commit is contained in:
FooIbar committed 2023-10-29 03:29:34 +08:00
1 parent d1e3965d62
commit 2f39359b47
2 files changed
+25 -22

No files matched your search

@@ -28,8 +28,10 @@ import java.util.Locale
class EhPageLoader(private val mGalleryInfo: GalleryInfo) : PageLoader2(), OnSpiderListener {
private lateinit var mSpiderQueen: SpiderQueen
override fun start() {
mSpiderQueen = obtainSpiderQueen(mGalleryInfo, SpiderQueen.MODE_READ)
mSpiderQueen.addOnSpiderListener(this)
if (!::mSpiderQueen.isInitialized) {
mSpiderQueen = obtainSpiderQueen(mGalleryInfo, SpiderQueen.MODE_READ)
mSpiderQueen.addOnSpiderListener(this)
}
}
override fun stop() {
@@ -45,6 +45,7 @@ import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.contract.ActivityResultContracts.CreateDocument
import androidx.activity.viewModels
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
@@ -59,6 +60,7 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import com.google.android.material.shape.MaterialShapeDrawable
import com.hippo.ehviewer.BuildConfig
@@ -78,9 +80,12 @@ import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.FileUtils
import com.hippo.ehviewer.util.getParcelableCompat
import com.hippo.ehviewer.util.getParcelableExtraCompat
import com.hippo.ehviewer.util.getValue
import com.hippo.ehviewer.util.isAtLeastO
import com.hippo.ehviewer.util.isAtLeastP
import com.hippo.ehviewer.util.lazyMut
import com.hippo.ehviewer.util.sendTo
import com.hippo.ehviewer.util.setValue
import com.hippo.unifile.UniFile
import dev.chrisbanes.insetter.applyInsetter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
@@ -116,6 +121,15 @@ import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.suspendCancellableCoroutine
import splitties.systemservices.clipboardManager
class GalleryModel : ViewModel() {
var galleryProvider: PageLoader2? = null
override fun onCleared() {
super.onCleared()
galleryProvider?.stop()
galleryProvider = null
}
}
class ReaderActivity : EhActivity() {
lateinit var binding: ReaderActivityBinding
private var mAction: String? = null
@@ -124,6 +138,7 @@ class ReaderActivity : EhActivity() {
private var mGalleryInfo: BaseGalleryInfo? = null
private var mPage: Int = 0
private var mCacheFileName: String? = null
private val vm: GalleryModel by viewModels()
/**
* Whether the menu is currently visible.
@@ -159,7 +174,7 @@ class ReaderActivity : EhActivity() {
}
}
}
var mGalleryProvider: PageLoader2? = null
var mGalleryProvider by lazyMut { vm::galleryProvider }
private var mCurrentIndex: Int = 0
private var mSavingPage = -1
private lateinit var builder: EditTextDialogBuilder
@@ -190,13 +205,6 @@ class ReaderActivity : EhActivity() {
}
private fun buildProvider(replace: Boolean = false) {
// TODO: Better solution for this dirty workaround
if (setOrientation(ReaderPreferences.defaultOrientationType().get())) {
// Orientation changed, we don't know if the activity will recreate, just recreate anyway
recreate()
return
}
if (mGalleryProvider != null) {
if (replace) mGalleryProvider!!.stop() else return
}
@@ -271,7 +279,6 @@ class ReaderActivity : EhActivity() {
private fun onInit() {
handleIntent(intent)
buildProvider()
}
private fun onRestore(savedInstanceState: Bundle) {
@@ -281,7 +288,6 @@ class ReaderActivity : EhActivity() {
mGalleryInfo = savedInstanceState.getParcelableCompat(KEY_GALLERY_INFO)
mPage = savedInstanceState.getInt(KEY_PAGE, -1)
mCurrentIndex = savedInstanceState.getInt(KEY_CURRENT_INDEX)
buildProvider()
}
override fun onNewIntent(intent: Intent?) {
@@ -328,6 +334,7 @@ class ReaderActivity : EhActivity() {
} else {
onRestore(savedInstanceState)
}
buildProvider()
binding = ReaderActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
builder = EditTextDialogBuilder(this, null, getString(R.string.archive_passwd))
@@ -338,6 +345,7 @@ class ReaderActivity : EhActivity() {
mGalleryProvider.let {
if (it == null) {
finish()
return
}
lifecycleScope.launchIO {
@@ -368,7 +376,6 @@ class ReaderActivity : EhActivity() {
updateViewerInset(ReaderPreferences.fullscreen().get())
binding.viewerContainer.removeAllViews()
setOrientation(ReaderPreferences.defaultOrientationType().get())
updateOrientationShortcut(ReaderPreferences.defaultOrientationType().get())
binding.viewerContainer.addView(viewer?.getView())
viewer?.setGalleryProvider(mGalleryProvider!!)
moveToPageIndex(mCurrentIndex)
@@ -379,10 +386,6 @@ class ReaderActivity : EhActivity() {
config = null
viewer?.destroy()
viewer = null
if (mGalleryProvider != null) {
mGalleryProvider!!.stop()
mGalleryProvider = null
}
readerSettingSheetDialog?.dismiss()
readerSettingSheetDialog = null
}
@@ -856,15 +859,13 @@ class ReaderActivity : EhActivity() {
/**
* Forces the user preferred [orientation] on the activity.
* Returns true if a restart is required.
*/
private fun setOrientation(orientation: Int): Boolean {
private fun setOrientation(orientation: Int) {
val newOrientation = OrientationType.fromPreference(orientation)
val restart = newOrientation.flag != requestedOrientation
if (restart) {
if (newOrientation.flag != requestedOrientation) {
requestedOrientation = newOrientation.flag
}
return restart
updateOrientationShortcut(ReaderPreferences.defaultOrientationType().get())
}
/**