Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Fix asset saving and loading of projects #243

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/electron/lib/api/apis/ProjectApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CoreGraphUpdateEvent,
CoreGraphUpdateParticipant,
} from "../../core-graph/CoreGraphInteractors";
import { type CacheUUID } from "../../../../shared/types/cache";

export class ProjectApi implements ElectronMainApi<ProjectApi> {
constructor(private readonly blix: Blix) {}
Expand Down Expand Up @@ -57,8 +58,13 @@ export class ProjectApi implements ElectronMainApi<ProjectApi> {
// }

async closeProject(uuid: UUID, graphs?: UUID[]) {
const cacheUUIDs = this.blix.projectManager.getProject(uuid)?.getCacheIds() || [];
const res = await this.blix.projectManager.removeProject(this.blix, uuid);
if (res === -1 && graphs) this.blix.graphManager.deleteGraphs(graphs);

if (res === -1 && graphs) {
this.blix.cacheManager.deleteAssets(cacheUUIDs);
this.blix.graphManager.deleteGraphs(graphs);
}
}

async addCacheObjects(projectUUID: UUID, cacheUUIDs: UUID[]): Promise<IpcResponse<string>> {
Expand Down
8 changes: 6 additions & 2 deletions src/electron/lib/cache/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@
JSON.stringify({ success: true, messageId: data.messageId } as CacheResponse)
);

this.notifyListeners();
// this.notifyListeners();
break;
case "cache-delete-all":
this.deleteAssets(Object.keys(this.cache));
socket.send(
JSON.stringify({ success: true, messageId: data.messageId } as CacheResponse)
);
this.notifyListeners();
break;
case "cache-subscribe":
this.listeners.add(socket);
Expand Down Expand Up @@ -205,10 +204,15 @@
return this.cache[cacheUUID];
}

getUUIDs(): CacheUUID[] {
return Object.keys(this.cache);

Check warning on line 208 in src/electron/lib/cache/CacheManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/cache/CacheManager.ts#L208

Added line #L208 was not covered by tests
}

deleteAssets(cacheUUID: CacheUUID[]) {
for (const uuid of cacheUUID) {
delete this.cache[uuid];
}
this.notifyListeners();

Check warning on line 215 in src/electron/lib/cache/CacheManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/cache/CacheManager.ts#L215

Added line #L215 was not covered by tests
}

async export(ids: CacheUUID[]) {
Expand Down
4 changes: 4 additions & 0 deletions src/electron/lib/projects/CoreProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,44 +115,48 @@
}

public addCacheObjects(cacheUUIDs: UUID[]): IpcResponse<string> {
try {
this._cache = [...this._cache, ...cacheUUIDs];
return { success: true, data: "Asset successfully added" };

Check warning on line 120 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L118-L120

Added lines #L118 - L120 were not covered by tests
} catch (e: any) {
return { success: false, data: e };

Check warning on line 122 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L122

Added line #L122 was not covered by tests
}
}

public removeCacheObjects(cacheUUIDs: UUID[]): IpcResponse<string> {
try {
this._cache = this._cache.filter((id) => !cacheUUIDs.includes(id));
return { success: true, data: "Asset successfully removed" };

Check warning on line 129 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L127-L129

Added lines #L127 - L129 were not covered by tests
} catch (e: any) {
return { success: false, data: e };

Check warning on line 131 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L131

Added line #L131 was not covered by tests
}
}

public addMediaOutputs(mediaOutputIds: UUID[]): IpcResponse<string> {
try {
mediaOutputIds.forEach((id) => {

Check warning on line 137 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L136-L137

Added lines #L136 - L137 were not covered by tests
if (!this._mediaIds.includes(id)) {
this._mediaIds.push(id);

Check warning on line 139 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L139

Added line #L139 was not covered by tests
}
});
return { success: true, data: "Media output successfully added" };

Check warning on line 142 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L142

Added line #L142 was not covered by tests
} catch (e: any) {
return { success: false, data: e };

Check warning on line 144 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L144

Added line #L144 was not covered by tests
}
}

public removeMediaOutputs(mediaOutputIds: UUID[]): IpcResponse<string> {
try {
this._mediaIds = this._mediaIds.filter((id) => !mediaOutputIds.includes(id));
return { success: true, data: "Media output successfully removed" };

Check warning on line 151 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L149-L151

Added lines #L149 - L151 were not covered by tests
} catch (e: any) {
return { success: false, data: e };

Check warning on line 153 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L153

Added line #L153 was not covered by tests
}
}

public getCacheIds() {
return [...this._cache];

Check warning on line 158 in src/electron/lib/projects/CoreProject.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/CoreProject.ts#L158

Added line #L158 was not covered by tests
}
}

