Skip to content

Commit

Permalink
Massive refactor in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
neilenns committed Jul 28, 2024
1 parent 6e6a2cb commit 54248c7
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 319 deletions.
34 changes: 10 additions & 24 deletions src/controllers/atisLetter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AtisLetterSettings } from "@actions/atisLetter";
import { Action } from "@elgato/streamdeck";
import { Controller } from "@interfaces/controller";
import { CompiledSvgTemplate, compileSvg } from "@root/utils/svg";
import TitleBuilder from "@root/utils/titleBuilder";
import { BaseController } from "./baseController";
import { stringOrUndefined } from "@root/utils/utils";
import svgManager from "@managers/svg";

const StateColor = {
CURRENT: "black",
Expand Down Expand Up @@ -32,11 +32,6 @@ export class AtisLetterController extends BaseController {
private _unavailableImagePath?: string;
private _updatedImagePath?: string;

// Pre-compiled action SVGs
private _compiledCurrentSvg: CompiledSvgTemplate;
private _compiledUnavailableSvg: CompiledSvgTemplate;
private _compiledUpdatedSvg: CompiledSvgTemplate;

/**
* Creates a new StationStatusController object.
* @param action The callsign for the action
Expand Down Expand Up @@ -102,10 +97,8 @@ export class AtisLetterController extends BaseController {
* Sets the currentImagePath and re-compiles the SVG template if necessary.
*/
set currentImagePath(newValue: string | undefined) {
if (!this._compiledCurrentSvg || this.currentImagePath !== newValue) {
this._currentImagePath = stringOrUndefined(newValue);
this._compiledCurrentSvg = compileSvg(this.currentImagePath);
}
this._currentImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.currentImagePath);
}

/**
Expand All @@ -120,10 +113,8 @@ export class AtisLetterController extends BaseController {
* Sets the updatedImagePath and re-compiles the SVG template if necessary.
*/
set updatedImagePath(newValue: string | undefined) {
if (!this._compiledUpdatedSvg || this.updatedImagePath !== newValue) {
this._updatedImagePath = stringOrUndefined(newValue);
this._compiledUpdatedSvg = compileSvg(this.updatedImagePath);
}
this._updatedImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.updatedImagePath);
}

/**
Expand All @@ -138,13 +129,8 @@ export class AtisLetterController extends BaseController {
* Sets the unavailableImagePath and re-compiles the SVG template if necessary.
*/
set unavailableImagePath(newValue: string | undefined) {
if (
!this._compiledUnavailableSvg ||
this.unavailableImagePath !== newValue
) {
this._unavailableImagePath = stringOrUndefined(newValue);
this._compiledUnavailableSvg = compileSvg(this.unavailableImagePath);
}
this._unavailableImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.unavailableImagePath);
}

