Hello Everyone
Today I will show you how you can create a secure countdown of any duration. For this tutorial, you need to have a Firebase account with a basic knowledge of how to setup Real time Database.
Note : This method does not belong to me. It belongs to Pixi Bomb. She has made a tutorial on YouTube regarding this topic. She used Fusion Table which is now deprecated so i replaced it with Google Firebase.
Components
Initialize Variables
- timestamp_new : it will store newly fetched timestamp (UnixTimeStamp)
- timestamp_old : it will store old timestamp which will be fetched from Firebase
- new : It will store a copy of timestamp_new variable
- request : it will store request type (data or update). determines whether we will fetch data from firebase or update existing data.
- wait : store time duration of countdown in seconds
- T : acts as a switch
- ID : stores user specific id
Procedures
This checks whether the user is online or not. If the user is offline it shows an error message.
This loads data from Firebase
This gets timestamp from current date time
Second function extracts timestamp from obtained resultNote : Here I am using web viewer to get timestamp since I didn’t find any api for that. The url used for web viewer is obtained from this site : https://unixtime.co.za/
The data is loaded in the web viewer and I use JavaScript to get the data from web-viewer
This function changes the state of button. and turns the timer on or off
This clears all the stored data in variable. Used when user is disconnected from internet.
Event Blocks
When screen is initialized, we will check if the user is online or not and then load data from web viewer to get the current timestamp.
These blocks extract the timestamp from web viewer using JavaScript
These two blocks get the data from Firebase and store it in variables and toggle state of button and timer.
Starts the countdown
Updates old data in Firebase with the new one
Updates time every second
When user disconnects, we will stop the timer so the app does not crash. When user reconnects to the internet, the circle is again repeated.
Download AIA File : CountDown.aia (10.1 KB)
Download App : CountDown.apk (6.0 MB)
Note : You need to change Firebase Database URL and API key
If you want to change the time duration, just change the value of wait variable. Value should be in seconds
I hope you liked the tutorial. I know that I didn’t explain well but I tried my best. Let me know about your thoughts in the comments.