Skip to content

Commit

Permalink
Limit property updates of custom_repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Jan 1, 2020
1 parent 667d39b commit 596ce96
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 38 deletions.
21 changes: 16 additions & 5 deletions src/HacsFrontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
property,
TemplateResult
} from "lit-element";
import { HACS } from "./Hacs";
import { HACS, Hacs } from "./Hacs";
import { HacsStyle } from "./style/hacs-style";

import {
Expand All @@ -20,7 +20,8 @@ import {
Critical,
SelectedValue,
LocationChangedEvent,
LovelaceConfig
LovelaceConfig,
RepositoryActionData
} from "./types";

import { load_lovelace } from "card-tools/src/hass";
Expand Down Expand Up @@ -52,12 +53,21 @@ class HacsFrontendBase extends LitElement {
if (this.hacs.isnullorempty(repositories))
repositories = this.hacs.repositories;
if (this.hacs.isnullorempty(status)) status = this.hacs.status;
this.hacs.configuration = configuration;
this.hacs.repositories = repositories;
this.hacs.status = status;
this.hacs = new Hacs(configuration, repositories, status);
this.requestUpdate();
}

private RepositoryAction(ev): void {
console.log(ev.detail);
const evdata: RepositoryActionData = ev.detail;
this.hacs.RepositoryWebSocketAction(
this.hass,
evdata.repo,
evdata.action,
evdata.category
);
}

public getRepositories(): void {
this.hass.connection
.sendMessagePromise({
Expand Down Expand Up @@ -144,6 +154,7 @@ class HacsFrontendBase extends LitElement {

protected firstUpdated() {
this.addEventListener("hacs-location-change", this.locationChanged);
this.addEventListener("hacs-repository-action", this.RepositoryAction);
this.addEventListener("hacs-onboarding-done", this.onboardingDone);
this.addEventListener("hacs-recreate", this._recreatehacs);
this.addEventListener("hacs-force-reload", this._reload);
Expand Down
66 changes: 51 additions & 15 deletions src/misc/CustomRepositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {
LitElement,
customElement,
property,
PropertyValues,
CSSResultArray,
css,
TemplateResult,
html
} from "lit-element";
import swal from "sweetalert";
import { HomeAssistant } from "custom-card-helpers";

import { HacsStyle } from "../style/hacs-style";
import { HACS } from "../Hacs";
Expand All @@ -17,9 +17,39 @@ import { Route, RepositoryData } from "../types";
@customElement("hacs-custom-repositories")
export class CustomRepositories extends LitElement {
@property({ type: Object }) public hacs!: HACS;
@property({ type: Array }) public custom!: RepositoryData[];
@property({ type: Object }) public hass!: HomeAssistant;
@property({ type: Object }) public route!: Route;
@property({ type: Boolean }) private background_task: boolean = true;
@property({ type: Array }) private custom!: RepositoryData[];

protected update(changedProperties: PropertyValues): void {
super.update(changedProperties);
console.log(changedProperties);
}

shouldUpdate(changedProperties: PropertyValues) {
changedProperties.forEach((_oldValue, propName) => {
if (propName === "hacs") {
this.background_task = this.hacs.status.background_task;
const customrepositories = this.getCustomRepositories();
if (!this.custom || this.custom.length !== customrepositories.length) {
this.custom = customrepositories;
}
}
});
return (
changedProperties.has("custom") ||
changedProperties.has("background_task")
);
}

getCustomRepositories(): RepositoryData[] {
return this.hacs.repositories
.sort((a, b) => (a.full_name > b.full_name ? 1 : -1))
.filter(repo => {
if (repo.custom) return true;
else return false;
});
}

Delete(ev) {
let RepoID: string;
Expand All @@ -37,7 +67,13 @@ export class CustomRepositories extends LitElement {
]
}).then(value => {
if (!this.hacs.isnullorempty(value)) {
this.hacs.RepositoryWebSocketAction(this.hass, RepoID, "delete");
this.dispatchEvent(
new CustomEvent("hacs-repository-action", {
detail: { repo: RepoID, action: "delete" },
bubbles: true,
composed: true
})
);
}
});
}
Expand All @@ -62,7 +98,13 @@ export class CustomRepositories extends LitElement {
}
var category = selected.category;
var repo = ev.composedPath()[2].children[0].value;
this.hacs.RepositoryWebSocketAction(this.hass, repo, "add", category);
this.dispatchEvent(
new CustomEvent("hacs-repository-action", {
detail: { repo: repo, action: "add", category: category },
bubbles: true,
composed: true
})
);
swal(
this.hacs.localize("settings.adding_new_repo", "{repo}", repo) +
"\n" +
Expand Down Expand Up @@ -100,26 +142,20 @@ export class CustomRepositories extends LitElement {
);
return html``;
}

this.custom = this.hacs.repositories.filter(function(repo) {
if (!repo.custom) return false;
return true;
});

console.log(`Render! ${new Date()}`);
return html`
<ha-card header="${this.hacs.localize("settings.custom_repositories")}">
<div class="card-content">
<div class="custom-repositories-list">
${this.hacs.status.background_task
${this.background_task
? html`
<i class="addition"
>${this.hacs.localize("settings.bg_task_custom")}</i
>
`
: html`
${this.custom
.sort((a, b) => (a.full_name > b.full_name ? 1 : -1))
.map(
${this.custom &&
this.custom.map(
repo =>
html`
<div
Expand Down
10 changes: 7 additions & 3 deletions src/misc/HiddenRepositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ import {
html
} from "lit-element";
import { HacsStyle } from "../style/hacs-style";
import { HomeAssistant } from "custom-card-helpers";
import { HACS } from "../Hacs";
import { RepositoryData } from "../types";

@customElement("hacs-hidden-repositories")
export class HiddenRepositories extends LitElement {
@property({ type: Array }) public _hidden!: RepositoryData[];
@property({ type: Object }) public hacs!: HACS;
@property({ type: Object }) public hass!: HomeAssistant;

UnHide(ev) {
var repo = ev.composedPath()[4].repoID;
this.hacs.RepositoryWebSocketAction(this.hass, repo, "unhide");
this.dispatchEvent(
new CustomEvent("hacs-repository-action", {
detail: { repo: repo, action: "unhide" },
bubbles: true,
composed: true
})
);
}

protected render(): TemplateResult | void {
Expand Down
18 changes: 3 additions & 15 deletions src/panels/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,25 @@ import {
property
} from "lit-element";

import { HomeAssistant } from "custom-card-helpers";
import { HACS } from "../Hacs";

import { Route } from "../types";

@customElement("hacs-settings")
export class HacsSettings extends LitElement {
@property({ type: Object }) public hacs!: HACS;
@property({ type: Object }) public hass!: HomeAssistant;
@property({ type: Object }) public route!: Route;

render(): TemplateResult | void {
if (this.hass === undefined || this.hacs === undefined) {
if (this.hacs === undefined) {
return html`
<hacs-progressbar></hacs-progressbar>
`;
}
return html`
<hacs-body>
<hacs-custom-repositories
.hacs=${this.hacs}
.hass=${this.hass}
.status=${this.hacs.status}
.route=${this.route}
>
<hacs-custom-repositories .hacs=${this.hacs} .route=${this.route}>
</hacs-custom-repositories>
<hacs-hidden-repositories
.hacs=${this.hacs}
.hass=${this.hass}
.status=${this.hacs.status}
>
<hacs-hidden-repositories .hacs=${this.hacs}>
</hacs-hidden-repositories>
</hacs-body>
`;
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,9 @@ export interface HacsBanner extends HTMLElement {
lovelaceconfig?: LovelaceConfig;
status?: Status;
}

export interface RepositoryActionData {
repo: string;
action: string;
category?: string;
}

0 comments on commit 596ce96

Please sign in to comment.