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

fix: Move the cursor to the end of the text after pasting the card number #1024

Closed

Conversation

borisprimer
Copy link
Contributor

Description

CHKT-4055

@borisprimer borisprimer requested a review from NQuinn27 October 14, 2024 16:58
@borisprimer borisprimer self-assigned this Oct 14, 2024
@borisprimer borisprimer requested review from a team as code owners October 14, 2024 16:58
if !newText.withoutWhiteSpace.isNumeric && !string.isEmpty { return false }

primerTextField.internalText = newText
Copy link
Contributor

Choose a reason for hiding this comment

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

Setting text below will also overwrite internalText so this isn't necessary

// Move the cursor to the last character of the text after pasting using Dispatch
DispatchQueue.main.async {
if let endPosition = primerTextField.position(from: primerTextField.endOfDocument, offset: 0) {
primerTextField.selectedTextRange = primerTextField.textRange(from: endPosition, to: endPosition)
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this also move the cursor to the end if the user edits the middle of the card string? e.g. if they missed a few numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true. hmmm...

@borisprimer borisprimer force-pushed the fix/ACC-4055-Drop-in-card-form-UX-issues-(pasting) branch from 6b0a0d7 to 29b46dd Compare October 16, 2024 17:13
@borisprimer borisprimer requested a review from jnewc October 16, 2024 17:14
@borisprimer
Copy link
Contributor Author

Please review again! @jnewc @NQuinn27 I ended up with a hacky solution but it works!

if let endPosition = primerTextField.position(from: primerTextField.beginningOfDocument, offset: text.count) {
// Delaying it for 0.01 seconds because setting the selectedTextRange
// before `shouldChangeCharactersIn:range` returns does not apply the value.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: {
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is already on main queue (which it should be) can you just dispatch to main without the delay? That should make it happen as soon as this function exits

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true, thanks!

// before `shouldChangeCharactersIn:range` returns does not apply the value.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: {
// Create a UITextRange from the endPosition to endPosition (for placing the cursor at the end)
primerTextField.selectedTextRange = primerTextField.textRange(from: endPosition, to: endPosition)
Copy link
Contributor

Choose a reason for hiding this comment

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

If the user pastes into the middle of the string what happens?

It's an edge case but I think having it move the cursor to the end of the text doesn't feel right. Could we move the cursor to the end of the inserted range instead?

// Add a small delay to ensure the cursor movement code has executed
let expectation = XCTestExpectation(description: "Wait for cursor to move")

DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you switch to just a straight main dispatch above, you can also do that here

@borisprimer borisprimer force-pushed the fix/ACC-4055-Drop-in-card-form-UX-issues-(pasting) branch from 29b46dd to 25bcb57 Compare October 17, 2024 08:25
@borisprimer borisprimer force-pushed the fix/ACC-4055-Drop-in-card-form-UX-issues-(pasting) branch from 25bcb57 to 3a4b11e Compare October 17, 2024 09:17
Copy link

@borisprimer borisprimer deleted the fix/ACC-4055-Drop-in-card-form-UX-issues-(pasting) branch October 21, 2024 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants