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

Commit

Permalink
Merge pull request #243 from COS301-SE-2023/fix/assest
Browse files Browse the repository at this point in the history
Fix asset saving and loading of projects
  • Loading branch information
ArmandKrynauw authored Oct 26, 2023
2 parents ba1b799 + d1cf161 commit e846523
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
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 @@ export class CacheManager {
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 @@ export class CacheManager {
return this.cache[cacheUUID];
}

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

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

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 @@ -153,6 +153,10 @@ export class CoreProject extends UniqueEntity {
return { success: false, data: e };
}
}

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

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 @@ export async function openProject(ctx: CommandContext, path?: string): Promise<C
layout: projectFile.layout,
});
updateRecentProjectsList(path);
for (const uuid of ctx.cacheManager.getUUIDs()) {
ctx.projectManager.addCacheObjects(projectId, [uuid]);
}
} 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

0 comments on commit e846523

Please sign in to comment.