Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add Clipboard Paste Functionality #11

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

BAbbottKlover
Copy link

Why?

Functionality for pasting codes was not available, so I added it as well as an attr to enable/disable, enabled by default. Also added the ability to long-click and paste from the clipboard via menu item.

Known Issues

Deprecation warning for KeyEvent.ACTION_MULTIPLE and its event.characters, there doesn't seem to be a great explanation let alone an alternative. See here.

Videos

Samsung A12 (API 31) Pixel 7 Pro (API 33) Emulator-Popup-Paste (API 30)
device-2023-05-18-151130.mp4
device-2023-05-18-151336.mp4
device-2023-05-18-163626.webm

@@ -90,6 +93,15 @@ class SmsConfirmationView @JvmOverloads constructor(
else SmsConfirmationViewStyleUtils.getFromAttributes(attrs, context)
updateState()

// Conditionally allow pasting with long-press to the component
if (allowCodePaste) {
this.rootView.setOnLongClickListener {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can simplify this a little bit to something lile

rootView.setOnLongClickListener {
    initPasteMenu().show()
    true
}

?

// Used to inflate and return the popup paste menu
private fun initPasteMenu() : PopupMenu {
val clipboard = getSystemService(context, ClipboardManager::class.java)
val pasteMenu = PopupMenu(this.context, this).apply {
Copy link
Owner

@fraggjkee fraggjkee May 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny: we can probably omit this like you did in the previous line

val item = clipboard?.primaryClip?.getItemAt(0)

// Get the clipboard item text.
val pasteData = item?.text?.toString() ?: ""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can let appendPaste to work with CharSequence and avoid toString conversion? I think appendPaste can handle this type without any additional changes


// If the clipboard doesn't contain data, disable the paste menu item.
// If it does contain data, decide whether you can handle the data.
pasteItem.isEnabled = when {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idea: how about not showing the popup menu at all if there is nothing we can do from there? I think the default OS popup menu just doesn't show "Paste" if the Clibpobar is empty, we can probably follow this here as well

@@ -202,7 +279,8 @@ class SmsConfirmationView @JvmOverloads constructor(

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN && requestFocus()) {
if (event.action == MotionEvent.ACTION_DOWN && !this.isFocused) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this change breaks the keyboard: open demo app -> dismiss keyboard -> try opening it again by clicking on the view-> woops...

val pasteData = item?.text?.toString() ?: ""

// Paste the text into the component
appendPaste(pasteData)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add some filtering for the pasted content: it is now possible to paste text and not just digits. I guess we can just extend appendSymbol a little bit to skip non-digit symbols

private fun handleKeyEvent(keyCode: Int, event: KeyEvent): Boolean = when {
event.action == KeyEvent.ACTION_MULTIPLE && event.keyCode == KeyEvent.KEYCODE_UNKNOWN && allowCodePaste -> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven't checked it by myself yet, but wondering if we can somehow use any of those functions instead:

@fraggjkee
Copy link
Owner

Hey @BAbbottKlover, thanks for your work, the view is indeed missing this functionality and it would be really nice to support it. I've left some comments for you, please take a look and let me know what you think.

* TODO: Look into `View::onReceiveContent`
@BAbbottKlover
Copy link
Author

Hi @fraggjkee! Thanks for the quick response, sorry I'm late to getting back to this, I just came back from a vacation. Working now to resolve these issues

@BAbbottKlover
Copy link
Author

Drafting PR until I can figure out how to properly handle auto-fill payloads

@BAbbottKlover BAbbottKlover marked this pull request as draft June 3, 2023 01:22
@fraggjkee
Copy link
Owner

Hey @BAbbottKlover I've added basic support for the copy-pasting functionality in 1.8.0 release. It is still missing handling for the hints panel that is rendered above the soft keyboard though, it's just a pop-up menu that is now shown in response to long click events. But I see that it is supported in your PR, hopefully, this part of your work can be reused for future releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants