feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 0df04c2e82
commit 571677dd28
  1. 3
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  2. 8
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 1
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  4. 35
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt
  5. 5
      app/src/main/res/layout/dialog_toc_regex_edit.xml

@ -205,6 +205,9 @@ object AnalyzeTxtFile {
} }
private fun getTocRule(book: Book, bookStream: RandomAccessFile, charset: Charset): Pattern? { private fun getTocRule(book: Book, bookStream: RandomAccessFile, charset: Charset): Pattern? {
if (book.tocUrl.isNotEmpty()) {
return Pattern.compile(book.tocUrl, Pattern.MULTILINE)
}
val tocRules = getTocRules() val tocRules = getTocRules()
var rulePattern: Pattern? = null var rulePattern: Pattern? = null
//首先获取128k的数据 //首先获取128k的数据

@ -62,6 +62,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
ReadAloudDialog.CallBack, ReadAloudDialog.CallBack,
ChangeSourceDialog.CallBack, ChangeSourceDialog.CallBack,
ReadBook.CallBack, ReadBook.CallBack,
TocRegexDialog.CallBack,
ColorPickerDialogListener { ColorPickerDialogListener {
private val requestCodeChapterList = 568 private val requestCodeChapterList = 568
private val requestCodeEditSource = 111 private val requestCodeEditSource = 111
@ -600,6 +601,13 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
*/ */
override fun onDialogDismissed(dialogId: Int) = Unit override fun onDialogDismissed(dialogId: Int) = Unit
override fun onTocRegexDialogResult(tocRegex: String) {
ReadBook.book?.let {
it.tocUrl = tocRegex
viewModel.loadChapterList(it)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {

@ -99,6 +99,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
if (book.isLocalBook()) { if (book.isLocalBook()) {
AnalyzeTxtFile.analyze(context, book).let { AnalyzeTxtFile.analyze(context, book).let {
App.db.bookChapterDao().delByBook(book.bookUrl)
App.db.bookChapterDao().insert(*it.toTypedArray()) App.db.bookChapterDao().insert(*it.toTypedArray())
App.db.bookDao().update(book) App.db.bookDao().update(book)
ReadBook.chapterSize = it.size ReadBook.chapterSize = it.size

@ -12,6 +12,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App import io.legado.app.App
@ -44,7 +45,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
private lateinit var adapter: TocRegexAdapter private lateinit var adapter: TocRegexAdapter
private var tocRegexLiveData: LiveData<List<TxtTocRule>>? = null private var tocRegexLiveData: LiveData<List<TxtTocRule>>? = null
var selectedName: String? = null var selectedName: String? = null
var durRegex: String? = null private var durRegex: String? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -80,6 +81,20 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
val itemTouchCallback = ItemTouchCallback() val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true itemTouchCallback.isCanDrag = true
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
tv_cancel.onClick {
dismiss()
}
tv_ok.onClick {
adapter.getItems().forEach { tocRule ->
if (selectedName == tocRule.name) {
val callBack = activity as? CallBack
callBack?.onTocRegexDialogResult(tocRule.rule)
dismiss()
return@onClick
}
}
}
} }
private fun initData() { private fun initData() {
@ -138,14 +153,14 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
rootView?.let { rootView?.let {
tocRule.name = tv_rule_name.text.toString() tocRule.name = tv_rule_name.text.toString()
tocRule.rule = tv_rule_regex.text.toString() tocRule.rule = tv_rule_regex.text.toString()
saveRule(rule, tocRule) saveRule(tocRule, rule)
} }
} }
cancelButton() cancelButton()
}.show().applyTint() }.show().applyTint()
} }
private fun saveRule(oldRule: TxtTocRule?, rule: TxtTocRule) { private fun saveRule(rule: TxtTocRule, oldRule: TxtTocRule? = null) {
launch(IO) { launch(IO) {
if (rule.serialNumber < 0) { if (rule.serialNumber < 0) {
rule.serialNumber = adapter.getItems().lastOrNull()?.serialNumber ?: 0 + 1 rule.serialNumber = adapter.getItems().lastOrNull()?.serialNumber ?: 0 + 1
@ -181,6 +196,16 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
updateItems(0, itemCount - 1, true) updateItems(0, itemCount - 1, true)
} }
} }
swt_enabled.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) {
getItem(holder.layoutPosition)?.let {
it.enable = isChecked
launch(IO) {
App.db.txtTocRule().update(it)
}
}
}
}
iv_edit.onClick { iv_edit.onClick {
editRule(getItem(holder.layoutPosition)) editRule(getItem(holder.layoutPosition))
} }
@ -228,4 +253,8 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
} }
} }
interface CallBack {
fun onTocRegexDialogResult(tocRegex: String) {}
}
} }

@ -2,7 +2,10 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp">
<io.legado.app.ui.widget.text.TextInputLayout <io.legado.app.ui.widget.text.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"

Loading…
Cancel
Save