From 0a8fc314b2cbe3cdb9c461874da67bde894d04ea Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 1 Sep 2019 22:22:40 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/ReplaceRuleDao.kt | 5 +- .../app/ui/replacerule/ReplaceRuleActivity.kt | 47 +++++++++---------- .../app/ui/replacerule/ReplaceRuleAdapter.kt | 10 ++-- .../ui/replacerule/ReplaceRuleViewModel.kt | 15 ++++++ 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index b291ac968..f00be116d 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt @@ -49,10 +49,7 @@ interface ReplaceRuleDao { fun enableAll(enable: Boolean) @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insert(vararg replaceRules: ReplaceRule): List - - @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insert(replaceRule: ReplaceRule): Long + fun insert(vararg replaceRule: ReplaceRule): List @Update fun update(replaceRules: ReplaceRule) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 0348ef71a..fda735181 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -23,9 +23,11 @@ import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast -class ReplaceRuleActivity : VMBaseActivity(R.layout.activity_replace_rule) { +class ReplaceRuleActivity : VMBaseActivity(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>? = null private var allEnabled = false @@ -51,31 +53,7 @@ class ReplaceRuleActivity : VMBaseActivity(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(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!") + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index 80cc79cce..78587b1a5 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -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(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) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index a1f9ca0d6..ac5ce0ca9 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -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) + } + } + }