Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

chore: code cleanup #216

Merged
merged 4 commits into from
Apr 11, 2024
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
27 changes: 4 additions & 23 deletions src/app/effects/app.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {MatDialog} from "@angular/material/dialog";
import {ChangeLogDialog} from "../modules/core/dialogs/change-log/change-log.dialog";
import showdown from "showdown";
import {WorkspaceService} from "../services/workspace.service";
import * as Blockly from "blockly/core";
import {VariableDialog} from "../modules/core/dialogs/variable/variable.dialog";

@Injectable({
providedIn: 'root',
Expand Down Expand Up @@ -38,9 +36,9 @@ export class AppEffects {
.pipe(filter(isToggled => !!isToggled), withLatestFrom(this.appState.codeEditor$))
.subscribe(([, codeEditorType]) => {
if (codeEditorType == CodeEditorType.Beginner) {
this.appState.setSelectedCodeEditor(CodeEditorType.CPP);
this.appState.selectedCodeEditor = CodeEditorType.CPP;
} else if (codeEditorType == CodeEditorType.CPP) {
this.appState.setSelectedCodeEditor(CodeEditorType.Beginner);
this.appState.selectedCodeEditor = CodeEditorType.Beginner;
}
});

Expand All @@ -61,15 +59,15 @@ export class AppEffects {
default:
break;
}
this.appState.setIsCodeEditorToggleConfirmed(false);
this.appState.isCodeEditorToggleConfirmed = false;
});



this.appState.releaseInfo$
.pipe(filter(releaseInfo => !!releaseInfo))
.subscribe(releaseInfo => {
const releaseVersion = this.appState.getReleaseVersion();
const releaseVersion = this.appState.releaseVersion;
if (!releaseVersion) {
return;
}
Expand Down Expand Up @@ -128,23 +126,6 @@ export class AppEffects {
}
})

this.appState.isDesktop$
.pipe(filter(isDesktop => !!isDesktop))
.subscribe(() => {
try {
Blockly.dialog.setPrompt((msg, defaultValue, callback) => {
this.dialog.open(VariableDialog, {
width: '400px',
data: { name: defaultValue }
}).afterClosed().subscribe(result => {
callback(result);
});
});
} catch (e) {
console.log(e);
throw e;
}
});
}


