Building our app
Whaddup InternetsI want to start this entry with a disclaimer; this blog will not be a lesson in how to write a Javascript Alarm clock nor will it be how to write beautiful code... I wont be posting much code as I consider myself a hacker at best (in the traditional not illegal sense) and so I wont be advocating that my particular coding approach is in any way best practice, in fact it may even be laughably bad in parts.
If you want JS and HTML lessons go here: http://w3schools.com/ ...I love this website wholeheartedly.
If you want the Windows App Store MSDN references go here: http://msdn.microsoft.com/en-us/library/windows/apps/br229519.aspx - I have been hitting this page about 200 times per day at the moment and its great for figuring out those new Windows 8 APIs (except bloody WinJS.UI controls as discussed below)
Now with that out of the way let's not let a lack of coding skill curb our enthusiasm for writing a spectacular app! I have been working on this app in my spare time for about 5 days now and here are my early impressions
- I am a lot further advanced than I expected to be a few days into this project
- I am shocked at how easy MSFT are making it for even a hacker like me to knock up a somewhat usable application using Javascript and HTML5... It's really bloody easy.
- I am having fun.
- I am surprised at how much the developer community outside of MSFT have NOT embraced this yet.
- Blend for Visual Studio 2012 is an amazing HTML5/JS/CSS tool and I'll probably use it a lot from now on.
In my last post I created a list of functions as product for my version 1.0 (or more correctly it should be 0.1) here's the list again as a reminder and a status update as to where I am up to at the time of this post:
- Main Function() - Ongoing
- Display clock() - Done
- Set alarm() - Done
- Run in background() - Next
- Save alarm data() - Done
- Read alarm data() - Done
- Set 24hr time() - Not Done
- setAMPM() - Not Done
- Update Clock() - Done
- Alarm Trigger() - Done
- Alarm Snooze - Done
- Wake Device() - Not Done
- Set Alarm Volume() - Removed from v0.1 (for simplicity we might as well use the native windows controls)
And here's a screen shot in "clock" mode, I have put all the set alarm controls under the clock for ease of testing but its likely I'll move this into its own settings page or something by the end of this phase:
The clock itself was basic to setup, right now its basically just a Javascript Date() object that updates every second and then tests to see if its time for an alarm, my primary area of concern here is that I have put the setInterval() function in the html <body> element:
<body onload="setInterval('updateClock()'), 1000)">
I am wondering if I can push this setInterval off to another processor with a WebWorker or some other async process and I feel like its a clumsy place to put this but it works so I'll play with it later.
My other internal question regarding the clock is whether I update it once a second or once a minute. I think I will move this to once a minute given that I don't need to show seconds... but this means I'll need to adjust my code for testing the alarm to disregard the seconds.
With the clock up and running OK the next part was to set an alarm. The "Add Alarm" button creates an new alarm object with all the info I need and saves it to roaming data, we then immediately read that data back into the alarm div just below the clock and also read it whenever we test for an alarm trigger.
the functions I created in the end are as follows:
setAlarm() - create a new alarm object, read required data from the controls and call saveAlarm(alarmdata)
saveAlarm(alarmInfo) - uses the stupidly easy "Windows.Storage.ApplicationData.current" to save the alarm data to the cloud (or locally if there is no connection)
readAlarmData() - read the data back out of the cloud storage
displayAlarm() - calls readAlarmData() to get the alarm data and then displays it in a <div> under the clock.
My plan is to support multiple alarms so this will eventually be an alarm object array but that's for the next version.
The only really non-standard JS control on the page is the TimePicker - this is the first control on the left under the clock.. its part of the WinJS.UI library which MSFT are giving us to make pretty Win8 apps:
This basically works great out of the box but I want to caution you about a gotcha I encountered referencing these controls in JavaScript as this took me AGES to work out... in fact I spent more time on this than anything else so far.
For my purposes I obviously need the current value of the TimePicker control so I can throw it into my alarm object. I looked it up and guess what this super awesome control out puts the current value as a JS Date() object with TimePicker.current.getTime().. TOO easy! but when I tried to reference the control in JS I kept getting the <div> element back and not the timepicker control.... here's what I had at the time.
HTML:
<div id="alarmPicker" data-win-control="WinJS.UI.TimePicker"></div>
Javascript:
var alarmControl = document.getElementById('alarmPicker');
"alarmControl" would of course come back as the <div> but I could not find ANYWHERE a guide as to how to access the "data-win-control" so I could call my current.getTime();
After several hours trying all manner of crazy shit I found a code example on some random blog (not on any MSFT site - sorry I didn't save the link) that had the answer.
var alarmControl = document.getElementById('alarmPicker').wincontrol;
I think this is also a code predictor bug in visual studio because "wincontrol" does not come up as an option at all... but it might be one of those things that an experienced Javascript dude would lol at me for.
anyway after getting that bit I simply wrote this bit of code:
Javascript:
var currentTime = alarmControl.current.getTime();
and yay I get back the current value of the TimePicker control... so moral of the story is:
Use .wincontrol to access WinJS.UI controls in the DOM.
that is literally the hardest bit of code I have had to write... the rest is pretty much native HTML DOM and native JS calls styled by CSS
Next Time: How its stupidly easy to play media and youTube videos....
Here's a screenshot when the YouTube alarm goes off...
Now I can wake up everyday to Gangnam style... curse or blessing?
Peace.