feat: 优化代码

pull/141/head
kunfei 5 years ago
parent 2b52966e46
commit 40e4c7acb3
  1. 34
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  2. 17
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@ -26,8 +26,7 @@ class ContentView(context: Context) : FrameLayout(context) {
upStyle() upStyle()
upTime() upTime()
content_text_view.upView = { content_text_view.upView = {
tv_bottom_left.text = it.title setProgress(it)
setPageIndex(it.index, it.pageSize)
} }
} }
@ -85,20 +84,19 @@ class ContentView(context: Context) : FrameLayout(context) {
} }
fun upTime() { fun upTime() {
tv_top_left.text = timeFormat.format(Date(System.currentTimeMillis())) if (ReadBookConfig.hideStatusBar) {
tv_bottom_left.text = timeFormat.format(Date(System.currentTimeMillis()))
}
} }
fun upBattery(battery: Int) { fun upBattery(battery: Int) {
tv_top_right.text = context.getString(R.string.battery_show, battery) if (ReadBookConfig.hideStatusBar) {
tv_bottom_right.text = context.getString(R.string.battery_show, battery)
}
} }
fun setContent(textPage: TextPage) { fun setContent(textPage: TextPage) {
tv_bottom_left.text = when (AppConfig.chineseConverterType) { setProgress(textPage)
1 -> ZhConvertBootstrap.newInstance().toSimple(textPage.title)
2 -> ZhConvertBootstrap.newInstance().toTraditional(textPage.title)
else -> textPage.title
}
setPageIndex(textPage.index, textPage.pageSize)
content_text_view.resetPageOffset() content_text_view.resetPageOffset()
content_text_view.setContent(textPage) content_text_view.setContent(textPage)
} }
@ -108,9 +106,19 @@ class ContentView(context: Context) : FrameLayout(context) {
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
fun setPageIndex(pageIndex: Int?, pageSize: Int) { fun setProgress(textPage: TextPage) = textPage.apply {
pageIndex?.let { val title = when (AppConfig.chineseConverterType) {
tv_bottom_right.text = "${pageIndex.plus(1)}/${pageSize}" 1 -> ZhConvertBootstrap.newInstance().toSimple(textPage.title)
2 -> ZhConvertBootstrap.newInstance().toTraditional(textPage.title)
else -> textPage.title
}
val progress = "${index.plus(1)}/$pageSize $readProgress"
if (ReadBookConfig.hideStatusBar) {
tv_top_left.text = title
tv_top_right.text = progress
} else {
tv_bottom_left.text = title
tv_bottom_right.text = progress
} }
} }

@ -5,6 +5,7 @@ import android.text.StaticLayout
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.ui.book.read.page.ChapterProvider
import java.text.DecimalFormat
data class TextPage( data class TextPage(
var index: Int = 0, var index: Int = 0,
@ -84,4 +85,20 @@ data class TextPage(
lineStart += textLine.text.length lineStart += textLine.text.length
} }
} }
val readProgress: String
get() {
val df = DecimalFormat("0.0%")
if (chapterSize == 0 || pageSize == 0 && chapterIndex == 0) {
return "0.0%"
} else if (pageSize == 0) {
return df.format((chapterIndex + 1.0f) / chapterSize.toDouble())
}
var percent =
df.format(chapterIndex * 1.0f / chapterSize + 1.0f / chapterSize * (index + 1) / pageSize.toDouble())
if (percent == "100.0%" && (chapterIndex + 1 != chapterSize || index + 1 != pageSize)) {
percent = "99.9%"
}
return percent
}
} }

Loading…
Cancel
Save