Some apps allows the users to get some extra credits by pushing a button or doing something every x amount of time. Example: Candy Crush let’s you roll a fortune wheel every 24 hours. In that period, the component that gives prizes should be disabled.
If you need something like that in your app, you might find this tutorial very useful.
In this case we will use a button. Each time the button is pressed, it will not be able to be clicked again for an hour. Of course, you can adapt this to your app and change the button for any other component, event or procedure that you need to disable.
Components you are going to need:
The clock properties should be:
(1000 milliseconds = 1 second, so this clock when enabled will call the Timer event every 1 second).
When the button is clicked, you are going to save in TinyDB the instant in milliseconds.
Note: The Clock.Get Millis block returns the current time in milliseconds since Unix Epoch (January 1, 1970, 0:00 UTC). Learn more here: http://currentmillis.com/
It’s basically a big 13 digits number that is incremented by 1 every millisecond (or you can say it’s incremented by 1000 every second).
Also, when the button is clicked, you have to disable the button and start a clock that will check if the current millis are greater than the millis saved in TinyDB + 360000 (that’s one hour in milliseconds).
So this is the click event blocks:
Next we need to make the timer event (clock) that will check every second if the time elapsed since the click is greater or equal to one hour.
So we compare the current millis to the millis saved in TinyDB when the button was clicked plus 3600000.
If the current millis are greater or equal to the millis saved + 3600000, it means an hour or more has passed since the click, so we do the following:
- Enable the button
- Stop the clock
- Delete the time saved in TinyDB, because we don’t need it anymore.
This is how the Timer event will look:
Finally this is the part that makes the trick interesting. We need to make sure that if the user quits the app, and opens it again, the button remains disabled until one hour or more has passed.
So we initialize the app asking if the tag LastClickMillis is not empty.
If it’s not empty, that means the button was pressed less than an hour ago, so we disable the button, and start the clock.
If it’s empty, it means that the button was not clicked in the previous session and it should be enabled on start.
This is how the Initialize event should look:
If you want to try it and not have to wait an hour, change the 3600000 for 60000, that will make the button wait just one minute to show up again. You can exit the app and open it again within that minute to check that the button remains disabled.
After a minute has passed the button will be enabled again.
As a challenge, we can research how to prevent the user from resetting the clock by deleting the app’s data in the device’s settings. Or how to deal with a cheater user that advances the device’s clock to enable the button before the time has passed.