ClickManager is designed to prevent unintended double-click events
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- 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' }
}
}
- Add the dependency:
dependencies {
implementation 'com.github.rafambn:ClickManager:3.1'
}
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.
- Declare the ClickManager variable:
lateinit var clickManager: ClickManager
- 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.
- 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
- 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()
}
- 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