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
| Name | Type | Starting value | Remember |
|---|---|---|---|
intake | Whole Number | 0 | On This iPad |
goal | Whole Number | 2500 | On This iPad |
dayStart | Anything | Empty | On 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
- In the Library (the left panel), open Data & Logic.
- 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. - Next to each variable's name, open its ⋯ menu and choose Remember on This iPad. The ⋯ turns into a green iPad.
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
Add a Text and type this into its Content:
{fixed(intake / 1000, 1)} L of {goal / 1000} L- 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
- Add a Button and set its Title to
+250 ml. 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)- Add a second button,
+500 ml, the same way withmin(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
- Tap the empty canvas around the device, so nothing is selected. The Inspector shows the screen itself.
- Open the Actions tab. When Shown is already picked: these steps run each time the screen appears.
Tap Add action → If, and set its Condition to:
dayStart != null && daysBetween(dayStart, today()) > 0- Under Then, tap Add action → Set Value. Set State to
intakeand type0into To. (The gray 0 in an empty box is only a hint; type it.) - Below the If (not inside it), tap Add action → Set Value. Set State to
dayStart, then next to To tap ⚡︎ → Formula… and typetoday().
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, sointakegoes back to 0. The first time,dayStartis empty:dayStart != nullis false, and&&doesn't look any further, so the formula never tries to count days from nothing. - The last step always runs, so
dayStartbecomes 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 ofdate(fromdayStart) andml(fromintake). 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, WheredaysBetween(it.date, today()) > 6.
A Repeater over history can then draw one bar per day.