Created
October 29, 2023 19:17
-
-
Save Princezhm/e103fdcc83cbc9f97770eda3877533e4 to your computer and use it in GitHub Desktop.
script to be used in scriptable, shows a countdown to the born of your future baby :)
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
/** | |
* the parameters we are accepting are: | |
* - pregnancy: the date of the pregnancy | |
* - estimatedDated: the date of the estimated date of birth | |
* - pictureRefreshInterval: the interval in minutes to refresh the picture | |
* - pictureCategories: the categories for the pictures | |
* - fontColor: font color | |
* | |
* if there is a variable missing, we have a default value for it. | |
*/ | |
const userTextConfig = args.widgetParameter | |
let userConfig = {} | |
try { | |
if(userTextConfig){ | |
userConfig = JSON.parse(userTextConfig); | |
} | |
}catch (e) { | |
throw new Error(`The parameter is not a valid JSON: --->${userTextConfig}<---`); | |
} | |
// USER VARIABLES AND DEFAULTS | |
const defaultConfig = { | |
pregnancy : '2023-06-26', | |
estimatedDated : '2024-03-26', | |
pictureRefreshInterval : 10, | |
pictureCategories: ['babies'], | |
fontColor: '#ffffff', | |
language: 'es' | |
} | |
const config = { ...defaultConfig, ...userConfig}; | |
// DO NOT EDIT BELOW THIS LINE | |
const _MS_PER_DAY = 1000 * 60 * 60 * 24; | |
const refreshInterval = 1000 * 60 * config.pictureRefreshInterval | |
const startColor = new Color("#1c1c1c00") | |
const endColor = new Color("#1c1c1cb4") | |
const gradient = new LinearGradient() | |
gradient.colors = [startColor, endColor] | |
gradient.locations = [0.25, 1] | |
const allowedLanguages = ['es', 'en'] | |
const imgUrlPrototype = (keywords) => `https://source.unsplash.com/random/featured/?${keywords.join(',')}` | |
async function getRandomPic() { | |
try { | |
let imgUrl = imgUrlPrototype(config.pictureCategories) | |
let imgRequest = new Request(imgUrl) | |
return await imgRequest.loadImage() | |
} catch (e) { | |
return null | |
} | |
} | |
function generateDifferences(firstDate, secondDate) { | |
const difference = Date.parse(secondDate) - Date.parse(firstDate) | |
const days = Math.floor(difference / (1000 * 60 * 60 * 24)); | |
const weeks = Math.floor(days / 7); | |
const months = Math.floor(days / 30); | |
const remainingWeekDays = Math.floor(days % 7); | |
const remainingMonthDays = Math.floor(days % 30); | |
return { | |
days, | |
weeks, | |
remainingWeekDays, | |
months, | |
remainingMonthDays | |
} | |
} | |
function generateText({weeks, remainingWeekDays, months, remainingMonthDays}) { | |
const weeksText = weeks > 0 ? (weeks > 1 ? `${weeks} semanas` : 'Una semana') : '' | |
const remainingWeekDaysText = remainingWeekDays > 0 ? (remainingWeekDays > 1 ? ` y ${remainingWeekDays} dias` : ' y un dia') : '' | |
const monthsText = months > 0 ? (months > 1 ? `${months} meses` : 'Un mes') : '' | |
const remainingMonthDaysText = remainingMonthDays > 0 ? (remainingMonthDays > 1 ? ` y ${remainingMonthDays} dias` : ' y un dia') : '' | |
return { | |
weeksTime: `${weeksText}${remainingWeekDaysText}`, | |
monthsTime: `${monthsText}${remainingMonthDaysText}` | |
} | |
} | |
function compileText(widget, text, fontSize, rightAlign = false){ | |
const widgetText = widget.addText(String(text)) | |
widgetText.font = Font.boldSystemFont(fontSize); | |
widgetText.textColor = new Color(config.fontColor); | |
widgetText.shadowColor = Color.black(); | |
widgetText.shadowOffset = new Point(2, 2); | |
widgetText.shadowRadius = 2; | |
widgetText.textOpacity = (1); | |
widgetText.leftAlignText(); | |
if(rightAlign) { | |
widgetText.rightAlignText(); | |
} | |
} | |
function addTextToWidget(widget, {weeksTime, monthsTime}= {}, header, rightAlign = false) { | |
if (!weeksTime && !monthsTime) return; | |
compileText(widget, header, 20, rightAlign) | |
if (monthsTime) { | |
compileText(widget, monthsTime, 16, rightAlign) | |
} | |
if (weeksTime) { | |
compileText(widget, weeksTime, 16, rightAlign) | |
} | |
} | |
async function createWidget(items) { | |
let widget = new ListWidget() | |
widget.backgroundImage = await getRandomPic() | |
widget.backgroundGradient = gradient | |
widget.backgroundColor = new Color("1c1c1c") | |
widget.refreshAfterDate = new Date(Date.now() + refreshInterval) | |
const elapsedTime = generateDifferences(new Date(config.pregnancy), new Date()); | |
const remainingTime = generateDifferences(new Date(), new Date(config.estimatedDated)); | |
const elapsedText = generateText(elapsedTime) | |
const remainingText = generateText(remainingTime) | |
const topStack = widget.addStack(); | |
widget.addSpacer(); | |
const bottomRow = widget.addStack(); | |
bottomRow.addSpacer(); | |
const bottomStack = bottomRow.addStack(); | |
topStack.layoutVertically(); | |
bottomStack.layoutVertically(); | |
addTextToWidget(topStack, elapsedText, config.topText) | |
addTextToWidget(bottomStack, remainingText, config.bottomText, true) | |
return widget | |
} | |
const widget = await createWidget() | |
if (!config.runsInWidget) { | |
await widget.presentLarge() | |
} | |
Script.setWidget(widget) | |
Script.complete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment