Last active
December 2, 2020 20:46
-
-
Save simonbs/c2ef6398bb7db602c80105b5001d77fc to your computer and use it in GitHub Desktop.
Indie App Santa
This file contains hidden or 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
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: red; icon-glyph: calendar-alt; | |
let data = await loadData() | |
let app = data.today[0] | |
let widget = null | |
if (config.runsInWidget) { | |
if (config.widgetFamily == "small") { | |
widget = await createSmallWidget(app) | |
} else { | |
widget = await createMediumWidget(app) | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
} else if (config.runsWithSiri) { | |
let widget = await createMediumWidget(app) | |
await widget.presentMedium() | |
Script.complete() | |
} else { | |
await presentMenu(app) | |
} | |
async function presentMenu(app) { | |
let alert = new Alert() | |
alert.title = app.name | |
alert.message = app.description | |
alert.addAction("View Small Widget") | |
alert.addAction("View Medium Widget") | |
alert.addAction("Open App Store") | |
alert.addCancelAction("Cancel") | |
let idx = await alert.presentSheet() | |
if (idx == 0) { | |
let widget = await createSmallWidget(app) | |
await widget.presentSmall() | |
} else if (idx == 1) { | |
let widget = await createMediumWidget(app) | |
await widget.presentMedium() | |
} else if (idx == 2) { | |
Safari.open(app.url) | |
} | |
} | |
async function createSmallWidget(app) { | |
let bgImage = await loadSmallBGImage() | |
let imageURL = app.image | |
let img = await loadImage(imageURL) | |
let w = new ListWidget() | |
w.url = app.url | |
w.refreshAfterDate = getNextRefreshDate() | |
w.setPadding(5, 10, 5, 10) | |
w.backgroundImage = bgImage | |
let wimg = w.addImage(img) | |
wimg.imageSize = new Size(60, 60) | |
wimg.cornerRadius = 12 | |
w.addSpacer(6) | |
let wname = w.addText(app.name) | |
wname.font = Font.boldSystemFont(20) | |
wname.textColor = Color.white() | |
wname.lineLimit = 1 | |
wname.minimumScaleFactor = 0.5 | |
w.addSpacer(2) | |
let wdesc = w.addText(app.description) | |
wdesc.font = Font.semiboldSystemFont(16) | |
wdesc.textColor = Color.white() | |
return w | |
} | |
async function createMediumWidget(app) { | |
let bgImage = await loadMediumBGImage() | |
let imageURL = app.image | |
let img = await loadImage(imageURL) | |
let w = new ListWidget() | |
w.url = app.url | |
w.refreshAfterDate = getNextRefreshDate() | |
w.setPadding(10, 10, 10, 10) | |
w.backgroundImage = bgImage | |
let hstack = w.addStack() | |
hstack.layoutHorizontally() | |
hstack.centerAlignContent() | |
let wimg = hstack.addImage(img) | |
wimg.cornerRadius = 16 | |
wimg.imageSize = new Size(75, 75) | |
hstack.addSpacer(8) | |
let detailsStack = hstack.addStack() | |
detailsStack.layoutVertically() | |
let wname = detailsStack.addText(app.name) | |
wname.font = Font.semiboldSystemFont(22) | |
detailsStack.addSpacer(2) | |
wname.textColor = Color.white() | |
wname.lineLimit = 1 | |
wname.minimumScaleFactor = 0.5 | |
let wdesc = detailsStack.addText(app.description) | |
wdesc.font = Font.regularSystemFont(17) | |
wdesc.textColor = Color.white() | |
wdesc.lineLimit = 0 | |
hstack.addSpacer() | |
w.addSpacer() | |
return w | |
} | |
function getNextRefreshDate() { | |
let interval = 60 * 60 * 1000 | |
let now = new Date() | |
let time = now.getTime() + interval | |
return new Date(time) | |
} | |
async function loadData() { | |
let url = "https://api.sheety.co/4d96b32779a88a9ad81851c7c514d54f/indieappsanta/today" | |
let req = new Request(url) | |
return req.loadJSON() | |
} | |
async function loadSmallBGImage() { | |
let url = "http://www.appcraftstudio.com/images/indieappsanta/small-widget-background.png" | |
return await loadImage(url) | |
} | |
async function loadMediumBGImage() { | |
let url = "http://www.appcraftstudio.com/images/indieappsanta/medium-widget-background.png" | |
return await loadImage(url) | |
} | |
async function loadImage(url) { | |
let req = new Request(url) | |
return req.loadImage() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment