mzeryck / Weather-Cal

This is a Scriptable widget that lets you display, position, and format multiple elements, including dates and events, weather information, battery level, and more. You can even create your own elements.
MIT License
808 stars 316 forks source link

URLs #170

Open EnricoBara opened 2 years ago

EnricoBara commented 2 years ago

Hi Max, I just discovered your widget and I don't know how I survived 1 year with the black calendar widget 😂 my compliments for your work, it's soo useful and really easy to set it up and customise! And moreover many many thanks for sharing it!

May I ask you if you can add a url for opening the calendar when I click the "date" element? Or if you can help me with a little input I'll update the code by myself. edit: added calshow:// on the widget url (out from scriptable) as a workaround but it doesn't lands on today

Also, what's the url of the apple weather app for opening it instead of open weather? edit: found weather:// opens weather in iOS 16!!!

Kind regards

mzeryck commented 2 years ago

Hi there, I'm so glad to hear you're enjoying the widget! Both of these are great ideas, and I'm so excited that there's finally a way to open the weather app via URL in iOS 16!

The calshow:// format uses seconds since January 1, 2001. Javascript's getTime() function returns milliseconds since January 1, 1970. This means we need to use getTime() on the current time, divide it by 1,000 to get seconds, and subtract the seconds between 1970 and 2001:

const secondsForToday = Math.floor(new Date().getTime() / 1000) - 978307200
const calendarUrl = "calshow:" + secondsForToday

A variation of this calculation is used around line 1053 to make each calendar event tappable, and the calendar will show the day of the event. A similar approach could be used for the date element!

EnricoBara commented 2 years ago

Oh thanks for the info now I understand why there's secondsForToday!

I got it working in this way:

 // Display the date on the widget.
  async date(column) {
    const secondsForToday = Math.floor(new Date().getTime() / 1000) - 978307200
        const dateUrl = "calshow:" + secondsForToday
    const dateSettings = this.settings.date
    if (!this.data.events && dateSettings.dynamicDateSize) { await this.setupEvents() }

    if (dateSettings.dynamicDateSize ? this.data.events.length : dateSettings.staticDateSize == "small") {
      this.provideText(this.formatDate(this.now,dateSettings.smallDateFormat), column, this.format.smallDate, true)
      column.url = dateUrl

    } else {
      const dateOneStack = this.align(column)
      const dateOne = this.provideText(this.formatDate(this.now,dateSettings.largeDateLineOne), dateOneStack, this.format.largeDate1)
      dateOneStack.setPadding(this.padding/2, this.padding, 0, this.padding)
      dateOneStack.url = dateUrl

      const dateTwoStack = this.align(column)
      const dateTwo = this.provideText(this.formatDate(this.now,dateSettings.largeDateLineTwo), dateTwoStack, this.format.largeDate2)
      dateTwoStack.setPadding(0, this.padding, this.padding, this.padding)
      dateTwoStack.url = dateUrl
    }
  },

for sure it would be interesting to also add a setting for a default app (if not calendar) but since my knowledge is limited to HTML and CSS I'm already satisfied with this one 😂

Thank you for the suggestion!

epizzarello commented 2 years ago

I just submitted a pull request that does all of these things @mzeryck I tried it on mine and it works

EnricoBara commented 2 years ago

Thanks @epizzarello I'm using your code, everything works fine!

Is it also possibile to add an url to the background, when an empty space is tapped? Or this can only be done out from scriptable in the widget setting?

epizzarello commented 2 years ago

Thanks @epizzarello I'm using your code, everything works fine!

Is it also possibile to add an url to the background, when an empty space is tapped? Or this can only be done out from scriptable in the widget setting?

@EnricoBara I don't know of a way to add a url to the background other than the widget settings, sorry!

EnricoBara commented 2 years ago

No problem thanks anyway!