The Sort by Location button's script is quite something. You should look forward to that! First, we'll start with the Sort by Time button:
on mouseUp global gReminderData set the itemdelimiter to tab sort gReminderData numeric by item 4 of each showdata writedata end mouseUp
on mouseUp global gReminderData mobileStartTrackingSensor "location", true put mobileSensorReading("location", false) into tLocation mobileStopTrackingSensor "location" set the itemdelimiter to comma put item 1 of tLocation into tLat put item 2 of tLocation into tLong set the itemdelimiter to tab sort gReminderData numeric by getdistance(tLat,tLong,item 5 of each) showdata writedata end mouseUp function getdistance pLat,pLong,pLocName if pLocName is empty then return 1000000 global gReminderData put empty into tLat put empty into tLong repeat with a = 1 to the number of lines in gReminderData if item 1 of tEntryDetails = "Location" then if item 2 of tEntryDetails = pLocName then put item 3 of tEntryDetails into tLat put item 4 of tEntryDetails into tLong end if end if end repeat if tLat is empty then return 1000000000 return distance(tLat,tLong,pLat,pLong) end getdistance function distance lat1,lon1,lat2,lon2 put 6371 into r put toRad((lat2-lat1)) into dLat put toRad((lon2-lon1)) into dLon put toRad(lat1) into lat1 put toRad(lat2) into lat2 put sin(dLat/2) * sin(dLat/2) + sin(dLon/2)*sin(dLon/2) * cos(lat1)*cos(lat2) into a put 2*atan2(sqrt(a),sqrt(1-a)) into c put r*c into d return d end distance function toRad pAngle return pAngle/180*PI end toRad
The first part of the mouseUp
handler just gets your current location. The distance
and toRad
functions are the same ones we looked at earlier. The magic happens in the way that the sort line uses a function to determine the sort order. By passing the Location that you associated with each reminder into the getdistance
function, it's possible to run through the list of locations to find a match and to then use that location's latitude and longitude to measure the distance from your current location. This distance is then used by the sort command to decide the order of the lines.
on mouseUp go to card "reminder" end mouseUp
on mouseUp go to card "location" end mouseUp
on mouseUp global gReminderData mobilePick gReminderData,1,"checkmark","cancelDone","picker" put the result into tItemsToDelete if tItemsToDelete = "0" then exit mouseUp set the itemdelimiter to comma repeat with a = the number of items in tItemsToDelete down to 1 delete line (item a of tItemsToDelete) of gReminderData end repeat if gReminderData is empty then put "no entries yet" into gReminderData showdata writedata end mouseUp
The delete handler uses mobilePick
with a particular set of parameters. One interesting parameter is checkmark
. Asking for that type of picker will then show a list with checkboxes in it when you're on iPad or Android. That would enable you to choose several entries to delete in one go. Hence, the repeat loop that goes through as many items as you choose.
All being well, you have recovered by now after trying to understand the sort-by-location function! You can at least see how tough the Stack script would have been if all of this code would have been placed in that one location. Let's go on to the next card…