Skip to content

Commit

Permalink
move commands into separated file and add updateProjectsIdInVikunja for
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiss committed Jul 18, 2024
1 parent ce476eb commit 31af453
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 53 deletions.
72 changes: 23 additions & 49 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ import {DEFAULT_SETTINGS, SettingTab, VikunjaPluginSettings} from "./src/setting
import {Tasks} from "./src/vikunja/tasks";
import {Processor} from "./src/processing/processor";
import {UserUser} from "./vikunja_sdk";
import {backendToFindTasks, chooseOutputFile} from "./src/enums";
import {appHasDailyNotesPluginLoaded} from "obsidian-daily-notes-interface";
import {getAPI} from "obsidian-dataview";
import {Label} from "./src/vikunja/labels";
import Commands from "./src/commands";

// Remember to rename these classes and interfaces!

export default class VikunjaPlugin extends Plugin {
settings: VikunjaPluginSettings;
tasksApi: Tasks;
userObject: UserUser | undefined;
foundProblem = false;
labelsApi: Label;
processor: Processor;
commands: Commands;

async onload() {
await this.loadSettings();
this.checkDependencies();
this.commands = new Commands(this.app, this);

this.commands.checkDependencies();
this.tasksApi = new Tasks(this.app, this);
this.userObject = undefined;
this.labelsApi = new Label(this.app, this);
Expand All @@ -42,41 +41,6 @@ export default class VikunjaPlugin extends Plugin {
await this.saveData(this.settings);
}

checkDependencies() {
let foundProblem = false;
if (this.settings.chooseOutputFile === chooseOutputFile.DailyNote && appHasDailyNotesPluginLoaded()) {
new Notice("Vikunja Plugin: Daily notes core plugin is not loaded. So we cannot create daily note. Please install daily notes core plugin to use Daily Note.")
if (this.settings.debugging) console.log("Vikunja Plugin: Daily notes core plugin is not loaded. So we cannot create daily note. Please install daily notes core plugin to use Daily Note.");
foundProblem = true;
}

if (this.settings.chooseOutputFile === chooseOutputFile.File) {
if (this.settings.chosenOutputFile === "") {
new Notice("Vikunja Plugin: Output file is not selected. Please select a file to use File as output.");
if (this.settings.debugging) console.log("Vikunja Plugin: Output file is not selected. Please select a file to use File as output.");
foundProblem = true;
}
if (this.app.vault.getAbstractFileByPath(this.settings.chosenOutputFile) === null) {
new Notice("Vikunja Plugin: Output file not found. Please select a valid file to use File as output.");
if (this.settings.debugging) console.log("Vikunja Plugin: Output file not found. Please select a valid file to use File as output.");
foundProblem = true;
}
}

if (this.settings.backendToFindTasks === backendToFindTasks.Dataview && getAPI(this.app) === undefined) {
new Notice("Vikunja Plugin: Obsidian Dataview plugin is not loaded. Please install Obsidian Dataview plugin to use Dataview.");
if (this.settings.debugging) console.log("Vikunja Plugin: Obsidian Dataview plugin is not loaded. Please install Obsidian Dataview plugin to use Dataview.");
foundProblem = true;
}

if (foundProblem) {
new Notice(
"Vikunja Plugin: Found problems. Please fix them in settings before using the plugin."
);
}

this.foundProblem = foundProblem;
}

async checkLastLineForUpdate() {
if (this.settings.debugging) console.log("Checking for task update");
Expand All @@ -96,15 +60,7 @@ export default class VikunjaPlugin extends Plugin {
new Notice('Start syncing with Vikunja');
await this.processor.exec();
});

// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'vikunja-execute-sync',
name: 'Trigger sync with Vikunja',
callback: async () => {
await this.processor.exec();
}
});
this.setupCommands();

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
Expand All @@ -125,6 +81,24 @@ export default class VikunjaPlugin extends Plugin {
);
}

private setupCommands() {
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'vikunja-execute-sync',
name: 'Trigger sync with Vikunja',
callback: async () => {
await this.processor.exec();
}
});
this.addCommand({
id: 'vikunja-move-tasks-to-default-project',
name: 'Move all tasks to default project',
callback: async () => {
await this.commands.moveAllTasksToDefaultProject();
}
})
}

