Hi!
I am wondering is it a nice way to implement weekly distance (steps) for watch face?
I did it with store the data in storage per day_of_week and then sum it up. But maybe there more elegant solution to it?
Code:
function storeWeeklyDistance() {
try {
// Get current distnace
// NB! distance is not reset to 0 in the Emulator on a next day
var dist = Mon.getInfo().distance;
// Convert type
if (dist == null) {
dist = 0;
}
// Save memory if distance is the same between ticks
if (self.previousDistance >= 0 && dist == previousDistance) {
return;
}
// If changed, store the latest distance
self.previousDistance = dist;
// Get today's date
var today = Time.Gregorian.info(Time.now(), Time.FORMAT_SHORT);
// Check if we need to reset the weekly distance because a new week has started
if (today.day_of_week == self.firstDayOfWeek && dist != 0) {
// Get the distance stored for the first day of the week
var currentDistance =
Application.Storage.getValue("weeklyDistance_" + today.day_of_week);
// If data exists and it's greater than the current distance, reset the weekly distance
if (currentDistance != null && currentDistance > dist) {
self.resetWeeklyDistance();
}
}
// Store the distance for the current day
Application.Storage.setValue("weeklyDistance_" + today.day_of_week, dist);
} catch (ex) {
}
}
}
Thanks in advance, Alex