Expand Down
35 changes: 14 additions & 21 deletions src/app/effects/blockly-editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class BlocklyEditorEffects {
.pipe(withLatestFrom(this.blocklyState.workspaceJSON$, this.appState.selectedRobotType$))
.subscribe(([, workspaceXml]) => {
this.workspaceService.saveWorkspaceTemp(workspaceXml).then(() => {});
this.localStorage.store("changedLanguage", this.appState.getSelectedRobotType().id);
this.localStorage.store("changedLanguage", this.appState.selectedRobotType.id);
});

// When all prerequisites are there, Create a new workspace and open the codeview if needed
Expand All @@ -75,7 +75,7 @@ export class BlocklyEditorEffects {
this.getXmlContent('./assets/blockly/leaphy-start.xml')
))
.subscribe(([[[element, config], robotType], baseToolboxXml, leaphyToolboxXml, startWorkspaceXml]) => {
const leaphyBlocks = getBlocks(this.appState.getSelectedRobotType().id);
const leaphyBlocks = getBlocks(this.appState.selectedRobotType.id);
Blockly.defineBlocksWithJsonArray(leaphyBlocks.block)
config.theme = Blockly.Theme.defineTheme('leaphy', {
'blockStyles': THEME.defaultBlockStyles,
Expand All @@ -92,21 +92,21 @@ export class BlocklyEditorEffects {
toolbox.getFlyout().autoClose = false;
const xml = Blockly.utils.xml.textToDom(startWorkspaceXml);
Blockly.Xml.domToWorkspace(xml, workspace);
this.blocklyState.setWorkspace(workspace);
this.blocklyState.setToolboxXml(toolboxXmlString);
if (this.appState.getCurrentEditor() == CodeEditorType.Beginner) {
this.blocklyState.workspace = workspace;
this.blocklyState.toolboxXml = toolboxXmlString;
if (this.appState.currentEditor == CodeEditorType.Beginner) {
this.workspaceService.restoreWorkspaceTemp().then(() => {});
}
toolbox.selectItemByPosition(0);
toolbox.refreshTheme();

setTimeout(() => this.blocklyState.setIsSideNavOpen(robotType.features.showCodeOnStart), 200);
setTimeout(() => this.blocklyState.isSideNavOpen = robotType.features.showCodeOnStart, 200);
});

// When a new project is started, reset the blockly code
this.appState.selectedRobotType$
.pipe(filter(robotType => !robotType))
.subscribe(() => this.codeEditorState.setCode(''))
.subscribe(() => this.codeEditorState.code = '')

// When the robot selection changes, set the toolbox and initialWorkspace
this.appState.selectedRobotType$
Expand All @@ -119,7 +119,7 @@ export class BlocklyEditorEffects {
))
.subscribe(([[robotType, workspace], baseToolboxXml, leaphyToolboxXml, startWorkspaceXml]) => {
const toolboxXmlString = this.loadToolBox(baseToolboxXml, leaphyToolboxXml, robotType);
this.blocklyState.setToolboxXml(toolboxXmlString);
this.blocklyState.toolboxXml = toolboxXmlString;

workspace.clear();
const xml = Blockly.utils.xml.textToDom(startWorkspaceXml);
Expand All @@ -139,8 +139,8 @@ export class BlocklyEditorEffects {
workspace.clearUndo();
workspace.addChangeListener(Blockly.Events.disableOrphans);
workspace.addChangeListener(async () => {
this.codeEditorState.setCode(arduino.workspaceToCode(workspace, this.appState.getSelectedRobotType().id));
this.blocklyState.setWorkspaceJSON(JSON.stringify(Blockly.serialization.workspaces.save(workspace)));
this.codeEditorState.code = arduino.workspaceToCode(workspace, this.appState.selectedRobotType.id);
this.blocklyState.workspaceJSON = JSON.stringify(Blockly.serialization.workspaces.save(workspace));
});
});

Expand All @@ -158,21 +158,14 @@ export class BlocklyEditorEffects {
filter(([previous, current]) => (current === CodeEditorType.CPP || current === CodeEditorType.Python ) && current !== previous)
)
.subscribe(() => {
this.blocklyState.setIsSideNavOpen(false);
});

// Toggle the isSideNavOpen state
this.blocklyState.isSideNavOpenToggled$
.pipe(filter(isToggled => !!isToggled), withLatestFrom(this.blocklyState.isSideNavOpen$))
.subscribe(([, isSideNavOpen]) => {
this.blocklyState.setIsSideNavOpen(!isSideNavOpen);
this.blocklyState.isSideNavOpen = false;
});

// Toggle the isSoundOn state
this.blocklyState.isSoundToggled$
.pipe(filter(isToggled => !!isToggled), withLatestFrom(this.blocklyState.isSoundOn$))
.subscribe(([, isSoundOn]) => {
this.blocklyState.setIsSoundOn(!isSoundOn);
this.blocklyState.isSoundOn = !isSoundOn;
});

// When the sound is turned on off, update the Blockly function
Expand All @@ -181,7 +174,7 @@ export class BlocklyEditorEffects {
.subscribe(([isSoundOn, basePlay]) => {
if (!basePlay) {
basePlay = Blockly.WorkspaceAudio.prototype.play;
this.blocklyState.setPlaySoundFunction(basePlay);
this.blocklyState.playSoundFunction = basePlay;
}
Blockly.WorkspaceAudio.prototype.play = function (name, opt_volume) {
if (isSoundOn) {
Expand All @@ -192,7 +185,7 @@ export class BlocklyEditorEffects {

// When the code editor is changed, clear the projectFilePath
this.appState.codeEditor$
.subscribe(() => this.blocklyState.setProjectFileHandle(null));
.subscribe(() => this.blocklyState.projectFileHandle = null);
}

private parseCategory(root: Document, category: HTMLElement, robotType: RobotType,) : HTMLElement {
Expand Down
6 changes: 3 additions & 3 deletions src/app/effects/code-editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class CodeEditorEffects {
this.appState.codeEditor$
.subscribe(codeEditor => {
if (codeEditor == CodeEditorType.Python) {
this.codeEditorState.setCode(this.codeEditorState.pythonProgram);
} else if (codeEditor == CodeEditorType.CPP && this.appState.getSelectedRobotType() == genericRobotType) {
this.codeEditorState.setCode(this.codeEditorState.originalProgram);
this.codeEditorState.code = `from leaphymicropython.utils.pins import set_pwm`;
} else if (codeEditor == CodeEditorType.CPP && this.appState.selectedRobotType == genericRobotType) {
this.codeEditorState.code = this.codeEditorState.originalProgram;
sverben marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/app/effects/dialog.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ export class DialogEffects {
// If the isSerialOutputWindowOpen is set to true open the dialog
this.dialogState.isSerialOutputWindowOpen$
.subscribe(() => {
if (this.dialogState.getIsSerialOutputWindowOpen() !== true)
if (this.dialogState.isSerialOutputWindowOpen !== true)
return;
this.dialog.open(SerialOutputComponent, {
width: "800px",
disableClose: true,
hasBackdrop: false,
}).afterClosed().subscribe(() => {
this.dialogState.setIsSerialOutputWindowOpen(false);
this.dialogState.isSerialOutputWindowOpen = false;
});
});


// If the isLibraryManagerWindowOpen is set to true open the dialog
this.dialogState.isLibraryManagerWindowOpen$
.subscribe(() => {
if (this.dialogState.getIsLibraryManagerWindowOpen() !== true)
if (this.dialogState.isLibraryManagerWindowOpen !== true)
return;
this.dialog.open(LibraryManagerComponent, {
disableClose: true,
}).afterClosed().subscribe(() => {
this.dialogState.setIsLibraryManagerWindowOpen(false);
this.dialogState.isLibraryManagerWindowOpen = false;
});
});

Expand Down
5 changes: 2 additions & 3 deletions src/app/effects/robot.wired.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class RobotWiredEffects {
this.dialogState.isSerialOutputListening$
.pipe(filter(isListening => !!isListening))
.subscribe(async () => {
if (this.robotWiredState.getPythonDeviceConnected())
if (this.robotWiredState.pythonDeviceConnected)
return;
const robotWiredState = this.robotWiredState;

Expand All @@ -38,8 +38,7 @@ export class RobotWiredEffects {
const date = new Date();

function makeString (chunkedStr: string) {
const serialData = { time: date, data: chunkedStr };
robotWiredState.setIncomingSerialData(serialData);
robotWiredState.incomingSerialData = { time: date, data: chunkedStr };
}

let i = this.logBuffer.indexOf("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class LeaphyBlocklyComponent implements AfterViewInit {
}

ngAfterViewInit() {
this.blocklyState.setBlocklyElement(this.blockContent.nativeElement);
this.blocklyState.blocklyElement = this.blockContent.nativeElement;
this.blocklyState.workspace$.subscribe(workspace => {
this.workspace = workspace
workspace.addChangeListener((event: any) => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/code-editor-cpp/code-editor-cpp.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class="monaco-editor"
[options]="editorOptions"
[ngModel]="codeEditorState.code$ | async"
(ngModelChange)="codeEditorState.setCode($event)"
(ngModelChange)="codeEditorState.code = $event"
/>
<app-button-bar></app-button-bar>
</div>
2 changes: 1 addition & 1 deletion src/app/modules/code-editor-cpp/code-editor-cpp.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CodeEditorCppPage implements AfterViewInit {

ngAfterViewInit(): void {
window.addEventListener("beforeunload", async () => {
await this.workspaceService.saveWorkspaceTemp(this.codeEditorState.getCode());
await this.workspaceService.saveWorkspaceTemp(this.codeEditorState.code);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class="monaco-editor"
[options]="editorOptions"
[ngModel]="codeEditorState.code$ | async"
(ngModelChange)="codeEditorState.setCode($event)"
(ngModelChange)="codeEditorState.code = ($event)"
/>

<app-button-bar></app-button-bar>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CodeEditorPythonPage implements AfterViewInit {

ngAfterViewInit(): void {
window.addEventListener("beforeunload", async () => {
await this.workspaceService.saveWorkspaceTemp(this.codeEditorState.getCode());
await this.workspaceService.saveWorkspaceTemp(this.codeEditorState.code);
})
}
}
4 changes: 2 additions & 2 deletions src/app/modules/components/header/header.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</ng-container>
</div>

<div *ngIf="(this.appState.selectedRobotType$ | async) === null" class="version-number">{{ (appState.releaseVersion$ | async) }}</div>
<div *ngIf="(this.appState.selectedRobotType$ | async) === null" class="version-number">{{ (appState.releaseVersion) }}</div>
</div>
</mat-toolbar>

Expand Down Expand Up @@ -133,7 +133,7 @@
</mat-menu>

<mat-menu #languageMenu="matMenu" class="matmenus">
<button mat-menu-item *ngFor="let language of appState.availableLanguages$ | async" [value]="language"
<button mat-menu-item *ngFor="let language of AppState.availableLanguages" [value]="language"
(click)="onLanguageChanged(language)">
<span [class.selected]="language.code === (appState.currentLanguage$ | async)?.code">{{language.name}}</span>
</button>
Expand Down
Loading
Loading