sozday-dlya-menya-prilozheni/recommendations.js

70 lines
3.8 KiB
JavaScript

/* Правила персональных подсказок. Значения демо-погоды не создают советов по одежде/воздуху. */
function localDateKey(date = new Date()) {
const y = date.getFullYear(), m = String(date.getMonth()+1).padStart(2,"0"), d = String(date.getDate()).padStart(2,"0");
return `${y}-${m}-${d}`;
}
function endHour(settings) {
return Number((settings.workEnd || "18:00").split(":")[0]);
}
function buildRecommendations(weather, settings = {}, now = new Date()) {
const result = [];
const add = (id,type,kind,icon,title,text,extra={}) => result.push({id:`${id}-${localDateKey(now)}`,type,kind,icon,title,text,category:type === "season" || type === "cinema" || type === "music" ? "leisure" : "health",...extra});
const hour = now.getHours(), day = now.getDay(), workday = day >= 1 && day <= 5;
const season = seasonForMonth(now.getMonth());
const uv = weather.daily[0] ? weather.daily[0].uv : 0;
if (!weather.isDemo && (weather.temp <= 3 || weather.feels <= 0)) {
add("warm-clothes","cold","cold","alert-circle",t("comfortTitle"),t("comfortText",{temp:weather.temp,feels:weather.feels}));
} else if (!weather.isDemo && weather.temp <= 10) {
add("extra-layer","cold","cold","info",t("coolTitle"),t("coolText",{temp:weather.temp}));
}
if (!weather.isDemo && weather.aqi !== null && weather.aqi > 40 && hour >= 17) {
add("mulled-wine","air","warm","home",t("mulledTitle"),t("mulledText",{aqi:weather.aqi}));
}
if (!weather.isDemo && uv >= 5 && hour >= 7 && hour <= 18) {
add("uv-protection","uv","warm","sun",t("uvTitle"),t("uvText",{uv}));
}
if (settings.musicAdvice !== false && workday && hour >= Math.max(15,endHour(settings)-1)) {
add("calm-music","music","calm","play",t(day === 5 ? "fridayMusicTitle" : "musicTitle"),t(day === 5 ? "fridayMusicText" : "musicText"),{target:"music"});
}
if (day === 5 || day === 6 || day === 0 || hour >= 17) {
add("cinema-evening","cinema","calm","play",t("cinemaTitle"),t("cinemaText"),{target:"events"});
}
if (season === "summer" && !weather.isDemo && weather.temp >= 22) {
add("summer-beach","season","outdoor","sun",t("beachTitle"),t("beachText"),{target:"events"});
} else if (season === "winter") {
add("winter-sled","season","outdoor","sparkles",t("sledTitle"),t("sledText"),{target:"events"});
} else {
add("season-activity","season","outdoor","calendar",t("seasonTitle"),t("seasonText"),{target:"events"});
}
if (result.length < 3) {
add("daily-care","wellbeing","calm","recommender",t("dailyCareTitle"),t("dailyCareText"),{target:"music"});
}
return result;
}
function getDoneIds() {
try { return JSON.parse(SafeStore.getItem("auada_done") || "[]"); } catch (_) { return []; }
}
function toggleDone(id) {
const done = getDoneIds();
const next = done.includes(id) ? done.filter(x => x !== id) : [...done,id];
SafeStore.setItem("auada_done", JSON.stringify(next));
}
function getNotificationHistory() {
try { return JSON.parse(SafeStore.getItem("auada_history") || "[]"); } catch (_) { return []; }
}
function rememberAdvice(items) {
const history = getNotificationHistory();
const existing = new Set(history.map(x => x.id));
const fresh = items.filter(x => !existing.has(x.id)).slice(0,3);
if (!fresh.length) return [];
const entries = fresh.map(x => ({id:x.id,title:x.title,text:x.text,time:new Date().toISOString()}));
SafeStore.setItem("auada_history", JSON.stringify([...entries,...history].slice(0,30)));
return entries;
}
function sendBrowserNotification(entry) {
if (typeof Notification === "undefined" || Notification.permission !== "granted") return;
try { new Notification(entry.title,{body:entry.text,tag:entry.id}); } catch (_) { /* История внутри приложения сохранена. */ }
}