COVID-19 Widget für iOS in der Schweiz 🇨🇭
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thanks to Dänu Probst (https://github.com/daenuprobst) for cleanly aggregating the data and | |
// Kevin Kub (https://github.com/kevinkub) for the widget skeleton! | |
class IncidenceWidget { | |
constructor() { | |
this.apiUrlCantons = (location) => `https://app.adintegra.com/covid/?c=${location}` | |
this.AbbrToCanton = { | |
'AG': 'Aargau', | |
'AI': 'Appenzell IR', | |
'AR': 'Appenzell AR', | |
'BE': 'Bern', | |
'BL': 'Baselland', | |
'BS': 'Basel-Stadt', | |
'FR': 'Fribourg', | |
'GE': 'Genf', | |
'GL': 'Glarus', | |
'GR': 'Graubünden', | |
'JU': 'Jura', | |
'LU': 'Luzern', | |
'NE': 'Neuchâtel', | |
'NW': 'Nidwalden', | |
'OW': 'Obwalden', | |
'SG': 'St. Gallen', | |
'SH': 'Schaffhausen', | |
'SO': 'Solothurn', | |
'SZ': 'Schwyz', | |
'TG': 'Thurgau', | |
'TI': 'Tessin', | |
'UR': 'Uri', | |
'VD': 'Vaud', | |
'VS': 'Wallis', | |
'ZG': 'Zug', | |
'ZH': 'Zürich', | |
}; | |
} | |
async run() { | |
let widget = await this.createWidget() | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
} | |
async createWidget(items) { | |
let data = await this.getData() | |
// Basic widget setup | |
let list = new ListWidget() | |
list.setPadding(0, 0, 0, 0) | |
let textStack = list.addStack() | |
textStack.setPadding(14, 14, 14, 14) | |
textStack.layoutVertically() | |
textStack.topAlignContent() | |
// Header | |
textStack.addSpacer() | |
let header = textStack.addText("🦠 Neuinfektionen") | |
header.font = Font.mediumSystemFont(13) | |
textStack.addSpacer() | |
if (data.error) { | |
// Error handling | |
let loadingIndicator = textStack.addText(data.error) | |
textStack.setPadding(14, 14, 14, 14) | |
loadingIndicator.font = Font.mediumSystemFont(12) | |
loadingIndicator.textOpacity = 0.5 | |
let spacer = textStack.addStack() | |
spacer.addSpacer(); | |
} else { | |
// Enable caching | |
list.refreshAfterDate = new Date(Date.now() + 60 * 60 * 1000) | |
// Add country level data | |
let countryCasesStack = textStack.addStack() | |
countryCasesStack.addSpacer() | |
let countryCasesLabel = countryCasesStack.addText(data.countryDiff.toString()) | |
countryCasesLabel.font = data.canton != ' ' ? Font.mediumSystemFont(14) : Font.mediumSystemFont(24); | |
countryCasesStack.addSpacer() | |
let countryLabelStack = textStack.addStack() | |
countryLabelStack.addSpacer() | |
let countryLabel = countryLabelStack.addText("Schweiz") | |
countryLabel.font = Font.mediumSystemFont(12) | |
countryLabel.textColor = Color.gray() | |
countryLabelStack.addSpacer() | |
textStack.addSpacer() | |
// Cantonal data | |
let cantonCasesStack = textStack.addStack() | |
cantonCasesStack.addSpacer() | |
let cantonCasesLabel = cantonCasesStack.addText(data.cantonDiff.toString()) | |
cantonCasesLabel.font = Font.mediumSystemFont(34) | |
cantonCasesLabel.textColor = Color.red(); | |
cantonCasesStack.addSpacer() | |
let cantonTotalLabelStack = textStack.addStack() | |
cantonTotalLabelStack.addSpacer() | |
let cantonTotalLabel = cantonTotalLabelStack.addText("Total " + data.cantonCases.toString()) | |
cantonTotalLabel.font = Font.mediumSystemFont(12); | |
cantonTotalLabelStack.addSpacer() | |
let cantonLabelStack = textStack.addStack() | |
cantonLabelStack.addSpacer() | |
let cantonLabel = cantonLabelStack.addText(data.canton) | |
cantonLabel.font = Font.mediumSystemFont(12) | |
cantonLabel.textColor = Color.gray() | |
cantonLabelStack.addSpacer() | |
textStack.addSpacer() | |
} | |
return list | |
} | |
async getData() { | |
let location = await this.getLocation() | |
try { | |
let currentData = await new Request(this.apiUrlCantons(location)).loadJSON() | |
return { | |
canton: (currentData.canton) ? this.AbbrToCanton[currentData.canton] : ' ', | |
cantonCases: (currentData.canton_cases) ? currentData.canton_cases : currentData.ch_cases, | |
cantonDiff: (currentData.canton_diff) ? currentData.canton_diff : ' ', | |
countryCases: currentData.ch_cases, | |
countryDiff: currentData.ch_diff, | |
trend: '→', | |
}; | |
} catch (e) { | |
return { error: "Fehler bei Datenabruf." }; | |
} | |
} | |
async getLocation() { | |
if (args.widgetParameter) { | |
return args.widgetParameter | |
} else { | |
return null; | |
} | |
} | |
} | |
await new IncidenceWidget().run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sehr gut. Viel Erfolg mit dem Widget. Ich wünsche dir, dass es in der Schweiz so gut ankommt wie in Deutschland!