Skip to content

Instantly share code, notes, and snippets.

@karlu92

karlu92/incidence.js

Last active Oct 24, 2020
Embed
What would you like to do?
COVID-19 Inzidenz-Widget für iOS innerhalb Deutschlands 🇩🇪
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
const newCasesApiUrl = `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_COVID19/FeatureServer/0/query?f=json&where=NeuerFall%20IN(1%2C%20-1)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22AnzahlFall%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&resultType=standard&cacheHint=true`;
const apiUrl = (location) => `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,last_update,cases,cases7_per_100k,cases_per_100k,BL,cases7_bl_per_100k,&geometry=${location.longitude.toFixed(3)}%2C${location.latitude.toFixed(3)}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json`
let widget = await createWidget()
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
let data, attr, header, label
const list = new ListWidget()
// neue fälle in DE
// fetch new cases
data1 = await new Request(newCasesApiUrl).loadJSON()
if(!data1 || !data1.features || !data1.features.length) {
const errorList = new ListWidget()
errorList.addText("Keine Ergebnisse für die Anfrage nach den Neuinfektionen.")
return errorList
}
// header = list.addText("🦠 Neuinfektionen ".toUpperCase())
// header.centerAlignText()
// header.font = Font.mediumSystemFont(10)
// label = list.addText("+"+data1.features[0].attributes.value)
// label.font = Font.mediumSystemFont(20)
// label.centerAlignText()
// const country = list.addText("Deutschland")
// country.centerAlignText()
// country.font = Font.mediumSystemFont(12)
// country.textColor = Color.gray()
// list.addSpacer()
//
let location
if(args.widgetParameter) {
const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat)
location = {
latitude: fixedCoordinates[0],
longitude: fixedCoordinates[1]
}
} else {
Location.setAccuracyToThreeKilometers()
location = await Location.current()
}
// const list = new ListWidget()
data = await new Request(apiUrl(location)).loadJSON()
if(!data || !data.features || !data.features.length) {
const errorList = new ListWidget()
errorList.addText("Keine Ergebnisse für den aktuellen Ort gefunden.")
return errorList
}
attr = data.features[0].attributes
const incidence = attr.cases7_per_100k.toFixed(1)
const cityName = attr.GEN
const incidenceBL = attr.cases7_bl_per_100k.toFixed(1)
const state = attr.BL
const incidenceDay = attr.cases_per_100k.toFixed(1)
const lastUpdated = attr.last_update
const cases = attr.cases
// const list = new ListWidget()
// if(Device.isUsingDarkAppearance()){
// const gradient = new LinearGradient()
// gradient.locations = [0, 1]
// gradient.colors = [
// new Color("111111"),
// new Color("222222")
// ]
// list.backgroundGradient = gradient
// }
const header1 = list.addText("🦠 Inzidenz".toUpperCase())
header1.font = Font.mediumSystemFont(13)
// let trend = " 🔻"
// if (incidence > incidenceDay) {
// trend = " 🔺"
// }
const label1 = list.addText(incidence)//+trend)
label1.font = Font.boldSystemFont(30)
label1.textColor = Color.green()
if(incidence >= 50) {
label1.textColor = Color.red()
} else if(incidence >= 35) {
label1.textColor = Color.orange()
} else if(incidenceBL >= 25) {
label1.textColor = Color.yellow()
}
list.addText(cityName)
// empty line
// list.addText("")
label = list.addText("+"+data1.features[0].attributes.value.toLocaleString()+" in DE")
label.font = Font.mediumSystemFont(14)
label.textColor = Color.gray()
const labelBL = list.addText(incidenceBL+"")
labelBL.font = Font.boldSystemFont(18)
labelBL.textColor = Color.green()
if(incidenceBL >= 50) {
labelBL.textColor = Color.red()
} else if(incidenceBL >= 35) {
labelBL.textColor = Color.orange()
} else if(incidenceBL >= 25) {
labelBL.textColor = Color.yellow()
}
// list.addText(state)
const labelState = list.addText(state)
labelState.font = Font.systemFont(13)
const updateLabel = list.addText(`Daten: ${lastUpdated.substr(0,10)}`)
updateLabel.font = Font.systemFont(10)
updateLabel.textColor = Color.gray()
return list
}
@karlu92

This comment has been minimized.

Copy link
Owner Author

@karlu92 karlu92 commented Oct 23, 2020

31D274C0-3CC6-4E41-B3C2-AF1127F30E25

@DrDreist

This comment has been minimized.

Copy link

@DrDreist DrDreist commented Oct 24, 2020

Hallo, gibt es eine Möglichkeit das Widget dauerhaft im Darkmode dar zu stellen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.