/**
Expand Down Expand Up @@ -247,22 +233,22 @@ export class AtisLetterController extends BaseController {
};

if (this.isUnavailable) {
this.setImage(this.unavailableImagePath, this._compiledUnavailableSvg, {
this.setImage(this.unavailableImagePath, {
...replacements,
stateColor: StateColor.CURRENT,
});
return;
}

if (this.isUpdated) {
this.setImage(this.updatedImagePath, this._compiledUpdatedSvg, {
this.setImage(this.updatedImagePath, {
...replacements,
stateColor: StateColor.UPDATED,
});
return;
}

this.setImage(this.currentImagePath, this._compiledCurrentSvg, {
this.setImage(this.currentImagePath, {
...replacements,
stateColor: StateColor.CURRENT,
});
Expand Down
27 changes: 11 additions & 16 deletions src/controllers/baseController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Action } from "@elgato/streamdeck";
import { Controller } from "@interfaces/controller";
import svgManager from "@managers/svg";
import { handleAsyncException } from "@root/utils/handleAsyncException";
import {
CompiledSvgTemplate,
generateSvgForSetImage,
isSvg,
Replacements,
} from "@root/utils/svg";
import { generateSvgForSetImage } from "@root/utils/svg";

/**
* Base implementation for a Controller that includes methods for
Expand Down Expand Up @@ -48,20 +44,19 @@ export abstract class BaseController implements Controller {
}

/**
* Sets the image on the tracked action after populating
* an SVG template with replacements, catching any exceptions
* that might occur.
* @param template The SVG template to populate
* Sets the image on the tracked action. If the image is a stored
* SVG template then the template is populated and used. Otherwise
* the path is used directly.
* @param imagePath The path to the image
* @param replacements The replacements to use
*/
setImage(
imagePath: string | undefined,
template: CompiledSvgTemplate,
replacements: Replacements
) {
setImage(imagePath: string | undefined, replacements: object) {
// Check to see if a compiled template exists
const template = svgManager.getTemplate(imagePath);

// Check and see if the image is an SVG. If so use the template, if not
// just pass the path and let StreamDeck do the rendering.
if (isSvg(imagePath)) {
if (template) {
const generatedSvg = generateSvgForSetImage(template, replacements);

Check failure on line 60 in src/controllers/baseController.ts

View workflow job for this annotation

GitHub Actions / build

Unsafe assignment of an error typed value

Check failure on line 60 in src/controllers/baseController.ts

View workflow job for this annotation

GitHub Actions / build

Unsafe call of an `error` type typed value
this.action.setImage(generatedSvg).catch((error: unknown) => {

Check failure on line 61 in src/controllers/baseController.ts

View workflow job for this annotation

GitHub Actions / build

Unsafe argument of type `any` assigned to a parameter of type `string | undefined`
handleAsyncException("Unable to set state image: ", error);
Expand Down
78 changes: 23 additions & 55 deletions src/controllers/hotline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { HotlineSettings } from "@actions/hotline";
import { Action } from "@elgato/streamdeck";
import { Controller } from "@interfaces/controller";
import { BaseController } from "./baseController";
import { CompiledSvgTemplate, compileSvg } from "@root/utils/svg";
import { stringOrUndefined } from "@root/utils/utils";
import TitleBuilder from "@root/utils/titleBuilder";
import svgManager from "@managers/svg";

const StateColor = {
NEITHER_ACTIVE: "#000",
Expand Down Expand Up @@ -42,14 +42,6 @@ export class HotlineController extends BaseController {
private _neitherActiveImagePath?: string;
private _receivingImagePath?: string;

// Pre-compiled action SVGs
private _compiledBothActiveSvg: CompiledSvgTemplate;
private _compiledUnavailableSvg: CompiledSvgTemplate;
private _compiledHotlineActiveSvg: CompiledSvgTemplate;
private _compiledListeningSvg: CompiledSvgTemplate;
private _compiledNeitherActiveSvg: CompiledSvgTemplate;
private _compiledReceivingSvg: CompiledSvgTemplate;

/**
* Creates a new HotlineController object.
* @param action The callsign for the action
Expand Down Expand Up @@ -101,10 +93,8 @@ export class HotlineController extends BaseController {
* Sets the bothActiveImagePath and re-compiles the SVG template if necessary.
*/
set bothActiveImagePath(newValue: string | undefined) {
if (!this._compiledBothActiveSvg || this.bothActiveImagePath !== newValue) {
this._bothActiveImagePath = stringOrUndefined(newValue);
this._compiledBothActiveSvg = compileSvg(this.bothActiveImagePath);
}
this._bothActiveImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.bothActiveImagePath);
}

/**
Expand All @@ -119,13 +109,8 @@ export class HotlineController extends BaseController {
* Sets the unavailableImagePath and re-compiles the SVG template if necessary.
*/
set unavailableImagePath(newValue: string | undefined) {
if (
!this._compiledUnavailableSvg ||
this.unavailableImagePath !== newValue
) {
this._unavailableImagePath = stringOrUndefined(newValue);
this._compiledUnavailableSvg = compileSvg(this.unavailableImagePath);
}
this._unavailableImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.unavailableImagePath);
}

/**
Expand All @@ -140,13 +125,8 @@ export class HotlineController extends BaseController {
* Sets the hotlineActiveImagePath and re-compiles the SVG template if necessary.
*/
set hotlineActiveImagePath(newValue: string | undefined) {
if (
!this._compiledHotlineActiveSvg ||
this.hotlineActiveImagePath !== newValue
) {
this._hotlineActiveImagePath = stringOrUndefined(newValue);
this._compiledHotlineActiveSvg = compileSvg(this.hotlineActiveImagePath);
}
this._hotlineActiveImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.hotlineActiveImagePath);
}

/**
Expand All @@ -161,10 +141,8 @@ export class HotlineController extends BaseController {
* Sets the listeningImagePath and re-compiles the SVG template if necessary.
*/
set listeningImagePath(newValue: string | undefined) {
if (!this._compiledListeningSvg || this.listeningImagePath !== newValue) {
this._listeningImagePath = stringOrUndefined(newValue);
this._compiledListeningSvg = compileSvg(this.listeningImagePath);
}
this._listeningImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.listeningImagePath);
}

/**
Expand All @@ -179,13 +157,8 @@ export class HotlineController extends BaseController {
* Sets the neitherActiveImagePath and re-compiles the SVG template if necessary.
*/
set neitherActiveImagePath(newValue: string | undefined) {
if (
!this._compiledNeitherActiveSvg ||
this.neitherActiveImagePath !== newValue
) {
this._neitherActiveImagePath = stringOrUndefined(newValue);
this._compiledNeitherActiveSvg = compileSvg(this.neitherActiveImagePath);
}
this._neitherActiveImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.neitherActiveImagePath);
}

/**
Expand All @@ -200,11 +173,10 @@ export class HotlineController extends BaseController {
* Sets the receivingImagePath and re-compiles the SVG template if necessary.
*/
set receivingImagePath(newValue: string | undefined) {
if (!this._compiledReceivingSvg || this.receivingImagePath !== newValue) {
this._receivingImagePath = stringOrUndefined(newValue);
this._compiledReceivingSvg = compileSvg(this.receivingImagePath);
}
this._receivingImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.receivingImagePath);
}

/**
* Returns the frequency for the primary callsign.
*/
Expand Down Expand Up @@ -408,7 +380,7 @@ export class HotlineController extends BaseController {
};

if (this.isAvailable !== undefined && !this.isAvailable) {
this.setImage(this.unavailableImagePath, this._compiledUnavailableSvg, {
this.setImage(this.unavailableImagePath, {
...replacements,
stateColor: StateColor.UNAVAILABLE,
});
Expand All @@ -418,7 +390,7 @@ export class HotlineController extends BaseController {
// This state is bad, it means Tx is cross coupled
// on both hotline and primary.
if (this.isTxHotline && this.isTxPrimary) {
this.setImage(this.bothActiveImagePath, this._compiledBothActiveSvg, {
this.setImage(this.bothActiveImagePath, {
...replacements,
stateColor: StateColor.BOTH_ACTIVE,
});
Expand All @@ -427,19 +399,15 @@ export class HotlineController extends BaseController {

// Hotline is active tx, takes priority over active rx.
if (this.isTxHotline) {
this.setImage(
this.hotlineActiveImagePath,
this._compiledHotlineActiveSvg,
{
...replacements,
stateColor: StateColor.HOTLINE_ACTIVE,
}
);
this.setImage(this.hotlineActiveImagePath, {
...replacements,
stateColor: StateColor.HOTLINE_ACTIVE,
});
return;
}

if (this.isReceiving) {
this.setImage(this.receivingImagePath, this._compiledReceivingSvg, {
this.setImage(this.receivingImagePath, {
...replacements,
stateColor: StateColor.RECEIVING,
});
Expand All @@ -448,15 +416,15 @@ export class HotlineController extends BaseController {

// Primary active rx.
if (this.isRxHotline) {
this.setImage(this.listeningImagePath, this._compiledReceivingSvg, {
this.setImage(this.listeningImagePath, {
...replacements,
stateColor: StateColor.LISTENING,
});
return;
}

// Nothing is active.
this.setImage(this.neitherActiveImagePath, this._compiledNeitherActiveSvg, {
this.setImage(this.neitherActiveImagePath, {
...replacements,
stateColor: StateColor.NEITHER_ACTIVE,
});
Expand Down
37 changes: 9 additions & 28 deletions src/controllers/pushToTalk.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Action } from "@elgato/streamdeck";
import { Controller } from "@interfaces/controller";
import { BaseController } from "./baseController";
import { CompiledSvgTemplate, compileSvg } from "@root/utils/svg";
import { PushToTalkSettings } from "@actions/pushToTalk";
import TitleBuilder from "@root/utils/titleBuilder";
import { stringOrUndefined } from "@root/utils/utils";
import svgManager from "@managers/svg";

const StateColor = {
NOT_TRANSMITTING: "black",
Expand All @@ -26,9 +26,6 @@ export class PushToTalkController extends BaseController {
private _notTransmittingImagePath?: string;
private _transmittingImagePath?: string;

private _compiledNotTransmittingSvg: CompiledSvgTemplate;
private _compiledTransmittingSvg: CompiledSvgTemplate;

/**
* Creates a new PushToTalkController object.
* @param action The callsign for the action
Expand Down Expand Up @@ -61,13 +58,8 @@ export class PushToTalkController extends BaseController {
* Sets the transmittingImagePath and re-compiles the SVG template if necessary.
*/
set transmittingImagePath(newValue: string | undefined) {
if (
!this._compiledTransmittingSvg ||
this.transmittingImagePath !== newValue
) {
this._transmittingImagePath = stringOrUndefined(newValue);
this._compiledTransmittingSvg = compileSvg(this.transmittingImagePath);
}
this._transmittingImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.transmittingImagePath);
}

/**
Expand All @@ -82,15 +74,8 @@ export class PushToTalkController extends BaseController {
* Sets the notTransmittingImagePath and re-compiles the SVG template if necessary.
*/
set notTransmittingImagePath(newValue: string | undefined) {
if (
!this._compiledNotTransmittingSvg ||
this.notTransmittingImagePath !== newValue
) {
this._notTransmittingImagePath = stringOrUndefined(newValue);
this._compiledNotTransmittingSvg = compileSvg(
this.notTransmittingImagePath
);
}
this._notTransmittingImagePath = stringOrUndefined(newValue);
svgManager.addTemplate(this.notTransmittingImagePath);
}

/**
Expand Down Expand Up @@ -164,19 +149,15 @@ export class PushToTalkController extends BaseController {
*/
public refreshImage() {
if (this.isTransmitting) {
this.setImage(this.transmittingImagePath, this._compiledTransmittingSvg, {
this.setImage(this.transmittingImagePath, {
stateColor: StateColor.TRANSMITTING,
});
return;
}

this.setImage(
this.notTransmittingImagePath,
this._compiledNotTransmittingSvg,
{
stateColor: StateColor.NOT_TRANSMITTING,
}
);
this.setImage(this.notTransmittingImagePath, {
stateColor: StateColor.NOT_TRANSMITTING,
});
}
}

Expand Down
Loading

0 comments on commit 54248c7

Please sign in to comment.