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

Refactor: extract button Toast display logic to prevent spamming #254

Merged
merged 16 commits into from
Dec 8, 2024

Conversation

Zafouche
Copy link
Collaborator

@Zafouche Zafouche commented Dec 5, 2024

This is a very small PR following the what I mentioned in this comment of PR #240:

@Redd87 I'll add the toast in my draft #239. Please merge this ASAP so I can resolve the conflicts as soon as possible. Or I can pull this branch into mine, but this PR is finished anyway so you can go ahead and merge it.

I unfortunately forgot to include the Toast addition to that PR, so here it is. Note that I defined a very simple logic to prevent the toasts from showing on top of each other if the user spams the More... button, e.g. only one Toast shows up at a time. This is a logic we could use in all places where we show Toasts as such, and use a custom class for it, but for now this suffices for the purpose of this PR.

Description

This PR introduces a utility mechanism to prevent the spamming of Toast messages in the app. The utility ensures that only one Toast can be displayed at a time, canceling the currently active Toast before showing a new one.

Motivation and Context

There are UI elements such as buttons that display Toast messages when clicked. A user could spam these buttons to repeatedly show Toast messages, and waste their system's resources. This PR implements a solution to mitigate this issue and ensure to Toasts do not show up one behind another at the same time when these spams occur.

ToastUtils

  • Added ToastUtils.showToast logic to cancel any currently active Toast before showing a new one.

Various screens:

  • Replaced spammable toasts with calls to the utility object.

How has this been tested?

Wrote a unit test (ToastUtilsMockTest) to verify the functionality, e.g. that the currently active Toast is canceled before displaying a new one.

@Zafouche Zafouche marked this pull request as ready for review December 5, 2024 21:50
@Zafouche
Copy link
Collaborator Author

Zafouche commented Dec 5, 2024

Quality Gate Failed Quality Gate failed

Failed conditions 10.0% Line Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Alt Text

@Zafouche
Copy link
Collaborator Author

Zafouche commented Dec 7, 2024

This PR will be transformed to an all-purpose utility addition for Toasts.

@Zafouche Zafouche changed the title chore(nominatim-location-picker): add toast on "More" button of location suggestions Refactor: extract button Toast display logic to prevent spamming Dec 7, 2024
@Zafouche
Copy link
Collaborator Author

Zafouche commented Dec 7, 2024

I've spent quite a bit of time searching for a solution to test Toasts through android tests, and tried implementing different ways myself. No results. Apparently, it is literally not possible to test Toasts for recent Android versions. See this discussion, where lots of people came to the same conclusion.

So instead, I wrote a simple unit test, that checks the functionality of the utility object.

Regarding the low coverage, it's because lots of Toast.makeText calls have simply been refactored to use this utility function, so that explains it. Also, the simple unit test fully covers the ToastUtils call.

Copy link

sonarqubecloud bot commented Dec 7, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
55.26% Line Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copy link
Collaborator

@Redd87 Redd87 left a comment

Choose a reason for hiding this comment

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

Not much to say, good job and LGTM

@Zafouche Zafouche merged commit e574e71 into main Dec 8, 2024
2 of 3 checks passed
@Zafouche Zafouche deleted the chore/nominatim-picker-toast-show-more branch December 8, 2024 13:06
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.

Implement toast logic such that a Toast cannot be spammed through spamming a button for example
2 participants