-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updated the settings page for the new personal-stat-avg feature * implement avg personal stats * removed unnecessary code * changed lets to const, made it on by default, changed abbreviation in settings page * default set to false * updated the changelog to reflect the addition of the new feature * const * fixed typo * fix * changed variable type to const for unchanging varibales
- Loading branch information
Showing
5 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
extension/scripts/features/avg-personal-stat/avg-personal-stat.entry.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
|
||
(async () => { | ||
if (!getPageStatus().access) return; | ||
|
||
featureManager.registerFeature( | ||
"Average Personal Stat", | ||
"personalstats", | ||
() => settings.pages.profile.avgpersonalstats, | ||
init, | ||
init, | ||
null, | ||
null, | ||
async () => { | ||
await checkDevice(); | ||
} | ||
); | ||
|
||
function init() { | ||
addFetchListener((event) => { | ||
const { | ||
page, | ||
json, | ||
fetch: { body }, | ||
} = event.detail; | ||
if (page !== "personalstats") return; | ||
if (body.includes("getGraphData")) { | ||
const graphData = json; | ||
calculateStatsAverage(graphData); | ||
} | ||
}); | ||
} | ||
|
||
function calculateStatsAverage(graphData) { | ||
for (const stat in graphData.data) { | ||
const statData = graphData.data[stat]; | ||
let userIndex = 2; | ||
for (const user of statData) { | ||
// Get Relevant Data | ||
const uid = user.uid; | ||
const userName = graphData.definitions[uid]; | ||
const lowerTime = user.data[0].time; | ||
const lowerVal = user.data[0].value; | ||
const userDatalen = user.data.length; | ||
const upperTime = user.data[userDatalen - 1].time; | ||
const upperVal = user.data[userDatalen - 1].value; | ||
|
||
// Calcualte Average | ||
const timeLength = (upperTime - lowerTime) / (60 * 60 * 24); | ||
const difference = upperVal - lowerVal; | ||
const avg = difference / timeLength; | ||
const roundedAvg = avg.toFixed(2); // Rounds to 2 decimal places | ||
const formattedAvg = roundedAvg.replace(/\B(?=(\d{3})+(?!\d))/g, ","); | ||
|
||
// Insert the data | ||
const element = document.querySelectorAll("div[class^='titleItem']")[userIndex]; | ||
element.textContent = `${userName} (${formattedAvg} per day)`; | ||
userIndex++; | ||
} | ||
} | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters