feat: 优化代码

pull/106/head
kunfei 5 years ago
parent db33516ac5
commit b2b68378f8
  1. 12
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  4. 25
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt

@ -13,8 +13,10 @@ import io.legado.app.model.WebBook
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChapter
import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
object ReadBook { object ReadBook {
@ -264,18 +266,18 @@ object ReadBook {
book!!.useReplaceRule book!!.useReplaceRule
) )
when (chapter.index) { when (chapter.index) {
durChapterIndex -> withContext(Main) { durChapterIndex -> {
curTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) curTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
callBack?.upContent() callBack?.upContent()
callBack?.upView() callBack?.upView()
curPageChanged() curPageChanged()
callBack?.contentLoadFinish() callBack?.contentLoadFinish()
} }
durChapterIndex - 1 -> withContext(Main) { durChapterIndex - 1 -> {
prevTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) prevTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
callBack?.upContent(-1) callBack?.upContent(-1)
} }
durChapterIndex + 1 -> withContext(Main) { durChapterIndex + 1 -> {
nextTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) nextTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize)
callBack?.upContent(1) callBack?.upContent(1)
} }

@ -345,6 +345,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override fun upView() { override fun upView() {
launch {
ReadBook.curTextChapter?.let { ReadBook.curTextChapter?.let {
tv_chapter_name.text = it.title tv_chapter_name.text = it.title
tv_chapter_name.visible() tv_chapter_name.visible()
@ -358,10 +359,13 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
tv_next.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1 tv_next.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1
} }
} }
}
override fun upPageProgress() { override fun upPageProgress() {
launch {
seek_read_page.progress = ReadBook.durPageIndex seek_read_page.progress = ReadBook.durPageIndex
} }
}
override fun showMenuBar() { override fun showMenuBar() {
read_menu.runMenuIn() read_menu.runMenuIn()

@ -91,7 +91,7 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
postEvent(EventBus.UP_CONFIG, true) postEvent(EventBus.UP_CONFIG, true)
} }
tv_text_bold.onClick { tv_text_bold.onClick {
with(ReadBookConfig.durConfig) { ReadBookConfig.durConfig.apply {
textBold = !textBold textBold = !textBold
tv_text_bold.isSelected = textBold tv_text_bold.isSelected = textBold
} }

@ -1,9 +1,13 @@
package io.legado.app.ui.book.read.page package io.legado.app.ui.book.read.page
import android.graphics.Point import android.graphics.Point
import android.graphics.Typeface
import android.text.Layout import android.text.Layout
import android.text.StaticLayout import android.text.StaticLayout
import android.text.TextPaint import android.text.TextPaint
import android.text.TextUtils
import io.legado.app.App
import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
@ -12,6 +16,8 @@ import io.legado.app.ui.book.read.page.entities.TextChar
import io.legado.app.ui.book.read.page.entities.TextLine import io.legado.app.ui.book.read.page.entities.TextLine
import io.legado.app.ui.book.read.page.entities.TextPage import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.dp import io.legado.app.utils.dp
import io.legado.app.utils.getPrefString
import io.legado.app.utils.removePref
object ChapterProvider { object ChapterProvider {
@ -31,10 +37,29 @@ object ChapterProvider {
} }
fun upStyle(config: ReadBookConfig.Config) { fun upStyle(config: ReadBookConfig.Config) {
val typeface: Typeface = try {
val fontPath = App.INSTANCE.getPrefString(PreferKey.readBookFont)
if (!TextUtils.isEmpty(fontPath)) {
Typeface.createFromFile(fontPath)
} else {
Typeface.SANS_SERIF
}
} catch (e: Exception) {
App.INSTANCE.removePref(PreferKey.readBookFont)
Typeface.SANS_SERIF
}
//标题
titlePaint.isAntiAlias = true
titlePaint.color = config.textColor() titlePaint.color = config.textColor()
titlePaint.letterSpacing = config.letterSpacing titlePaint.letterSpacing = config.letterSpacing
titlePaint.typeface = Typeface.create(typeface, Typeface.BOLD)
//正文
contentPaint.isAntiAlias = true
contentPaint.color = config.textColor() contentPaint.color = config.textColor()
contentPaint.letterSpacing = config.letterSpacing contentPaint.letterSpacing = config.letterSpacing
val bold = if (config.textBold) Typeface.BOLD else Typeface.NORMAL
contentPaint.typeface = Typeface.create(typeface, bold)
//间距
lineSpacingExtra = config.lineSpacingExtra lineSpacingExtra = config.lineSpacingExtra
paragraphSpacing = config.paragraphSpacing paragraphSpacing = config.paragraphSpacing
titlePaint.textSize = (config.textSize + 2).dp.toFloat() titlePaint.textSize = (config.textSize + 2).dp.toFloat()

Loading…
Cancel
Save