Skip to content

Commit

Permalink
fix: import groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Nov 18, 2023
1 parent 019fa0f commit 7758e5c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
4 changes: 1 addition & 3 deletions lib/Data/ImportExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,7 @@ class DataImportExport extends CoreBase {
}

if (!config || config.surfaces) {
for (const [id, surface] of Object.entries(data.surfaces || {})) {
this.surfaces.importSurface(id, surface)
}
this.surfaces.importSurfaces(data.surfaceGroups || {}, data.surfaces || {})
}

if (!config || config.triggers) {
Expand Down
54 changes: 50 additions & 4 deletions lib/Surface/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,15 +1124,61 @@ class SurfaceController extends CoreBase {
* @param {*} config
* @returns {void}
*/
importSurface(surfaceId, config) {
const device = this.#getSurfaceHandlerForId(surfaceId, true)
if (device) {
#importSurface(surfaceId, config) {
const surface = this.#getSurfaceHandlerForId(surfaceId, true)
if (surface) {
// Device is currently loaded
device.setPanelConfig(config)
surface.setPanelConfig(config.config)
surface.saveGroupConfig(config.groupConfig)
surface.setPanelName(config.name)

// Update the groupId
const newGroupId = config.groupId ?? null
if (surface.getGroupId() !== newGroupId && this.#getGroupForId(newGroupId)) {
this.#detachSurfaceFromGroup(surface)
surface.setGroupId(newGroupId)
this.#attachSurfaceToGroup(surface)
}
} else {
// Device is not loaded
this.setDeviceConfig(surfaceId, config)
}
}

/**
* Import a surface configuration
* @param {Record<string, *>} surfaceGroups
* @param {Record<string, *>} surfaces
* @returns {void}
*/
importSurfaces(surfaceGroups, surfaces) {
// try {
// this.#detachSurfaceFromGroup(surface)

// this.#attachSurfaceToGroup(surface)
// } catch (e) {
// this.logger.warn('Could not reattach a surface')
// }

for (const [id, surfaceGroup] of Object.entries(surfaceGroups)) {
let group = this.#getGroupForId(id, true)
if (!group) {
// Group does not exist
group = new SurfaceGroup(this.registry, id, null, this.isPinLockEnabled())
this.#surfaceGroups.set(id, group)
}

// Sync config
group.setName(surfaceGroup.name ?? '')
for (const [key, value] of Object.entries(surfaceGroup)) {
if (key === 'name') continue
group.setGroupConfigValue(key, value)
}
}

for (const [id, surface] of Object.entries(surfaces)) {
this.#importSurface(id, surface)
}

this.updateDevicesList()
}
Expand Down

0 comments on commit 7758e5c

Please sign in to comment.