Hi,
I am working on a datafield that can aid in race planning. For this purpose the user can input the length and target pace of each "lap" and the datafield keeps track how far ahead or behind the person is during the activity. My issue is that I need an array of distances and an array of times to be input. Distances could be either in km or miles, and times should be in a m:ss format (maybe even h:mm:ss, but not supporting this right now).
The best (only?) solution I have come up with is the following: In my Settings I have two "String" Properties that need to be filled in with something "1.2, 3.4, 0.8" for the distances and "10:30, 23:32, 8:03" for the times. I then parse these strings in my "initialize" method, and convert them to arrays stat store the distances in meters and the times in milliseconds (taking care to convert everything to integers so as to save memory) and then I use these in my watchface code to do what it needs to do.
I am looking for better solutions to this.
My holy grail would be to be able to by some magic have the "Settings Processing" done in the Garmin Connect App on the Phone or PC rather than on the device itself. Is there any way to something like that?
Alternatively could I write a 'companion' IQ App that somehow reads the Settings and creates the data structures for the Datafield? This would require an additonal step by the user, but would allow the datafield itself to be much leaner.
And less ambitiously, is there an efficient way to do the above? Forcing the user to convert miles to meters, and m:sec to millisecs seems a pain, but might be an option, but I would still need to convert a string to an array. Just for reference, here is what I am currently doing:
var string = (Application.getApp().getProperty("lapTimes").toString()); var index = string.find(","); while (index != null) { var item = string.substring(0,index); var length = string.length(); string = string.substring(index+1,length); lapTimesArray.add(stringToMs(item)); index = string.find(","); } lapTimesArray.add(stringToMs(string));
The "stringToMs" method is defined as follows:
function stringToMs (string) { var index = string.find(":"); var ms; if (index != null) { var minString = string.substring(0,index); var mins = minString.toNumber(); minString = null; var secString = string.substring(index+1,string.length()); var sec = secString.toNumber(); secString = null; ms = (mins*60+sec)*1000; } else { var mins = string.toNumber(); ms = mins*60*1000; } return ms; }
Clearly, I must also have a msToString task that converts ms times to strings, for display purposes:
function msToString (timeInms) { var negative = timeInms < 0; timeInms = negative ? -(timeInms) : timeInms; var totalsecs = timeInms/1000; var hours = Math.floor(totalsecs/3600); var mins = Math.floor((totalsecs-hours*3600)/60); var secs = Math.floor(totalsecs-hours*3600-mins*60); var outstring = ""; var signString = negative ? "-" : ""; if (hours == 0) { outstring = signString+mins.format("%0d")+":"+secs.format("%02d"); } else { outstring = signString+hours.format("%d")+":"+mins.format("%02d")+":"+secs.format("%02d"); } return outstring; }
It seems that a lot of what a datafield or watchface will be doing is convert times to strings, so I was wondering if there is something more efficient (built in??) than what I am doing.
Thanks for any suggestions!