|
// Variables used by Scriptable. |
|
// These must be at the very top of the file. Do not edit. |
|
// icon-color: deep-gray; icon-glyph: futbol; |
|
const competitionId = "ned.1" |
|
const teamId = 142; |
|
const fixturesUrl = `https://site.api.espn.com/apis/site/v2/sports/soccer/${competitionId}/teams/${teamId}/schedule?fixture=true`; |
|
const widgetSize = config.widgetFamily |
|
const maxEvents = widgetSize === "large" ? 4 : 2 |
|
|
|
async function getTeamImg(imageUrl) { |
|
let imgReq = new Request(imageUrl) |
|
let img = await imgReq.loadImage() |
|
return img |
|
} |
|
|
|
function createDivider() { |
|
const drawContext = new DrawContext() |
|
drawContext.size = new Size(543, 1) |
|
const path = new Path() |
|
path.addLine(new Point(1000, 20)) |
|
drawContext.addPath(path) |
|
drawContext.setStrokeColor(Device.isUsingDarkAppearance() ? new Color("#fff", 1) : new Color("#000000", 1)) |
|
drawContext.setLineWidth(1) |
|
drawContext.strokePath() |
|
return drawContext.getImage() |
|
} |
|
|
|
async function createWidget() { |
|
let req = new Request(fixturesUrl); |
|
let res = await req.loadJSON(); |
|
let events = res.events; |
|
|
|
let teamImg = await getTeamImg(res.team.logo); |
|
|
|
let w = new ListWidget(); |
|
w.backgroundColor = Device.isUsingDarkAppearance() ? new Color("#2C2C2E", 1) : new Color("#ffffff", 1) |
|
w.useDefaultPadding() |
|
|
|
const limitedEvents = events.slice(0, maxEvents) |
|
|
|
const imageSize = widgetSize === "large" ? 32 : 26; |
|
|
|
w.addSpacer() |
|
|
|
if (widgetSize === "large") { |
|
|
|
const teamName = |
|
events[0].competitions[0].competitors[0].id == teamId |
|
? events[0].competitions[0].competitors[0].team.displayName |
|
: events[0].competitions[0].competitors[1].team.displayName; |
|
let titleStack = w.addStack() |
|
let title = titleStack.addText(`${teamName}'s upcoming matches`) |
|
title.font = Font.boldSystemFont(16); |
|
|
|
w.addSpacer() |
|
|
|
} |
|
|
|
for (let i = 0; i < limitedEvents.length; i++) { |
|
let e = events[i] |
|
const competitors = e.competitions[0].competitors; |
|
|
|
if (widgetSize === "large" || i > 0) { |
|
w.addSpacer(10) |
|
} |
|
|
|
let homeImg = "" |
|
let awayImg = "" |
|
|
|
if (competitors[0].id == teamId) { |
|
homeImg = teamImg |
|
awayImg = await getTeamImg(competitors[1].team.logos[0].href) |
|
} else { |
|
homeImg = await getTeamImg(competitors[0].team.logos[0].href) |
|
awayImg = teamImg |
|
} |
|
|
|
let rowStack = w.addStack() |
|
rowStack.centerAlignContent() |
|
rowStack.addSpacer(20) |
|
|
|
// home team image |
|
let homeImageStack = rowStack.addStack(); |
|
let homeImage = homeImageStack.addImage(homeImg); |
|
homeImage.imageSize = new Size(imageSize, imageSize) |
|
homeImageStack.addSpacer(10) |
|
|
|
// home team name |
|
let homeNameStack = rowStack.addStack(); |
|
let homeName = homeNameStack.addText(competitors[0].team.displayName); |
|
homeName.font = Font.mediumSystemFont(12); |
|
homeNameStack.size = new Size(90, 14) |
|
homeNameStack.addSpacer() |
|
|
|
let separatorStack = rowStack.addStack(); |
|
let separator = separatorStack.addText('-') |
|
separator.font = Font.mediumSystemFont(12) |
|
separatorStack.size = new Size(24, 12) |
|
separatorStack.addSpacer(10) |
|
|
|
// away team name |
|
let awayNameStack = rowStack.addStack(); |
|
awayNameStack.addSpacer() |
|
let awayName = awayNameStack.addText(competitors[1].team.displayName); |
|
awayName.font = Font.mediumSystemFont(12); |
|
awayNameStack.size = new Size(100, 14) |
|
awayNameStack.addSpacer(10) |
|
|
|
// away team image |
|
let awayImageStack = rowStack.addStack(); |
|
let awayImage = awayImageStack.addImage(awayImg); |
|
awayImage.imageSize = new Size(imageSize, imageSize); |
|
|
|
w.addSpacer(5) |
|
|
|
let infoRowStack = w.addStack() |
|
infoRowStack.centerAlignContent() |
|
infoRowStack.addSpacer() |
|
|
|
let dateStack = infoRowStack.addStack() |
|
const dateFormatter = new DateFormatter() |
|
dateFormatter.useMediumDateStyle() |
|
dateFormatter.useShortTimeStyle() |
|
let parsedDate = new Date(Date.parse(e.date)) |
|
let formattedDate = dateFormatter.string(parsedDate) |
|
|
|
let date = dateStack.addText(formattedDate) |
|
date.font = Font.mediumSystemFont(10) |
|
date.textOpacity = 0.5 |
|
|
|
dateStack.addSpacer(10) |
|
|
|
infoRowStack.addSpacer() |
|
|
|
if (i !== maxEvents - 1) { |
|
w.addSpacer(10) |
|
|
|
let dividerStack = w.addStack() |
|
let divider = dividerStack.addImage(createDivider()) |
|
divider.imageOpacity = 0.5 |
|
} |
|
} |
|
|
|
w.addSpacer() |
|
|
|
return w |
|
} |
|
|
|
const widget = await createWidget() |
|
|
|
Script.setWidget(widget) |
|
Script.complete() |
|
|
|
await widget.presentLarge() |
Why do I become like this? I did what I was told to do.