pull/1705/head
parent
16228b4331
commit
ff4e37134f
@ -0,0 +1,156 @@ |
||||
package io.legado.app.ui.widget.keyboard |
||||
|
||||
import android.content.Context |
||||
import android.os.Bundle |
||||
import android.view.MenuItem |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import androidx.appcompat.widget.Toolbar |
||||
import androidx.recyclerview.widget.ItemTouchHelper |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BaseDialogFragment |
||||
import io.legado.app.base.adapter.ItemViewHolder |
||||
import io.legado.app.base.adapter.RecyclerAdapter |
||||
import io.legado.app.data.appDb |
||||
import io.legado.app.data.entities.KeyboardAssist |
||||
import io.legado.app.databinding.DialogMultipleEditTextBinding |
||||
import io.legado.app.databinding.DialogRecyclerViewBinding |
||||
import io.legado.app.databinding.Item1lineTextAndDelBinding |
||||
import io.legado.app.lib.dialogs.alert |
||||
import io.legado.app.lib.theme.primaryColor |
||||
import io.legado.app.ui.widget.recycler.ItemTouchCallback |
||||
import io.legado.app.ui.widget.recycler.VerticalDivider |
||||
import io.legado.app.utils.applyTint |
||||
import io.legado.app.utils.setLayout |
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding |
||||
import io.legado.app.utils.visible |
||||
import kotlinx.coroutines.Dispatchers.IO |
||||
import kotlinx.coroutines.launch |
||||
|
||||
class KeyboardAssistsConfig : BaseDialogFragment(R.layout.dialog_recycler_view), |
||||
Toolbar.OnMenuItemClickListener { |
||||
|
||||
private val binding by viewBinding(DialogRecyclerViewBinding::bind) |
||||
private val adapter by lazy { KeyAdapter(requireContext()) } |
||||
|
||||
override fun onStart() { |
||||
super.onStart() |
||||
setLayout(0.9f, 0.9f) |
||||
} |
||||
|
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { |
||||
binding.toolBar.setBackgroundColor(primaryColor) |
||||
binding.toolBar.setTitle(R.string.assists_key_config) |
||||
initView() |
||||
initMenu() |
||||
initData() |
||||
} |
||||
|
||||
private fun initView() { |
||||
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) |
||||
binding.recyclerView.addItemDecoration(VerticalDivider(requireContext())) |
||||
binding.recyclerView.adapter = adapter |
||||
val itemTouchCallback = ItemTouchCallback(adapter) |
||||
itemTouchCallback.isCanDrag = true |
||||
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView) |
||||
|
||||
} |
||||
|
||||
private fun initMenu() { |
||||
binding.toolBar.setOnMenuItemClickListener(this) |
||||
binding.toolBar.inflateMenu(R.menu.keyboard_assists_config) |
||||
binding.toolBar.menu.applyTint(requireContext()) |
||||
} |
||||
|
||||
private fun initData() { |
||||
launch { |
||||
appDb.keyboardAssistsDao.flowAll.collect { |
||||
adapter.setItems(it) |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun onMenuItemClick(item: MenuItem?): Boolean { |
||||
when (item?.itemId) { |
||||
R.id.menu_add -> editKey(null) |
||||
} |
||||
return false |
||||
} |
||||
|
||||
private fun editKey(keyboardAssist: KeyboardAssist?) { |
||||
alert { |
||||
setTitle("辅助按键") |
||||
val alertBinding = DialogMultipleEditTextBinding.inflate(layoutInflater).apply { |
||||
layout1.hint = "key" |
||||
layout2.hint = "value" |
||||
layout2.visible() |
||||
} |
||||
setCustomView(alertBinding.root) |
||||
cancelButton() |
||||
okButton { |
||||
launch(IO) { |
||||
val newKeyboardAssist = KeyboardAssist( |
||||
key = alertBinding.edit1.text.toString(), |
||||
value = alertBinding.edit2.text.toString() |
||||
) |
||||
if (keyboardAssist == null) { |
||||
newKeyboardAssist.serialNo = appDb.keyboardAssistsDao.maxSerialNo + 1 |
||||
appDb.keyboardAssistsDao.insert(newKeyboardAssist) |
||||
} else { |
||||
newKeyboardAssist.serialNo = keyboardAssist.serialNo |
||||
appDb.keyboardAssistsDao.delete(keyboardAssist) |
||||
appDb.keyboardAssistsDao.insert(newKeyboardAssist) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private inner class KeyAdapter(context: Context) : |
||||
RecyclerAdapter<KeyboardAssist, Item1lineTextAndDelBinding>(context), |
||||
ItemTouchCallback.Callback { |
||||
|
||||
private var isMoved = false |
||||
|
||||
override fun getViewBinding(parent: ViewGroup): Item1lineTextAndDelBinding { |
||||
return Item1lineTextAndDelBinding.inflate(inflater, parent, false) |
||||
} |
||||
|
||||
override fun convert( |
||||
holder: ItemViewHolder, |
||||
binding: Item1lineTextAndDelBinding, |
||||
item: KeyboardAssist, |
||||
payloads: MutableList<Any> |
||||
) { |
||||
binding.textView.text = item.key |
||||
} |
||||
|
||||
override fun registerListener(holder: ItemViewHolder, binding: Item1lineTextAndDelBinding) { |
||||
binding.ivDelete.setOnClickListener { |
||||
getItemByLayoutPosition(holder.layoutPosition)?.let { keyboardAssist -> |
||||
editKey(keyboardAssist) |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun swap(srcPosition: Int, targetPosition: Int): Boolean { |
||||
swapItem(srcPosition, targetPosition) |
||||
isMoved = true |
||||
return true |
||||
} |
||||
|
||||
override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { |
||||
if (isMoved) { |
||||
for ((index, item) in getItems().withIndex()) { |
||||
item.serialNo = index + 1 |
||||
} |
||||
launch(IO) { |
||||
appDb.keyboardAssistsDao.update(*getItems().toTypedArray()) |
||||
} |
||||
} |
||||
isMoved = false |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,45 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:overScrollMode="ifContentScrolls" |
||||
android:paddingLeft="16dp" |
||||
android:paddingRight="16dp"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical"> |
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:id="@+id/layout_1" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content"> |
||||
|
||||
<io.legado.app.ui.widget.text.AutoCompleteTextView |
||||
android:id="@+id/edit_1" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:id="@+id/layout_2" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:visibility="gone"> |
||||
|
||||
<io.legado.app.ui.widget.text.AutoCompleteTextView |
||||
android:id="@+id/edit_2" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
|
||||
|
||||
</LinearLayout> |
||||
|
||||
</androidx.core.widget.NestedScrollView> |
@ -0,0 +1,11 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto"> |
||||
|
||||
<item |
||||
android:id="@+id/menu_add" |
||||
android:icon="@drawable/ic_add" |
||||
android:title="@string/add" |
||||
app:showAsAction="always" /> |
||||
|
||||
</menu> |
Loading…
Reference in new issue