Skip to content

Instantly share code, notes, and snippets.

@cassmtnr
Last active June 7, 2022 07:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cassmtnr/efffbe09d20013986cd4ffc0f362a3e2 to your computer and use it in GitHub Desktop.
Save cassmtnr/efffbe09d20013986cd4ffc0f362a3e2 to your computer and use it in GitHub Desktop.
Date and Greeting with Unsplash Collection Background
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: home;
let collection = "932809";
const FORCE_IMAGE_UPDATE = true;
// Store current datetime
const date = new Date();
let widget = new ListWidget();
setDate(widget);
let widgetInputRAW = args.widgetParameter;
if (widgetInputRAW) {
try {
widgetInputRAW.toString();
if (widgetInputRAW.toString() !== "") {
collection = widgetInputRAW.toString();
}
} catch (e) {
throw new Error("Please long press the widget and add a parameter.");
}
}
// Add more minimal overlay
let gradient = new LinearGradient();
gradient.colors = [new Color("#000000", 0.5), new Color("#ffffff", 0)];
gradient.locations = [0, 0.5];
widget.backgroundGradient = gradient;
widget.addSpacer();
setGreeting(widget);
// Look for the image file
await setBackground(widget);
// Finalize widget settings
widget.setPadding(16, 16, 16, 0);
widget.spacing = -3;
Script.setWidget(widget);
widget.presentSmall();
Script.complete();
function setDate(widget) {
// Format the date info
let df = new DateFormatter();
df.dateFormat = "EEEE";
let weekday = widget.addText(df.string(date).toUpperCase());
let day = widget.addText(date.getDate().toString());
weekday.font = Font.semiboldSystemFont(12);
day.font = Font.lightSystemFont(34);
weekday.textColor = Color.white();
day.textColor = Color.white();
}
function setGreeting(widget) {
// Date Calculations
const month = date.getMonth();
const hour = date.getHours();
// Greetings arrays per time period.
let greetingsMorning = ["Good morning"];
let greetingsAfternoon = ["Good afternoon"];
let greetingsEvening = ["Good evening"];
let greetingsNight = ["Bedtime", "Put your pajamas"];
let greetingsLateNight = ["Zzz...", "Should be sleeping"];
let holidaysByDate = {
// month,date: greeting
"1,1": "Happy " + date.getFullYear().toString() + "!",
"10,31": "Happy Halloween!",
"12,25": "Merry Christmas!",
"7,30": "Happy Birthday!",
};
let holidayKeyDate = (month + 1).toString() + "," + date.getDate().toString();
// Support for multiple greetings per time period
function randomGreeting(greetingArray) {
return Math.floor(Math.random() * greetingArray.length);
}
let greeting = new String("Howdy.");
if (hour >= 1 && hour < 5) {
// 1am - 5am
greeting = greetingsLateNight[randomGreeting(greetingsLateNight)];
} else if (hour >= 22 || hour < 1) {
// 10pm - 1am
greeting = greetingsNight[randomGreeting(greetingsNight)];
} else if (hour < 12) {
// Before noon (5am - 12pm)
greeting = greetingsMorning[randomGreeting(greetingsMorning)];
} else if (hour >= 12 && hour <= 17) {
// 12pm - 5pm
greeting = greetingsAfternoon[randomGreeting(greetingsAfternoon)];
} else if (hour >= 17 && hour < 22) {
// 5pm - 10pm
greeting = greetingsEvening[randomGreeting(greetingsEvening)];
}
// Overwrite all greetings if specific holiday
if (holidaysByDate[holidayKeyDate]) {
greeting = holidaysByDate[holidayKeyDate];
}
// Greeting label
let hello = widget.addText(greeting);
hello.font = Font.semiboldSystemFont(16);
hello.textColor = Color.white();
}
async function setBackground(widget) {
let files = FileManager.local();
const path = files.documentsDirectory() + "/calendar_widget.jpg";
const modificationDate = files.modificationDate(path);
// Download image if it doesn't exist, wasn't created today, or update is forced
if (
!modificationDate ||
!sameDay(modificationDate, date) ||
FORCE_IMAGE_UPDATE
) {
try {
let img = await provideImage(collection);
files.writeImage(path, img);
widget.backgroundImage = img;
} catch {
widget.backgroundImage = files.readImage(path);
}
} else {
widget.backgroundImage = files.readImage(path);
}
}
// Fetch a image from Unsplash by it's collection id
async function provideImage(id) {
const img = await downloadImage(
"https://source.unsplash.com/collection/" + id
);
return img;
}
// Helper function to download images
async function downloadImage(url) {
const req = new Request(url);
return await req.loadImage();
}
// Crop an image into a rect
function cropImage(img, rect) {
let draw = new DrawContext();
draw.respectScreenScale = true;
draw.drawImageInRect(img, rect);
return draw.getImage();
}
// Formats the times under each event
function formatTime(date) {
let df = new DateFormatter();
df.useNoDateStyle();
df.useShortTimeStyle();
return df.string(date);
}
// Determines if two dates occur on the same day
function sameDay(d1, d2) {
return (
d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment