-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwidget.min.js
More file actions
1 lines (1 loc) · 5.51 KB
/
widget.min.js
File metadata and controls
1 lines (1 loc) · 5.51 KB
1
const apiUrlBase="https://intensiv-widget.juliankern.com/beds",getApiUrl=(a,b)=>a?`${apiUrlBase}?lat=${a.latitude.toFixed(3)}&lng=${a.longitude.toFixed(3)}`:b?`${apiUrlBase}?state=${b}`:apiUrlBase,defaultCfg={layout:"simple"},CONFIG=Object.assign({},defaultCfg,arguments[0]);init();async function init(){CONFIG.debug&&console.log("init called");const a=await createWidget();config.runsInWidget||(await a.presentSmall()),Script.setWidget(a),Script.complete(),CONFIG.debug&&console.log("complete")}async function createWidget(){CONFIG.debug&&console.log("createWidget called");const a=await getData(),b=new ListWidget;CONFIG.debug&&console.log("data received");const c=newStack(b,4);let d=Color.black();Device.isUsingDarkAppearance()&&(d=Color.white()),addIcon("stethoscope",c,13,d);const e=c.addText("Freie ITS-Betten");if(e.font=Font.mediumSystemFont(12),CONFIG.debug&&console.log("base constructed"),a){b.addSpacer();let c={overall:saveLoadData(a.overall,"DE")};a.state&&(c.state=saveLoadData(a.state,a.state.shortName),CONFIG.debug&&(console.log("render state datablock"),console.log(a.state),console.log(c.state)),renderDatablock(b,a.state,c.state),b.addSpacer(4)),CONFIG.debug&&(console.log("render overall datablock"),console.log(a.overall),console.log(c.overall)),renderDatablock(b,a.overall,c.overall),b.refreshAfterDate=new Date(Date.now()+1800000),CONFIG.debug&&console.log("render updated block"),b.addSpacer(6);const d=new DateFormatter;d.useShortDateStyle(),d.useShortTimeStyle();const e=b.addText(`↻ ${d.string(new Date(a.overall.updated))}`);e.font=Font.regularSystemFont(9),e.textColor=Color.gray()}else b.addSpacer(),b.addText("Daten nicht verf\xFCgbar");return b}function renderDatablock(a,b,c){const d=newStack(a,4),e=getPercentageColor(b.used);CONFIG.debug&&console.log("render percentLabel");const f=d.addText(`${b.used.toFixed(2)}%`);f.font=Font.mediumSystemFont(22),f.textColor=e;const g=getBedsTrendIcon(b,c);CONFIG.debug&&(console.log("render trend icon"),console.log(g)),g&&addIcon(g,d,15,e),CONFIG.debug&&console.log("render number stack");const h=newStack(a,2);if("extended"===CONFIG.layout){CONFIG.debug&&console.log("render extended datablock");const a=h.addText(b.shortName||"DE");a.font=Font.semiboldSystemFont(10),CONFIG.debug&&console.log("absolute numbers");const d=h.addText(`${b.absolute.free}/${b.absolute.total}`);d.font=Font.mediumSystemFont(10),d.textColor=e;const f=getBedsTrendAbsolute(b,c);CONFIG.debug&&(console.log("relative number"),console.log(f));const g=h.addText(f);g.font=Font.mediumSystemFont(10),g.textColor=Color.gray()}else{CONFIG.debug&&console.log("render simple datablock");const a=h.addText(b.name||"Deutschland");a.font=Font.lightSystemFont(12)}CONFIG.debug&&console.log("render datablock complete")}function getPercentageColor(a){return 25>=a?Color.red():50>=a?Color.orange():Color.green()}async function getData(){try{CONFIG.debug&&(console.log("try getting data"),console.log(getApiUrl(null,args.widgetParameter)));let a;if(args.widgetParameter)a=await new Request(getApiUrl(null,args.widgetParameter)).loadJSON();else{const b=await getLocation();a=await new Request(getApiUrl(b)).loadJSON()}return a}catch(a){return CONFIG.debug&&(console.log("error getting data"),console.log(a)),null}}async function getLocation(){try{return CONFIG.debug&&console.log("try getting location"),Location.setAccuracyToThreeKilometers(),await Location.current()}catch(a){return CONFIG.debug&&(console.log("error getting location"),console.log(a)),null}}function getBedsTrend(a,b){let c=" ";if(0<Object.keys(b).length){const d=getDataForDate(b);d&&(c=a.absolute.free===d.absolute.free?" ":a.absolute.free<d.absolute.free?"\u2193":"\u2191")}return c}function getBedsTrendIcon(a,b){if(0<Object.keys(b).length){const c=getDataForDate(b);if(c)return a.absolute.free===c.absolute.free?void 0:a.absolute.free<c.absolute.free?"chevron.down":"chevron.up"}}function getBedsTrendAbsolute(a,b){if(0<Object.keys(b).length){const c=getDataForDate(b);if(CONFIG.debug&&(console.log("getBedsTrendAbsolute"),console.log(c)),c){let b=a.absolute.free-c.absolute.free;return(CONFIG.debug&&console.log(b),0===b)?"":(0<b&&(b=`+${b}`),` (${b})`)}}return""}function getDataForDate(a,b=!0,c=""){let d=c,e=1;const f=new Date,g=`${f.getFullYear()}-${("0"+(f.getMonth()+1)).slice(-2)}-${("0"+f.getDate()).slice(-2)}`;return CONFIG.debug&&(console.log("getDataForDate"),console.log(g),console.log(Object.keys(a))),"undefined"==typeof a[g]&&(e=2),b&&(f.setDate(f.getDate()-e),d=`${f.getFullYear()}-${("0"+(f.getMonth()+1)).slice(-2)}-${("0"+f.getDate()).slice(-2)}`),CONFIG.debug&&(console.log(d),console.log("getDataForDate result:"),console.log(a[d])),"undefined"!=typeof a[d]&&a[d]}function saveLoadData(a,b=""){var c=Math.max;const d=a.updated.substr(0,10),e=loadData(b);if(e){e[d]=a;const f=Object.keys(e),g=f.slice(c(Object.keys(e).length-7,0));let h={};g.forEach(a=>h[a]=e[a]);const{fm:i,path:j}=getFM(b);return i.writeString(j,JSON.stringify(h)),e}return{}}function loadData(a){const{fm:b,path:c}=getFM(a);if(b.fileExists(c)){const a=b.readString(c);return JSON.parse(a)}return{}}function getFM(a){let b,c;try{b=FileManager.iCloud(),c=getFilePath(b,a)}catch(d){b=FileManager.local(),c=getFilePath(b,a)}return{fm:b,path:c}}function addIcon(a,b,c,d){const e=SFSymbol.named(a);e.applyFont(Font.mediumSystemFont(22));const f=b.addImage(e.image);d&&(f.tintColor=d),f.imageSize=new Size(c,c),f.resizeable=!1}function newStack(a,b){const c=a.addStack();return c.layoutHorizontally(),c.centerAlignContent(),c.setPadding(0,0,0,0),c.spacing=b,c}function getFilePath(a,b){return a.joinPath(a.documentsDirectory(),`its-beds-${b}.json`)}