feat: 优化代码

pull/98/head
kunfei 5 years ago
parent 44ce6fa9fc
commit 2a9ca1f315
  1. 2
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 7
      app/src/main/java/io/legado/app/help/AppConfig.kt
  3. 5
      app/src/main/java/io/legado/app/help/BookHelp.kt
  4. 1
      app/src/main/java/io/legado/app/ui/book/read/config/MoreConfigDialog.kt
  5. 3
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  6. 10
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  7. 70
      app/src/main/java/io/legado/app/ui/widget/ChineseConverter.kt
  8. 14
      app/src/main/res/layout/dialog_read_book_style.xml
  9. 7
      app/src/main/res/values/arrays.xml
  10. 2
      app/src/main/res/values/strings.xml
  11. 8
      app/src/main/res/xml/pref_config_read.xml

@ -31,5 +31,5 @@ object PreferKey {
const val webDavAccount = "web_dav_account" const val webDavAccount = "web_dav_account"
const val webDavPassword = "web_dav_password" const val webDavPassword = "web_dav_password"
const val changeSourceLoadToc = "changeSourceLoadToc" const val changeSourceLoadToc = "changeSourceLoadToc"
const val isChineseConverterEnable = "isChineseConverterEnable" const val chineseConverterType = "chineseConverterType"
} }

@ -78,6 +78,9 @@ object AppConfig {
val clickAllNext: Boolean get() = App.INSTANCE.getPrefBoolean(PreferKey.clickAllNext, false) val clickAllNext: Boolean get() = App.INSTANCE.getPrefBoolean(PreferKey.clickAllNext, false)
val isChineseConverterEnable: String? var chineseConverterType: Int
get() = App.INSTANCE.getPrefString(PreferKey.isChineseConverterEnable) get() = App.INSTANCE.getPrefInt(PreferKey.chineseConverterType)
set(value) {
App.INSTANCE.putPrefInt(PreferKey.chineseConverterType, value)
}
} }

@ -2,6 +2,7 @@ package io.legado.app.help
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import com.github.houbb.opencc4j.util.ZhConverterUtil
import io.legado.app.App import io.legado.app.App
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@ -275,6 +276,10 @@ object BookHelp {
} }
} }
} }
when (AppConfig.chineseConverterType) {
2 -> c = ZhConverterUtil.toTraditional(c)
1 -> c = ZhConverterUtil.toSimple(c)
}
val indent = App.INSTANCE.getPrefInt("textIndent", 2) val indent = App.INSTANCE.getPrefInt("textIndent", 2)
return c.replace("\\s*\\n+\\s*".toRegex(), "\n" + " ".repeat(indent)) return c.replace("\\s*\\n+\\s*".toRegex(), "\n" + " ".repeat(indent))
} }

@ -95,7 +95,6 @@ class MoreConfigDialog : DialogFragment() {
PreferKey.hideStatusBar -> postEvent(EventBus.UP_CONFIG, true) PreferKey.hideStatusBar -> postEvent(EventBus.UP_CONFIG, true)
PreferKey.hideNavigationBar -> postEvent(EventBus.UP_CONFIG, true) PreferKey.hideNavigationBar -> postEvent(EventBus.UP_CONFIG, true)
PreferKey.keepLight -> postEvent(PreferKey.keepLight, true) PreferKey.keepLight -> postEvent(PreferKey.keepLight, true)
PreferKey.isChineseConverterEnable -> postEvent(EventBus.UP_CONFIG, true)
} }
} }

