Our latest Academy, focused on Databases and the Cloud, shipped this week. If you want to build modern interconnected apps, then a firm grasp on how to use and connect to databases is a must. Being able to store data in the cloud means your app can automatically sync with the latest information wherever it is running, update itself and give your users up to the minute access to resources.
Happily LiveCode makes it easy to do all this and our academy shows you how.
This academy is split into 6 sections, each showing you how to create a basic app which demonstrates an aspect of using databases and the cloud. You'll learn to create a text editor, login system, inventory system, birthday reminders app, media player and a cloud based picture viewer. Every video is accompanied by written documentation and a sample stack for you to work with. For the sample stack we've used our Mobile App Template as this saves us, and you, reinventing the wheel. The Academy is about using databases, not creating a basic app.
So What Will You Learn?
This academy is packed with information. I'm going to pick out a few small samples here to give you an idea of what is covered.
Integrate Server
This example shows you how to delete a record, by accessing a server script also written in LiveCode. I found this interesting because it demonstrates how you can integrate LiveCode "regular" scripting with LiveCode Server, and really benefit your apps.
Still working on the Card script, next we deal with actually deleting records.
on buttonPushedDelete
put urlEncode(field "idField") into tID
put "http://techsupport.on-rev.com/cloudtest/clouddb3.lc?" & \
"ID=" & tID into tURL
put URL tURL into tTemp
readFromDatabase
end buttonPushedDelete
If you recall, from the first video, we put a handler in the code for the Delete button, which was simply to call buttonPushedDelete. This is the handler it is calling. In this handler, we get the contents of the ID field the user has selected to delete, and we put it into a variable, tID. We then send this to the LiveCode server script clouddb3, using a url call. This particular script contains instructions to delete the contents of tID, as we shall see in the next video.
Local Notifications
This section is from the Birthday Reminders app, and is showing you how to use local notifications to set a reminder.
on setReminder
put "INSERT into reminder_details VALUES" \
&& "('"& gName &"','"& gPhone &"','"& gBirthday &"');" \
into tSQL
revExecuteSQL sDatabaseID2, tSQL
When this handler is called, we construct an SQL statement using the global variables we declared at the top of our scripts. We're constructing an insert statement with the name, phone number and birthday date. If our data was John, 1234, 5/12/98 then the statement would look like:
INSERT into reminder_details VALUES 'John', '1234', '5/12/98'
We put the statement into the variable tSQL, then we execute it using revExecuteSQL, defining the database with its connection ID.
if the environment is "mobile" then
-- ensures date is of this year and not set to 19**
set the itemdel to space
put item 4 of the internet date into tInternet
If we detect we are running on mobile we are going to set a local notification. We first make sure that the date of the birthday is shown as this year, not the actual year the person was born. We set the item delimiter to space, and put item 4 of the internet date into our variable tInternet - this will be the current year.
set the itemDel to slash
put tInternet into item 3 of gBirthday
Now we set the item delimiter to be a slash, and put our saved year into the third item in the birthday date variable, gBirthday.
convert gBirthday to seconds
mobileCreateLocalNotification \
"You have a birthday notification",\
"Show Me",, gBirthday + 32400, true, "1"
We convert the birthday date to seconds, as the notifications needs a time set in seconds. Then we use the mobileCreateLocalNotification command to pop up a notification on the users device. You can see from the dictionary the syntax you can use with this command:
Syntax:
mobileCreateLocalNotification alertBody, alertButtonMessage,
alertPayload, alertTime, playSound [, badgeValue]
Parameters:
alertBody - The text that is to be displayed on the notification dialog that is raised when the application is not running.
alertButtonMessage - The button text on the notification dialog that is to appear on the button that launches the application, when the application is not running.
alertPayload - A text payload that can be sent with the notification request. This payload is presented to the user via the localNotificationReceived message.
alertTime - The time at which the alert is to be sent to the application. This parameter needs to be set in seconds and is the number of seconds since the UNIX Epoch, at which the notification should be triggered.
playSound - A boolean to indicate if a sound is to be played when the alert is received.
badgeValue - The number value to which the badge of the application logo is to be set. 0 hides the badge. >0 displays the value on the badge.
Our command is going to pop up a dialog that says "You have a Birthday Notification". It will have a button on it labeled "Show me". It will produce this reminder on the day of the birthday at 9am. 32400 seconds = 9 hours from midnight. "True" means the alert will play a sound, and the "1" means we'll be displaying a number for the notification. Note that there is a comma between "Show me" and gBirthday - this denotes that we are not setting anything for the alertPayload parameter.
end if
end setReminder
As always, we need to end our if's and our handler.
Get the Time
The final sample I'm going to show you is a way to get and display the current time, from the media player app.
The first thing we need to do is lock the screen, because we will be doing quite a bit of work to display the time and we don't want the user to see it happening.
on getTime
lock screen
We lock the screen
if sTime is 1 then
The time variable will contain 1 if the app has just been opened, as this is in our open card handler.
if the environment is mobile then
put mobileControlGet(mobilePlayer, currentTime) \
into tCurrentTime
Handling the case when the user is on mobile, we use mobileControlGet to get the current time shown on the mobile player and put it into the variable tCurrentTime.
put mobileControlGet(mobilePlayer, playbackState) into tState
We then get the state of playback of the player, ie whether it is playing, stopped or paused, and put that into tState.
put 1000 into tScale
The current time returned by mobileControlGet is in milliseconds. We'll be using tScale to turn it into seconds.
else
put the currentTime of player mediaPlayer \
into tCurrentTime
put 600 into tScale
end if
if we are not on mobile we get the current time from the desktop player and put it into tCurrentTime, and use 600 as the scale factor. This is because the number of intervals per second of a player on desktop is defined by the timeScale property, which by default is 600.
set the numberformat to 00.##
We are going to display minutes and seconds, so we need a suitable format for that. Using 00 before the decimal point tells LiveCode that if there are no numbers here, zeros should be added. The number of hash symbols after the point indicates the number of digits that should be displayed, if more are present after the calculation is done, they will be trimmed.
put round (tCurrentTime / tScale) into tSeconds
We divide the current time shown on the player by the scale factor to get it in seconds, and round the result as we don't want fractions of a second.
put round (tSeconds div 60) into tMinutes
We divide the seconds by 60 to get the minutes.
put tSeconds mod 60 into tSeconds
Mod gives you the remainder left over when one number is divided by another. This calculation gives us the number of left over seconds after we have calculated the whole minutes, which is the number of seconds we will finally want to display. We put this remainder into our tSeconds variable now that we've done all the rest of the calculations on it.
put tMinutes : tSeconds into field time
We display the minutes and seconds that the player has been playing in the field called time.
send getTime to me in 500 milliseconds
Now that we've done all that, we wait 500 milliseconds and do it all over again. This ensures the timer field is constantly updating.
end if
The if we are ending here refers right back to the start of the handler, where we checked if sTime was 1.
if tState is stopped then
put 00:00 into field time
exit getTime
end if
Before we triumphantly show the user the end result of all this, we need to check one more thing. Is the player actually playing anything? If not, we put zero into the time field and exit the handler.
unlock screen
end getTime
Now it is safe to unlock the screen, and show the timer, set to the correct play time.
I hope this is enough to give you some flavour of the new academy, if you'd like to see more of it, it is available here. |