Browse Source

漫画(图片)和书籍(文本)可以分别设置布局 (#4479)

* 漫画(图片)和书籍(文本)可以分别设置布局

* 优化
niuhb 1 year ago
parent
commit
22004a6e6b

+ 1 - 0
app/src/main/java/io/legado/app/constant/PreferKey.kt

@@ -69,6 +69,7 @@ object PreferKey {
     const val launcherIcon = "launcherIcon"
     const val textSelectAble = "selectText"
     const val shareLayout = "shareLayout"
+    const val comicStyleSelect = "comicStyleSelect"
     const val readStyleSelect = "readStyleSelect"
     const val systemTypefaces = "system_typefaces"
     const val readBodyToLh = "readBodyToLh"

+ 35 - 3
app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt

@@ -56,6 +56,7 @@ object ReadBookConfig {
             }
         }
 
+    var isComic: Boolean = false
     var bg: Drawable? = null
     var bgMeanColor: Int = 0
     val textColor: Int get() = durConfig.curTextColor()
@@ -150,9 +151,13 @@ object ReadBookConfig {
     fun deleteDur(): Boolean {
         if (configList.size > 5) {
             configList.removeAt(styleSelect)
-            if (styleSelect > 0) {
-                styleSelect -= 1
+            if (styleSelect <= readStyleSelect) {
+                readStyleSelect -= 1
             }
+            if (styleSelect <= comicStyleSelect) {
+                comicStyleSelect -= 1
+            }
+            styleSelect = initSelectStyle()
             return true
         }
         return false
@@ -173,13 +178,20 @@ object ReadBookConfig {
             field = value
             appCtx.putPrefInt(PreferKey.autoReadSpeed, value)
         }
-    var styleSelect = appCtx.getPrefInt(PreferKey.readStyleSelect)
+    var readStyleSelect = appCtx.getPrefInt(PreferKey.readStyleSelect)
         set(value) {
             field = value
             if (appCtx.getPrefInt(PreferKey.readStyleSelect) != value) {
                 appCtx.putPrefInt(PreferKey.readStyleSelect, value)
             }
         }
+    var comicStyleSelect = appCtx.getPrefInt(PreferKey.comicStyleSelect, readStyleSelect)
+        set(value) {
+            field = value
+            if (appCtx.getPrefInt(PreferKey.comicStyleSelect) != value) {
+                appCtx.putPrefInt(PreferKey.comicStyleSelect, value)
+            }
+        }
     var shareLayout = appCtx.getPrefBoolean(PreferKey.shareLayout)
         set(value) {
             field = value
@@ -188,6 +200,26 @@ object ReadBookConfig {
             }
         }
 
+    var styleSelect = initSelectStyle()
+    fun initSelectStyle(): Int {
+        return if (isComic) {
+            comicStyleSelect
+        }else {
+            readStyleSelect
+        }
+    }
+
+    fun updateStyleSelect(value: Int) {
+        if (styleSelect != value) {
+            if (isComic) {
+                comicStyleSelect = value
+            }else {
+                readStyleSelect = value
+            }
+            styleSelect = value
+        }
+    }
+
     /**
      * 两端对齐
      */

+ 3 - 1
app/src/main/java/io/legado/app/help/storage/Restore.kt

@@ -259,7 +259,9 @@ object Restore {
             edit.apply()
         }
         ReadBookConfig.apply {
-            styleSelect = appCtx.getPrefInt(PreferKey.readStyleSelect)
+            comicStyleSelect = appCtx.getPrefInt(PreferKey.comicStyleSelect)
+            readStyleSelect = appCtx.getPrefInt(PreferKey.readStyleSelect)
+            styleSelect = initSelectStyle()
             shareLayout = appCtx.getPrefBoolean(PreferKey.shareLayout)
             hideStatusBar = appCtx.getPrefBoolean(PreferKey.hideStatusBar)
             hideNavigationBar = appCtx.getPrefBoolean(PreferKey.hideNavigationBar)

+ 14 - 1
app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@@ -255,6 +255,11 @@ class ReadBookActivity : BaseReadBookActivity(),
     private var justInitData: Boolean = false
     private var syncDialog: AlertDialog? = null
 
+    override fun onStart() {
+        viewModel.initBookType(intent) { upStyle() }
+        super.onStart()
+    }
+
     @SuppressLint("ClickableViewAccessibility")
     override fun onActivityCreated(savedInstanceState: Bundle?) {
         super.onActivityCreated(savedInstanceState)
@@ -401,6 +406,14 @@ class ReadBookActivity : BaseReadBookActivity(),
         return super.onMenuOpened(featureId, menu)
     }
 
+    private fun upStyle() {
+        val oldIndex = ReadBookConfig.styleSelect
+        ReadBookConfig.styleSelect = ReadBookConfig.initSelectStyle()
+        if (oldIndex != ReadBookConfig.styleSelect){
+            postEvent(EventBus.UP_CONFIG, arrayListOf(1, 2, 5))
+        }
+    }
+
     /**
      * 更新菜单
      */
@@ -580,7 +593,7 @@ class ReadBookActivity : BaseReadBookActivity(),
             }
 
             R.id.menu_cover_progress -> ReadBook.book?.let {
-                ReadBook.uploadProgress({ toastOnUi(R.string.upload_book_success) })
+                ReadBook.uploadProgress { toastOnUi(R.string.upload_book_success) }
             }
 
             R.id.menu_same_title_removed -> {

+ 11 - 0
app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@@ -23,6 +23,7 @@ import io.legado.app.help.book.isLocalModified
 import io.legado.app.help.book.removeType
 import io.legado.app.help.book.simulatedTotalChapterNum
 import io.legado.app.help.config.AppConfig
+import io.legado.app.help.config.ReadBookConfig
 import io.legado.app.help.coroutine.Coroutine
 import io.legado.app.model.ImageProvider
 import io.legado.app.model.ReadAloud
@@ -67,6 +68,16 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
         AppConfig.detectClickArea()
     }
 
+    fun initBookType(intent: Intent, callBack: (() -> Unit)? = null) {
+        val bookUrl = intent.getStringExtra("bookUrl")
+        val book = when {
+            bookUrl.isNullOrEmpty() -> appDb.bookDao.lastReadBook
+            else -> appDb.bookDao.getBook(bookUrl)
+        }
+        ReadBookConfig.isComic = book?.type == BookType.image
+        callBack?.invoke()
+    }
+
     /**
      * 初始化
      */

+ 1 - 1
app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt

@@ -165,7 +165,7 @@ class ReadStyleDialog : BaseDialogFragment(R.layout.dialog_read_book_style),
     private fun changeBgTextConfig(index: Int) {
         val oldIndex = ReadBookConfig.styleSelect
         if (index != oldIndex) {
-            ReadBookConfig.styleSelect = index
+            ReadBookConfig.updateStyleSelect(index)
             upView()
             styleAdapter.notifyItemChanged(oldIndex)
             styleAdapter.notifyItemChanged(index)