Skip to content

Commit

Permalink
1.3.0 - feat: streaks
Browse files Browse the repository at this point in the history
  • Loading branch information
zoreet committed Apr 30, 2024
1 parent b3041a1 commit a9c9a56
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.3.0

feat: Streaks - track and display the number of consecutive days a habit has been completed

## 1.2.1

fix: daysToShow is converted to a number if the user passes it as a string
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"id": "habit-tracker-21",
"name": "Habit Tracker 21",
"version": "1.2.1",
"version": "1.3.0",
"minAppVersion": "1.1.0",
"description": "Your 21-day journey to habit formation, simplified",
"author": "Zoreet",
"authorUrl": "https://github.com/zoreet",
"isDesktopOnly": false
}
}
20 changes: 20 additions & 0 deletions src/HabitTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,30 @@ export default class HabitTracker {

habitCell.setAttribute('date', dateString)
habitCell.setAttribute('habit', path)

habitCell.setAttribute('streak', this.findStreak(entries, currentDate))

currentDate.setDate(currentDate.getDate() + 1)
}
}

// get streak
// based on an array of dates, get the current streak for the given date
findStreak(entries, date) {
let currentDate = new Date(date);
let streak = 0

// console.log('entries', entries)
// console.log('date', date)

while (entries.includes(this.getDateId(currentDate))) {
streak++
currentDate.setDate(currentDate.getDate() - 1)
}

return streak.toString();
}

async toggleHabit(el) {
const habit = el.getAttribute('habit')
const date = el.getAttribute('date')
Expand Down
12 changes: 11 additions & 1 deletion src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
}

.habit-tick--true:before {
background: var(--checkbox-color);
background: var(--checkbox-color);
border-radius: 10px;
content: '';
display: inline-block;
Expand All @@ -80,6 +80,7 @@
transition: all 0.3s ease;
}


.habit-tick--true + .habit-tick--true {
text-align: left;
}
Expand Down Expand Up @@ -111,3 +112,12 @@
display: table-cell;
}
}

/* streaks */
/* for every 2+ day streaks, show a counter on the most recent date */
.habit-tick--true + .habit-tick--true:has(+ .habit-tick--false):before,
.habit-tick--true + .habit-tick--true:last-child:before {
text-align: center;
content: attr(streak);
line-height: 15px;
}
5 changes: 3 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"1.1.4": "1.1.0",
"1.1.5": "1.1.0",
"1.2.0": "1.1.0",
"1.2.1": "1.1.0"
}
"1.2.1": "1.1.0",
"1.3.0": "1.1.0"
}

0 comments on commit a9c9a56

Please sign in to comment.