Time for action – making the home card buttons work

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:

  1. Edit the script of the Sort by Time button on the first card.
  2. Type in the following short handler:
    on mouseUp
      global gReminderData
      set the itemdelimiter to tab
      sort gReminderData numeric by item 4 of each
      showdata
      writedata
    end mouseUp

    Note

    LiveCode's sort command is powerful and in the preceding case, it sorts the list of reminders based on the notification seconds value. Once the lines are sorted, the list is recreated for the user to see and the text file is rewritten.

  3. Get mentally prepared and then edit the script of the Sort by Location button.
  4. Type in all of the following lines of code:
    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

    Note

    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.

  5. For a moment's relaxation, edit the Create Reminder… button script and set it to this:
    on mouseUp
      go to card "reminder"
    end mouseUp
  6. Likewise, set the Create Location… button script to the following:
    on mouseUp
      go to card "location"
    end mouseUp
  7. For the last script for this card, edit the Delete Location or Reminder button script and type the following in:
    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

Note

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.

What just happened?

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…

Creating a location card

Next up, we will create the card that we will show when the user touches the Create a Location… button on the first card.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.140