allow pinning the current clip by clicking the icon

This commit is contained in:
tibbi 2022-01-29 18:50:32 +01:00
parent 34388b634f
commit 6551889195
8 changed files with 88 additions and 22 deletions

View File

@ -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'

View File

@ -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<ListItem>, val itemClick: (clip: Clip) -> Unit) :
RecyclerView.Adapter<ClipsKeyboardAdapter.ViewHolder>() {
class ClipsKeyboardAdapter(
val context: Context, var items: ArrayList<ListItem>, val refreshClipsListener: RefreshClipsListener,
val itemClick: (clip: Clip) -> Unit
) : RecyclerView.Adapter<ClipsKeyboardAdapter.ViewHolder>() {
private val layoutInflater = LayoutInflater.from(context)
private val baseConfig = context.config
@ -62,10 +70,31 @@ class ClipsKeyboardAdapter(val context: Context, var items: ArrayList<ListItem>,
}
}
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))
}
}
}
}

View File

@ -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()
}

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.keyboard.interfaces
interface RefreshClipsListener {
fun refreshClips()
}

View File

@ -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()

View File

@ -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<ListItem>()
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()
}

View File

@ -1,10 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/clips_section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.8"
android:padding="@dimen/medium_margin"
android:textSize="@dimen/normal_text_size"
tools:text="@string/clipboard_current" />
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/clips_section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.8"
android:padding="@dimen/medium_margin"
android:textSize="@dimen/normal_text_size"
tools:text="@string/clipboard_current" />
<ImageView
android:id="@+id/clips_section_icon"
android:layout_width="@dimen/clip_pin_size"
android:layout_height="@dimen/clip_pin_size"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/clips_section_label"
android:alpha="0.8"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:padding="@dimen/small_margin"
android:src="@drawable/ic_pin" />
</RelativeLayout>

View File

@ -7,6 +7,7 @@
<dimen name="clip_drag_icon_height">40dp</dimen>
<dimen name="key_height">60dp</dimen>
<dimen name="vertical_correction">-10dp</dimen>
<dimen name="clip_pin_size">28dp</dimen>
<dimen name="keyboard_text_size">22sp</dimen>
<dimen name="preview_text_size">26sp</dimen>