feat: 优化代码

pull/132/head
kunfei 5 years ago
parent f769b087a6
commit d8abfbe364
  1. 3
      app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt
  2. 71
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt
  3. 39
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt
  4. 3
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  5. 6
      app/src/main/java/io/legado/app/ui/widget/text/AutoCompleteTextView.kt
  6. 5
      app/src/main/res/menu/txt_toc_regex.xml
  7. 2
      app/src/main/res/values/strings.xml

@ -16,6 +16,9 @@ interface TxtTocRuleDao {
@get:Query("select * from txtTocRules where enable = 1 order by serialNumber") @get:Query("select * from txtTocRules where enable = 1 order by serialNumber")
val enabled: List<TxtTocRule> val enabled: List<TxtTocRule>
@get:Query("select ifNull(max(serialNumber), 0) from txtTocRules")
val lastOrderNum: Int
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg rule: TxtTocRule) fun insert(vararg rule: TxtTocRule)

@ -15,6 +15,7 @@ import androidx.lifecycle.Observer
import androidx.recyclerview.widget.ItemTouchHelper 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 com.google.android.material.snackbar.Snackbar
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
@ -27,9 +28,10 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.cancelButton import io.legado.app.lib.dialogs.cancelButton
import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.customView
import io.legado.app.lib.dialogs.okButton import io.legado.app.lib.dialogs.okButton
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.applyTint import io.legado.app.ui.widget.text.AutoCompleteTextView
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_edit_text.*
import kotlinx.android.synthetic.main.dialog_toc_regex.* import kotlinx.android.synthetic.main.dialog_toc_regex.*
import kotlinx.android.synthetic.main.dialog_toc_regex_edit.view.* import kotlinx.android.synthetic.main.dialog_toc_regex_edit.view.*
import kotlinx.android.synthetic.main.item_toc_regex.view.* import kotlinx.android.synthetic.main.item_toc_regex.view.*
@ -40,11 +42,12 @@ import java.util.*
class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
private val importTocRuleKey = "tocRuleUrl"
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
private var durRegex: String? = null private var durRegex: String? = null
lateinit var viewModel: TocRegexViewModel
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -58,6 +61,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
viewModel = getViewModel(TocRegexViewModel::class.java)
return inflater.inflate(R.layout.dialog_toc_regex, container) return inflater.inflate(R.layout.dialog_toc_regex, container)
} }
@ -121,17 +125,51 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) { when (item?.itemId) {
R.id.menu_add -> editRule() R.id.menu_add -> editRule()
R.id.menu_default -> importDefault() R.id.menu_default -> viewModel.importDefault()
R.id.menu_import -> showImportDialog()
} }
return false return false
} }
private fun importDefault() { @SuppressLint("InflateParams")
launch(IO) { private fun showImportDialog() {
AnalyzeTxtFile.getDefaultRules().let { val aCache = ACache.get(requireContext(), cacheDir = false)
App.db.txtTocRule().insert(*it.toTypedArray()) val defaultUrl = "https://gitee.com/fisher52/YueDuJson/raw/master/myTxtChapterRule.json"
} val cacheUrls: MutableList<String> = aCache
.getAsString(importTocRuleKey)
?.splitNotBlank(",")
?.toMutableList()
?: mutableListOf()
if (cacheUrls.contains(defaultUrl)) {
cacheUrls.add(0, defaultUrl)
} }
requireContext().alert(titleResource = R.string.import_book_source_on_line) {
var editText: AutoCompleteTextView? = null
customView {
layoutInflater.inflate(R.layout.dialog_edit_text, null).apply {
editText = edit_view
edit_view.setFilterValues(cacheUrls)
edit_view.delCallBack = {
cacheUrls.remove(it)
aCache.put(importTocRuleKey, cacheUrls.joinToString(","))
}
}
}
okButton {
val text = editText?.text?.toString()
text?.let {
if (!cacheUrls.contains(it)) {
cacheUrls.add(0, it)
aCache.put(importTocRuleKey, cacheUrls.joinToString(","))
}
Snackbar.make(tool_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show()
viewModel.importOnLine(it) { msg ->
tool_bar.snackbar(msg)
}
}
}
cancelButton()
}.show().applyTint()
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@ -151,25 +189,13 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
rootView?.apply { rootView?.apply {
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(tocRule, rule) viewModel.saveRule(tocRule, rule)
} }
} }
cancelButton() cancelButton()
}.show().applyTint() }.show().applyTint()
} }
private fun saveRule(rule: TxtTocRule, oldRule: TxtTocRule? = null) {
launch(IO) {
if (rule.serialNumber < 0) {
rule.serialNumber = adapter.getItems().lastOrNull()?.serialNumber ?: 0 + 1
}
oldRule?.let {
App.db.txtTocRule().delete(oldRule)
}
App.db.txtTocRule().insert(rule)
}
}
inner class TocRegexAdapter(context: Context) : inner class TocRegexAdapter(context: Context) :
SimpleRecyclerAdapter<TxtTocRule>(context, R.layout.item_toc_regex), SimpleRecyclerAdapter<TxtTocRule>(context, R.layout.item_toc_regex),
ItemTouchCallback.OnItemTouchCallbackListener { ItemTouchCallback.OnItemTouchCallbackListener {
@ -240,7 +266,6 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
} }
} }
companion object { companion object {
fun show(fragmentManager: FragmentManager, tocRegex: String? = null) { fun show(fragmentManager: FragmentManager, tocRegex: String? = null) {
val dialog = TocRegexDialog() val dialog = TocRegexDialog()

@ -0,0 +1,39 @@
package io.legado.app.ui.book.read.config
import android.app.Application
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.TxtTocRule
import io.legado.app.model.localBook.AnalyzeTxtFile
class TocRegexViewModel(application: Application) : BaseViewModel(application) {
fun saveRule(rule: TxtTocRule, oldRule: TxtTocRule? = null) {
execute {
if (rule.serialNumber < 0) {
rule.serialNumber = App.db.txtTocRule().lastOrderNum + 1
}
oldRule?.let {
App.db.txtTocRule().delete(oldRule)
}
App.db.txtTocRule().insert(rule)
}
}
fun importDefault() {
execute {
AnalyzeTxtFile.getDefaultRules().let {
App.db.txtTocRule().insert(*it.toTypedArray())
}
}
}
fun importOnLine(url: String, finally: (msg: String) -> Unit) {
execute {
}.onFinally {
}
}
}

@ -214,7 +214,8 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
customView { customView {
layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { layoutInflater.inflate(R.layout.dialog_edit_text, null).apply {
editText = edit_view editText = edit_view
edit_view.setFilterValues(cacheUrls) { edit_view.setFilterValues(cacheUrls)
edit_view.delCallBack = {
cacheUrls.remove(it) cacheUrls.remove(it)
aCache.put(importRecordKey, cacheUrls.joinToString(",")) aCache.put(importRecordKey, cacheUrls.joinToString(","))
} }

@ -42,15 +42,13 @@ class AutoCompleteTextView : AppCompatAutoCompleteTextView {
return super.onTouchEvent(event) return super.onTouchEvent(event)
} }
fun setFilterValues(values: List<String>?, delCallBack: ((value: String) -> Unit)? = null) { fun setFilterValues(values: List<String>?) {
this.delCallBack = delCallBack
values?.let { values?.let {
setAdapter(MyAdapter(context, values)) setAdapter(MyAdapter(context, values))
} }
} }
fun setFilterValues(vararg value: String, delCallBack: ((value: String) -> Unit)? = null) { fun setFilterValues(vararg value: String) {
this.delCallBack = delCallBack
setAdapter(MyAdapter(context, value.toMutableList())) setAdapter(MyAdapter(context, value.toMutableList()))
} }

@ -15,4 +15,9 @@
android:title="@string/import_default_rule" android:title="@string/import_default_rule"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/menu_import"
android:title="@string/import_replace_rule_on_line"
app:showAsAction="never" />
</menu> </menu>

@ -226,7 +226,7 @@
<string name="open_from_other">打开外部书籍</string> <string name="open_from_other">打开外部书籍</string>
<string name="origin_show">来源:%s</string> <string name="origin_show">来源:%s</string>
<string name="import_replace_rule">本地导入</string> <string name="import_replace_rule">本地导入</string>
<string name="import_replace_rule_on_line">网络导入</string> <string name="import_replace_rule_on_line">导入在线规则</string>
<string name="check_update_interval">检查更新间隔</string> <string name="check_update_interval">检查更新间隔</string>
<string name="bookshelf_px_0">按阅读时间</string> <string name="bookshelf_px_0">按阅读时间</string>
<string name="bookshelf_px_1">按更新时间</string> <string name="bookshelf_px_1">按更新时间</string>

Loading…
Cancel
Save