generated from tpl/obsidian-sample-plugin
Reorganize utils dir
This commit is contained in:
parent
113ddefac7
commit
e4a416ec2f
|
@ -5,7 +5,7 @@ import {
|
|||
BookTrackerSettingTab,
|
||||
} from "@ui/settings";
|
||||
import { getBookByLegacyId, type SearchResult } from "@data-sources/goodreads";
|
||||
import { Templater } from "@utils/templater";
|
||||
import { Templater } from "@utils/Templater";
|
||||
import {
|
||||
GoodreadsSearchModal,
|
||||
GoodreadsSearchSuggestModal,
|
||||
|
@ -18,7 +18,8 @@ import {
|
|||
READ_STATE,
|
||||
TO_BE_READ_STATE,
|
||||
} from "./const";
|
||||
import { ReadingLog, Storage } from "@utils/storage";
|
||||
import { Storage } from "@utils/Storage";
|
||||
import { ReadingLog } from "@utils/ReadingLog";
|
||||
import { registerReadingLogCodeBlockProcessor } from "@ui/code-blocks";
|
||||
|
||||
export default class BookTrackerPlugin extends Plugin {
|
||||
|
@ -32,7 +33,7 @@ export default class BookTrackerPlugin extends Plugin {
|
|||
|
||||
this.templater = new Templater(this.app);
|
||||
this.storage = new Storage(this.app, this);
|
||||
this.readingLog = new ReadingLog(this);
|
||||
this.readingLog = new ReadingLog(this.storage);
|
||||
|
||||
this.addCommand({
|
||||
id: "search-goodreads",
|
||||
|
|
|
@ -21,11 +21,3 @@ export interface Book {
|
|||
isbn: string;
|
||||
isbn13: string;
|
||||
}
|
||||
|
||||
export interface ReadingLogEntry {
|
||||
book: string;
|
||||
pagesRead: number;
|
||||
pagesReadTotal: number;
|
||||
pagesRemaining: number;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<script lang="ts">
|
||||
import type { ReadingLogEntry } from "@src/types";
|
||||
import type { ReadingLogEntry } from "@utils/ReadingLog";
|
||||
import { Edit, Trash, Plus } from "lucide-svelte";
|
||||
import { ReadingLogEntryEditModal } from "@ui/modals";
|
||||
import type BookTrackerPlugin from "@src/main";
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import ReadingLogEntryEditModalView from "./ReadingLogEntryEditModalView.svelte";
|
||||
import type { ReadingLogEntry } from "@src/types";
|
||||
import type { ReadingLogEntry } from "@utils/ReadingLog";
|
||||
import { SvelteModal } from "./SvelteModal";
|
||||
import type BookTrackerPlugin from "@src/main";
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<script lang="ts">
|
||||
import type BookTrackerPlugin from "@src/main";
|
||||
import type { ReadingLogEntry } from "@src/types";
|
||||
import type { ReadingLogEntry } from "@utils/ReadingLog";
|
||||
import FileSuggest from "@ui/components/suggesters/FileSuggest.svelte";
|
||||
|
||||
interface Props {
|
||||
|
|
|
@ -1,50 +1,24 @@
|
|||
import BookTrackerPlugin from "@src/main";
|
||||
import type { ReadingLogEntry } from "@src/types";
|
||||
import { App } from "obsidian";
|
||||
import type { Storage } from "./Storage";
|
||||
|
||||
export class Storage {
|
||||
public constructor(
|
||||
private readonly app: App,
|
||||
private readonly plugin: BookTrackerPlugin
|
||||
) {}
|
||||
|
||||
private getFilePath(filename: string): string {
|
||||
return `${this.plugin.manifest.dir!!}/${filename}`;
|
||||
}
|
||||
|
||||
public async readJSON<T>(filename: string): Promise<T | null> {
|
||||
const filePath = this.getFilePath(filename);
|
||||
const content = await this.app.vault.adapter.read(filePath);
|
||||
if (!content) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(content) as T;
|
||||
} catch (error) {
|
||||
console.error(`Error parsing JSON from ${filePath}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async writeJSON<T>(filename: string, data: T): Promise<void> {
|
||||
const filePath = this.getFilePath(filename);
|
||||
const content = JSON.stringify(data, null, 2);
|
||||
await this.app.vault.adapter.write(filePath, content);
|
||||
}
|
||||
export interface ReadingLogEntry {
|
||||
book: string;
|
||||
pagesRead: number;
|
||||
pagesReadTotal: number;
|
||||
pagesRemaining: number;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
export class ReadingLog {
|
||||
private entries: ReadingLogEntry[] = [];
|
||||
|
||||
public constructor(private readonly plugin: BookTrackerPlugin) {
|
||||
public constructor(private readonly storage: Storage) {
|
||||
this.loadEntries().catch((error) => {
|
||||
console.error("Failed to load reading log entries:", error);
|
||||
});
|
||||
}
|
||||
|
||||
private async loadEntries() {
|
||||
const entries = await this.plugin.storage.readJSON<ReadingLogEntry[]>(
|
||||
const entries = await this.storage.readJSON<ReadingLogEntry[]>(
|
||||
"reading-log.json"
|
||||
);
|
||||
if (entries) {
|
||||
|
@ -63,7 +37,7 @@ export class ReadingLog {
|
|||
|
||||
private async storeEntries() {
|
||||
this.sortEntries();
|
||||
await this.plugin.storage.writeJSON("reading-log.json", this.entries);
|
||||
await this.storage.writeJSON("reading-log.json", this.entries);
|
||||
}
|
||||
|
||||
public getEntries(): ReadingLogEntry[] {
|
|
@ -0,0 +1,34 @@
|
|||
import BookTrackerPlugin from "@src/main";
|
||||
import { App } from "obsidian";
|
||||
|
||||
export class Storage {
|
||||
public constructor(
|
||||
private readonly app: App,
|
||||
private readonly plugin: BookTrackerPlugin
|
||||
) {}
|
||||
|
||||
private getFilePath(filename: string): string {
|
||||
return `${this.plugin.manifest.dir!!}/${filename}`;
|
||||
}
|
||||
|
||||
public async readJSON<T>(filename: string): Promise<T | null> {
|
||||
const filePath = this.getFilePath(filename);
|
||||
const content = await this.app.vault.adapter.read(filePath);
|
||||
if (!content) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(content) as T;
|
||||
} catch (error) {
|
||||
console.error(`Error parsing JSON from ${filePath}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async writeJSON<T>(filename: string, data: T): Promise<void> {
|
||||
const filePath = this.getFilePath(filename);
|
||||
const content = JSON.stringify(data, null, 2);
|
||||
await this.app.vault.adapter.write(filePath, content);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue