addig a clipsAdapter for displaying stored clips

This commit is contained in:
tibbi 2022-01-27 23:24:23 +01:00
parent 54a9f7b286
commit a9204c90df
5 changed files with 131 additions and 58 deletions

View File

@ -9,8 +9,10 @@ import com.simplemobiletools.commons.extensions.underlineText
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.adapters.ClipsActivityAdapter
import com.simplemobiletools.keyboard.dialogs.AddClipDialog
import com.simplemobiletools.keyboard.extensions.clipsDB
import com.simplemobiletools.keyboard.models.Clip
import kotlinx.android.synthetic.main.activity_manage_clipboard_items.*
class ManageClipboardItemsActivity : SimpleActivity() {
@ -48,8 +50,13 @@ class ManageClipboardItemsActivity : SimpleActivity() {
private fun updateClips() {
ensureBackgroundThread {
val clips = clipsDB.getClips()
val clips = clipsDB.getClips().toMutableList() as ArrayList<Clip>
runOnUiThread {
ClipsActivityAdapter(this, clips, clipboard_items_list) {
}.apply {
clipboard_items_list.adapter = this
}
clipboard_items_list.beVisibleIf(clips.isNotEmpty())
clipboard_items_placeholder.beVisibleIf(clips.isEmpty())
clipboard_items_placeholder_2.beVisibleIf(clips.isEmpty())

View File

@ -0,0 +1,109 @@
package com.simplemobiletools.keyboard.adapters
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.interfaces.ItemMoveCallback
import com.simplemobiletools.commons.interfaces.ItemTouchHelperContract
import com.simplemobiletools.commons.interfaces.StartReorderDragListener
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView
import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.models.Clip
import kotlinx.android.synthetic.main.item_clip_in_activity.view.*
import java.util.*
class ClipsActivityAdapter(
activity: BaseSimpleActivity, var items: ArrayList<Clip>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), ItemTouchHelperContract {
private var touchHelper: ItemTouchHelper? = null
private var startReorderDragListener: StartReorderDragListener
init {
setupDragListener(true)
touchHelper = ItemTouchHelper(ItemMoveCallback(this))
touchHelper!!.attachToRecyclerView(recyclerView)
startReorderDragListener = object : StartReorderDragListener {
override fun requestDrag(viewHolder: RecyclerView.ViewHolder) {
touchHelper?.startDrag(viewHolder)
}
}
}
override fun getActionMenuId() = R.menu.cab_clips
override fun onBottomActionMenuCreated(view: BottomActionMenuView) {}
override fun actionItemPressed(id: Int) {
if (selectedKeys.isEmpty()) {
return
}
when (id) {
R.id.cab_delete -> deleteSelection()
}
}
override fun getSelectableItemCount() = items.size
override fun getIsItemSelectable(position: Int) = true
override fun getItemSelectionKey(position: Int) = items.getOrNull(position)?.id?.toInt()
override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id == key.toLong() }
override fun onActionModeDestroyed() {
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_clip_in_activity, parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.bindView(item, true, true) { itemView, layoutPosition ->
setupView(itemView, item, holder)
}
bindViewHolder(holder)
}
override fun getItemCount() = items.size
private fun deleteSelection() {}
private fun getItemWithKey(key: Int): Clip? = items.firstOrNull { it.id == key.toLong() }
private fun getSelectedItems() = items.filter { selectedKeys.contains(it.id!!.toInt()) } as ArrayList<Clip>
private fun setupView(view: View, clip: Clip, holder: ViewHolder) {
val isSelected = selectedKeys.contains(clip.id!!.toInt())
view.apply {
clip_value.text = clip.value
clip_value.setTextColor(textColor)
clip_holder.isSelected = isSelected
}
}
override fun onRowMoved(fromPosition: Int, toPosition: Int) {
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(items, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(items, i, i - 1)
}
}
notifyItemMoved(fromPosition, toPosition)
}
override fun onRowSelected(myViewHolder: ViewHolder?) {}
override fun onRowClear(myViewHolder: ViewHolder?) {}
}

View File

@ -1,54 +0,0 @@
package com.simplemobiletools.keyboard.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.commons.extensions.removeUnderlines
import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.extensions.config
import com.simplemobiletools.keyboard.models.Clip
import kotlinx.android.synthetic.main.item_clip.view.*
import java.util.*
class ClipsAdapter(val context: Context, var clips: ArrayList<Clip>, val itemClick: (clip: Clip) -> Unit) :
RecyclerView.Adapter<ClipsAdapter.ViewHolderr>() {
private val layoutInflater = LayoutInflater.from(context)
private val baseConfig = context.config
private var textColor = baseConfig.textColor
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderr {
val view = layoutInflater.inflate(R.layout.item_clip, parent, false)
return ViewHolderr(view)
}
override fun onBindViewHolder(holder: ViewHolderr, position: Int) {
val item = clips[position]
holder.bindView(item) { itemView ->
setupView(itemView, item)
}
}
override fun getItemCount() = clips.size
private fun setupView(view: View, clip: Clip) {
view.clip_value.apply {
text = clip.value
removeUnderlines()
setTextColor(textColor)
}
}
open inner class ViewHolderr(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(clip: Clip, callback: (itemView: View) -> Unit): View {
return itemView.apply {
callback(this)
setOnClickListener {
itemClick.invoke(clip)
}
}
}
}
}

View File

@ -4,14 +4,16 @@
android:id="@+id/clip_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/small_margin"
android:background="@drawable/clipboard_background">
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:foreground="@drawable/selector">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/clip_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/medium_margin"
android:padding="@dimen/activity_margin"
android:textSize="@dimen/normal_text_size"
tools:text="Hello, how are you?" />

View File

@ -0,0 +1,9 @@
<?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/cab_delete"
android:icon="@drawable/ic_delete_vector"
android:title="@string/delete"
app:showAsAction="ifRoom" />
</menu>