Time for action – adding stack-level functions

For this app, we're going to put some of the logic in the buttons on the cards themselves, but it still leaves a good amount that will have to go into the Stack script. To make it less overwhelming, we'll show one or two functions at a time followed by some explanation about any interesting points in the following steps:

  1. Open the Stack script.
  2. Type in the the following handlers:
    on openstack
      if the platform is "iphone" then iPhoneSetKeyboardReturnKey "done"
      readdata
      showdata
    end openstack
    
    on returnInField
      focus on nothing
    end returnInField

    Note

    Android OS keyboards generally have a button dedicated to the function putting the keyboard away. On iOS, this isn't the case, as the button that sits where the Return key should be, may have a special word instead, such as Send, or Done. Unfortunately, we are entering text into fields that are able to take a return character. To solve the issue, we set the Return button to say Done, which will lead the user to expect the keyboard to go away when that button is pressed. We will also trap the returnInField message and use it as a way to actually put the keyboard away.

  3. Next, type in the functions that will read, and write the list of reminders as a text file to the documents folder on your device:
    on writedata
      global gReminderData
      put specialFolderPath("documents") & "/reminders.txt" into tRemindersPath
      if gReminderData is empty then put "no entries yet" into gReminderData
      open file tRemindersPath
      write gReminderData to file tRemindersPath
      close file tRemindersPath
      clearnotifiers
      setupnotifiers
    end writedata
    
    on readdata
      global gReminderData
      put specialFolderPath("documents") & "/reminders.txt" into tRemindersPath
      if there is a file tRemindersPath then
         open file tRemindersPath
         read from file tRemindersPath until eof
         close file tRemindersPath
         put it into gReminderData
      else
         open file tRemindersPath
         write "no entries yet" to file tRemindersPath
         close file tRemindersPath
         put "no entries yet" into gReminderData
      end if
    end readdata

    Note

    These two functions are using the straightforward ability that LiveCode has to read and write text files. Note that specialFolderPath is being used to help work out where the file will be saved. This works even when you test on desktop machines. The LiveCode Dictionary shows a full list of special folder paths, including many that don't apply to mobile apps.

  4. You can put the following showdata function into the Home card's Card script as well, but having it in the Stack level keeps it near other functions that are related to it. Type it in now:
    on showdata
      global gReminderData
      go card "home"
      put empty into field "reminders"
      put gReminderData into field "data"
      if gReminderData = "no entries yet" then
        exit showdata
      end if
      set the itemdelimiter to tab
      put 1 into tLineNumber
      repeat with a = 1 to the number of lines in gReminderData
        put line a of gReminderData into tEntry
        if item 1 of tEntry = "Reminder" then
          put item 2 of tEntry into tTitle
          put item 3 of tEntry into tDescription
          put item 4 of tEntry into tNotificationTime
          convert tNotificationTime from seconds to abbreviated time and long date
          put item 5 of tEntry into tLocationName
          put tTitle & ":" && tDescription && tNotificationTime && tLocationName into line tLineNumber of field "reminders"
          add 1 to tLineNumber
        end if
      end repeat
    end showdata

    Note

    If you recall the sample text file from earlier, the showdata function takes each line and splits the tab delimited items into chunks of information to present to the user. One cute trick is that the notification time, which is a long number of seconds, is converted into a human-readable form, showing both the date and time of the notification. The data field is used to show the raw data that has been saved. In the final application, you would not show this, but it's handy to check whether the reminder information looks correct or not.

  5. The last functions in the Stack script are used to set up the notifications themselves:
    on clearnotifiers
      mobileCancelAllLocalNotifications
    end clearnotifiers
    
    on setupnotifiers
      global gReminderData
      if gReminderData = "no entries yet" then exit setupnotifiers
      set the itemdelimiter to tab
      repeat with a = 1 to the number of lines in gReminderData
        put line a of gReminderData into tEntryDetails
        if item 1 of tEntryDetails = "Reminder" then
          put item 2 of tEntryDetails && "-" && item 3 of tEntryDetails into alertBody
          put "OK" into alertButtonMessage
          put tEntryDetails into alertPayload
          put item 4 of tEntryDetails into alertTime
          put item 6 of tEntryDetails into playSound
          mobileCreateLocalNotification alertBody, alertButtonMessage, alertPayload, alertTime, playSound
        end if
      end repeat
    end setupnotifiers
    
    on localNotificationReceived pMsg
      answer "Local Notification:" && pMsg
    end localNotificationReceived

    Note

    Many mobile apps that use notifications don't ever clear them. In general, maybe they don't need to be cleared. Once they go by, they're gone for good! Well, not always. Sometimes, you'll go into an app just ahead of when a notification comes and you'd do the task, only to then be pestered with notifications about something you already did! In our app, we clear all the notifications that were due and recreate the whole list again. This way, any that you have deleted won't come back to haunt you later. To help in debugging, alertPayload is filled in with the entire reminder entry and will be shown to you when the notification comes in.

What just happened?

In addition to getting your fingers nicely warmed up, you entered all the functions to read and write the reminders data and to create and receive the notification messages.

Home card scripts

We're not going to put any scripts into the card level; they can just be inside various buttons. Starting with the ones 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