Advertisement
Guest User

CovidHU Scriptable widget iOS

a guest
Mar 16th, 2021
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // CovidHU Scriptable
  2.  
  3. const url = "https://coronavirus-19-api.herokuapp.com/countries/hungary"
  4. const req = new Request(url)
  5. const res = await req.loadJSON()
  6.  
  7. // dark mode 0|1
  8. const darkMode = 0
  9.  
  10. // lang en|hu
  11. const lang = "en"
  12.  
  13. if (darkMode) {
  14.     labelColor = Color.white()
  15.     dataColor = Color.red()
  16.     bgColor = Color.black()
  17. } else {
  18.     labelColor = Color.black()
  19.     dataColor = Color.red()
  20.     bgColor = Color.white()    
  21. }
  22.  
  23. if (lang === "hu") {
  24.     countryLabel = "Magyarország"
  25. } else {
  26.     countryLabel = "Hungary"
  27. }
  28.  
  29. if (config.runsInWidget) {
  30.   let widget = await createWidget("COVID-19", countryLabel, `${res.todayCases}`, `${res.todayDeaths}`, `${res.critical}`, `${res.deathsPerOneMillion}`, "#fff")
  31.   Script.setWidget(widget)
  32.   Script.complete()
  33. }
  34.  
  35. function addRow(c1, c2, label, data) {
  36.     let lbl = c1.addText(label)
  37.     lbl.font = Font.semiboldRoundedSystemFont(14)
  38.     lbl.textColor = labelColor
  39.  
  40.     let dta = c2.addText(data)
  41.     dta.rightAlignText()
  42.     dta.font = Font.semiboldRoundedSystemFont(14)
  43.     dta.textColor = dataColor
  44.  }
  45.  
  46.  
  47. async function createWidget(pretitle, title, todayCases, todayDeaths, critical, deathsPerOneMillion) {
  48.  
  49.   let w = new ListWidget()
  50.   w.backgroundColor = bgColor
  51.   w.setPadding(10, 10, 10, 10)
  52.  
  53.   let row = w.addStack()
  54.   let column = row.addStack()
  55.   column.layoutVertically()
  56.   let preText = column.addText(pretitle)
  57.   preText.textColor = labelColor
  58.   row.addSpacer(4)
  59.  
  60.   let titleTxt = column.addText(title)
  61.  
  62.   if (lang === "hu") {
  63.     titleTxt.font=Font.systemFont(13)
  64.   } else {
  65.     titleTxt.font=Font.systemFont(16)
  66.   }
  67.  
  68.   titleTxt.textColor = labelColor
  69.  
  70.   let req = new Request("https://i.imgur.com/cP7u2uM_d.webp")
  71.   let icon = await req.loadImage()
  72.  
  73.   row.layoutHorizontally()
  74.   row.addSpacer(6)
  75.  
  76.   let iconImg = row.addImage(icon)
  77.   iconImg.imageSize = new Size(40,40)
  78.   w.addSpacer(8)
  79.  
  80.   let cont = w.addStack()
  81.   cont.layoutHorizontally()
  82.   let col1 = cont.addStack()
  83.   col1.layoutVertically()
  84.   col1.addSpacer(4)
  85.   let col2 = cont.addStack()
  86.   col2.layoutVertically()
  87.   col2.addSpacer(4)
  88.  
  89.  
  90.   if (lang === "hu") {
  91.     // HU labels
  92.     addRow(col1, col2, "Új eset: ", todayCases)
  93.     addRow(col1, col2, "Kritikus: ", critical)
  94.     addRow(col1, col2, "Napi halál: ", todayDeaths)
  95.     addRow(col1, col2, "Halál/mill.fő: ", deathsPerOneMillion)
  96.   } else {
  97.     addRow(col1, col2, "Today: ", todayCases)
  98.     addRow(col1, col2, "Critical: ", critical)
  99.     addRow(col1, col2, "Deaths: ", todayDeaths)
  100.     addRow(col1, col2, "Deaths/mill.: ", deathsPerOneMillion)
  101.   }
  102.   return w
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement