Skip to content

Conditions

dromse edited this page Sep 13, 2024 · 2 revisions

To empower users with more flexibility in filtering and displaying tasks based on their own criteria, our team (consisting of just one member) has introduced a new feature called Conditions (from v1.5.0) using the #if tag.

Where are conditions stored?

By default, the plugin scans the Conditions/ folder for available JavaScript files that define these conditions.
However, you can customize this behavior. If you'd prefer to keep your condition scripts in a more discreet location, such as .scripts/, or even directly in the root directory /, you can easily change the default folder in the plugin settings.

How to create conditions?

To create a condition, add a JavaScript file that defines your custom logic. For instance, you could write a script that returns true or false depending on the specific day of the week:

// weekday.js
const weekday = (arg) => {
    const dayOfWeek = new Date().toLocaleDateString('en-US', { weekday: 'long' }).toLowerCase()

    if(dayOfWeek === arg) {
        return true;
    }

    return false;
}

export default weekday

Or filter by date range:

// range.js
export default (arg) => {
    const dayOfMonth = new Date().getDate()
    
    const rangeDays = arg.split('-').map(day=>Number(day))

    if(dayOfMonth >= rangeDays[0]  && dayOfMonth <= rangeDays[1]) {
        return true;
    }

    return false;
}

How to use conditions?

Using conditions is straightforward. Simply add a tag with your desired condition to a task in the main editor or within the plugin.
Here are some examples:

- [ ] shows task on every friday #if/weekday/friday
- [ ] show task from 15 to 30 day of month #if/range/15-30

image

Also you can leverage conditions without using argument parameter for example:

- [ ] go hiking #if/good-weather

And the code for condition using fetch API to get current weather from Open-Meteo:

// good-weather.js
export default async function() {
  try {
    const response = await fetch(
      "https://api.open-meteo.com/v1/forecast?latitude=50.45&longitude=30.52&current_weather=true",
    );

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();
    const temperature = data.current_weather.temperature;
    const windSpeed = data.current_weather.windspeed;
    const weatherCode = data.current_weather.weathercode;

    if (temperature > 30 || weatherCode >= 80 || windSpeed > 10) {
      return false; // Not good
    }

    return true; // good
  } catch (error) {
    console.error("Error fetching weather data:", error);
    return false;
  }
}

How to show conditions?

Once you've added conditions to your tasks, enable the filter in the plugin's UI:

  • Check the "Filter by condition" radio button.
  • Tasks that meet the specified conditions will appear in the Plugin UI. For example, tasks for a Friday and the 30th day of the month will be displayed accordingly like below.

image

Ideas to move forward

With the power of JavaScript and web APIs, you can now create your conditions to filter and display tasks based on various factors dynamically. Here are some ideas to get you started:

  • Weather-Based Tasks: Fetch weather data using APIs like Open-Meteo to show tasks only when the weather is suitable. For example, you can just display outdoor activities if the weather is good.

  • Date and Time Conditions: Display tasks based on specific dates, times, or ranges. You can show relevant tasks after a certain date/time or within a specified time frame.

  • Location-Based Tasks: Use geolocation APIs to fetch the user's current location and show tasks that are relevant to that location, such as nearby errands or location-specific reminders.

  • Calendar Integration: Integrate with calendar APIs to display tasks related to upcoming events, deadlines, or appointments automatically.

  • Custom Reminders: Utilize the Notifications API to send reminders for tasks at specific times or under certain conditions, such as when the weather changes or an event is approaching.

  • Device and Network Conditions: Tailor task visibility based on the user’s device, battery status, or network connection. For example, show tasks that require a strong internet connection only when the user is online.