parent
7441fcd156
commit
2f33336d74
@ -0,0 +1,49 @@ |
||||
package io.legado.app.data.dao |
||||
|
||||
import androidx.lifecycle.LiveData |
||||
import androidx.paging.DataSource |
||||
import androidx.room.* |
||||
import io.legado.app.data.entities.ReplaceRule |
||||
|
||||
|
||||
@Dao |
||||
interface ReplaceRuleDao { |
||||
|
||||
@Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC") |
||||
fun observeAll(): DataSource.Factory<Int, ReplaceRule> |
||||
|
||||
@Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC") |
||||
fun observeAllIds(): LiveData<List<Int>> |
||||
|
||||
@get:Query("SELECT MAX(sortOrder) FROM replace_rules") |
||||
val maxOrder: Int |
||||
|
||||
@get:Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC") |
||||
val all: List<ReplaceRule> |
||||
|
||||
@get:Query("SELECT * FROM replace_rules WHERE isEnabled = 1 ORDER BY sortOrder ASC") |
||||
val allEnabled: List<ReplaceRule> |
||||
|
||||
@Query("SELECT * FROM replace_rules WHERE id = :id") |
||||
fun findById(id: Int): ReplaceRule? |
||||
|
||||
@Query("SELECT * FROM replace_rules WHERE id in (:ids)") |
||||
fun findByIds(vararg ids: Int): List<ReplaceRule> |
||||
|
||||
@Query("SELECT * FROM replace_rules WHERE isEnabled = 1 AND scope LIKE '%' || :scope || '%'") |
||||
fun findEnabledByScope(scope: String): List<ReplaceRule> |
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE) |
||||
fun insert(vararg replaceRules: ReplaceRule) |
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE) |
||||
fun insert(replaceRule: ReplaceRule): Long |
||||
|
||||
@Update |
||||
fun update(vararg replaceRules: ReplaceRule) |
||||
|
||||
@Delete |
||||
fun delete(vararg replaceRules: ReplaceRule) |
||||
|
||||
|
||||
} |
@ -0,0 +1,53 @@ |
||||
package io.legado.app.ui.replacerule |
||||
|
||||
import android.os.Bundle |
||||
import androidx.appcompat.app.AppCompatActivity |
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.Observer |
||||
import androidx.paging.LivePagedListBuilder |
||||
import androidx.paging.PagedList |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import io.legado.app.App |
||||
import io.legado.app.R |
||||
import io.legado.app.data.entities.ReplaceRule |
||||
import kotlinx.android.synthetic.main.activity_replace_rule.* |
||||
import org.jetbrains.anko.doAsync |
||||
import org.jetbrains.anko.toast |
||||
|
||||
|
||||
class ReplaceRuleActivity : AppCompatActivity() { |
||||
private lateinit var adapter: ReplaceRuleAdapter |
||||
private var rulesLiveData: LiveData<PagedList<ReplaceRule>>? = null |
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) { |
||||
super.onCreate(savedInstanceState) |
||||
setContentView(R.layout.activity_replace_rule) |
||||
rv_replace_rule.layoutManager = LinearLayoutManager(this) |
||||
initRecyclerView() |
||||
initDataObservers() |
||||
} |
||||
|
||||
private fun initRecyclerView() { |
||||
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) } |
||||
} |
||||
override fun delete(rule: ReplaceRule) { |
||||
doAsync { App.db.replaceRuleDao().delete(rule) } |
||||
} |
||||
override fun edit(rule: ReplaceRule) { |
||||
toast("Edit function not implemented!") |
||||
} |
||||
} |
||||
rv_replace_rule.adapter = adapter |
||||
} |
||||
|
||||
private fun initDataObservers() { |
||||
rulesLiveData?.removeObservers(this) |
||||
rulesLiveData = LivePagedListBuilder(App.db.replaceRuleDao().observeAll(), 30).build() |
||||
rulesLiveData?.observe(this, Observer<PagedList<ReplaceRule>> { adapter.submitList(it) }) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,70 @@ |
||||
package io.legado.app.ui.replacerule |
||||
|
||||
import android.content.Context |
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import androidx.core.view.isGone |
||||
import androidx.paging.PagedListAdapter |
||||
import androidx.recyclerview.widget.DiffUtil |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
import io.legado.app.R |
||||
import io.legado.app.data.entities.ReplaceRule |
||||
import kotlinx.android.synthetic.main.item_relace_rule.view.* |
||||
import org.jetbrains.anko.sdk27.listeners.onClick |
||||
|
||||
|
||||
class ReplaceRuleAdapter(context: Context) : |
||||
PagedListAdapter<ReplaceRule, ReplaceRuleAdapter.MyViewHolder>(DIFF_CALLBACK) { |
||||
|
||||
var onClickListener: OnClickListener? = null |
||||
|
||||
companion object { |
||||
|
||||
@JvmField |
||||
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<ReplaceRule>() { |
||||
override fun areItemsTheSame(oldItem: ReplaceRule, newItem: ReplaceRule): Boolean = |
||||
oldItem.id == newItem.id |
||||
|
||||
override fun areContentsTheSame(oldItem: ReplaceRule, newItem: ReplaceRule): Boolean = |
||||
oldItem.id == newItem.id |
||||
&& oldItem.pattern == newItem.pattern |
||||
&& oldItem.replacement == newItem.replacement |
||||
&& oldItem.isRegex == newItem.isRegex |
||||
&& oldItem.isEnabled == newItem.isEnabled |
||||
&& oldItem.scope == newItem.scope |
||||
} |
||||
} |
||||
|
||||
init { |
||||
notifyDataSetChanged() |
||||
} |
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
||||
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_relace_rule, parent, false)) |
||||
} |
||||
|
||||
override fun onBindViewHolder(holder: MyViewHolder, pos: Int) { |
||||
getItem(pos)?.let { holder.bind(it, onClickListener, pos == itemCount - 1) } |
||||
} |
||||
|
||||
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { |
||||
fun bind(rule: ReplaceRule, listener: OnClickListener?, hideDivider: Boolean) = with(itemView) { |
||||
cb_enable.text = rule.name |
||||
cb_enable.isChecked = rule.isEnabled |
||||
divider.isGone = hideDivider |
||||
iv_delete.onClick { listener?.delete(rule) } |
||||
iv_edit.onClick { listener?.edit(rule) } |
||||
cb_enable.onClick { |
||||
rule.isEnabled = cb_enable.isChecked |
||||
listener?.update(rule) |
||||
} |
||||
} |
||||
} |
||||
|
||||
interface OnClickListener { |
||||
fun update(rule: ReplaceRule) |
||||
fun delete(rule: ReplaceRule) |
||||
fun edit(rule: ReplaceRule) |
||||
} |
||||
} |
@ -0,0 +1,5 @@ |
||||
package io.legado.app.utils |
||||
|
||||
import android.os.Environment |
||||
|
||||
fun getSdPath() = Environment.getExternalStorageDirectory().absolutePath |
@ -0,0 +1,9 @@ |
||||
package io.legado.app.utils |
||||
|
||||
import com.jayway.jsonpath.ReadContext |
||||
|
||||
fun ReadContext.readString(path: String) = this.read(path, String::class.java) |
||||
|
||||
fun ReadContext.readBool(path: String) = this.read(path, Boolean::class.java) |
||||
|
||||
fun ReadContext.readInt(path: String) = this.read(path, Int::class.java) |
@ -0,0 +1,4 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> |
||||
<item android:drawable="@android:color/transparent"/> |
||||
</selector> |
@ -0,0 +1,17 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:width="24dp" |
||||
android:height="24dp" |
||||
android:viewportWidth="24" |
||||
android:viewportHeight="24"> |
||||
|
||||
<path |
||||
android:fillColor="#595757" |
||||
android:pathData="M14.434,20 L9.566,20 C7.535,20,5.863,18.665,5.761,16.961 L5.18,7.199 L4,7.199 L4,5.919 L8.402,5.919 L8.644,5.315 C8.957,4.526,9.827,4,10.813,4 L13.188,4 C14.172,4,15.043,4.526,15.355,5.313 L15.598,5.918 L20,5.918 L20,7.198 L18.82,7.198 L18.238,16.96 C18.137,18.665,16.466,20,14.434,20 Z M6.706,7.199 L7.283,16.898 C7.344,17.917,8.347,18.719,9.566,18.719 L14.433,18.719 C15.653,18.719,16.656,17.916,16.716,16.898 L17.293,7.199 L6.706,7.199 Z M10.01,5.919 L13.99,5.919 L13.91,5.718 C13.806,5.457,13.515,5.28,13.187,5.28 L10.812,5.28 C10.484,5.28,10.193,5.457,10.089,5.718 L10.01,5.919 Z" /> |
||||
<path |
||||
android:fillColor="#595757" |
||||
android:pathData="M9.542,16.509 L9.127,8.83 L10.648,8.773 L11.064,16.449 L9.542,16.509 Z" /> |
||||
<path |
||||
android:fillColor="#595757" |
||||
android:pathData="M14.457,16.509 L12.936,16.449 L13.352,8.773 L14.873,8.83 L14.457,16.509 Z" /> |
||||
</vector> |
@ -0,0 +1,14 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:width="24dp" |
||||
android:height="24dp" |
||||
android:viewportWidth="24" |
||||
android:viewportHeight="24"> |
||||
|
||||
<path |
||||
android:fillColor="#595757" |
||||
android:pathData="M5.6,20.009c-0.881,0-1.6-0.789-1.6-1.758V5.749C4,4.78,4.719,3.991,5.6,3.991h10.62 c0.882,0,1.599,0.789,1.599,1.758v1.258h-1.483l-0.076-1.258c0-0.122-0.062-0.172-0.062-0.172L5.6,5.582 c0.002,0.015-0.039,0.069-0.039,0.167v12.502c0,0.107,0.051,0.164,0.063,0.172l10.596-0.005c-0.002-0.014,0.039-0.067,0.039-0.167 l0.016-4.739h1.469l0.075,4.739c0,0.969-0.717,1.758-1.599,1.758H5.6z" /> |
||||
<path |
||||
android:fillColor="#595757" |
||||
android:pathData="M 12.549 13.354 L 13.738 12.323 L 13.738 12.323 L 18.967 7.553 L 20 8.646 L 14.658 13.514 L 13.54 14.515 Z" /> |
||||
</vector> |
@ -0,0 +1,30 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout |
||||
xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context="io.legado.app.ui.search.SearchActivity"> |
||||
|
||||
<io.legado.app.ui.widget.TitleBar |
||||
android:id="@+id/titleBar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
app:layout_constraintTop_toTopOf="parent" |
||||
app:title="净化替换"/> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/rv_replace_rule" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:paddingStart="8dp" |
||||
android:paddingEnd="8dp" |
||||
android:layout_marginBottom="8dp" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@+id/titleBar" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintVertical_bias="0.0"/> |
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
@ -0,0 +1,57 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout |
||||
xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:paddingStart="8dp" |
||||
android:paddingEnd="8dp"> |
||||
|
||||
<androidx.appcompat.widget.AppCompatCheckBox |
||||
android:id="@+id/cb_enable" |
||||
android:text="Rule Name" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:paddingTop="16dp" |
||||
android:paddingBottom="16dp" |
||||
android:textSize="16sp" |
||||
android:textColor="@color/text_default" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintEnd_toStartOf="@+id/iv_delete" |
||||
app:layout_constraintTop_toTopOf="parent" |
||||
app:layout_constraintBottom_toBottomOf="parent" /> |
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView |
||||
android:id="@+id/iv_edit" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:padding="8dp" |
||||
android:background="@drawable/bg_ib_pre_round" |
||||
android:contentDescription="@string/edit" |
||||
android:src="@drawable/ic_edit" |
||||
app:tint="@color/text_default" |
||||
app:layout_constraintEnd_toStartOf="@id/iv_delete" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent"/> |
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView |
||||
android:id="@+id/iv_delete" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:padding="8dp" |
||||
android:background="@drawable/bg_ib_pre_round" |
||||
android:contentDescription="@string/delete" |
||||
android:src="@drawable/ic_clear_all" |
||||
app:tint="@color/text_default" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent"/> |
||||
|
||||
<View |
||||
android:id="@+id/divider" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="0.5dp" |
||||
android:background="@color/divider" |
||||
app:layout_constraintBottom_toBottomOf="parent"/> |
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
Loading…
Reference in new issue