feat: 优化代码

pull/123/head
kunfei 5 years ago
parent f1ed9e6e45
commit 6984c6998c
  1. 10
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 2
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 51
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  4. 1
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  5. 2
      app/src/main/java/io/legado/app/model/Rss.kt
  6. 8
      app/src/main/java/io/legado/app/model/WebBook.kt
  7. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  8. 5
      app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
  9. 4
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  10. 3
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  11. 5
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt

@ -237,13 +237,7 @@ object BookHelp {
private var bookName: String? = null
private var bookOrigin: String? = null
private var replaceRules: List<ReplaceRule> = arrayListOf()
var bodyIndentCount = App.INSTANCE.getPrefInt(PreferKey.bodyIndent, 2)
set(value) {
field = value
App.INSTANCE.putPrefInt(PreferKey.bodyIndent, value)
bodyIndent = " ".repeat(value)
}
var bodyIndent = " ".repeat(bodyIndentCount)
fun disposeContent(
@ -281,6 +275,6 @@ object BookHelp {
1 -> c = ZhConvertBootstrap.newInstance().toSimple(c)
2 -> c = ZhConvertBootstrap.newInstance().toTraditional(c)
}
return c.replace("\\s*\\n+\\s*".toRegex(), "\n$bodyIndent")
return c.replace("\\s*\\n+\\s*".toRegex(), "\n${ReadBookConfig.bodyIndent}")
}
}

@ -17,7 +17,7 @@ interface JsExtensions {
fun ajax(urlStr: String): String? {
return try {
val analyzeUrl = AnalyzeUrl(urlStr, null, null, null, null, null)
val call = analyzeUrl.getResponse()
val call = analyzeUrl.getResponse(urlStr)
val response = call.execute()
response.body()
} catch (e: Exception) {

@ -26,24 +26,6 @@ object ReadBookConfig {
}
val durConfig get() = getConfig(styleSelect)
private val shareConfig get() = getConfig(5)
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, value)
}
var pageAnim = App.INSTANCE.getPrefInt(PreferKey.pageAnim)
set(value) {
field = value
isScroll = value == 3
App.INSTANCE.putPrefInt(PreferKey.pageAnim, value)
}
var isScroll = pageAnim == 3
val clickTurnPage get() = App.INSTANCE.getPrefBoolean(PreferKey.clickTurnPage, true)
var bg: Drawable? = null
init {
@ -113,6 +95,39 @@ object ReadBookConfig {
}
//配置写入读取
var styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect)
set(value) {
field = value
if (App.INSTANCE.getPrefInt(PreferKey.readStyleSelect) != value) {
App.INSTANCE.putPrefInt(PreferKey.readStyleSelect, value)
}
}
var shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout)
set(value) {
field = value
if (App.INSTANCE.getPrefBoolean(PreferKey.shareLayout) != value) {
App.INSTANCE.putPrefBoolean(PreferKey.shareLayout, value)
}
}
var pageAnim = App.INSTANCE.getPrefInt(PreferKey.pageAnim)
set(value) {
field = value
isScroll = value == 3
if (App.INSTANCE.getPrefInt(PreferKey.pageAnim) != value) {
App.INSTANCE.putPrefInt(PreferKey.pageAnim, value)
}
}
var isScroll = pageAnim == 3
val clickTurnPage get() = App.INSTANCE.getPrefBoolean(PreferKey.clickTurnPage, true)
var bodyIndentCount = App.INSTANCE.getPrefInt(PreferKey.bodyIndent, 2)
set(value) {
field = value
bodyIndent = " ".repeat(value)
if (App.INSTANCE.getPrefInt(PreferKey.bodyIndent, 2) != value) {
App.INSTANCE.putPrefInt(PreferKey.bodyIndent, value)
}
}
var bodyIndent = " ".repeat(bodyIndentCount)
var hideStatusBar = App.INSTANCE.getPrefBoolean(PreferKey.hideStatusBar)
var hideNavigationBar = App.INSTANCE.getPrefBoolean(PreferKey.hideNavigationBar)
var textBold: Boolean

@ -114,6 +114,7 @@ object Restore {
pageAnim = App.INSTANCE.getPrefInt(PreferKey.pageAnim)
hideStatusBar = App.INSTANCE.getPrefBoolean(PreferKey.hideStatusBar)
hideNavigationBar = App.INSTANCE.getPrefBoolean(PreferKey.hideNavigationBar)
bodyIndentCount = App.INSTANCE.getPrefInt(PreferKey.bodyIndent, 2)
}
ChapterProvider.upStyle()
ReadBook.loadContent()

