PadCodedocs

Remember data

Keep values after the app closes, and start fresh each day, like a water tracker.

What you'll build: a water tracker, like the Water Tracker app. Buttons add 250 or 500 ml, a bar fills toward a daily goal, and the count and the goal are still there when the app opens again. On a new day, the count starts over at 0.

What you'll learn:

  • Keep a variable's value after the app closes, with Remember.
  • Run steps each time a screen appears, with When Shown.
  • Notice a new day with a date formula.

#The variables

NameTypeStarting valueRemember
intakeWhole Number0On This iPad
goalWhole Number2500On This iPad
dayStartAnythingEmptyOn This iPad

dayStart keeps the day the count belongs to. It's Anything so it can start empty: the very first time the app opens, there's no day yet.

#Steps

#Make the variables and remember them

  1. In the Library (the left panel), open Data & Logic.
  2. Add the three variables from the table with Add variable. Each is named after its type, so tap each name and type the one from the table. Set goal's starting value to 2500.
  3. Next to each variable's name, open its ⋯ menu and choose Remember on This iPad. The ⋯ turns into a green iPad.
Remembered variables in Data & Logic: dayStart, goal and history show a green iPad, and dayStart is Anything, which starts empty.

Each time the app starts, a variable starts from its starting value. A remembered one starts from where it was left instead, and every change to it is kept as it happens. There's no Save step.

#Show the day's progress

  1. Add a Text and type this into its Content:

    {fixed(intake / 1000, 1)} L of {goal / 1000} L

  2. Add a Progress Bar (under Status). Next to Value, tap ⚡︎ → Formula… and type min(intake, goal). Next to Total, tap ⚡︎ → App data → goal.

fixed(…, 1) writes the liters with one decimal, like 1.8. min(…) stops the bar at full once the goal is passed.

#Add water

  1. Add a Button and set its Title to +250 ml.
  2. In its Actions tab, tap Add action → Set Value. Set State to intake, then next to To tap ⚡︎ → Formula… and type:

    min(intake + 250, 10000)

  3. Add a second button, +500 ml, the same way with min(intake + 500, 10000).

min(…, 10000) keeps one stray tap from adding more than 10 liters.

#Change the goal

Add a Stepper. Set its Title to Daily goal: {goal / 1000} L, its Value to goal, its Range to 1000 and 5000, and its Step to 250.

#Start over on a new day

  1. Tap the empty canvas around the device, so nothing is selected. The Inspector shows the screen itself.
  2. Open the Actions tab. When Shown is already picked: these steps run each time the screen appears.
  3. Tap Add action → If, and set its Condition to:

    dayStart != null && daysBetween(dayStart, today()) > 0

  4. Under Then, tap Add action → Set Value. Set State to intake and type 0 into To. (The gray 0 in an empty box is only a hint; type it.)
  5. Below the If (not inside it), tap Add action → Set Value. Set State to dayStart, then next to To tap ⚡︎ → Formula… and type today().

Tap Preview and add some water. Tap Done editing, then open Preview again: the count is still there.

#How it works

  • Remember: a remembered variable is saved as soon as it changes, and the app starts from the saved value next time. Preview remembers too, so you can test it there.
  • When Shown on the screen runs each time it appears, including when the app opens.
  • The If: today() is the start of today. daysBetween(dayStart, today()) counts the days from the day the count belongs to; more than 0 means it's a new day, so intake goes back to 0. The first time, dayStart is empty: dayStart != null is false, and && doesn't look any further, so the formula never tries to count days from nothing.
  • The last step always runs, so dayStart becomes today. Because it's remembered, tomorrow's first launch can compare against it.

#Keep a week of history

Water Tracker also keeps the last six days, for a row of bars under the count. In its new-day steps, before intake goes back to 0, it adds a record with the last day's date and amount to a remembered list, history, and drops records more than six days old:

  • Add to List into history, with a record of date (from dayStart) and ml (from intake). To build a record from values, send them through a reusable action, as in A list you can add to.
  • Remove from List from history, Where daysBetween(it.date, today()) > 6.

A Repeater over history can then draw one bar per day.

#See also