From 6551889195a050effa7e57ee6e617dbcde7ac862 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 29 Jan 2022 18:50:32 +0100 Subject: [PATCH] allow pinning the current clip by clicking the icon --- app/build.gradle | 2 +- .../keyboard/adapters/ClipsKeyboardAdapter.kt | 41 ++++++++++++++++--- .../keyboard/extensions/Context.kt | 6 +++ .../interfaces/RefreshClipsListener.kt | 5 +++ .../keyboard/models/ClipsSectionLabel.kt | 2 +- .../keyboard/views/MyKeyboardView.kt | 22 ++++++---- .../main/res/layout/item_section_label.xml | 31 ++++++++++---- app/src/main/res/values/dimens.xml | 1 + 8 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/keyboard/interfaces/RefreshClipsListener.kt diff --git a/app/build.gradle b/app/build.gradle index a7b14cf..f057122 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -64,7 +64,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:18b6427fa9' + implementation 'com.github.SimpleMobileTools:Simple-Commons:8d9a4a709b' kapt 'androidx.room:room-compiler:2.3.0' implementation 'androidx.room:room-runtime:2.3.0' diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/adapters/ClipsKeyboardAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/adapters/ClipsKeyboardAdapter.kt index e999e55..61598a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/adapters/ClipsKeyboardAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/adapters/ClipsKeyboardAdapter.kt @@ -1,16 +1,22 @@ package com.simplemobiletools.keyboard.adapters +import android.annotation.SuppressLint import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.StaggeredGridLayoutManager +import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.removeUnderlines +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.keyboard.R +import com.simplemobiletools.keyboard.extensions.clipsDB import com.simplemobiletools.keyboard.extensions.config +import com.simplemobiletools.keyboard.extensions.getCurrentClip import com.simplemobiletools.keyboard.helpers.ITEM_CLIP import com.simplemobiletools.keyboard.helpers.ITEM_SECTION_LABEL +import com.simplemobiletools.keyboard.interfaces.RefreshClipsListener import com.simplemobiletools.keyboard.models.Clip import com.simplemobiletools.keyboard.models.ClipsSectionLabel import com.simplemobiletools.keyboard.models.ListItem @@ -18,8 +24,10 @@ import kotlinx.android.synthetic.main.item_clip_on_keyboard.view.* import kotlinx.android.synthetic.main.item_section_label.view.* import java.util.* -class ClipsKeyboardAdapter(val context: Context, var items: ArrayList, val itemClick: (clip: Clip) -> Unit) : - RecyclerView.Adapter() { +class ClipsKeyboardAdapter( + val context: Context, var items: ArrayList, val refreshClipsListener: RefreshClipsListener, + val itemClick: (clip: Clip) -> Unit +) : RecyclerView.Adapter() { private val layoutInflater = LayoutInflater.from(context) private val baseConfig = context.config @@ -62,10 +70,31 @@ class ClipsKeyboardAdapter(val context: Context, var items: ArrayList, } } - private fun setupSection(view: View, label: ClipsSectionLabel) { - view.clips_section_label.apply { - text = label.value - setTextColor(textColor) + @SuppressLint("UseCompatLoadingForDrawables") + private fun setupSection(view: View, sectionLabel: ClipsSectionLabel) { + view.apply { + clips_section_label.apply { + text = sectionLabel.value + setTextColor(textColor) + } + + clips_section_icon.apply { + applyColorFilter(textColor) + + if (sectionLabel.isCurrent) { + setImageDrawable(resources.getDrawable(R.drawable.ic_pin)) + setOnClickListener { + ensureBackgroundThread { + val currentClip = context.getCurrentClip() ?: return@ensureBackgroundThread + val clip = Clip(null, currentClip) + context.clipsDB.insertOrUpdate(clip) + refreshClipsListener.refreshClips() + } + } + } else { + setImageDrawable(resources.getDrawable(R.drawable.ic_pin_filled)) + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/extensions/Context.kt index 186b61d..d12749b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/extensions/Context.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.keyboard.extensions +import android.content.ClipboardManager import android.content.Context import com.simplemobiletools.keyboard.databases.ClipsDatabase import com.simplemobiletools.keyboard.helpers.Config @@ -8,3 +9,8 @@ import com.simplemobiletools.keyboard.interfaces.ClipsDao val Context.config: Config get() = Config.newInstance(applicationContext) val Context.clipsDB: ClipsDao get() = ClipsDatabase.getInstance(applicationContext).ClipsDao() + +fun Context.getCurrentClip(): String? { + val clipboardManager = (getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager) + return clipboardManager.primaryClip?.getItemAt(0)?.text?.trim()?.toString() +} diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/interfaces/RefreshClipsListener.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/interfaces/RefreshClipsListener.kt new file mode 100644 index 0000000..5d977e1 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/interfaces/RefreshClipsListener.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.keyboard.interfaces + +interface RefreshClipsListener { + fun refreshClips() +} diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/models/ClipsSectionLabel.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/models/ClipsSectionLabel.kt index 011acbe..2c7bb0d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/models/ClipsSectionLabel.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/models/ClipsSectionLabel.kt @@ -1,3 +1,3 @@ package com.simplemobiletools.keyboard.models -data class ClipsSectionLabel(val value: String) : ListItem() +data class ClipsSectionLabel(val value: String, val isCurrent: Boolean) : ListItem() diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt index 5127b88..57c1d42 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt @@ -35,12 +35,14 @@ import com.simplemobiletools.keyboard.activities.SettingsActivity import com.simplemobiletools.keyboard.adapters.ClipsKeyboardAdapter import com.simplemobiletools.keyboard.extensions.clipsDB import com.simplemobiletools.keyboard.extensions.config +import com.simplemobiletools.keyboard.extensions.getCurrentClip import com.simplemobiletools.keyboard.helpers.* import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_DELETE import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_ENTER import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_MODE_CHANGE import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SHIFT import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE +import com.simplemobiletools.keyboard.interfaces.RefreshClipsListener import com.simplemobiletools.keyboard.models.Clip import com.simplemobiletools.keyboard.models.ClipsSectionLabel import com.simplemobiletools.keyboard.models.ListItem @@ -366,6 +368,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut if (clipboardContent?.isNotEmpty() == true) { handleClipboard() } + setupStoredClips() } mClipboardManagerHolder!!.apply { @@ -600,8 +603,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private fun handleClipboard() { if (mToolbarHolder != null && mPopupParent.id != R.id.mini_keyboard_view) { - val clipboardManager = (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager) - val clipboardContent = clipboardManager.primaryClip?.getItemAt(0)?.text?.trim() + val clipboardContent = context.getCurrentClip() if (clipboardContent?.isNotEmpty() == true) { mToolbarHolder?.apply { clipboard_value.apply { @@ -1331,13 +1333,13 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private fun setupStoredClips() { ensureBackgroundThread { val clips = ArrayList() - val clipboardManager = (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager) - val clipboardContent = clipboardManager.primaryClip?.getItemAt(0)?.text?.trim()?.toString() + val clipboardContent = context.getCurrentClip() val pinnedClips = context.clipsDB.getClips() val isCurrentClipPinnedToo = pinnedClips.any { clipboardContent?.isNotEmpty() == true && it.value.trim() == clipboardContent } + if (!isCurrentClipPinnedToo && clipboardContent?.isNotEmpty() == true) { - val section = ClipsSectionLabel(context.getString(R.string.clipboard_current)) + val section = ClipsSectionLabel(context.getString(R.string.clipboard_current), true) clips.add(section) val clip = Clip(-1, clipboardContent) @@ -1345,7 +1347,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } if (!isCurrentClipPinnedToo && clipboardContent?.isNotEmpty() == true) { - val section = ClipsSectionLabel(context.getString(R.string.clipboard_pinned)) + val section = ClipsSectionLabel(context.getString(R.string.clipboard_pinned), false) clips.add(section) } @@ -1363,7 +1365,13 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut clips_list.beVisibleIf(clips.isNotEmpty()) } - val adapter = ClipsKeyboardAdapter(context, clips) { clip -> + val refreshClipsListener = object : RefreshClipsListener { + override fun refreshClips() { + setupStoredClips() + } + } + + val adapter = ClipsKeyboardAdapter(context, clips, refreshClipsListener) { clip -> mOnKeyboardActionListener!!.onText(clip.value) vibrateIfNeeded() } diff --git a/app/src/main/res/layout/item_section_label.xml b/app/src/main/res/layout/item_section_label.xml index a750f63..c689176 100644 --- a/app/src/main/res/layout/item_section_label.xml +++ b/app/src/main/res/layout/item_section_label.xml @@ -1,10 +1,27 @@ - + android:layout_height="wrap_content"> + + + + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 1039e57..10ea499 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -7,6 +7,7 @@ 40dp 60dp -10dp + 28dp 22sp 26sp