@ -35,7 +35,7 @@ object Rss {
): Coroutine<String> {
return Coroutine.async(scope, context) {
val body = AnalyzeUrl(rssArticle.link, baseUrl = rssArticle.origin)
.getResponseAwait()
.getResponseAwait(rssArticle.origin)
.body
val analyzeRule = AnalyzeRule()
analyzeRule.setContent(

@ -37,7 +37,7 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
val res = analyzeUrl.getResponseAwait()
val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl)
BookList.analyzeBookList(
res.body,
bookSource,
@ -65,7 +65,7 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
val res = analyzeUrl.getResponseAwait()
val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl)
BookList.analyzeBookList(
res.body,
bookSource,
@ -96,7 +96,7 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
analyzeUrl.getResponseAwait().body
analyzeUrl.getResponseAwait(bookSource.bookSourceUrl).body
}
BookInfo.analyzeBookInfo(book, body, bookSource, book.bookUrl)
book
@ -122,7 +122,7 @@ class WebBook(val bookSource: BookSource) {
ruleUrl = book.tocUrl,
baseUrl = book.bookUrl,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait().body
).getResponseAwait(bookSource.bookSourceUrl).body
}
BookChapterList.analyzeChapterList(this, book, body, bookSource, book.tocUrl)
}

@ -610,7 +610,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
override fun ajax(urlStr: String): String? {
return try {
val analyzeUrl = AnalyzeUrl(urlStr, null, null, null, baseUrl, book)
val call = analyzeUrl.getResponse()
val call = analyzeUrl.getResponse(urlStr)
val response = call.execute()
response.body()
} catch (e: Exception) {

@ -63,7 +63,7 @@ object BookChapterList {
ruleUrl = nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait()
).getResponseAwait(bookSource.bookSourceUrl)
.body?.let { nextBody ->
chapterData = analyzeChapterList(
book, nextUrl, nextBody, tocRule, listRule, bookSource
@ -132,7 +132,8 @@ object BookChapterList {
ruleUrl = chapterData.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait().body ?: throw Exception("${chapterData.nextUrl}, 下载失败")
).getResponseAwait(bookSource.bookSourceUrl).body
?: throw Exception("${chapterData.nextUrl}, 下载失败")
val nextChapterData = analyzeChapterList(
book, chapterData.nextUrl, nextBody, tocRule, listRule, bookSource,
false

@ -56,7 +56,7 @@ object BookContent {
ruleUrl = nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait()
).getResponseAwait(bookSource.bookSourceUrl)
.body?.let { nextBody ->
contentData =
analyzeContent(
@ -80,7 +80,7 @@ object BookContent {
ruleUrl = item.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait()
).getResponseAwait(bookSource.bookSourceUrl)
.body?.let {
contentData =
analyzeContent(

@ -11,7 +11,6 @@ import androidx.fragment.app.DialogFragment
import io.legado.app.R
import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey
import io.legado.app.help.BookHelp
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.theme.accentColor
@ -118,7 +117,7 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
title = getString(R.string.text_indent),
items = resources.getStringArray(R.array.indent).toList()
) { _, index ->
BookHelp.bodyIndentCount = index
ReadBookConfig.bodyIndentCount = index
postEvent(EventBus.UP_CONFIG, true)
}
}

@ -9,7 +9,6 @@ import io.legado.app.App
import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp
import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.ui.book.read.page.entities.TextLine
@ -34,7 +33,6 @@ object ChapterProvider {
var typeface: Typeface = Typeface.SANS_SERIF
var titlePaint = TextPaint()
var contentPaint = TextPaint()
private var bodyIndent = BookHelp.bodyIndent
init {
upStyle()
@ -227,6 +225,7 @@ object ChapterProvider {
desiredWidth: Float
) {
var x = 0f
val bodyIndent = ReadBookConfig.bodyIndent
val icw = StaticLayout.getDesiredWidth(bodyIndent, textPaint) / bodyIndent.length
for (i in 0..bodyIndent.lastIndex) {
val x1 = x + icw
@ -348,8 +347,6 @@ object ChapterProvider {
titlePaint.textSize = (ReadBookConfig.textSize + 2).dp.toFloat()
contentPaint.textSize = ReadBookConfig.textSize.dp.toFloat()
bodyIndent = BookHelp.bodyIndent
upSize()
}

Loading…
Cancel
Save