Tutorial - How to disable a component for an hour/day


#1

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:
One button.
One clock.
One TinyDB.

The clock properties should be:
cefec0f4a4f9b162bee03e1ee2643e04ba800b3b
(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:
0720ad5aa7058ac250bb089d9e5219ddc049ecea

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:

  1. Enable the button
  2. Stop the clock
  3. Delete the time saved in TinyDB, because we don’t need it anymore.

This is how the Timer event will look:
d975431942b08c44a50db93c1f3b6fb0b3f81a82

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:
252035cab80f99a3f1cb52becb707d7c12ec5ea8

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.


How to disable button1 on screen1 when
How to make button enable and disable with countdown timer
How to get international times from Internet?
Help In Disable Component
Need help problem
#2

Nice tutorial @Italo :+1:


#3

Very simple and useful, @Italo


Task Hour/Day + Countdown Timer + Prevent Hacking
#4

time is milisecond or second?


#5

For more simplicity and to avoid extra blocks that are not needed, I opted to save it as milliseconds, but if you want to use seconds, divide the milliseconds by 1000.


#6

can i show time count in the screen for help user to see time,when time count start from 1 hour to 00 second.
is it possible using tiny db?


#7

You have to use a clock to do that. TinyDB only stores the time. The clock is the one component that will be called every second to update the label with the time left.


#8

but how to do?can you please give the blocks?
it will helpfull


#9

If anyone increase system time.then he/she need not to wait until 1 hours.users can hack this easily.how to do it without system time.
can you try please using time interval where user can not be able to hack.


#10

#11

is it possible do this within tinydb?


#12

The above example makes use of a TinyDB.


#13

but if anyone increase system time,they can not wait given times.


#14

@maksud, l recommend you to actually practice the given example if you really want to create the related app, asking around without practicing wouldn’t let you go anywhere and our advices here would mean nothing.

So, start practicing and in no time you will master it since AB is easy.


#15

Thank you for your recommendation.
i also try to practice all time.


#16

Can you explain how to do timer using firebase


#17

I don’t know anything about Firebase, sorry. Never used it yet. May be somebody else can help with that.


#18

Timer using firebase? I haven’t heard any method about it using AI platform. I know that there is method to ask for server timestamp (https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) but currently there is no way to do it using AI. Of course, you can achieve this using php server.


#19

i used tiny db method bit they are showing error(milies cant accept the argument date,year)


#20

Italo, we are kindred spirits. I hate Firebase too lol :fire: