mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-05-27 04:07:10 +00:00
properly re-sort the list when copying an existing clipboard entry
This commit is contained in:
parent
f915e46468
commit
a9411b3631
2 changed files with 16 additions and 8 deletions
|
@ -5,7 +5,6 @@ package helium314.keyboard.latin
|
||||||
import android.content.ClipboardManager
|
import android.content.ClipboardManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import androidx.preference.PreferenceManager
|
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import helium314.keyboard.compat.ClipboardManagerCompat
|
import helium314.keyboard.compat.ClipboardManagerCompat
|
||||||
|
@ -51,15 +50,23 @@ class ClipboardHistoryManager(
|
||||||
val clipData = clipboardManager.primaryClip ?: return
|
val clipData = clipboardManager.primaryClip ?: return
|
||||||
if (clipData.itemCount == 0) return
|
if (clipData.itemCount == 0) return
|
||||||
clipData.getItemAt(0)?.let { clipItem ->
|
clipData.getItemAt(0)?.let { clipItem ->
|
||||||
// Starting from API 30, onPrimaryClipChanged() can be called multiple times
|
val timeStamp = ClipboardManagerCompat.getClipTimestamp(clipData) ?: System.currentTimeMillis()
|
||||||
// for the same clip. We can identify clips with their timestamps since API 26.
|
|
||||||
// We use that to prevent unwanted duplicates.
|
|
||||||
val timeStamp = ClipboardManagerCompat.getClipTimestamp(clipData)?.also { stamp ->
|
|
||||||
if (historyEntries.any { it.timeStamp == stamp }) return
|
|
||||||
} ?: System.currentTimeMillis()
|
|
||||||
|
|
||||||
val content = clipItem.coerceToText(latinIME)
|
val content = clipItem.coerceToText(latinIME)
|
||||||
if (TextUtils.isEmpty(content)) return
|
if (TextUtils.isEmpty(content)) return
|
||||||
|
|
||||||
|
val duplicateEntryIndex = historyEntries.indexOfFirst { it.content.toString() == content.toString() }
|
||||||
|
if (duplicateEntryIndex != -1) {
|
||||||
|
val existingEntry = historyEntries[duplicateEntryIndex]
|
||||||
|
if (existingEntry.timeStamp == timeStamp) return // nothing to change (may occur frequently starting with API 30)
|
||||||
|
// older entry with the same text already exists, update the timestamp and re-sort the list
|
||||||
|
existingEntry.timeStamp = timeStamp
|
||||||
|
historyEntries.removeAt(duplicateEntryIndex)
|
||||||
|
historyEntries.add(0, existingEntry)
|
||||||
|
sortHistoryEntries()
|
||||||
|
val newIndex = historyEntries.indexOf(existingEntry)
|
||||||
|
onHistoryChangeListener?.onClipboardHistoryEntryMoved(duplicateEntryIndex, newIndex)
|
||||||
|
return
|
||||||
|
}
|
||||||
if (historyEntries.any { it.content.toString() == content.toString() }) return
|
if (historyEntries.any { it.content.toString() == content.toString() }) return
|
||||||
|
|
||||||
val entry = ClipboardHistoryEntry(timeStamp, content)
|
val entry = ClipboardHistoryEntry(timeStamp, content)
|
||||||
|
|
|
@ -719,6 +719,7 @@ public final class InputLogic {
|
||||||
case KeyCode.CLIPBOARD_CUT:
|
case KeyCode.CLIPBOARD_CUT:
|
||||||
if (mConnection.hasSelection()) {
|
if (mConnection.hasSelection()) {
|
||||||
mConnection.copyText();
|
mConnection.copyText();
|
||||||
|
// fake delete keypress to remove the text
|
||||||
final Event backspaceEvent = LatinIME.createSoftwareKeypressEvent(KeyCode.DELETE,
|
final Event backspaceEvent = LatinIME.createSoftwareKeypressEvent(KeyCode.DELETE,
|
||||||
event.getMX(), event.getMY(), event.isKeyRepeat());
|
event.getMX(), event.getMY(), event.isKeyRepeat());
|
||||||
handleBackspaceEvent(backspaceEvent, inputTransaction, currentKeyboardScript);
|
handleBackspaceEvent(backspaceEvent, inputTransaction, currentKeyboardScript);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue