Created
December 8, 2020 18:40
-
-
Save james-a-rob/a4a9e6cad19f9b137c37b07b6c361ebb to your computer and use it in GitHub Desktop.
JS script written to help meditation. Run using the Scriptable IOS device.
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
const meditaionVideoData = { | |
morning: ['https://www.youtube.com/watch?v=wnusFbC0E80', 'https://www.youtube.com/watch?v=L-ecrmgzdGc', 'https://www.youtube.com/watch?v=fH3N7GTFDkw', 'https://www.youtube.com/watch?v=9Uk_w-r7zv4', 'https://www.youtube.com/watch?v=3RZ8n86CmJg', 'https://www.youtube.com/watch?v=xmKhN63yCPw&vl=en', 'https://www.youtube.com/watch?v=GneKaqMdKyk', 'https://www.youtube.com/watch?v=WYP_W49o1vQ', 'https://www.youtube.com/watch?v=-ZfC7FWjscc'], | |
day:['https://www.youtube.com/watch?v=ZToicYcHIOU'], | |
evening: ['https://www.youtube.com/watch?v=4jqGrF3Zqtg', "https://m.youtube.com/watch?v=aEqlQvczMJQ", "https://m.youtube.com/watch?v=bG3AcN-XOrw", "https://m.youtube.com/watch?v=qy8PKKwbjM0"]} | |
const ui = new UITable(); | |
const row = new UITableRow() | |
const buttonRow = new UITableRow(); | |
const button= buttonRow.addButton("MEDITATE NOW") | |
const fileManager = FileManager.local() | |
const file = fileManager.joinPath(fileManager.documentsDirectory(), "data.json") | |
!fileManager.fileExists(file) && fileManager.writeString(file, JSON.stringify({dates: []})) | |
const fileData = fileManager.readString(file) | |
const dates = JSON.parse(fileData).dates; | |
const text = row.addText("Your Current Streak is", getCurrentStreak(dates).toString()+" day") | |
row.height=500; | |
button.centerAligned(); | |
text.centerAligned() | |
button.onTap = () => { | |
Safari.openInApp(selectMeditation(meditaionVideoData), true); | |
updateStreak(dates); | |
} | |
ui.addRow(row) | |
ui.addRow(buttonRow) | |
ui.present(true) | |
function getRandomInt(min, max){ | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function getTimePeriod(currentTime){ | |
var currentDate = new Date(); | |
var currentHour = currentDate.getHours(); | |
if(currentHour > 4 && currentHour < 12){ | |
return 'morning'; | |
}else if(currentHour > 12 && currentHour < 18){ | |
return 'day'; | |
}else{ | |
return 'evening'; | |
} | |
} | |
function selectMeditation(meditationVideos){ | |
var timePeriod = getTimePeriod(); | |
var videosForTimePeriod = meditationVideos[timePeriod]; | |
var randomIndex = Math.floor(Math.random() * videosForTimePeriod.length); | |
return videosForTimePeriod[randomIndex]; | |
} | |
function checkDatesMatch(dateA, dateB){ | |
return dateA.getDay() === dateB.getDay() && dateA.getMonth() === dateB.getMonth() && dateA.getYear() === dateB.getYear(); | |
} | |
function getCurrentStreak(dates){ | |
var currentDate = new Date(); | |
var currentStreak = 0; | |
if(!dates){return 0;} | |
dates.forEach((date)=>{ | |
var savedDate = new Date(date); | |
if(checkDatesMatch(savedDate, currentDate)){ | |
currentStreak += 1; | |
}else{ | |
currentDate.setDate(currentDate.getDate() -1); | |
if(checkDatesMatch(savedDate, currentDate)){ | |
currentStreak += 1; | |
} | |
} | |
}); | |
return currentStreak; | |
} | |
function updateStreak(dates){ | |
var currentDate = new Date(); | |
var mostRecentDate = new Date(dates[0]); | |
var datesMatch = checkDatesMatch(mostRecentDate, currentDate); | |
if(datesMatch){ | |
console.log('do not add to fs'); | |
}else{ | |
console.log('add to fs'); | |
dates.unshift(currentDate.toISOString()) | |
const data = {dates:dates} | |
fileManager.writeString(file, JSON.stringify(data)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment