feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 7e1b37b36d
commit 0b53a2c4b4
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 124
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  3. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/read/config/PaddingConfigDialog.kt
  5. 19
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  6. 36
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  7. 4
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  8. 4
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt

@ -37,4 +37,5 @@ object PreferKey {
const val lastBackup = "lastBackup"
const val bodyIndent = "textIndent"
const val shareLayout = "shareLayout"
const val readStyleSelect = "readStyleSelect"
}

@ -6,6 +6,7 @@ import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import io.legado.app.App
import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.utils.*
@ -23,12 +24,19 @@ object ReadBookConfig {
val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
GSON.fromJsonArray<Config>(json)!!
}
val durConfig
get() = getConfig(styleSelect)
val durConfig get() = getConfig(styleSelect)
private val shareConfig get() = getConfig(5)
var styleSelect
get() = App.INSTANCE.getPrefInt("readStyleSelect")
set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value)
var styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect)
set(value) {
field = value
App.INSTANCE.putPrefInt(PreferKey.readStyleSelect, value)
}
var shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout)
set(value) {
field = value
App.INSTANCE.putPrefBoolean(PreferKey.shareLayout)
}
var bg: Drawable? = null
init {
@ -40,6 +48,9 @@ object ReadBookConfig {
if (configList.size < 5) {
resetAll()
}
if (configList.size < 6) {
configList.add(Config())
}
return configList[index]
}
@ -94,7 +105,106 @@ object ReadBookConfig {
}
}
data class Config(
//配置写入读取
var textBold: Boolean
get() = if (shareLayout) shareConfig.textBold else durConfig.textBold
set(value) = if (shareLayout) shareConfig.textBold = value else durConfig.textBold = value
var textSize: Int
get() = if (shareLayout) shareConfig.textSize else durConfig.textSize
set(value) = if (shareLayout) shareConfig.textSize = value else durConfig.textSize = value
var letterSpacing: Float
get() = if (shareLayout) shareConfig.letterSpacing else durConfig.letterSpacing
set(value) =
if (shareLayout) shareConfig.letterSpacing = value else durConfig.letterSpacing = value
var lineSpacingExtra: Int
get() = if (shareLayout) shareConfig.lineSpacingExtra else durConfig.lineSpacingExtra
set(value) =
if (shareLayout) shareConfig.lineSpacingExtra = value
else durConfig.lineSpacingExtra = value
var paragraphSpacing: Int
get() = if (shareLayout) shareConfig.paragraphSpacing else durConfig.paragraphSpacing
set(value) =
if (shareLayout) shareConfig.paragraphSpacing = value
else durConfig.paragraphSpacing = value
var titleCenter: Boolean
get() = if (shareLayout) shareConfig.titleCenter else durConfig.titleCenter
set(value) =
if (shareLayout) shareConfig.titleCenter = value else durConfig.titleCenter = value
var paddingBottom: Int
get() = if (shareLayout) shareConfig.paddingBottom else durConfig.paddingBottom
set(value) =
if (shareLayout) shareConfig.paddingBottom = value else durConfig.paddingBottom = value
var paddingLeft: Int
get() = if (shareLayout) shareConfig.paddingLeft else durConfig.paddingLeft
set(value) =
if (shareLayout) shareConfig.paddingLeft = value else durConfig.paddingLeft = value
var paddingRight: Int
get() = if (shareLayout) shareConfig.paddingRight else durConfig.paddingRight
set(value) =
if (shareLayout) shareConfig.paddingRight = value else durConfig.paddingRight = value
var paddingTop: Int
get() = if (shareLayout) shareConfig.paddingTop else durConfig.paddingTop
set(value) =
if (shareLayout) shareConfig.paddingTop = value else durConfig.paddingTop = value
var headerPaddingBottom: Int
get() = if (shareLayout) shareConfig.headerPaddingBottom else durConfig.headerPaddingBottom
set(value) =
if (shareLayout) shareConfig.headerPaddingBottom = value
else durConfig.headerPaddingBottom = value
var headerPaddingLeft: Int
get() = if (shareLayout) shareConfig.headerPaddingLeft else durConfig.headerPaddingLeft
set(value) =
if (shareLayout) shareConfig.headerPaddingLeft = value
else durConfig.headerPaddingLeft = value
var headerPaddingRight: Int
get() = if (shareLayout) shareConfig.headerPaddingRight else durConfig.headerPaddingRight
set(value) =
if (shareLayout) shareConfig.headerPaddingRight = value
else durConfig.headerPaddingRight = value
var headerPaddingTop: Int
get() = if (shareLayout) shareConfig.headerPaddingTop else durConfig.headerPaddingTop
set(value) =
if (shareLayout) shareConfig.headerPaddingTop = value
else durConfig.headerPaddingTop = value
var footerPaddingBottom: Int
get() = if (shareLayout) shareConfig.footerPaddingBottom else durConfig.footerPaddingBottom
set(value) =
if (shareLayout) shareConfig.footerPaddingBottom = value
else durConfig.footerPaddingBottom = value
var footerPaddingLeft: Int
get() = if (shareLayout) shareConfig.footerPaddingLeft else durConfig.footerPaddingLeft
set(value) =
if (shareLayout) shareConfig.footerPaddingLeft = value
else durConfig.footerPaddingLeft = value
var footerPaddingRight: Int
get() = if (shareLayout) shareConfig.footerPaddingRight else durConfig.footerPaddingRight
set(value) =
if (shareLayout) shareConfig.footerPaddingRight = value
else durConfig.footerPaddingRight = value
var footerPaddingTop: Int
get() = if (shareLayout) shareConfig.footerPaddingTop else durConfig.footerPaddingTop
set(value) =
if (shareLayout) shareConfig.footerPaddingTop = value
else durConfig.footerPaddingTop = value
class Config(
var bgStr: String = "#EEEEEE",//白天背景
var bgStrNight: String = "#000000",//夜间背景
var bgType: Int = 0,//白天背景类型
@ -138,7 +248,7 @@ object ReadBookConfig {
} else {
textColor = "#${color.hexString}"
}
ChapterProvider.upStyle(this)
ChapterProvider.upStyle()
}
fun setStatusIconDark(isDark: Boolean) {

@ -385,12 +385,12 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
cursor_left,
Gravity.BOTTOM or Gravity.START,
cursor_left.x.toInt() + cursor_left.width,
page_view.height - cursor_left.y.toInt() + ReadBookConfig.durConfig.textSize.dp + popup.height
page_view.height - cursor_left.y.toInt() + ReadBookConfig.textSize.dp + popup.height
)
} else {
popup.update(
cursor_left.x.toInt() + cursor_left.width,
page_view.height - cursor_left.y.toInt() + ReadBookConfig.durConfig.textSize.dp + popup.height,
page_view.height - cursor_left.y.toInt() + ReadBookConfig.textSize.dp + popup.height,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
)
}
@ -665,7 +665,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
upSystemUiVisibility()
page_view.upBg()
page_view.upStyle()
ChapterProvider.upStyle(ReadBookConfig.durConfig)
ChapterProvider.upStyle()
if (it) {
ReadBook.loadContent()
} else {

@ -49,7 +49,7 @@ class PaddingConfigDialog : DialogFragment() {
ReadBookConfig.save()
}
private fun initData() = with(ReadBookConfig.durConfig) {
private fun initData() = with(ReadBookConfig) {
//正文
dsb_padding_top.progress = paddingTop
dsb_padding_bottom.progress = paddingBottom
@ -67,7 +67,7 @@ class PaddingConfigDialog : DialogFragment() {
dsb_footer_padding_right.progress = footerPaddingRight
}
private fun initView() = with(ReadBookConfig.durConfig) {
private fun initView() = with(ReadBookConfig) {
//正文
dsb_padding_top.onChanged = {
paddingTop = it

@ -76,7 +76,7 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
}
private fun initData() {
cb_share_layout.isChecked = getPrefBoolean(PreferKey.shareLayout)
cb_share_layout.isChecked = ReadBookConfig.shareLayout
requireContext().getPrefInt(PreferKey.pageAnim).let {
if (it >= 0 && it < rg_page_anim.childCount) {
rg_page_anim.check(rg_page_anim[it].id)
@ -92,14 +92,14 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
postEvent(EventBus.UP_CONFIG, true)
}
tv_title_center.onClick {
ReadBookConfig.durConfig.apply {
ReadBookConfig.apply {
titleCenter = !titleCenter
tv_title_center.isSelected = titleCenter
}
postEvent(EventBus.UP_CONFIG, true)
}
tv_text_bold.onClick {
ReadBookConfig.durConfig.apply {
ReadBookConfig.apply {
textBold = !textBold
tv_text_bold.isSelected = textBold
}
@ -125,19 +125,19 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
}
}
dsb_text_size.onChanged = {
ReadBookConfig.durConfig.textSize = it + 5
ReadBookConfig.textSize = it + 5
postEvent(EventBus.UP_CONFIG, true)
}
dsb_text_letter_spacing.onChanged = {
ReadBookConfig.durConfig.letterSpacing = (it - 50) / 100f
ReadBookConfig.letterSpacing = (it - 50) / 100f
postEvent(EventBus.UP_CONFIG, true)
}
dsb_line_size.onChanged = {
ReadBookConfig.durConfig.lineSpacingExtra = it
ReadBookConfig.lineSpacingExtra = it
postEvent(EventBus.UP_CONFIG, true)
}
dsb_paragraph_spacing.onChanged = {
ReadBookConfig.durConfig.paragraphSpacing = it
ReadBookConfig.paragraphSpacing = it
postEvent(EventBus.UP_CONFIG, true)
}
rg_page_anim.onCheckedChange { _, checkedId ->
@ -154,7 +154,8 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
}
cb_share_layout.onCheckedChangeListener = { checkBox, isChecked ->
if (checkBox.isPressed) {
putPrefBoolean(PreferKey.shareLayout, isChecked)
ReadBookConfig.shareLayout = isChecked
upStyle()
}
}
bg0.onClick { changeBg(0) }
@ -190,7 +191,7 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
}
private fun upStyle() {
ReadBookConfig.durConfig.let {
ReadBookConfig.let {
tv_title_center.isSelected = it.titleCenter
tv_text_bold.isSelected = it.textBold
dsb_text_size.progress = it.textSize - 5

@ -32,7 +32,7 @@ object ChapterProvider {
private var bodyIndent = BookHelp.bodyIndent
init {
upStyle(ReadBookConfig.durConfig)
upStyle()
}
/**
@ -160,7 +160,7 @@ object ChapterProvider {
//最后一行
textLine.text = "$words\n"
stringBuilder.append("\n")
var x = if (ReadBookConfig.durConfig.titleCenter)
var x = if (ReadBookConfig.titleCenter)
(visibleWidth - layout.getLineMax(lineIndex)) / 2
else 0f
for (i in words.indices) {
@ -297,7 +297,7 @@ object ChapterProvider {
/**
* 更新样式
*/
fun upStyle(config: ReadBookConfig.Config) {
fun upStyle() {
typeface = try {
val fontPath = App.INSTANCE.getPrefString(PreferKey.readBookFont)
if (!TextUtils.isEmpty(fontPath)) {
@ -311,34 +311,34 @@ object ChapterProvider {
}
//标题
titlePaint.isAntiAlias = true
titlePaint.color = config.textColor()
titlePaint.letterSpacing = config.letterSpacing
titlePaint.color = ReadBookConfig.durConfig.textColor()
titlePaint.letterSpacing = ReadBookConfig.letterSpacing
titlePaint.typeface = Typeface.create(typeface, Typeface.BOLD)
//正文
contentPaint.isAntiAlias = true
contentPaint.color = config.textColor()
contentPaint.letterSpacing = config.letterSpacing
val bold = if (config.textBold) Typeface.BOLD else Typeface.NORMAL
contentPaint.color = ReadBookConfig.durConfig.textColor()
contentPaint.letterSpacing = ReadBookConfig.letterSpacing
val bold = if (ReadBookConfig.textBold) Typeface.BOLD else Typeface.NORMAL
contentPaint.typeface = Typeface.create(typeface, bold)
//间距
lineSpacingExtra = config.lineSpacingExtra.dp.toFloat()
paragraphSpacing = config.paragraphSpacing.dp
titlePaint.textSize = (config.textSize + 2).dp.toFloat()
contentPaint.textSize = config.textSize.dp.toFloat()
lineSpacingExtra = ReadBookConfig.lineSpacingExtra.dp.toFloat()
paragraphSpacing = ReadBookConfig.paragraphSpacing.dp
titlePaint.textSize = (ReadBookConfig.textSize + 2).dp.toFloat()
contentPaint.textSize = ReadBookConfig.textSize.dp.toFloat()
bodyIndent = BookHelp.bodyIndent
upSize(config)
upSize()
}
/**
* 更新View尺寸
*/
fun upSize(config: ReadBookConfig.Config) {
paddingLeft = config.paddingLeft.dp
paddingTop = config.paddingTop.dp
visibleWidth = viewWidth - paddingLeft - config.paddingRight.dp
visibleHeight = viewHeight - paddingTop - config.paddingBottom.dp
fun upSize() {
paddingLeft = ReadBookConfig.paddingLeft.dp
paddingTop = ReadBookConfig.paddingTop.dp
visibleWidth = viewWidth - paddingLeft - ReadBookConfig.paddingRight.dp
visibleHeight = viewHeight - paddingTop - ReadBookConfig.paddingBottom.dp
}
}

@ -51,10 +51,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
ReadBookConfig.durConfig.let {
ReadBookConfig.let {
ChapterProvider.viewWidth = w
ChapterProvider.viewHeight = h
ChapterProvider.upSize(ReadBookConfig.durConfig)
ChapterProvider.upSize()
}
}

@ -28,7 +28,7 @@ class ContentView(context: Context) : FrameLayout(context) {
}
fun upStyle() {
ReadBookConfig.durConfig.apply {
ReadBookConfig.apply {
tv_top_left.typeface = ChapterProvider.typeface
tv_top_right.typeface = ChapterProvider.typeface
tv_bottom_left.typeface = ChapterProvider.typeface
@ -61,7 +61,7 @@ class ContentView(context: Context) : FrameLayout(context) {
footerPaddingRight.dp,
footerPaddingBottom.dp
)
textColor().let {
durConfig.textColor().let {
tv_top_left.setTextColor(it)
tv_top_right.setTextColor(it)
tv_bottom_left.setTextColor(it)

Loading…
Cancel
Save