Skip to content

This library aims to prevent double clicks in Android applications using views.

License

Notifications You must be signed in to change notification settings

rafambn/ClickManager

Repository files navigation

ClickManager

ClickManager is designed to prevent unintended double-click events

The problem

If you have an activity where a button starts another activity, for example, if you accidentally click the button multiple times, it will start the activity several times.

This happens because the clicks are queued to be executed, but if this causes logic problems, like in the example, you will need to handle it in your code. The problem can also occur if your action executes something asynchronously. This project was developed with the intention of preventing such problems in a simple way for the user. ## How to install

Gradle

  1. Add the JitPack repository to your build file in your root build.gradle at the end of repositories:
allprojects {
	repositories {
		maven { url 'https://jitpack.io' }
	}
}
  1. Add the dependency:
dependencies {
	implementation 'com.github.rafambn:ClickManager:3.1'
}

How it works

In the image above, the blue spots are from a different viewGroup than the red ones, which means that if both blue or red buttons are pressed, only one will be executed, while if a blue and a red button are clicked, both will be executed.

After some time of being clicked, the view group will be unblocked for clicking again. The time is determined by the minClickInterval on the annotation and it is different for each view. If the view is async, the view group will only be unblocked if the Runnable is executed.

How to use

  1. Declare the ClickManager variable:
lateinit var clickManager: ClickManager
  1. Annotate the views and interfaces that you want to manage:
@ManageClick(gruopId = 0, isAsync = false, minClickInterval = 1000L)
lateinit var button: Button
@ManageClick(gruopId = 0, isAsync = false, minClickInterval = 1000L)
lateinit var myInterface: MyInterface

*The default values of ManageClick are the ones described above.

  1. Instantiate the ClickManager:
myInterface = object : MyInterface {
	override fun onClick() {
		// some code here
	}

	override fun onLongClick() {
		// some other code here
	}
}
button.setOnClickListener { 
	// some other other code xD
}
clickManager = ClickManager(this, 0)
myAdapter.setListener(myInterface)

*If you use hierarchy on the activity you must instantiate the ClickManager providing the numbers of parent classes that the annotation is present

  1. For an async method or methods that have a callback, you must pass a Runnable to be executed when the task its done and also annotate the field as async:
@ManageClick(gruopId = 0, isAsync = true, minClickInterval = 1000L)
lateinit var button: Button
button.setOnClickListener { doesAsyncStuff(clickManager.getUnblocker(0)) }
suspend fun(unBlocker: Runnable) {
	// Does some async stuff
	unBlocker.run()
}

Limitations

  • The ClickManager must be instantiated after all listeners are set to its views and interfaces are instantiate but before any manipulation of interfaces.
  • It will only manage clicks of the “onClick” methods of the interfaces. Further improvements will allow it to manage other methods of interfaces.
  • If the listeners of the view change it will not work
  • It is designed to only work in views activities, it wasn’t tested or designed to work with Compose.

Feel free to improve upon these limitations or report issues

About

This library aims to prevent double clicks in Android applications using views.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages