Skip to content

Instantly share code, notes, and snippets.

@odi89
Created January 19, 2021 15:18
Show Gist options
  • Save odi89/3cf3e709fdd37db06733aaecf59066bd to your computer and use it in GitHub Desktop.
Save odi89/3cf3e709fdd37db06733aaecf59066bd to your computer and use it in GitHub Desktop.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: grimace;
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// Corona Zahlen Hamburg | https://www.hamburg.de/corona-zahlen
// Credits:
// kevinkub https://gist.github.com/kevinkub/46caebfebc7e26be63403a7f0587f664
// rphl https://gist.github.com/rphl/0491c5f9cb345bf831248732374c4ef5
// eqsOne https://talk.automators.fm/t/widget-examples/7994/379
let widget = new ListWidget()
    widget.setPadding(8, 16, 16, 16)
const spc = 3
let hourNow = new Date().getHours()
//Define nighttime (19h - 7h) for styling changes
var nightTime = (hourNow >= 19 || hourNow < 7)
//Title text
let titleTxt = widget.addText("🚀 Styrkegruppa")
titleTxt.font= Font.boldSystemFont(17)
titleTxt.leftAlignText()
widget.addSpacer(spc*2)
//Value text
let vlFnt = Font.semiboldSystemFont(20)
//Subtitle text
let ptFnt = Font.systemFont(8)
let ptCol = Color.DarkGray
//Backgrund- & text colors
  //titleTxt.textColor = Color.lightGray()
  //ptCol = Color.gray()
  const gradient = new LinearGradient()
  gradient.locations = [0, 1]
  gradient.colors = [
    new Color("192331"),
    new Color("222222")
  ]
  //widget.backgroundGradient = gradient
  //titleTxt.textColor = Color.darkGray()
  //ptCol = Color.darkGray()
await setup()
if (!config.runsInWidget) widget.presentSmall()
Script.setWidget(widget)
Script.complete()
async function setup() {
    const data = await fetchData()
    console.log("hi")
    const {peopleOnGym, liveCount, lastOnGym, totalDaylyGymGoers, users} = data
  //
    let tx2 = widget.addText(`${peopleOnGym}`)
      tx2.leftAlignText()
      tx2.font = vlFnt
  let tx1 = widget.addText("Har vært på gymmen")
  tx1.textColor = ptCol
  tx1.font= ptFnt
  tx1.leftAlignText()
  widget.addSpacer(spc)
  // Livecount brukere
    let tx4 = widget.addText(`${liveCount}`)
      tx4.leftAlignText()
      tx4.font = vlFnt
      if (parseFloat(liveCount) >= 6)  {
          tx4.textColor = Color.red()
      } else if (parseFloat(liveCount) >= 3)  {
          tx4.textColor = Color.orange()
      } else  {
          tx4.textColor = Color.green()
      }
      let tx3 = widget.addText("Stk den siste timen")
      tx3.textColor = ptCol
      tx3.font= ptFnt
      tx3.leftAlignText()
      
      widget.addSpacer(spc*4)       
      // UPDATED
  
  let tx5 = widget.addText(`Klokken ${lastOnGym.time}`)
  tx5.font = Font.systemFont(14)
  tx5.rightAlignText()
  
        tx5.textColor = Color.green()
  let tx6 = widget.addText(`Var ${lastOnGym.name} på Gymmen `)
  tx6.textColor = ptCol
  tx6.font= ptFnt
  tx6.rightAlignText()
}
async function  fetchData(){
  try{  
    let endpoint = "https://gymbro-999c6.web.app/api/private/info"  
    const request = new Request(endpoint)
    
    const res = await request.loadJSON()
    console.log(res)
    return res
    } catch (error) {
      console.log("inside error")
       const dummyData = {
        "date": "10/29/2020",
        "peopleOnGym": 4,
        "liveCount": 1,
        "lastOnGym": {
          "date": "20/01/2021",
          "time": "00:04:19",
          "name": "John",
          "lastname": "Doe"
        },
        "totalDaylyGymGoers": 4,
        "users": [
          "Ole",
          "Silje",
          "Pål",
          "Mariu"
        ]
}
        return dummyData
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment