Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Global variables for the script
- */
- const isWidget = config.runsInWidget
- const dayColors = [
- new Color("#ff7b1c"),
- new Color("#ce6a23")
- ]
- const nightColors = [
- new Color("#1b2556"),
- new Color("#1e2c72")
- ]
- const locationFileName = "location.json"
- const parseDateFormatter = new DateFormatter()
- parseDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
- const uiDateFormatter = new DateFormatter()
- uiDateFormatter.dateFormat = "HH:mm"
- const fileManager = FileManager.local()
- /*
- * Methods
- */
- const getFilePath = (fileName) => {
- const docsPath = fileManager.documentsDirectory()
- const path = fileManager.joinPath(docsPath, fileName)
- return path
- }
- const saveToFile = (content, fileName) => {
- fileManager.writeString(getFilePath(fileName), content)
- }
- const readFromFile = (fileName) => {
- return fileManager.readString(getFilePath(fileName))
- }
- const fetchTimes = async (location) => {
- const { latitude, longitude } = location
- // fetch sunrise/sunset times
- const sunriseApiUrl = "https://api.sunrise-sunset.org/json?lat=" + latitude + "&lng=" + longitude + "&formatted=0"
- const timesRequest = new Request(sunriseApiUrl)
- const timesJsonResponse = (await timesRequest.loadJSON())
- const timesJson = timesJsonResponse.results
- const sunriseDateRaw = timesJson.sunrise
- const sunsetDateRaw = timesJson.sunset
- const sunriseDate = parseDateFormatter.date(sunriseDateRaw)
- const sunsetDate = parseDateFormatter.date(sunsetDateRaw)
- return {
- sunriseDate, sunsetDate
- }
- }
- const buildWidget = (
- sunriseDateFormatted,
- sunsetDateFormatted,
- bgColors
- ) => {
- const list = new ListWidget()
- const background = new LinearGradient()
- background.colors = bgColors
- background.locations = [0.0, 1.0]
- list.backgroundGradient = background
- const timesFont = Font.systemFont(15)
- const sunriseTitle = list.addText("Sunrise")
- sunriseTitle.font = Font.title2()
- const sunriseDateText = list.addText(sunriseDateFormatted)
- sunriseDateText.font = timesFont
- list.addSpacer(16)
- const sunsetTitle = list.addText("Sunset")
- sunsetTitle.font = Font.title2()
- const sunsetText = list.addText(sunsetDateFormatted)
- sunsetText.font = timesFont
- return list
- }
- const fetchLocationForeground = async () => {
- // fetch approximate location
- Location.setAccuracyToThreeKilometers()
- const location = Location.current()
- const fetchedLocation = await location
- const latitude = fetchedLocation.latitude
- const longitude = fetchedLocation.longitude
- return {
- latitude, longitude
- }
- }
- const fetchLocation = async () => {
- if (isWidget) {
- const cachedLocationRaw = readFromFile(locationFileName)
- return JSON.parse(cachedLocationRaw)
- } else {
- return await fetchLocationForeground()
- }
- }
- const isDayMode = (sunriseDate, sunsetDate) => {
- const date = new Date()
- return date >= sunriseDate || date <= sunsetDate
- }
- // Main body
- const location = await fetchLocation()
- const {
- sunriseDate, sunsetDate
- } = await fetchTimes(location)
- const sunsetDateFormatted = uiDateFormatter.string(sunsetDate)
- const sunriseDateFormatted = uiDateFormatter.string(sunriseDate)
- const list = buildWidget(
- sunriseDateFormatted,
- sunsetDateFormatted,
- isDayMode(sunriseDate, sunsetDate) ? dayColors : nightColors
- )
- if (!isWidget) {
- saveToFile(JSON.stringify(location), locationFileName)
- const alert = new Alert()
- alert.title = "Updated current location successfully"
- alert.addAction("OK")
- alert.addAction("See widget preview (small)")
- alert.addAction("See widget preview (medium)")
- alert.addAction("See widget preview (large)")
- const selection = await alert.present()
- switch (selection) {
- case 1:
- await list.presentSmall()
- break
- case 2:
- await list.presentMedium()
- break
- case 3:
- await list.presentLarge()
- break
- }
- Script.complete()
- return
- }
- Script.setWidget(list)
- Script.complete()
Add Comment
Please, Sign In to add comment