Skip to content

Commit d83ece2

Browse files
authored
Merge pull request #2569 from UltimateHackingKeyboard/feat-reorder-connections
feat: when reordering connections, change the ids of the related device actions accordingly
2 parents caf0c7b + 87af525 commit d83ece2

3 files changed

Lines changed: 66 additions & 10 deletions

File tree

packages/uhk-common/src/config-serializer/config-items/host-connection.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export class HostConnection {
4646
@assertEnum(HostConnections) type: HostConnections;
4747

4848
address: string;
49+
/**
50+
* Position in the host connections array.
51+
* It is not serialised used to track the reordering.
52+
*/
53+
index: number;
4954
name: string;
5055
switchover: boolean;
5156

@@ -55,6 +60,7 @@ export class HostConnection {
5560
if (other) {
5661
this.type = other.type;
5762
this.address = other.address;
63+
this.index = other.index;
5864
this.name = other.name;
5965
this.switchover = other.switchover;
6066
}

packages/uhk-common/src/config-serializer/config-items/user-configuration.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,9 @@ export class UserConfiguration implements MouseSpeedConfiguration {
662662
if (this.userConfigMinorVersion >= 1) {
663663
this.hostConnections = [];
664664
for(let i = 0; i < HOST_CONNECTION_COUNT_MAX; i++) {
665-
this.hostConnections.push(new HostConnection().fromBinary(buffer, serialisationInfo));
665+
const hostConnection = new HostConnection().fromBinary(buffer, serialisationInfo);
666+
hostConnection.index = i;
667+
this.hostConnections.push(hostConnection);
666668
}
667669
}
668670

@@ -732,7 +734,9 @@ export class UserConfiguration implements MouseSpeedConfiguration {
732734

733735
this.hostConnections = [];
734736
for (let i = 0; i < HOST_CONNECTION_COUNT_MAX; i++) {
735-
this.hostConnections.push(new HostConnection().fromBinary(buffer, serialisationInfo));
737+
const hostConnection = new HostConnection().fromBinary(buffer, serialisationInfo);
738+
hostConnection.index = i;
739+
this.hostConnections.push(hostConnection);
736740
}
737741

738742
this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(uhkBuffer => {
@@ -927,8 +931,11 @@ export class UserConfiguration implements MouseSpeedConfiguration {
927931
const serialisationInfo = this.getSerialisationInfo();
928932

929933
if (this.userConfigMinorVersion >= 1) {
930-
this.hostConnections = jsonObject.hostConnections.map((hostConnection: any) => {
931-
return new HostConnection().fromJsonObject(hostConnection, serialisationInfo);
934+
this.hostConnections = jsonObject.hostConnections.map((hostConnection: any, index: number) => {
935+
const connection = new HostConnection().fromJsonObject(hostConnection, serialisationInfo)
936+
connection.index = index;
937+
938+
return connection;
932939
});
933940
}
934941

@@ -995,8 +1002,11 @@ export class UserConfiguration implements MouseSpeedConfiguration {
9951002

9961003
const serialisationInfo = this.getSerialisationInfo();
9971004

998-
this.hostConnections = jsonObject.hostConnections.map((hostConnection: any) => {
999-
return new HostConnection().fromJsonObject(hostConnection, serialisationInfo);
1005+
this.hostConnections = jsonObject.hostConnections.map((hostConnection: any, index: number) => {
1006+
const connection = new HostConnection().fromJsonObject(hostConnection, serialisationInfo);
1007+
connection.index = index;
1008+
1009+
return connection;
10001010
});
10011011

10021012
this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => {

packages/uhk-web/src/app/store/reducers/user-configuration.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { HOST_CONNECTION_COUNT_MAX } from 'uhk-common';
21
import {
32
BacklightingMode,
3+
ConnectionsAction,
44
Constants,
55
emptyHostConnection,
66
getDefaultHalvesInfo,
77
HalvesInfo,
8+
HOST_CONNECTION_COUNT_MAX,
89
HostConnection,
910
HostConnections,
1011
initBacklightingColorPalette,
@@ -30,9 +31,9 @@ import {
3031
SwitchLayerAction,
3132
UserConfiguration
3233
} from 'uhk-common';
33-
import { BleAddingStates } from '../../models';
34-
import { BleAddingState } from '../../models';
3534
import {
35+
BleAddingStates,
36+
BleAddingState,
3637
BacklightingOption,
3738
defaultLastEditKey,
3839
ExchangeKey,
@@ -900,7 +901,46 @@ export function reducer(
900901
case UserConfig.ActionTypes.ReorderHostConnections: {
901902
const payload = (action as UserConfig.ReorderHostConnectionsAction).payload;
902903
const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration);
903-
userConfiguration.hostConnections = payload;
904+
const processedConnectionActions = new WeakSet<ConnectionsAction>()
905+
userConfiguration.hostConnections = payload.map((reorderedConnection, index) => {
906+
if (reorderedConnection.index === index) {
907+
return reorderedConnection;
908+
}
909+
910+
userConfiguration.keymaps = userConfiguration.keymaps.map(keymap => {
911+
keymap = Object.assign(new Keymap(), keymap)
912+
keymap.layers = keymap.layers.map(layer => {
913+
layer = Object.assign(new Layer(), layer);
914+
layer.modules = layer.modules.map(module => {
915+
module = Object.assign(new Module(), module);
916+
module.keyActions = module.keyActions.map(keyAction => {
917+
if (keyAction instanceof ConnectionsAction
918+
&& keyAction.hostConnectionId === reorderedConnection.index
919+
&& !processedConnectionActions.has(keyAction)) {
920+
const newKeyAction = new ConnectionsAction(keyAction);
921+
newKeyAction.hostConnectionId = index;
922+
processedConnectionActions.add(newKeyAction);
923+
924+
return newKeyAction;
925+
}
926+
927+
return keyAction;
928+
})
929+
930+
return module;
931+
})
932+
933+
return layer;
934+
})
935+
936+
return keymap;
937+
})
938+
939+
const newConnection = new HostConnection(reorderedConnection);
940+
newConnection.index = index;
941+
942+
return newConnection;
943+
})
904944

905945
return {
906946
...state,

0 commit comments

Comments
 (0)