2 min read
Step 4: Streaks
Calculate and display streaks.
Where We Are
Habits are checkable. Now let’s show how many days in a row each habit has been completed.
Calculate the Streak
Add a method to the Habit model that calculates the current streak:
- Start from today (or yesterday if today isn't complete yet)
- Count backwards through consecutive completed days
- Return the count
For example: if a habit was completed today, yesterday, and the day before,
the streak is 3.
Display Streaks
Update each habit row to show the streak:
- Show a flame emoji and the number (e.g., "🔥 5")
- Only show the streak if it's greater than 0
- Place it on the right side of the row
- Use a subtle secondary color
Test It
- Run the app
- Check off a habit today
- The streak should show ”🔥 1”
- (You can test longer streaks by manually adding past dates in the code temporarily)
Add a Summary
At the top of the screen, below the date, add a summary:
- "X of Y completed today"
- Show a subtle progress indicator
- Update it as habits are checked off
Checkpoint
By now you should have:
- Streaks calculate correctly
- Streak displays next to each habit
- Daily summary shows at the top
- Zero streaks are hidden
What You Learned
- Date math in Swift
- Computed properties
- Displaying secondary information
- Summary/aggregate data