Guest User

Untitled

a guest
Sep 21st, 2020
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Global variables for the script
  3. */
  4. const isWidget = config.runsInWidget
  5.  
  6. const dayColors = [
  7.   new Color("#ff7b1c"),
  8.   new Color("#ce6a23")
  9. ]
  10.  
  11. const nightColors = [
  12.   new Color("#1b2556"),
  13.   new Color("#1e2c72")
  14. ]
  15.  
  16. const locationFileName = "location.json"
  17.  
  18. const parseDateFormatter = new DateFormatter()
  19. parseDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
  20.  
  21. const uiDateFormatter = new DateFormatter()
  22. uiDateFormatter.dateFormat = "HH:mm"
  23.  
  24. const fileManager = FileManager.local()
  25.  
  26. /*
  27.  * Methods
  28.  */
  29.  
  30. const getFilePath = (fileName) => {
  31.   const docsPath = fileManager.documentsDirectory()
  32.   const path = fileManager.joinPath(docsPath, fileName)
  33.  
  34.   return path
  35. }
  36.  
  37. const saveToFile = (content, fileName) => {
  38.   fileManager.writeString(getFilePath(fileName), content)
  39. }
  40.  
  41. const readFromFile = (fileName) => {
  42.   return fileManager.readString(getFilePath(fileName))
  43. }
  44.  
  45. const fetchTimes = async (location) => {
  46.  
  47.   const { latitude, longitude } = location
  48.  
  49.   // fetch sunrise/sunset times
  50.   const sunriseApiUrl = "https://api.sunrise-sunset.org/json?lat=" + latitude + "&lng=" + longitude + "&formatted=0"
  51.  
  52.   const timesRequest = new Request(sunriseApiUrl)
  53.   const timesJsonResponse = (await timesRequest.loadJSON())
  54.  
  55.   const timesJson = timesJsonResponse.results
  56.  
  57.   const sunriseDateRaw = timesJson.sunrise
  58.   const sunsetDateRaw = timesJson.sunset
  59.  
  60.   const sunriseDate = parseDateFormatter.date(sunriseDateRaw)
  61.   const sunsetDate = parseDateFormatter.date(sunsetDateRaw)
  62.  
  63.   return {
  64.     sunriseDate, sunsetDate
  65.   }
  66. }
  67.  
  68. const buildWidget = (
  69.   sunriseDateFormatted,
  70.   sunsetDateFormatted,
  71.   bgColors
  72. ) => {
  73.   const list = new ListWidget()
  74.   const background = new LinearGradient()
  75.   background.colors = bgColors
  76.   background.locations = [0.0, 1.0]
  77.   list.backgroundGradient = background
  78.  
  79.   const timesFont = Font.systemFont(15)
  80.  
  81.   const sunriseTitle = list.addText("Sunrise")
  82.   sunriseTitle.font = Font.title2()
  83.  
  84.   const sunriseDateText = list.addText(sunriseDateFormatted)
  85.   sunriseDateText.font = timesFont
  86.  
  87.   list.addSpacer(16)
  88.  
  89.   const sunsetTitle = list.addText("Sunset")
  90.   sunsetTitle.font = Font.title2()
  91.  
  92.   const sunsetText = list.addText(sunsetDateFormatted)
  93.   sunsetText.font = timesFont
  94.  
  95.   return list
  96. }
  97.  
  98. const fetchLocationForeground = async () => {
  99.   // fetch approximate location
  100.   Location.setAccuracyToThreeKilometers()
  101.   const location = Location.current()
  102.   const fetchedLocation = await location
  103.   const latitude = fetchedLocation.latitude
  104.   const longitude = fetchedLocation.longitude
  105.  
  106.   return {
  107.     latitude, longitude
  108.   }
  109. }
  110.  
  111. const fetchLocation = async () => {
  112.   if (isWidget) {
  113.     const cachedLocationRaw = readFromFile(locationFileName)
  114.    
  115.     return JSON.parse(cachedLocationRaw)
  116.   } else {
  117.     return await fetchLocationForeground()
  118.   }
  119. }
  120.  
  121. const isDayMode = (sunriseDate, sunsetDate) => {
  122.   const date = new Date()
  123.  
  124.   return date >= sunriseDate || date <= sunsetDate
  125. }
  126.  
  127. // Main body
  128. const location = await fetchLocation()
  129.  
  130. const {
  131.   sunriseDate, sunsetDate
  132. } = await fetchTimes(location)
  133.  
  134. const sunsetDateFormatted = uiDateFormatter.string(sunsetDate)
  135. const sunriseDateFormatted = uiDateFormatter.string(sunriseDate)
  136.  
  137. const list = buildWidget(
  138. sunriseDateFormatted,
  139. sunsetDateFormatted,
  140. isDayMode(sunriseDate, sunsetDate) ? dayColors : nightColors
  141. )
  142.  
  143. if (!isWidget) {
  144.   saveToFile(JSON.stringify(location), locationFileName)
  145.  
  146.   const alert = new Alert()
  147.   alert.title = "Updated current location successfully"
  148.  
  149.   alert.addAction("OK")
  150.   alert.addAction("See widget preview (small)")
  151.   alert.addAction("See widget preview (medium)")
  152.   alert.addAction("See widget preview (large)")
  153.  
  154.   const selection = await alert.present()
  155.  
  156.   switch (selection) {
  157.     case 1:
  158.       await list.presentSmall()
  159.       break
  160.     case 2:
  161.       await list.presentMedium()
  162.       break
  163.     case 3:
  164.       await list.presentLarge()
  165.       break
  166.   }
  167.  
  168.  
  169.   Script.complete()
  170.   return
  171. }
  172.  
  173. Script.setWidget(list)
  174. Script.complete()
Add Comment
Please, Sign In to add comment