From 1d61c72515ff9800c45be5ca97aad5b293e0eec2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 2 Sep 2019 21:05:04 +0800 Subject: [PATCH] up --- app/src/main/AndroidManifest.xml | 1 - .../io/legado/app/data/dao/ReplaceRuleDao.kt | 6 +- .../app/ui/replacerule/ReplaceEditActivity.kt | 33 -------- .../app/ui/replacerule/ReplaceEditDialog.kt | 61 ++++++++++++++ .../ui/replacerule/ReplaceEditViewModel.kt | 26 ++++++ .../app/ui/replacerule/ReplaceRuleActivity.kt | 3 +- .../main/res/layout/activity_replace_edit.xml | 7 -- .../main/res/layout/dialog_replace_edit.xml | 83 +++++++++++++++++++ 8 files changed, 175 insertions(+), 45 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditActivity.kt create mode 100644 app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt delete mode 100644 app/src/main/res/layout/activity_replace_edit.xml create mode 100644 app/src/main/res/layout/dialog_replace_edit.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a3aa7b7b5..bca5f59fb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -61,7 +61,6 @@ - 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 43225b80a..6c0031aa4 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 @@ -13,7 +13,7 @@ interface ReplaceRuleDao { fun observeAll(): DataSource.Factory @Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC") - fun observeAllIds(): LiveData> + fun observeAllIds(): LiveData> @get:Query("SELECT MAX(sortOrder) FROM replace_rules") val maxOrder: Int @@ -25,10 +25,10 @@ interface ReplaceRuleDao { val allEnabled: List @Query("SELECT * FROM replace_rules WHERE id = :id") - fun findById(id: Int): ReplaceRule? + fun findById(id: Long): ReplaceRule? @Query("SELECT * FROM replace_rules WHERE id in (:ids)") - fun findByIds(vararg ids: Int): List + fun findByIds(vararg ids: Long): List @Query( """SELECT * FROM replace_rules WHERE isEnabled = 1 diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditActivity.kt deleted file mode 100644 index 464fcf9a9..000000000 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditActivity.kt +++ /dev/null @@ -1,33 +0,0 @@ -package io.legado.app.ui.replacerule - -import android.os.Bundle -import android.view.Menu -import android.view.MenuItem -import io.legado.app.R -import io.legado.app.base.VMBaseActivity -import io.legado.app.utils.getViewModel - -class ReplaceEditActivity : - VMBaseActivity(R.layout.activity_replace_edit, false) { - override val viewModel: ReplaceEditViewModel - get() = getViewModel(ReplaceEditViewModel::class.java) - - override fun onActivityCreated(savedInstanceState: Bundle?) { - - } - - override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { - menuInflater.inflate(R.menu.replace_edit, menu) - return super.onCompatCreateOptionsMenu(menu) - } - - override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.menu_save -> { - } - } - return super.onCompatOptionsItemSelected(item) - } - - -} diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt new file mode 100644 index 000000000..b47f0c3a9 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt @@ -0,0 +1,61 @@ +package io.legado.app.ui.replacerule + +import android.os.Bundle +import android.util.DisplayMetrics +import android.view.LayoutInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import android.view.ViewGroup.LayoutParams.WRAP_CONTENT +import androidx.appcompat.widget.Toolbar +import androidx.fragment.app.DialogFragment +import androidx.lifecycle.Observer +import io.legado.app.R +import io.legado.app.data.entities.ReplaceRule +import io.legado.app.utils.applyTint +import io.legado.app.utils.getViewModel +import kotlinx.android.synthetic.main.dialog_replace_edit.* + +class ReplaceEditDialog : DialogFragment(), + Toolbar.OnMenuItemClickListener { + private lateinit var viewModel: ReplaceEditViewModel + + override fun onStart() { + super.onStart() + val dm = DisplayMetrics() + activity?.windowManager?.defaultDisplay?.getMetrics(dm) + dialog?.window?.setLayout((dm.widthPixels * 0.9).toInt(), WRAP_CONTENT) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + viewModel = getViewModel(ReplaceEditViewModel::class.java) + return inflater.inflate(R.layout.dialog_replace_edit, container) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + tool_bar.inflateMenu(R.menu.replace_edit) + tool_bar.menu.applyTint(requireContext(), false) + tool_bar.setOnMenuItemClickListener(this) + viewModel.replaceRuleData.observe(this, Observer { + upReplaceView(it) + }) + } + + override fun onMenuItemClick(item: MenuItem?): Boolean { + when (item?.itemId) { + R.id.menu_save -> { + } + } + return true + } + + private fun upReplaceView(replaceRule: ReplaceRule) { + + } + +} diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt index 5e643727d..aa09b0003 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt @@ -1,9 +1,35 @@ package io.legado.app.ui.replacerule import android.app.Application +import android.content.Intent +import androidx.lifecycle.MutableLiveData +import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.ReplaceRule class ReplaceEditViewModel(application: Application) : BaseViewModel(application) { + val replaceRuleData = MutableLiveData() + + fun initData(intent: Intent) { + execute { + replaceRuleData.value ?: let { + val id = intent.getLongExtra("data", -1) + if (id > 0) { + App.db.replaceRuleDao().findById(id)?.let { + replaceRuleData.postValue(it) + } + } + } + } + } + + fun save(replaceRule: ReplaceRule, success: () -> Unit) { + execute { + App.db.replaceRuleDao().insert(replaceRule) + }.onSuccess { + success() + } + } } 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 a1854810c..41a83d31f 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 @@ -44,7 +44,8 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - + R.id.menu_add_replace_rule -> + ReplaceEditDialog().show(supportFragmentManager, "replaceNew") } return super.onCompatOptionsItemSelected(item) } diff --git a/app/src/main/res/layout/activity_replace_edit.xml b/app/src/main/res/layout/activity_replace_edit.xml deleted file mode 100644 index d829e291c..000000000 --- a/app/src/main/res/layout/activity_replace_edit.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_replace_edit.xml b/app/src/main/res/layout/dialog_replace_edit.xml new file mode 100644 index 000000000..d68f7dd3e --- /dev/null +++ b/app/src/main/res/layout/dialog_replace_edit.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file