Skip to content

Instantly share code, notes, and snippets.

@pie6k
Created January 3, 2023 23:37
Show Gist options
  • Save pie6k/5d3c82d6802ea549791088665d041638 to your computer and use it in GitHub Desktop.
Save pie6k/5d3c82d6802ea549791088665d041638 to your computer and use it in GitHub Desktop.
Scriptable widget showing app revenue
// Copy this code and paste it into Scriptable code editor on your phone
async function loadStats() {
const request = new Request("https://your-endpoint-giving-you-revenue-data.com");
const { todayUSD, todayOrders, revenue30USD, orders30 } = await request.loadJSON();
return { todayUSD, todayOrders, revenue30USD, orders30 };
}
const usdFormatter = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
function createWidget({ todayUSD, todayOrders, revenue30USD, orders30 }) {
const widget = new ListWidget();
const higlightColor = new Color("#A091FA");
const gradient = new LinearGradient();
gradient.locations = [0, 1];
gradient.colors = [new Color("#141414"), new Color("#141414")];
widget.backgroundGradient = gradient;
// Show app icon and title
const titleStack = widget.addStack();
titleStack.addSpacer(4);
let titleElement = titleStack.addText("Screen Studio stats");
titleElement.textColor = higlightColor;
titleElement.font = Font.mediumSystemFont(13);
widget.addSpacer(12);
// Show API
let nameElement = widget.addText(` ${todayOrders} sales today • ${usdFormatter.format(todayUSD)}`);
nameElement.textColor = Color.white();
nameElement.font = Font.systemFont(18);
widget.addSpacer(2);
let descriptionElement = widget.addText(` ${orders30} sales in 30 days • ${usdFormatter.format(revenue30USD)}`);
descriptionElement.textColor = Color.white();
descriptionElement.font = Font.systemFont(18);
return widget;
}
const stats = await loadStats();
const widget = createWidget(stats);
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
widget.presentMedium();
}
Script.complete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment