diff --git a/app/src/main/res/drawable/bg1.jpg b/app/src/main/assets/bg/bg1.jpg similarity index 100% rename from app/src/main/res/drawable/bg1.jpg rename to app/src/main/assets/bg/bg1.jpg diff --git a/app/src/main/assets/readConfig.json b/app/src/main/assets/readConfig.json index 6ab073605..617d671ab 100644 --- a/app/src/main/assets/readConfig.json +++ b/app/src/main/assets/readConfig.json @@ -1,18 +1,20 @@ [ { "bgStr": "#F3F3F3", - "bgInt": 0, "bgType": 0, "darkStatusIcon": true, "textColor": "#3E3D3B", "textSize": 16, "letterSpacing": 1, "lineSpacingExtra": 15, - "lineSpacingMultiplier": 3 + "lineSpacingMultiplier": 3, + "paddingLeft": 16, + "paddingRight": 16, + "paddingTop": 0, + "paddingBottom": 0 }, { "bgStr": "#C6BAA1", - "bgInt": 0, "bgType": 0, "darkStatusIcon": true, "textColor": "#3E3D3B", @@ -27,7 +29,6 @@ }, { "bgStr": "#E1F1DA", - "bgInt": 0, "bgType": 0, "darkStatusIcon": true, "textColor": "#3E3D3B", @@ -42,7 +43,6 @@ }, { "bgStr": "#015A86", - "bgInt": 0, "bgType": 0, "darkStatusIcon": false, "textColor": "#3E3D3B", @@ -57,7 +57,6 @@ }, { "bgStr": "#808080", - "bgInt": 0, "bgType": 0, "darkStatusIcon": false, "textColor": "#3E3D3B", diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index cb7805fbc..266b83731 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -3,9 +3,9 @@ package io.legado.app.help import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable +import com.google.gson.reflect.TypeToken import io.legado.app.App import io.legado.app.utils.GSON -import io.legado.app.utils.fromJsonArray import io.legado.app.utils.getPrefInt import io.legado.app.utils.putPrefInt import java.io.BufferedWriter @@ -14,25 +14,30 @@ import java.io.FileWriter import java.io.IOException object ReadBookConfig { - private val configList = arrayListOf() - private var styleSelect - get() = App.INSTANCE.getPrefInt("readStyleSelect") - set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value) - var bg: Drawable? = null - - init { + private val configList: ArrayList by lazy { + val list: ArrayList = arrayListOf() val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + "config") val json = if (configFile.exists()) { String(configFile.readBytes()) } else { String(App.INSTANCE.assets.open("readConfig.json").readBytes()) } - GSON.fromJsonArray(json)?.let { - configList.clear() - configList.addAll(it) - } ?: reset() + try { + val listType = object : TypeToken>() {}.type + GSON.fromJson>(json, listType)?.let { + list.addAll(it) + } + } catch (e: Exception) { + list.addAll(getOnError()) + } + list } + private var styleSelect + get() = App.INSTANCE.getPrefInt("readStyleSelect") + set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value) + var bg: Drawable? = null + fun getConfig(): Config { return configList[styleSelect] } @@ -41,7 +46,8 @@ object ReadBookConfig { getConfig().apply { when (bgType) { 0 -> bg = ColorDrawable(Color.parseColor(bgStr)) - + 1 -> bg = Drawable.createFromStream(App.INSTANCE.assets.open("bg" + File.separator + bgStr), "bg") + 2 -> bg = Drawable.createFromPath(bgStr) } } } @@ -61,23 +67,40 @@ object ReadBookConfig { } fun reset() { - val json = String(App.INSTANCE.assets.open("readConfig.json").readBytes()) - GSON.fromJsonArray(json)?.let { + try { + val json = String(App.INSTANCE.assets.open("readConfig.json").readBytes()) + val listType = object : TypeToken>() {}.type + GSON.fromJson>(json, listType)?.let { + configList.clear() + configList.addAll(it) + } + } catch (e: Exception) { configList.clear() - configList.addAll(it) + configList.addAll(getOnError()) } } + private fun getOnError(): ArrayList { + val list = arrayListOf() + list.add(Config()) + list.add(Config()) + list.add(Config()) + list.add(Config()) + return list + } + data class Config( - var bgStr: String = "#F3F3F3", - var bgInt: Int = 0, + var bgStr: String = "#015A86", var bgType: Int = 0, var darkStatusIcon: Boolean = true, - var textColor: String = "#3E3D3B", - var textSize: Int = 16, var letterSpacing: Int = 1, var lineSpacingExtra: Int = 15, - var lineSpacingMultiplier: Int = 3 + var lineSpacingMultiplier: Int = 3, + var paddingBottom: Int = 0, + var paddingLeft: Int = 16, + var paddingRight: Int = 16, + var paddingTop: Int = 0, + var textColor: String = "#3E3D3B", + var textSize: Int = 15 ) - } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt index b555c93c1..43125c38e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.widget.page import android.content.Context +import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.util.AttributeSet import android.widget.FrameLayout @@ -34,13 +35,14 @@ class ContentView : FrameLayout { } fun setBg(bg: Drawable?) { - ImageLoader.load(context, bg) - .centerCrop() - .setAsDrawable(bgImage) - } - - fun setBgColor(color: Int) { - page_panel.setBackgroundColor(color) + if (bg is ColorDrawable) { + bgImage.background = bg + bgImage + } else { + ImageLoader.load(context, bg) + .centerCrop() + .setAsDrawable(bgImage) + } } fun upTime() { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt index 142153eee..640d1578a 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt @@ -6,7 +6,7 @@ import android.graphics.Canvas import android.util.AttributeSet import android.view.MotionEvent import android.widget.FrameLayout -import io.legado.app.R +import io.legado.app.help.ReadBookConfig import io.legado.app.ui.widget.page.delegate.CoverPageDelegate import io.legado.app.ui.widget.page.delegate.PageDelegate @@ -169,10 +169,12 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att } fun upBg() { - val bg = context.getDrawable(R.drawable.bg1) - curPage?.setBg(bg) - prevPage?.setBg(bg) - nextPage?.setBg(bg) + ReadBookConfig.bg ?: let { + ReadBookConfig.upBg() + } + curPage?.setBg(ReadBookConfig.bg) + prevPage?.setBg(ReadBookConfig.bg) + nextPage?.setBg(ReadBookConfig.bg) } fun upTime() {