From eb90dfa89a2e1a7bb78c8033cc3a7c068540c539 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Sun, 15 Oct 2023 10:17:03 +0200 Subject: [PATCH 1/9] calendar: Add menu --- apps/calendar/calendar.js | 62 ++++++++++++++++++++++++++++++++------- apps/calendar/settings.js | 4 +-- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index d7c43eb1f..e2c29786c 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -1,3 +1,4 @@ +{ const maxX = g.getWidth(); const maxY = g.getHeight(); const fontSize = g.getWidth() > 200 ? 2 : 1; @@ -65,22 +66,22 @@ if (settings.ndColors === true) { bgOtherEvent = cyan; } -function getDowLbls(locale) { +getDowLbls = function(locale) { let days = startOnSun ? [0, 1, 2, 3, 4, 5, 6] : [1, 2, 3, 4, 5, 6, 0]; const d = new Date(); return days.map(i => { d.setDate(d.getDate() + (i + 7 - d.getDay()) % 7); return require("locale").dow(d, 1); }); -} +}; -function sameDay(d1, d2) { +sameDay = function(d1, d2) { return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate(); -} +}; -function drawCalendar(date) { +drawCalendar = function(date) { g.setBgColor(bgColor); g.clearRect(0, 0, maxX, maxY); g.setBgColor(bgColorMonth); @@ -231,9 +232,39 @@ function drawCalendar(date) { ); } } -} +}; -function setUI() { +showMenu = function() { + const menu = { + "" : { + title : "Calendar", + remove: () => { + require("widget_utils").show(); + } + }, + "< Back": () => { + require("widget_utils").hide(); + E.showMenu(); + drawCalendar(date); + setUI(); + }, + /*LANG*/"Settings": () => { + const appSettings = eval(require('Storage').read('calendar.settings.js')); + appSettings(showMenu); + }, + /*LANG*/"Launch Alarms": () => { + load("alarm.app.js"); + }, + /*LANG*/"Exit": () => load(), + }; + if (!require("Storage").read("alarm.app.js")) { + delete menu[/*LANG*/"Launch Alarms"]; + } + require("widget_utils").show(); + E.showMenu(menu); +}; + +setUI = function() { Bangle.setUI({ mode : "custom", swipe: (dirLR, dirUD) => { @@ -257,7 +288,14 @@ function setUI() { drawCalendar(date); } }, - btn: (n) => n === (process.env.HWVERSION === 2 ? 1 : 3) && load(), + btn: (n) => { + if (process.env.HWVERSION === 2 || n === 2) { + showMenu(); + } else if (n === 3) { + // directly exit only on Bangle.js 1 + load(); + } + }, touch: (n,e) => { events.sort((a,b) => a.date - b.date); const menu = events.filter(ev => ev.date.getFullYear() === date.getFullYear() && ev.date.getMonth() === date.getMonth()).map(e => { @@ -270,15 +308,19 @@ function setUI() { } menu[""] = { title: require("locale").month(date) + " " + date.getFullYear() }; menu["< Back"] = () => { + require("widget_utils").hide(); E.showMenu(); drawCalendar(date); setUI(); }; + require("widget_utils").show(); E.showMenu(menu); } }); -} +}; drawCalendar(date); setUI(); -// No space for widgets! +Bangle.loadWidgets(); +require("widget_utils").hide(); // No space for widgets! +} diff --git a/apps/calendar/settings.js b/apps/calendar/settings.js index 54ed50a64..0a09a2bed 100644 --- a/apps/calendar/settings.js +++ b/apps/calendar/settings.js @@ -1,6 +1,6 @@ (function (back) { - var FILE = "calendar.json"; - var settings = require('Storage').readJSON(FILE, true) || {}; + const FILE = "calendar.json"; + const settings = require('Storage').readJSON(FILE, true) || {}; if (settings.ndColors === undefined) if (process.env.HWVERSION == 2) { settings.ndColors = true; From abdbe1767edbfdde0a7f5f70b59c4e9733296ae3 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Sun, 15 Oct 2023 10:30:41 +0200 Subject: [PATCH 2/9] calendar: Update version --- apps/calendar/ChangeLog | 1 + apps/calendar/metadata.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/calendar/ChangeLog b/apps/calendar/ChangeLog index 12776867f..106827aad 100644 --- a/apps/calendar/ChangeLog +++ b/apps/calendar/ChangeLog @@ -15,3 +15,4 @@ Display events for current month on touch 0.14: Add support for holidays 0.15: Edit holidays on device in settings +0.16: Add menu to fast open settings to edit holidays diff --git a/apps/calendar/metadata.json b/apps/calendar/metadata.json index bd35c8879..a1ec183cb 100644 --- a/apps/calendar/metadata.json +++ b/apps/calendar/metadata.json @@ -1,7 +1,7 @@ { "id": "calendar", "name": "Calendar", - "version": "0.15", + "version": "0.16", "description": "Simple calendar", "icon": "calendar.png", "screenshots": [{"url":"screenshot_calendar.png"}], From 404ceca0b25302bea378fb2e93d840610059735f Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Sun, 15 Oct 2023 10:42:07 +0200 Subject: [PATCH 3/9] calendar: prefix functions with const --- apps/calendar/calendar.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index 37004084e..2c54f1174 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -66,7 +66,7 @@ if (settings.ndColors === true) { bgOtherEvent = cyan; } -getDowLbls = function(locale) { +const getDowLbls = function(locale) { let days = startOnSun ? [0, 1, 2, 3, 4, 5, 6] : [1, 2, 3, 4, 5, 6, 0]; const d = new Date(); return days.map(i => { @@ -75,14 +75,14 @@ getDowLbls = function(locale) { }); }; -sameDay = function(d1, d2) { +const sameDay = function(d1, d2) { "jit"; return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate(); }; -drawEvent = function(ev, curDay, x1, y1, x2, y2) { +const drawEvent = function(ev, curDay, x1, y1, x2, y2) { "ram"; switch(ev.type) { case "e": // alarm/event @@ -102,7 +102,7 @@ drawEvent = function(ev, curDay, x1, y1, x2, y2) { } } -drawCalendar = function(date) { +const drawCalendar = function(date) { g.setBgColor(bgColor); g.clearRect(0, 0, maxX, maxY); g.setBgColor(bgColorMonth); @@ -238,7 +238,7 @@ drawCalendar = function(date) { } // end for (y = 0; y < rowN - 1; y++) }; // end function drawCalendar -showMenu = function() { +const showMenu = function() { const menu = { "" : { title : "Calendar", @@ -268,7 +268,7 @@ showMenu = function() { E.showMenu(menu); }; -setUI = function() { +const setUI = function() { Bangle.setUI({ mode : "custom", swipe: (dirLR, dirUD) => { From 107468bcdca05745d8ade9ca4201b43b87eb6e81 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Mon, 16 Oct 2023 19:08:35 +0200 Subject: [PATCH 4/9] calendar: drop settings.ndColors, fixes widgets --- apps/calendar/ChangeLog | 2 ++ apps/calendar/README.md | 6 +--- apps/calendar/calendar.js | 72 +++++++++++++++++++------------------ apps/calendar/metadata.json | 2 +- apps/calendar/settings.js | 13 ------- 5 files changed, 41 insertions(+), 54 deletions(-) diff --git a/apps/calendar/ChangeLog b/apps/calendar/ChangeLog index 106827aad..206b3080e 100644 --- a/apps/calendar/ChangeLog +++ b/apps/calendar/ChangeLog @@ -16,3 +16,5 @@ 0.14: Add support for holidays 0.15: Edit holidays on device in settings 0.16: Add menu to fast open settings to edit holidays + Drop "B2 Colors" setting, use theme dark indicator instead + Display Widgets in menus diff --git a/apps/calendar/README.md b/apps/calendar/README.md index 5f90d0d52..03a151bf5 100644 --- a/apps/calendar/README.md +++ b/apps/calendar/README.md @@ -1,6 +1,6 @@ # Calendar -Basic calendar +Monthly calendar, displays holidays uploaded from the web interface and scheduled events. ## Usage @@ -11,7 +11,3 @@ Basic calendar - Touch to display events for current month - Press the button (button 3 on Bangle.js 1) to exit - Holidays have same color as weekends and can be edited with the 'Download'-interface, e.g. by uploading an iCalendar file. - -## Settings - -- B2 Colors: use non-dithering colors (default, recommended for Bangle 2) or the original color scheme. diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index 2c54f1174..4d0db5867 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -32,30 +32,30 @@ const date = new Date(); const timeutils = require("time_utils"); let settings = require('Storage').readJSON("calendar.json", true) || {}; let startOnSun = ((require("Storage").readJSON("setting.json", true) || {}).firstDayOfWeek || 0) === 0; - // all alarms that run on a specific date -const events = (require("Storage").readJSON("sched.json",1) || []).filter(a => a.on && a.date).map(a => { - const date = new Date(a.date); - const time = timeutils.decodeTime(a.t); - date.setHours(time.h); - date.setMinutes(time.m); - date.setSeconds(time.s); - return {date: date, msg: a.msg, type: "e"}; -}); -// add holidays & other events -(require("Storage").readJSON("calendar.days.json",1) || []).forEach(d => { - const date = new Date(d.date); - const o = {date: date, msg: d.name, type: d.type}; - if (d.repeat) { - o.repeat = d.repeat; - } - events.push(o); -}); +let events; -if (settings.ndColors === undefined) { - settings.ndColors = !g.theme.dark; -} +const loadEvents = () => { + // all alarms that run on a specific date + events = (require("Storage").readJSON("sched.json",1) || []).filter(a => a.on && a.date).map(a => { + const date = new Date(a.date); + const time = timeutils.decodeTime(a.t); + date.setHours(time.h); + date.setMinutes(time.m); + date.setSeconds(time.s); + return {date: date, msg: a.msg, type: "e"}; + }); + // add holidays & other events + (require("Storage").readJSON("calendar.days.json",1) || []).forEach(d => { + const date = new Date(d.date); + const o = {date: date, msg: d.name, type: d.type}; + if (d.repeat) { + o.repeat = d.repeat; + } + events.push(o); + }); +}; -if (settings.ndColors === true) { +if (!g.theme.dark) { bgColor = white; bgColorMonth = blue; bgColorDow = black; @@ -249,26 +249,30 @@ const showMenu = function() { "< Back": () => { require("widget_utils").hide(); E.showMenu(); - drawCalendar(date); setUI(); }, + /*LANG*/"Exit": () => load(), /*LANG*/"Settings": () => { const appSettings = eval(require('Storage').read('calendar.settings.js')); - appSettings(showMenu); + appSettings(() => { + loadEvents(); + showMenu(); + }); }, - /*LANG*/"Launch Alarms": () => { - load("alarm.app.js"); - }, - /*LANG*/"Exit": () => load(), }; - if (!require("Storage").read("alarm.app.js")) { - delete menu[/*LANG*/"Launch Alarms"]; + if (require("Storage").read("alarm.app.js")) { + menu[/*LANG*/"Launch Alarms"] = () => { + load("alarm.app.js"); + }; } require("widget_utils").show(); E.showMenu(menu); }; const setUI = function() { + require("widget_utils").hide(); // No space for widgets! + drawCalendar(date); + Bangle.setUI({ mode : "custom", swipe: (dirLR, dirUD) => { @@ -314,7 +318,6 @@ const setUI = function() { menu["< Back"] = () => { require("widget_utils").hide(); E.showMenu(); - drawCalendar(date); setUI(); }; require("widget_utils").show(); @@ -323,9 +326,8 @@ const setUI = function() { }); }; -require("Font8x12").add(Graphics); -drawCalendar(date); -setUI(); +loadEvents(); Bangle.loadWidgets(); -require("widget_utils").hide(); // No space for widgets! +require("Font8x12").add(Graphics); +setUI(); } diff --git a/apps/calendar/metadata.json b/apps/calendar/metadata.json index a1ec183cb..e263efe35 100644 --- a/apps/calendar/metadata.json +++ b/apps/calendar/metadata.json @@ -2,7 +2,7 @@ "id": "calendar", "name": "Calendar", "version": "0.16", - "description": "Simple calendar", + "description": "Monthly calendar, displays holidays uploaded from the web interface and scheduled events.", "icon": "calendar.png", "screenshots": [{"url":"screenshot_calendar.png"}], "tags": "calendar,tool", diff --git a/apps/calendar/settings.js b/apps/calendar/settings.js index 40eca9f68..434aa74f5 100644 --- a/apps/calendar/settings.js +++ b/apps/calendar/settings.js @@ -2,12 +2,6 @@ var FILE = "calendar.json"; const HOLIDAY_FILE = "calendar.days.json"; var settings = require('Storage').readJSON(FILE, true) || {}; - if (settings.ndColors === undefined) - if (process.env.HWVERSION == 2) { - settings.ndColors = true; - } else { - settings.ndColors = false; - } const holidays = require("Storage").readJSON(HOLIDAY_FILE,1).sort((a,b) => new Date(a.date) - new Date(b.date)) || []; function writeSettings() { @@ -132,13 +126,6 @@ E.showMenu({ "": { "title": "Calendar" }, "< Back": () => back(), - 'B2 Colors': { - value: settings.ndColors, - onchange: v => { - settings.ndColors = v; - writeSettings(); - } - }, /*LANG*/"Edit Holidays": () => editdates(), /*LANG*/"Add Holiday": () => { holidays.push({ From f78818ae00ba483dbca2aee1baa99e5cb0e283fb Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Thu, 19 Oct 2023 20:00:54 +0200 Subject: [PATCH 5/9] calendar: re-add ndColors setting --- apps/calendar/ChangeLog | 1 - apps/calendar/README.md | 4 ++++ apps/calendar/calendar.js | 8 ++++++-- apps/calendar/settings.js | 16 +++++++++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/apps/calendar/ChangeLog b/apps/calendar/ChangeLog index 206b3080e..6edb54f65 100644 --- a/apps/calendar/ChangeLog +++ b/apps/calendar/ChangeLog @@ -16,5 +16,4 @@ 0.14: Add support for holidays 0.15: Edit holidays on device in settings 0.16: Add menu to fast open settings to edit holidays - Drop "B2 Colors" setting, use theme dark indicator instead Display Widgets in menus diff --git a/apps/calendar/README.md b/apps/calendar/README.md index 03a151bf5..7fa7bea1c 100644 --- a/apps/calendar/README.md +++ b/apps/calendar/README.md @@ -11,3 +11,7 @@ Monthly calendar, displays holidays uploaded from the web interface and schedule - Touch to display events for current month - Press the button (button 3 on Bangle.js 1) to exit - Holidays have same color as weekends and can be edited with the 'Download'-interface, e.g. by uploading an iCalendar file. + +## Settings + +B2 Colors: use non-dithering colors (default, recommended for Bangle 2) or the original color scheme. diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index 4d0db5867..d0cc87cfa 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -55,7 +55,11 @@ const loadEvents = () => { }); }; -if (!g.theme.dark) { +if (settings.ndColors === undefined) { + settings.ndColors = !g.theme.dark; +} + +if (settings.ndColors === true) { bgColor = white; bgColorMonth = blue; bgColorDow = black; @@ -100,7 +104,7 @@ const drawEvent = function(ev, curDay, x1, y1, x2, y2) { g.setColor(bgOtherEvent).fillRect(x1+1, y1+1, x2-1, y2-1); break; } -} +}; const drawCalendar = function(date) { g.setBgColor(bgColor); diff --git a/apps/calendar/settings.js b/apps/calendar/settings.js index 18c2e6ec9..38896362b 100644 --- a/apps/calendar/settings.js +++ b/apps/calendar/settings.js @@ -3,12 +3,19 @@ const HOLIDAY_FILE = "calendar.days.json"; var settings = require('Storage').readJSON(FILE, true) || {}; if (settings.ndColors === undefined) + if (process.env.HWVERSION == 2) { + settings.ndColors = true; + } else { + settings.ndColors = false; + } + if (settings.ndColors === undefined) { if (process.env.HWVERSION == 2) { settings.ndColors = true; } else { settings.ndColors = false; } - const holidays = require("Storage").readJSON(HOLIDAY_FILE,1).sort((a,b) => new Date(a.date) - new Date(b.date)) || []; + } + const holidays = (require("Storage").readJSON(HOLIDAY_FILE,1)||[]).sort((a,b) => new Date(a.date) - new Date(b.date)) || []; function writeSettings() { require('Storage').writeJSON(FILE, settings); @@ -132,6 +139,13 @@ E.showMenu({ "": { "title": "Calendar" }, "< Back": () => back(), + 'B2 Colors': { + value: settings.ndColors, + onchange: v => { + settings.ndColors = v; + writeSettings(); + } + }, /*LANG*/"Edit Holidays": () => editdates(), /*LANG*/"Add Holiday": () => { holidays.push({ From 2bd209f0a1aa39efeb3f6c36b73b556c49e4bd95 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Thu, 19 Oct 2023 21:40:54 +0200 Subject: [PATCH 6/9] calendar: tweaks --- apps/calendar/calendar.js | 81 +++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index d0cc87cfa..fcf697256 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -33,6 +33,15 @@ const timeutils = require("time_utils"); let settings = require('Storage').readJSON("calendar.json", true) || {}; let startOnSun = ((require("Storage").readJSON("setting.json", true) || {}).firstDayOfWeek || 0) === 0; let events; +const dowLbls = () => { + const locale = require('locale').name; + const days = startOnSun ? [0, 1, 2, 3, 4, 5, 6] : [1, 2, 3, 4, 5, 6, 0]; + const d = new Date(); + return days.map(i => { + d.setDate(d.getDate() + (i + 7 - d.getDay()) % 7); + return require("locale").dow(d, 1); + }); +}(); const loadEvents = () => { // all alarms that run on a specific date @@ -70,15 +79,6 @@ if (settings.ndColors === true) { bgOtherEvent = cyan; } -const getDowLbls = function(locale) { - let days = startOnSun ? [0, 1, 2, 3, 4, 5, 6] : [1, 2, 3, 4, 5, 6, 0]; - const d = new Date(); - return days.map(i => { - d.setDate(d.getDate() + (i + 7 - d.getDay()) % 7); - return require("locale").dow(d, 1); - }); -}; - const sameDay = function(d1, d2) { "jit"; return d1.getFullYear() === d2.getFullYear() && @@ -106,6 +106,30 @@ const drawEvent = function(ev, curDay, x1, y1, x2, y2) { } }; +const calcDays = (month, monthMaxDayMap, dowNorm) => { + "jit"; + const maxDay = colN * (rowN - 1) + 1; + const days = []; + let nextMonthDay = 1; + let thisMonthDay = 51; + const month2 = month; + let prevMonthDay = monthMaxDayMap[month > 0 ? month - 1 : 11] - dowNorm + 1; + + for (let i = 0; i < maxDay; i++) { + if (i < dowNorm) { + days.push(prevMonthDay); + prevMonthDay++; + } else if (thisMonthDay <= monthMaxDayMap[month] + 50) { + days.push(thisMonthDay); + thisMonthDay++; + } else { + days.push(nextMonthDay); + nextMonthDay++; + } + } + return days; +}; + const drawCalendar = function(date) { g.setBgColor(bgColor); g.clearRect(0, 0, maxX, maxY); @@ -144,7 +168,6 @@ const drawCalendar = function(date) { true ); - let dowLbls = getDowLbls(require('locale').name); dowLbls.forEach((lbl, i) => { g.drawString(lbl, i * colW + colW / 2, headerH + rowH / 2); }); @@ -168,23 +191,7 @@ const drawCalendar = function(date) { 11: 31 }; - let days = []; - let nextMonthDay = 1; - let thisMonthDay = 51; - let prevMonthDay = monthMaxDayMap[month > 0 ? month - 1 : 11] - dowNorm + 1; - for (let i = 0; i < colN * (rowN - 1) + 1; i++) { - if (i < dowNorm) { - days.push(prevMonthDay); - prevMonthDay++; - } else if (thisMonthDay <= monthMaxDayMap[month] + 50) { - days.push(thisMonthDay); - thisMonthDay++; - } else { - days.push(nextMonthDay); - nextMonthDay++; - } - } - + const days = calcDays(month, monthMaxDayMap, dowNorm); const weekBeforeMonth = new Date(date.getTime()); weekBeforeMonth.setDate(weekBeforeMonth.getDate() - 7); const week2AfterMonth = new Date(date.getFullYear(), date.getMonth()+1, 0); @@ -194,8 +201,15 @@ const drawCalendar = function(date) { ev.date.setFullYear(ev.date.getMonth() < 6 ? week2AfterMonth.getFullYear() : weekBeforeMonth.getFullYear()); } }); - const eventsThisMonth = events.filter(ev => ev.date > weekBeforeMonth && ev.date < week2AfterMonth); - eventsThisMonth.sort((a,b) => a.date - b.date); + + const eventsThisMonthPerDay = events.filter(ev => ev.date > weekBeforeMonth && ev.date < week2AfterMonth).reduce((acc, ev) => { + const day = ev.date.getDate(); + if (!acc[day]) { + acc[day] = []; + } + acc[day].push(ev); + return acc; + }, []); let i = 0; g.setFont("8x12", fontSize); for (y = 0; y < rowN - 1; y++) { @@ -210,13 +224,14 @@ const drawCalendar = function(date) { const x2 = x * colW + colW; const y2 = y * rowH + headerH + rowH + rowH; - if (eventsThisMonth.length > 0) { + const eventsThisDay = eventsThisMonthPerDay[curDay.getDate()]; + if (eventsThisDay && eventsThisDay.length > 0) { + eventsThisDay.sort((a,b) => a.date - b.date); // Display events for this day - eventsThisMonth.forEach((ev, idx) => { + eventsThisDay.forEach((ev, idx) => { if (sameDay(ev.date, curDay)) { drawEvent(ev, curDay, x1, y1, x2, y2); - - eventsThisMonth.splice(idx, 1); // this event is no longer needed + eventsThisDay.splice(idx, 1); // this event is no longer needed } }); } From 2dec53ae2f58d55cbbc299ca2b80bb3fdf8b03ac Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Thu, 19 Oct 2023 21:45:28 +0200 Subject: [PATCH 7/9] calendar: test --- apps/calendar/calendar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index fcf697256..4720e27ee 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -33,7 +33,7 @@ const timeutils = require("time_utils"); let settings = require('Storage').readJSON("calendar.json", true) || {}; let startOnSun = ((require("Storage").readJSON("setting.json", true) || {}).firstDayOfWeek || 0) === 0; let events; -const dowLbls = () => { +const dowLbls = function() { const locale = require('locale').name; const days = startOnSun ? [0, 1, 2, 3, 4, 5, 6] : [1, 2, 3, 4, 5, 6, 0]; const d = new Date(); From 4fcd9dba4020de8de041767804ca1bd0e0958cbd Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Thu, 19 Oct 2023 22:08:28 +0200 Subject: [PATCH 8/9] calendar: reload settings after change --- apps/calendar/calendar.js | 58 +++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index 4720e27ee..32c0e8886 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -18,19 +18,18 @@ const red = "#d41706"; const blue = "#0000ff"; const yellow = "#ffff00"; const cyan = "#00ffff"; -let bgColor = color4; -let bgColorMonth = color1; -let bgColorDow = color2; -let bgColorWeekend = color3; -let fgOtherMonth = gray1; -let fgSameMonth = white; -let bgEvent = blue; -let bgOtherEvent = "#ff8800"; +let bgColor; +let bgColorMonth; +let bgColorDow; +let bgColorWeekend; +let fgOtherMonth; +let fgSameMonth; +let bgEvent; +let bgOtherEvent; const eventsPerDay=6; // how much different events per day we can display const date = new Date(); const timeutils = require("time_utils"); -let settings = require('Storage').readJSON("calendar.json", true) || {}; let startOnSun = ((require("Storage").readJSON("setting.json", true) || {}).firstDayOfWeek || 0) === 0; let events; const dowLbls = function() { @@ -64,20 +63,31 @@ const loadEvents = () => { }); }; -if (settings.ndColors === undefined) { - settings.ndColors = !g.theme.dark; -} - -if (settings.ndColors === true) { - bgColor = white; - bgColorMonth = blue; - bgColorDow = black; - bgColorWeekend = yellow; - fgOtherMonth = blue; - fgSameMonth = black; - bgEvent = color2; - bgOtherEvent = cyan; -} +const loadSettings = () => { + let settings = require('Storage').readJSON("calendar.json", true) || {}; + if (settings.ndColors === undefined) { + settings.ndColors = !g.theme.dark; + } + if (settings.ndColors === true) { + bgColor = white; + bgColorMonth = blue; + bgColorDow = black; + bgColorWeekend = yellow; + fgOtherMonth = blue; + fgSameMonth = black; + bgEvent = color2; + bgOtherEvent = cyan; + } else { + bgColor = color4; + bgColorMonth = color1; + bgColorDow = color2; + bgColorWeekend = color3; + fgOtherMonth = gray1; + fgSameMonth = white; + bgEvent = blue; + bgOtherEvent = "#ff8800"; + } +}; const sameDay = function(d1, d2) { "jit"; @@ -274,6 +284,7 @@ const showMenu = function() { /*LANG*/"Settings": () => { const appSettings = eval(require('Storage').read('calendar.settings.js')); appSettings(() => { + loadSettings(); loadEvents(); showMenu(); }); @@ -345,6 +356,7 @@ const setUI = function() { }); }; +loadSettings(); loadEvents(); Bangle.loadWidgets(); require("Font8x12").add(Graphics); From 7a6aa80f3068eca246e309c0de18c4e688a22509 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Fri, 20 Oct 2023 07:02:35 +0200 Subject: [PATCH 9/9] calendar: cleanup --- apps/calendar/calendar.js | 1 - apps/calendar/settings.js | 10 ++-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/apps/calendar/calendar.js b/apps/calendar/calendar.js index 32c0e8886..7477775ca 100644 --- a/apps/calendar/calendar.js +++ b/apps/calendar/calendar.js @@ -236,7 +236,6 @@ const drawCalendar = function(date) { const eventsThisDay = eventsThisMonthPerDay[curDay.getDate()]; if (eventsThisDay && eventsThisDay.length > 0) { - eventsThisDay.sort((a,b) => a.date - b.date); // Display events for this day eventsThisDay.forEach((ev, idx) => { if (sameDay(ev.date, curDay)) { diff --git a/apps/calendar/settings.js b/apps/calendar/settings.js index 38896362b..50beed8c0 100644 --- a/apps/calendar/settings.js +++ b/apps/calendar/settings.js @@ -1,13 +1,7 @@ (function (back) { - var FILE = "calendar.json"; + const FILE = "calendar.json"; const HOLIDAY_FILE = "calendar.days.json"; - var settings = require('Storage').readJSON(FILE, true) || {}; - if (settings.ndColors === undefined) - if (process.env.HWVERSION == 2) { - settings.ndColors = true; - } else { - settings.ndColors = false; - } + const settings = require('Storage').readJSON(FILE, true) || {}; if (settings.ndColors === undefined) { if (process.env.HWVERSION == 2) { settings.ndColors = true;