Created
November 26, 2020 04:37
-
-
Save beefan/1aa574c7a482cad0a5125df075cbf4d3 to your computer and use it in GitHub Desktop.
REM Sleep Helper Widget
This file contains hidden or 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
// SleepyTime | |
// when to set your alarm if you go to bed now | |
// wake up in between REM cycles to feel rested | |
// | |
const REM_CYCLE_LENGTH = 90 | |
let widget = new ListWidget() | |
buildWidget() | |
// for debugging, allows script to run in app | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
function buildWidget() { | |
widget.addSpacer(1) | |
let title = "GET YOUR REM" | |
let titleElement = widget.addText(title) | |
titleElement.font = Font.boldSystemFont(10) | |
titleElement.textColor = Color.cyan() | |
widget.addSpacer(3) | |
let introText = "Sleep now wake up at:" | |
let introElement = widget.addText(introText) | |
introElement.font = Font.systemFont(10) | |
introElement.textColor = Color.gray() | |
widget.addSpacer(5) | |
const wakeUpTimes = getWakeUpTimes() | |
wakeUpTimes.forEach((time, index) => { | |
let wakeUpTimeStack = widget.addStack() | |
wakeUpTimeStack.centerAlignContent() | |
wakeUpTimeStack.addSpacer(10) | |
let cycleText = `${index + 3} cycles` | |
let c = wakeUpTimeStack.addText(cycleText) | |
c.font = Font.systemFont(10) | |
wakeUpTimeStack.addSpacer(10) | |
let wakeUpTime = wakeUpTimeStack.addText(time) | |
wakeUpTime.font = Font.systemFont(14) | |
// use fontColor to indicate ideal bed time | |
// red and yellow is less REM than ideal | |
let fontColor; | |
switch(index) { | |
case 0: | |
fontColor = Color.red() | |
break | |
case 1: | |
fontColor = Color.yellow() | |
break | |
case 2: | |
fontColor = Color.green() | |
break | |
case 3: | |
fontColor = Color.green() | |
break | |
} | |
wakeUpTime.textColor = fontColor | |
widget.addSpacer(4) | |
}) | |
widget.addSpacer(2) | |
let footer = widget.addText("5-6 cycles is a good sleep") | |
footer.leftAlignText() | |
footer.font = Font.systemFont(8) | |
footer.textColor = Color.green() | |
} | |
function getWakeUpTimes() { | |
// the avg. human falls asleep in 14 minutes | |
let nowOffset = addMinutes(new Date(), 14) | |
let wakeUpTimes = [3, 4, 5, 6].map((cycles) => { | |
time = addMinutes( | |
nowOffset, | |
REM_CYCLE_LENGTH * cycles | |
) | |
return timeStr(time) | |
}) | |
return wakeUpTimes | |
} | |
function timeStr(date) { | |
let hour = date.getHours() | |
let twelveHour = hour > 12 ? hour - 12 : hour | |
twelveHour = hour == 0 ? 12 : twelveHour | |
let minutes = date.getMinutes() | |
minutes = minutes > 9 ? minutes : `0${minutes}` | |
let time = `${twelveHour}:${minutes}` | |
return `${time} ${hour > 12 ? "PM" : "AM"}` | |
} | |
function addMinutes(date, minutes) { | |
return new Date(date.getTime() + minutes*60000) | |
} |
@au5ton this is not the case on my screen. Reducing the spacing on line 38 should prevent it from happening though
I have an iPhone SE (2020) so that might have something to do with it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Times with 2 digits (ex:
10:30 PM
) are truncated when shown on the home screen, but not in the debugging screen.