Skip to content

Commit

Permalink
Adds option to reset daily progress when updating a goal
Browse files Browse the repository at this point in the history
  • Loading branch information
lynchjames committed Sep 2, 2023
1 parent 7d81790 commit f91459c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/goal-history/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class GoalHistoryHelper {
return undefined;
}
const goalHistory = await this.loadHistory();
const item = this.goalItemForDate(goalHistory, path, this.today());
const item = await this.goalItemForDate(goalHistory, path, this.today());
return item;
}

Expand Down Expand Up @@ -71,8 +71,16 @@ export class GoalHistoryHelper {
let historyForPath = goalHistory[path] ?? [];
historyForPath = historyForPath.filter(ghi => ghi.date != item.date);
historyForPath.push(item);
console.log(item);
goalHistory[path] = historyForPath;
this.goalFile.saveJson(GOAL_HISTORY_PATH, goalHistory);
await this.goalFile.saveJson(GOAL_HISTORY_PATH, goalHistory);
}

async resetDailyProgress(path: string) {
const item = await this.todaysGoalItem(path);
item.startCount = item.endCount;
console.log(item);
await this.saveGoal(path, item);
}

goalItemForDate(goalHistory:GoalHistory, path: string, date: string): GoalHistoryItem {
Expand Down
56 changes: 46 additions & 10 deletions src/modals/goal-modal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Modal, Setting, TAbstractFile, TFile, TFolder } from "obsidian";
import { App, Modal, Setting, TAbstractFile, TFile, TFolder, TextComponent } from "obsidian";
import { NoteGoalHelper } from "../note-goal";
import type WritingGoals from "../main";
import { SettingsHelper } from "../settings/settings-helper";
Expand Down Expand Up @@ -30,7 +30,7 @@ export default class GoalModal extends Modal {
this.target = target;
}

onOpen() {
async onOpen() {
const { contentEl } = this;

contentEl.createEl("h2", { text: "Set your writing goal" });
Expand Down Expand Up @@ -64,15 +64,51 @@ export default class GoalModal extends Modal {
this.userSubmittedDailyGoalCount = value;
})
.setValue(dailyGoalCount.toString()));

if(dailyGoalCount > 0){
const dailyGoalProgress = new Setting(contentEl)
.setName(await this.dailyGoalProgressText());

const button = new Setting(contentEl)
.addButton((btn) =>
btn.setButtonText("Save goal")
.setCta()
.onClick(() => {
this.close();
this.onSubmit();
}));
const buttons = new Setting(contentEl)
.addButton((btn) =>
btn.setButtonText("Reset Daily Progress")
.setClass("mod-warning")
.onClick(async () => {
await this.onResetDailyProgress(dailyGoalProgress);
this.plugin.loadNoteGoalData();
}))
.addButton((btn) =>
btn.setButtonText("Save goal")
.setCta()
.onClick(() => {
this.close();
this.onSubmit();
}));
} else {
const saveButton = new Setting(contentEl)
.addButton((btn) =>
btn.setButtonText("Save goal")
.setCta()
.onClick(() => {
this.close();
this.onSubmit();
}));
}
}

async dailyGoalProgressText() {
return `Current daily goal progress: ${(await this.calcProgress()).toLocaleString()} words`;
}

async calcProgress() {
const todaysDailyGoalProgress = await this.goalHistoryHelper.todaysGoalItem(this.target.path);
return todaysDailyGoalProgress.endCount - todaysDailyGoalProgress.startCount;
}

async onResetDailyProgress(dailyGoalProgress: Setting) {
await this.goalHistoryHelper.resetDailyProgress(this.target.path);
dailyGoalProgress.setName(await this.dailyGoalProgressText());
await this.plugin.loadNoteGoalData(true);
}

async onSubmit() {
Expand Down

0 comments on commit f91459c

Please sign in to comment.