private async handleEditorChange() {
if (this.settings.debugging) console.log("Editor changed");
return;
Expand Down
71 changes: 71 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {backendToFindTasks, chooseOutputFile} from "../enums";
import {appHasDailyNotesPluginLoaded} from "obsidian-daily-notes-interface";
import {App, Notice} from "obsidian";
import {getAPI} from "obsidian-dataview";
import VikunjaPlugin from "../../main";

export default class Commands {
private plugin: VikunjaPlugin;
private readonly app: App;
private foundProblem = false;

constructor(app: App, plugin: VikunjaPlugin) {
this.plugin = plugin;
this.app = app;
}

isEverythingSetup() {
return this.foundProblem;
}

checkDependencies() {
let foundProblem = false;
if (this.plugin.settings.chooseOutputFile === chooseOutputFile.DailyNote && appHasDailyNotesPluginLoaded()) {
new Notice("Vikunja Plugin: Daily notes core plugin is not loaded. So we cannot create daily note. Please install daily notes core plugin to use Daily Note.")
if (this.plugin.settings.debugging) console.log("Vikunja Plugin: Daily notes core plugin is not loaded. So we cannot create daily note. Please install daily notes core plugin to use Daily Note.");
foundProblem = true;
}

if (this.plugin.settings.chooseOutputFile === chooseOutputFile.File) {
if (this.plugin.settings.chosenOutputFile === "") {
new Notice("Vikunja Plugin: Output file is not selected. Please select a file to use File as output.");
if (this.plugin.settings.debugging) console.log("Vikunja Plugin: Output file is not selected. Please select a file to use File as output.");
foundProblem = true;
}
if (this.app.vault.getAbstractFileByPath(this.plugin.settings.chosenOutputFile) === null) {
new Notice("Vikunja Plugin: Output file not found. Please select a valid file to use File as output.");
if (this.plugin.settings.debugging) console.log("Vikunja Plugin: Output file not found. Please select a valid file to use File as output.");
foundProblem = true;
}
}

if (this.plugin.settings.backendToFindTasks === backendToFindTasks.Dataview && getAPI(this.app) === undefined) {
new Notice("Vikunja Plugin: Obsidian Dataview plugin is not loaded. Please install Obsidian Dataview plugin to use Dataview.");
if (this.plugin.settings.debugging) console.log("Vikunja Plugin: Obsidian Dataview plugin is not loaded. Please install Obsidian Dataview plugin to use Dataview.");
foundProblem = true;
}

if (foundProblem) {
new Notice(
"Vikunja Plugin: Found problems. Please fix them in settings before using the plugin."
);
}

this.foundProblem = foundProblem;
}

// Move all tasks from Vault to default project in Vikunja
async moveAllTasksToDefaultProject() {
if (this.isEverythingSetup()) {
new Notice("Vikunja Plugin: Found problems in plugin. Have to be fixed first. Syncing is stopped.");
return;
}
if (this.plugin.settings.debugging) console.log("Move all tasks to default project");

const vaultSearcher = this.plugin.processor.getVaultSearcher();
const taskParser = this.plugin.processor.getTaskParser();

const tasks = (await vaultSearcher.getTasks(taskParser)).filter(task => task.task.id !== undefined).map(task => task.task);
await this.plugin.tasksApi.updateProjectsIdInVikunja(tasks, this.plugin.settings.defaultVikunjaProject);
}
}
4 changes: 2 additions & 2 deletions src/processing/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Processor {
async exec() {
if (this.plugin.settings.debugging) console.log("Processor: Start processing");

if (this.plugin.foundProblem) {
if (this.plugin.commands.isEverythingSetup()) {
new Notice("Vikunja Plugin: Found problems in plugin. Have to be fixed first. Syncing is stopped.");
if (this.plugin.settings.debugging) console.log("Processor: Found problems in plugin. Have to be fixed first.");
return;
Expand Down Expand Up @@ -101,7 +101,7 @@ class Processor {
async updateTasksOnStartup() {
if (this.plugin.settings.debugging) console.log("Processor: Update tasks in vault and vikunja");
if (this.alreadyUpdateTasksOnStartup) throw new Error("Update tasks on startup can only be called once");
if (this.plugin.foundProblem) {
if (this.plugin.commands.isEverythingSetup()) {
if (this.plugin.settings.debugging) console.log("Processor: Found problems in plugin. Have to be fixed first. Syncing is stopped.");
return;
}
Expand Down
13 changes: 11 additions & 2 deletions src/settings/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class SettingTab extends PluginSettingTab {
.addButton(button => button
.setButtonText("Check")
.onClick(async () => {
this.plugin.checkDependencies();
this.plugin.commands.checkDependencies();
new Notice("Check for dependencies done.");
})
);
Expand Down Expand Up @@ -249,7 +249,6 @@ export class SettingTab extends PluginSettingTab {
}));



new Setting(containerEl).setHeading().setName('Pull: Obsidian <- Vikunja').setDesc('');

const targetDesc = document.createDocumentFragment();
Expand Down Expand Up @@ -440,6 +439,16 @@ export class SettingTab extends PluginSettingTab {
});
}
)
new Setting(containerEl)
.setName("Move all tasks to selected default project")
.setDesc("This will move all tasks from Vault to the selected default project in Vikunja. This will not delete any tasks in Vikunja, but only move them to the selected project. This helps, if you make a wrong decision in the past. This does not create any tasks in Vikunja.")
.addButton(button => button
.setButtonText("Move all tasks")
.onClick(async () => {
await this.plugin.commands.moveAllTasksToDefaultProject();
}
));


new Setting(containerEl)
.setHeading()
Expand Down
18 changes: 18 additions & 0 deletions src/vikunja/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ class Tasks {
return this.tasksApi.tasksIdGet(param);
}

async updateProjectsIdInVikunja(tasks: ModelsTask[], projectId: number) {
if (this.plugin.settings.debugging) console.log("TasksApi: Updating project id in tasks", projectId);
return await Promise.all(tasks.map(task => this.updateProjectIdInVikunja(task, projectId)));
}

async updateProjectIdInVikunja(task: ModelsTask, projectId: number) {
if (!task.id) throw new Error("TasksApi: Task id is not defined");
if (this.plugin.settings.debugging) console.log("TasksApi: Updating project id in task", task.id, projectId);

if (task.projectId === projectId) {
if (this.plugin.settings.debugging) console.log("TasksApi: Project id is already set to", projectId, "in task", task.id);
return;
}

task.projectId = projectId;
await this.updateTask(task);
}

private async updateLabelsInVikunja(task: ModelsTask) {
try {
await this.addLabelToTask(task);
Expand Down

0 comments on commit 31af453

Please sign in to comment.