@ -78,6 +78,9 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
} }
private fun initOnClick() { private fun initOnClick() {
chinese_converter.onChanged {
postEvent(EventBus.UP_CONFIG, true)
}
tv_text_bold.onClick { tv_text_bold.onClick {
with(ReadBookConfig.getConfig()) { with(ReadBookConfig.getConfig()) {
textBold = !textBold textBold = !textBold

@ -9,11 +9,9 @@ import android.view.Gravity
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.ImageView import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView import androidx.appcompat.widget.AppCompatImageView
import com.github.houbb.opencc4j.util.ZhConverterUtil
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppConst.TIME_FORMAT import io.legado.app.constant.AppConst.TIME_FORMAT
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.AppConfig
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.view_book_page.view.* import kotlinx.android.synthetic.main.view_book_page.view.*
@ -113,13 +111,7 @@ class ContentView : FrameLayout {
fun setContent(textPage: TextPage?) { fun setContent(textPage: TextPage?) {
if (textPage != null) { if (textPage != null) {
content_text_view.gravity = Gravity.START content_text_view.gravity = Gravity.START
content_text_view.text = if ("0" != AppConfig.isChineseConverterEnable) { content_text_view.text = textPage.text
if ("1" == AppConfig.isChineseConverterEnable)
ZhConverterUtil.toTraditional(textPage.text.toString())
else ZhConverterUtil.toSimple(textPage.text.toString())
} else {
textPage.text
}
tv_bottom_left.text = textPage.title tv_bottom_left.text = textPage.title
pageSize = textPage.pageSize pageSize = textPage.pageSize
setPageIndex(textPage.index) setPageIndex(textPage.index)

@ -0,0 +1,70 @@
package io.legado.app.ui.widget
import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import io.legado.app.App
import io.legado.app.R
import io.legado.app.help.AppConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.dp
import io.legado.app.utils.getCompatColor
import org.jetbrains.anko.sdk27.listeners.onClick
class ChineseConverter(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
private val spannableString = SpannableString("简/繁")
private val enabledSpan = ForegroundColorSpan(App.INSTANCE.accentColor)
private var onChanged: (() -> Unit)? = null
init {
background = Selector.shapeBuild()
.setCornerRadius(1.dp)
.setStrokeWidth(1.dp)
.setDisabledStrokeColor(context.getCompatColor(R.color.md_grey_500))
.setDefaultStrokeColor(ThemeStore.textColorSecondary(context))
.setSelectedStrokeColor(ThemeStore.accentColor(context))
.setPressedBgColor(context.getCompatColor(R.color.transparent30))
.create()
setTextColor(
Selector.colorBuild()
.setDefaultColor(ThemeStore.textColorSecondary(context))
.setSelectedColor(ThemeStore.accentColor(context))
.setDisabledColor(context.getCompatColor(R.color.md_grey_500))
.create()
)
upUi(AppConfig.chineseConverterType)
onClick {
selectType()
}
}
private fun upUi(type: Int) {
spannableString.removeSpan(enabledSpan)
when (type) {
1 -> spannableString.setSpan(enabledSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
2 -> spannableString.setSpan(enabledSpan, 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
}
text = spannableString
}
private fun selectType() {
context.alert(titleResource = R.string.chinese_converter) {
items(context.resources.getStringArray(R.array.chinese_mode).toList()) { _, i ->
AppConfig.chineseConverterType = i
upUi(i)
onChanged?.invoke()
}
}.show()
}
fun onChanged(unit: () -> Unit) {
onChanged = unit
}
}

@ -65,6 +65,20 @@
android:gravity="center" android:gravity="center"
android:textSize="14sp" android:textSize="14sp"
app:layout_constraintLeft_toRightOf="@+id/tv_text_indent" app:layout_constraintLeft_toRightOf="@+id/tv_text_indent"
app:layout_constraintRight_toLeftOf="@id/chinese_converter"
app:layout_constraintTop_toTopOf="parent" />
<io.legado.app.ui.widget.ChineseConverter
android:id="@+id/chinese_converter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:gravity="center"
android:textSize="14sp"
app:layout_constraintLeft_toRightOf="@+id/tv_padding"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />

@ -144,13 +144,8 @@
<string-array name="chinese_mode"> <string-array name="chinese_mode">
<item>关闭</item> <item>关闭</item>
<item>简体转繁体</item>
<item>繁体转简体</item> <item>繁体转简体</item>
<item>简体转繁体</item>
</string-array> </string-array>
<string-array name="chinese_mode_v">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources> </resources>

@ -605,5 +605,5 @@
<string name="add_to_text_context_menu_s">长按文字在操作菜单中显示阅读·搜索</string> <string name="add_to_text_context_menu_s">长按文字在操作菜单中显示阅读·搜索</string>
<string name="add_to_text_context_menu_t">文字操作显示搜索</string> <string name="add_to_text_context_menu_t">文字操作显示搜索</string>
<string name="record_log">记录日志</string> <string name="record_log">记录日志</string>
<string name="is_chinese_converter_enable">中文简繁体转换</string> <string name="chinese_converter">中文简繁体转换</string>
</resources> </resources>

@ -28,14 +28,6 @@
android:key="volumeKeyPage" android:key="volumeKeyPage"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.NameListPreference
android:defaultValue="0"
android:key="isChineseConverterEnable"
android:entries="@array/chinese_mode"
android:entryValues="@array/chinese_mode_v"
android:title="@string/is_chinese_converter_enable"
app:iconSpaceReserved="false" />
<io.legado.app.lib.theme.prefs.ATESwitchPreference <io.legado.app.lib.theme.prefs.ATESwitchPreference
android:defaultValue="false" android:defaultValue="false"
android:title="@string/click_all_next_page" android:title="@string/click_all_next_page"

Loading…
Cancel
Save