prepare for showing clip sections at keyboard

This commit is contained in:
tibbi 2022-01-28 23:18:53 +01:00
parent 5c58c1224b
commit 8372368cc9
6 changed files with 43 additions and 9 deletions

View File

@ -9,10 +9,11 @@ import com.simplemobiletools.commons.extensions.removeUnderlines
import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.extensions.config
import com.simplemobiletools.keyboard.models.Clip
import com.simplemobiletools.keyboard.models.ListItem
import kotlinx.android.synthetic.main.item_clip_on_keyboard.view.*
import java.util.*
class ClipsKeyboardAdapter(val context: Context, var clips: ArrayList<Clip>, val itemClick: (clip: Clip) -> Unit) :
class ClipsKeyboardAdapter(val context: Context, var clips: ArrayList<ListItem>, val itemClick: (clip: Clip) -> Unit) :
RecyclerView.Adapter<ClipsKeyboardAdapter.ViewHolderr>() {
private val layoutInflater = LayoutInflater.from(context)
@ -27,13 +28,15 @@ class ClipsKeyboardAdapter(val context: Context, var clips: ArrayList<Clip>, val
override fun onBindViewHolder(holder: ViewHolderr, position: Int) {
val item = clips[position]
holder.bindView(item) { itemView ->
setupView(itemView, item)
when (item) {
is Clip -> setupClip(itemView, item)
}
}
}
override fun getItemCount() = clips.size
private fun setupView(view: View, clip: Clip) {
private fun setupClip(view: View, clip: Clip) {
view.clip_value.apply {
text = clip.value
removeUnderlines()
@ -42,11 +45,14 @@ class ClipsKeyboardAdapter(val context: Context, var clips: ArrayList<Clip>, val
}
open inner class ViewHolderr(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(clip: Clip, callback: (itemView: View) -> Unit): View {
fun bindView(any: Any, callback: (itemView: View) -> Unit): View {
return itemView.apply {
callback(this)
setOnClickListener {
itemClick.invoke(clip)
if (any is Clip) {
setOnClickListener {
itemClick.invoke(any)
}
}
}
}

View File

@ -10,3 +10,7 @@ const val MAX_KEYS_PER_MINI_ROW = 5
// shared prefs
const val VIBRATE_ON_KEYPRESS = "vibrate_on_keypress"
const val SHOW_POPUP_ON_KEYPRESS = "show_popup_on_keypress"
// differentiate current and pinned clips at the keyboards' Clipboard section
const val ITEM_SECTION_LABEL = 0
const val ITEM_CLIP = 1

View File

@ -9,4 +9,4 @@ import androidx.room.PrimaryKey
data class Clip(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "value") var value: String
)
) : ListItem()

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.keyboard.models
data class ClipsSectionLabel(val value: String) : ListItem()

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.keyboard.models
open class ListItem

View File

@ -42,6 +42,8 @@ import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_MODE_
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SHIFT
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE
import com.simplemobiletools.keyboard.models.Clip
import com.simplemobiletools.keyboard.models.ClipsSectionLabel
import com.simplemobiletools.keyboard.models.ListItem
import kotlinx.android.synthetic.main.keyboard_popup_keyboard.view.*
import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
import java.util.*
@ -1326,14 +1328,30 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private fun setupStoredClips() {
ensureBackgroundThread {
val clips = context.clipsDB.getClips().toMutableList() as ArrayList<Clip>
val clips = ArrayList<ListItem>()
val clipboardManager = (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
val clipboardContent = clipboardManager.primaryClip?.getItemAt(0)?.text?.trim()?.toString()
if (clipboardContent?.isNotEmpty() == true) {
val section = ClipsSectionLabel(context.getString(R.string.clipboard_current))
clips.add(section)
val clip = Clip(-1, clipboardContent)
clips.add(clip)
}
if (clipboardContent?.isNotEmpty() == true) {
val section = ClipsSectionLabel(context.getString(R.string.clipboard_pinned))
clips.add(section)
}
clips.addAll(context.clipsDB.getClips())
Handler(Looper.getMainLooper()).post {
setupClipsAdapter(clips)
}
}
}
private fun setupClipsAdapter(clips: ArrayList<Clip>) {
private fun setupClipsAdapter(clips: ArrayList<ListItem>) {
mClipboardManagerHolder?.apply {
clipboard_content_placeholder_1.beVisibleIf(clips.isEmpty())
clipboard_content_placeholder_2.beVisibleIf(clips.isEmpty())