pull/32/head
kunfei 5 years ago
parent 8bdc639246
commit 0a8fc314b2
  1. 5
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 47
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  3. 10
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  4. 15
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt

@ -49,10 +49,7 @@ interface ReplaceRuleDao {
fun enableAll(enable: Boolean)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg replaceRules: ReplaceRule): List<Long>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(replaceRule: ReplaceRule): Long
fun insert(vararg replaceRule: ReplaceRule): List<Long>
@Update
fun update(replaceRules: ReplaceRule)

@ -23,9 +23,11 @@ import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast
class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activity_replace_rule) {
class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activity_replace_rule),
ReplaceRuleAdapter.CallBack {
override val viewModel: ReplaceRuleViewModel
get() = getViewModel(ReplaceRuleViewModel::class.java)
private lateinit var adapter: ReplaceRuleAdapter
private var rulesLiveData: LiveData<PagedList<ReplaceRule>>? = null
private var allEnabled = false
@ -51,31 +53,7 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
private fun initRecyclerView() {
ATH.applyEdgeEffectColor(rv_replace_rule)
rv_replace_rule.layoutManager = LinearLayoutManager(this)
adapter = ReplaceRuleAdapter(this)
adapter.onClickListener = object : ReplaceRuleAdapter.OnClickListener {
override fun update(rule: ReplaceRule) {
doAsync {
App.db.replaceRuleDao().update(rule)
updateEnableAll()
}
}
override fun delete(rule: ReplaceRule) {
doAsync {
App.db.replaceRuleDao().delete(rule)
updateEnableAll()
}
}
override fun edit(rule: ReplaceRule) {
doAsync {
App.db.replaceRuleDao().enableAll(!allEnabled)
allEnabled = !allEnabled
}
toast("Edit function not implemented!")
}
}
adapter = ReplaceRuleAdapter(this, this)
rv_replace_rule.adapter = adapter
rv_replace_rule.addItemDecoration(
DividerItemDecoration(this, DividerItemDecoration.VERTICAL).apply {
@ -121,4 +99,21 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
}).attachToRecyclerView(rv_replace_rule)
}
override fun update(rule: ReplaceRule) {
viewModel.update(rule)
}
override fun delete(rule: ReplaceRule) {
viewModel.delete(rule)
}
override fun edit(rule: ReplaceRule) {
doAsync {
App.db.replaceRuleDao().enableAll(!allEnabled)
allEnabled = !allEnabled
}
toast("Edit function not implemented!")
}
}

@ -14,11 +14,9 @@ import kotlinx.android.synthetic.main.item_relace_rule.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class ReplaceRuleAdapter(context: Context) :
class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
PagedListAdapter<ReplaceRule, ReplaceRuleAdapter.MyViewHolder>(DIFF_CALLBACK) {
var onClickListener: OnClickListener? = null
companion object {
@JvmField
@ -45,11 +43,11 @@ class ReplaceRuleAdapter(context: Context) :
}
override fun onBindViewHolder(holder: MyViewHolder, pos: Int) {
getItem(pos)?.let { holder.bind(it, onClickListener) }
getItem(pos)?.let { holder.bind(it, callBack) }
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(rule: ReplaceRule, listener: OnClickListener?) = with(itemView) {
fun bind(rule: ReplaceRule, listener: CallBack?) = with(itemView) {
tv_name.text = rule.name
swt_enabled.isChecked = rule.isEnabled
// divider.isGone = hideDivider
@ -64,7 +62,7 @@ class ReplaceRuleAdapter(context: Context) :
}
}
interface OnClickListener {
interface CallBack {
fun update(rule: ReplaceRule)
fun delete(rule: ReplaceRule)
fun edit(rule: ReplaceRule)

@ -1,8 +1,23 @@
package io.legado.app.ui.replacerule
import android.app.Application
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.ReplaceRule
class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) {
fun update(rule: ReplaceRule) {
execute {
App.db.replaceRuleDao().update(rule)
}
}
fun delete(rule: ReplaceRule) {
execute {
App.db.replaceRuleDao().delete(rule)
}
}
}

Loading…
Cancel
Save