Last active
August 30, 2024 18:00
-
-
Save dcatanzaro/4541f2a06e42110908fef25c2a2ee30c to your computer and use it in GitHub Desktop.
Widget iOS: Feriados Argentina
This file contains 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
//Twitter: @DamianCatanzaro | |
const widget = await createWidget(); | |
if (config.runsInWidget) { | |
Script.setWidget(widget); | |
Script.complete(); | |
} else { | |
widget.presentLarge(); | |
} | |
async function getNextHoliday() { | |
const now = new Date(); | |
const year = now.getFullYear(); | |
const url = `https://nolaborables.com.ar/api/v2/feriados/${year}`; | |
const reqHolidays = new Request(url); | |
const resHolidays = await reqHolidays.loadJSON(); | |
const today = { | |
day: now.getDate(), | |
month: now.getMonth() + 1, | |
}; | |
let holiday = resHolidays.find( | |
(h) => | |
(h.mes === today.month && h.dia > today.day) || h.mes > today.month | |
); | |
if (!holiday) { | |
holiday = resHolidays[0]; | |
} | |
return holiday; | |
} | |
function diffDays(date_1, date_2) { | |
const date1 = new Date(date_1); | |
const date2 = new Date(date_2); | |
const diffTime = Math.abs(date2 - date1); | |
const diff = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); | |
return diff; | |
} | |
async function createWidget() { | |
const now = new Date(); | |
const months = [ | |
"Enero", | |
"Febrero", | |
"Marzo", | |
"Abril", | |
"Mayo", | |
"Junio", | |
"Julio", | |
"Agosto", | |
"Septiembre", | |
"Octubre", | |
"Noviembre", | |
"Diciembre", | |
]; | |
const year = now.getFullYear(); | |
let yearNext = year; | |
const nextHoliday = await getNextHoliday(); | |
if (nextHoliday.id == "año-nuevo") { | |
yearNext++; | |
} | |
const diff = diffDays( | |
`${now.getMonth() + 1}/${now.getDate()}/${year}`, | |
`${nextHoliday.mes}/${nextHoliday.dia}/${yearNext}` | |
); | |
const widget = new ListWidget(); | |
widget.addSpacer(40); | |
const title = widget.addText("Faltan"); | |
title.textColor = new Color("#555"); | |
title.textOpacity = 1; | |
title.centerAlignText(); | |
title.font = new Font("PingFangTC-Medium", 18); | |
const time = widget.addText(`${diff} días`); | |
time.textColor = new Color("#2e7d33"); | |
time.textOpacity = 1; | |
time.centerAlignText(); | |
time.font = new Font("PingFangTC-Medium", 24); | |
widget.addSpacer(10); | |
const day = widget.addText( | |
`${nextHoliday.dia} de ${months[nextHoliday.mes - 1]} de ${yearNext}` | |
); | |
day.textColor = Color.black(); | |
day.textOpacity = 1; | |
day.leftAlignText(); | |
day.font = new Font("PingFangTC-Medium", 10); | |
const text = widget.addText(nextHoliday.motivo); | |
text.textColor = new Color("#2e7d33"); | |
text.textOpacity = 1; | |
text.leftAlignText(); | |
text.font = new Font("PingFangTC-Medium", 10); | |
const imgReq = await new Request("https://i.imgur.com/RWYIJAs.png"); | |
const img = await imgReq.loadImage(); | |
widget.backgroundImage = img; | |
return widget; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment