Skip to content

Commit

Permalink
Merge pull request #15 from cloud-atlas-ai:mark-as-done-simplification
Browse files Browse the repository at this point in the history
Mark-as-done-simplification
  • Loading branch information
muness committed Dec 24, 2023
2 parents f76d6f2 + 66c57b1 commit 094bf11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 45 deletions.
47 changes: 13 additions & 34 deletions src/amTaskWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,30 @@ import {
ViewPlugin,
ViewUpdate,
} from '@codemirror/view';
import { RegExpCursor } from "./regexp-cursor";

const COMPLETED_AM_TASK = /^\s*[-*+]\s\[[xX]\]\s\[⚓\]\(https:\/\/app\.amazingmarvin\.com\/#t=([^)\s]+)/;

export function amTaskWatcher(app: App, plugin: AmazingMarvinPlugin) {
export function amTaskWatcher(_app: App, plugin: AmazingMarvinPlugin) {
return ViewPlugin.fromClass(
class {
constructor(public view: EditorView) {
this.updateDecorations(view);
}

update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) {
this.updateDecorations(update.view);
if (!update.docChanged) {
return;
}
}

updateDecorations(view: EditorView) {
// Process only visible ranges
for (let part of view.visibleRanges) {
const taskCursor = new RegExpCursor(view.state.doc,
"^\\s*([-*+])\\s\\[(.)\\]",
{}, part.from, part.to);


while (!taskCursor.next().done) {
let { from, to } = taskCursor.value;
const line = view.state.doc.lineAt(from);
// Check if the task is marked as completed
if (line.text.match(/^\s*[-*+]\s\[[xX]\]\s\[⚓\]/)) {
// Logic for handling completed tasks with deep links
this.handleCompletedTask(line.text);
update.changes.iterChanges((fromA, _toA, _fromB, _toB, change) => {
//only match if the change is a single character and it's an X or x
if (change.length === 1 && (change.sliceString(0, 1) === "X" || change.sliceString(0, 1) === "x")) {
let line = update.state.doc.lineAt(fromA).text;

const match = line.match(COMPLETED_AM_TASK);
if (match && match[1]) {
plugin.markDone(match[1]);
}
}
}
}

handleCompletedTask(taskLine: string) {
const regex = /https:\/\/app\.amazingmarvin\.com\/#t=([^)\s]+)/;
const match = taskLine.match(regex);

let itemId: string;
if (match && match[1]) {
itemId = match[1];
plugin.markDone(itemId);
}
});
}
},
{
Expand Down
14 changes: 3 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
App,
Notice,
Plugin,
Tasks,
normalizePath,
request,
requestUrl,
} from "obsidian";

Expand Down Expand Up @@ -48,14 +45,13 @@ const CONSTANTS = {
categoriesEndpoint: '/api/categories',
childrenEndpoint: '/api/children',
scheduledOnDayEndpoint: '/api/todayItems',
dueOnDayEndpoint: '/api/dueItems,'
dueOnDayEndpoint: '/api/dueItems'
}

export default class AmazingMarvinPlugin extends Plugin {

settings: AmazingMarvinPluginSettings;
categories: Category[] = [];
markAsDoneAttempted: string[] = [];

createFolder = async (path: string) => {
try {
Expand Down Expand Up @@ -84,6 +80,7 @@ export default class AmazingMarvinPlugin extends Plugin {
id: 'am-import',
name: 'Import Categories and Tasks',
callback: () => {
animateNotice(new Notice('Importing from Amazing Marvin...'));
this.sync().then(() => {
new Notice('Amazing Marvin data imported successfully.');
}).catch((error) => {
Expand Down Expand Up @@ -135,18 +132,13 @@ export default class AmazingMarvinPlugin extends Plugin {
}

async markDone(taskId: string) {
if (this.markAsDoneAttempted.includes(taskId)) {
return;
}

const opt = this.settings;
const requestBody = {
itemId: taskId,
timeZoneOffset: new Date().getTimezoneOffset()
};

try {
this.markAsDoneAttempted.push(taskId);
const remoteResponse = await requestUrl({
url: `https://serv.amazingmarvin.com/api/markDone`,
method: 'POST',
Expand Down Expand Up @@ -408,7 +400,7 @@ export default class AmazingMarvinPlugin extends Plugin {
if (settings.showStartDate && task.startDate) {
details += `Start Date:: [[${task.startDate}]] `;
}
if (settings.showScheduledDate && task.day) {
if (settings.showScheduledDate && task.day && task.day !== 'unassigned') {
details += `Scheduled Date:: [[${task.day}]] `;
}

Expand Down

0 comments on commit 094bf11

Please sign in to comment.