andrewmcgivery / obsidian-soundscapes

A plugin for Obsidian.MD that adds a music/ambiance player to the status bar to play Lofi beats, nature sounds, ambiance, relaxing music, and more.
MIT License
46 stars 7 forks source link

Play specific Soundscape on File open #39

Open pMat-19 opened 2 months ago

pMat-19 commented 2 months ago

Hi, I'm not great at programming Obsidian plugins but I thought I could ask you to add the following functionality to the plugin: start playing a specific Soundscape on File open. Here's what should be added (AI generated):

  1. Add a new setting to store the folder-to-soundscape mappings.
  2. Listen for the 'file-open' event and trigger a new method to handle the event.
  3. In the new method, get the folder of the opened file and check if there's a designated soundscape for that folder.
  4. If a soundscape is found, update the current soundscape and start playing it. If the soundscape is the same as the one that is already playing, then do nothing.

Here's the code to implement these steps:

// Step 1: Add a new setting to store folder-to-soundscape mappings
interface SoundscapesSettings {
  // ... (existing settings)
  folderSoundscapeMap: Record<string, string>;
}

const DEFAULT_SETTINGS: SoundscapesSettings = {
  // ... (existing default settings)
  folderSoundscapeMap: {},
};

// Step 2: Listen for the 'file-open' event and trigger a new method
async onload() {
  // ... (existing code)
  this.registerEvent(this.app.workspace.on('file-open', this.onFileOpen.bind(this)));
}

// Step 3: Implement the onFileOpen method
async onFileOpen(file: TAbstractFile) {
  if (file instanceof TFile) {
    const folder = file.parent.path;
    const soundscape = this.settings.folderSoundscapeMap[folder];

    // Step 4: If a soundscape is found, update the current soundscape and start playing
    if (soundscape) {
      this.settings.soundscape = soundscape;
      this.onSoundscapeChange();
    }
  }
}

// Add a method to update the folder-to-soundscape mappings
async updateFolderSoundscapeMap(folder: string, soundscape: string) {
  this.settings.folderSoundscapeMap[folder] = soundscape;
  await this.saveSettings();
}

With these changes:

  1. We added a new folderSoundscapeMap setting to store the mappings between folders and soundscapes.
  2. We registered an event listener for the 'file-open' event, which triggers the onFileOpen method.
  3. In the onFileOpen method, we get the folder of the opened file and check if there's a designated soundscape for that folder using the folderSoundscapeMap.
  4. If a soundscape is found, we update the current soundscape (this.settings.soundscape) and call onSoundscapeChange() to start playing it.
  5. We also added an updateFolderSoundscapeMap method to allow updating the folder-to-soundscape mappings. You can call this method whenever you want to associate a folder with a soundscape.

Note: Make sure to update the plugin's settings UI to allow users to configure the folder-to-soundscape mappings. You can use the updateFolderSoundscapeMap method to save the mappings when the user makes changes in the settings.

Thank you for your consideration!