Files
raucao 2cb54813b9 Add weekly and daily wallpaper rotation
Rotate through photos from enabled feeds with a persisted shuffle-bag queue. Weekly changes at local Monday midnight and daily at local midnight; Off keeps the selected photo. Legacy configs migrate to the weekly default, and Rotation now sits above Feeds in the Photos settings.
2026-11-08 22:25:59 +04:00

63 lines
1.5 KiB
JavaScript

export const DAY_MS = 24 * 60 * 60 * 1000;
// Local calendar day index. Using the local Y/M/D components keeps the index
// stable across DST transitions and makes it change at local midnight.
function localDayIndex(date) {
return Math.floor(
Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) / DAY_MS
);
}
// Weekly periods start on Monday at local midnight. 1970-01-05 was a Monday,
// which is day 4 in this day-index scheme.
export function rotationIndex(now, mode) {
const date = new Date(now);
if (mode === 'weekly') {
return Math.floor((localDayIndex(date) - 4) / 7);
}
if (mode === 'daily') {
return localDayIndex(date);
}
return null;
}
export function nextRotationStart(now, mode) {
const date = new Date(now);
if (mode === 'weekly') {
const daysUntilMonday = (8 - date.getDay()) % 7 || 7;
const next = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() + daysUntilMonday,
0,
0,
0,
0
);
return next.getTime();
}
if (mode === 'daily') {
const next = new Date(date);
next.setHours(24, 0, 0, 0);
return next.getTime();
}
return null;
}
export function shuffle(items, random = Math.random) {
const result = [...items];
for (let index = result.length - 1; index > 0; index -= 1) {
const swapIndex = Math.floor(random() * (index + 1));
[result[index], result[swapIndex]] = [result[swapIndex], result[index]];
}
return result;
}