export interface ProjectFile {
Expand Down
3 changes: 3 additions & 0 deletions src/electron/lib/projects/ProjectCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@
layout: projectFile.layout,
});
updateRecentProjectsList(path);
for (const uuid of ctx.cacheManager.getUUIDs()) {
ctx.projectManager.addCacheObjects(projectId, [uuid]);

Check warning on line 333 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L332-L333

Added lines #L332 - L333 were not covered by tests
}
} else if (fileName === "cache/cache.json") {
const cacheJSON = JSON.parse(data.toString("utf-8")) as {
[key: string]: { uuid: CacheUUID; metadata: CacheMetadata };
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/ui/tiles/Assets.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<script lang="ts">
import type { CacheUUID } from "@shared/types/cache";
import { cacheStore } from "../../lib/stores/CacheStore";
import {
faBacon,
faBowlRice,
Expand All @@ -15,9 +13,11 @@
faLemon,
faPizzaSlice,
} from "@fortawesome/free-solid-svg-icons";
import type { CacheUUID } from "@shared/types/cache";
import Fa from "svelte-fa";
import { toastStore } from "../../lib/stores/ToastStore";
import { cacheStore } from "../../lib/stores/CacheStore";
import { projectsStore } from "../../lib/stores/ProjectStore";
import { toastStore } from "../../lib/stores/ToastStore";

let selectedCacheItems: CacheUUID[] = [];
// Not used at the moment
Expand Down
28 changes: 14 additions & 14 deletions src/frontend/ui/tiles/Media.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
<!-- This pane is for showing media content large-scale -->
<script lang="ts">
import Image from "../utils/mediaDisplays/Image.svelte";
import TextBox from "../utils/mediaDisplays/TextBox.svelte";
import { mediaStore } from "../../lib/stores/MediaStore";
import { writable, type Readable, derived, get } from "svelte/store";
import { MediaDisplayType, type DisplayableMediaOutput } from "@shared/types/media";
import { onDestroy } from "svelte";
import ColorDisplay from "../utils/mediaDisplays/ColorDisplay.svelte";
import SelectionBox from "../utils/graph/SelectionBox.svelte";
import { type SelectionBoxItem } from "../../types/selection-box";
import WebView from "./WebView.svelte";
import { TweakApi } from "../../lib/webview/TweakApi";
import {
faBacon,
faBowlRice,
Expand All @@ -25,9 +14,20 @@
faLemon,
faPizzaSlice,
} from "@fortawesome/free-solid-svg-icons";
import { MediaDisplayType, type DisplayableMediaOutput } from "@shared/types/media";
import { onDestroy } from "svelte";
import Fa from "svelte-fa";
import { toastStore } from "../../lib/stores/ToastStore";
import { derived, writable, type Readable } from "svelte/store";
import { mediaStore } from "../../lib/stores/MediaStore";
import { projectsStore } from "../../lib/stores/ProjectStore";
import { toastStore } from "../../lib/stores/ToastStore";
import { TweakApi } from "../../lib/webview/TweakApi";
import { type SelectionBoxItem } from "../../types/selection-box";
import SelectionBox from "../utils/graph/SelectionBox.svelte";
import ColorDisplay from "../utils/mediaDisplays/ColorDisplay.svelte";
import Image from "../utils/mediaDisplays/Image.svelte";
import TextBox from "../utils/mediaDisplays/TextBox.svelte";
import WebView from "./WebView.svelte";

export let projectId: string;

Expand Down Expand Up @@ -61,7 +61,7 @@
([$projectsStore, $mediaOutputList]) => {
const project = $projectsStore.projects.find((p) => p.id === projectId);

console.log(projectId.slice(0, 6), $mediaOutputList);
// console.log(projectId.slice(0, 6), $mediaOutputList);

if (!project) {
return [];
Expand All @@ -80,7 +80,7 @@
id: media.nodeId,
title: media.mediaId,
}));
console.log(projectId.slice(0, 6), selectedItems);
// console.log(projectId.slice(0, 6), selectedItems);
});

$: selectedMediaId.set(selectedItems.find((item) => item.id === selectedItemId)?.title ?? "");
Expand Down
13 changes: 8 additions & 5 deletions src/frontend/ui/tiles/WebView.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!-- Renders a custom webview defined, for instance, by a plugin -->
<script lang="ts">
import { createEventDispatcher } from "svelte";
import TextBox from "../../ui/utils/mediaDisplays/TextBox.svelte";
import { createEventDispatcher, onDestroy } from "svelte";
import { type RendererId } from "../../../shared/types/typeclass";
import type { TweakApi } from "../../lib/webview/TweakApi";
import { onDestroy } from "svelte";
import { blixStore } from "../../lib/stores/BlixStore";
import { projectsStore } from "../../lib/stores/ProjectStore";
import type { TweakApi } from "../../lib/webview/TweakApi";
import TextBox from "../../ui/utils/mediaDisplays/TextBox.svelte";

let webview: Electron.WebviewTag | null = null;

Expand Down Expand Up @@ -38,7 +38,10 @@
}
break;
case "exportResponse":
// dispatch("exportResponse", event.args);
const id = event.args[0] as { cacheUUID: string };
if ("cacheUUID" in id && $projectsStore.activeProject) {
window.apis.projectApi.addCacheObjects($projectsStore.activeProject.id, [id.cacheUUID]);
}
break;
}
});
Expand Down
Loading