Skip to content

Commit ac5558b

Browse files
committed
fix(harmonyos): share live device state across account pages
Settings and connection pages kept private device snapshots after background presence updates changed the sidebar. Read the shared observable directory in both pages and preserve device metadata while refreshing its rows.
1 parent 4117124 commit ac5558b

12 files changed

Lines changed: 61 additions & 25 deletions

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/AccountProfilePanel.ets‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RemoteI18n } from '../../i18n/RemoteI18n';
33
import { accountDeviceDisplayName, CloudAccountDevice } from '../../services/CloudAccountClient';
44
import { AccountDeviceSelectionPolicy } from '../policy/AccountDeviceSelectionPolicy';
55
import { DeviceModelSettingsEntryPolicy } from '../policy/DeviceModelSettingsEntryPolicy';
6+
import { DeviceDirectoryEntry, DeviceDirectoryState } from '../state/DeviceDirectoryState';
67
import {
78
CARD,
89
CONTENT_ON_ACTION,
@@ -20,6 +21,7 @@ import { DefaultAccountAvatar } from './DefaultAccountAvatar';
2021
/** Account identity and device management shared by Settings and account entry. */
2122
@ComponentV2
2223
export struct AccountProfilePanel {
24+
@Param directoryState: DeviceDirectoryState = new DeviceDirectoryState();
2325
@Param accountUsername: string = '';
2426
@Param accountAvatarUrl: string = '';
2527
@Param accountUserId: string = '';
@@ -42,7 +44,6 @@ export struct AccountProfilePanel {
4244
async (): Promise<CloudAccountDevice[]> => [];
4345
@Event cloudSelectDevice: (device: CloudAccountDevice) => Promise<void> =
4446
async (_device: CloudAccountDevice): Promise<void> => {};
45-
@Local accountDevices: CloudAccountDevice[] = [];
4647
@Local accountDevicesBusy: boolean = false;
4748
@Local accountDevicesError: string = '';
4849
@Local switchingDeviceId: string = '';
@@ -172,7 +173,7 @@ export struct AccountProfilePanel {
172173
if (!this.isAccountAuthenticated()) {
173174
Text(RemoteI18n.t('remote.settings.deviceLoginRequired'))
174175
.fontSize(MobileDesignTypography.bodySmall.size).lineHeight(MobileDesignTypography.bodySmall.lineHeight).fontColor(MUTED).width('100%')
175-
} else if (this.accountDevicesBusy && this.accountDevices.length === 0) {
176+
} else if (this.accountDevicesBusy && this.directoryState.devices.length === 0) {
176177
Text(RemoteI18n.t('remote.settings.deviceLoading'))
177178
.fontSize(MobileDesignTypography.bodySmall.size).fontColor(MUTED).width('100%')
178179
} else if (this.desktopDevices().length === 0) {
@@ -349,7 +350,7 @@ export struct AccountProfilePanel {
349350
this.accountDevicesBusy = true;
350351
this.accountDevicesError = '';
351352
try {
352-
this.accountDevices = await this.cloudListDevices();
353+
await this.cloudListDevices();
353354
} catch (err) {
354355
this.accountDevicesError = err instanceof Error ? err.message :
355356
RemoteI18n.t('remote.settings.deviceLoadFailed');
@@ -358,8 +359,8 @@ export struct AccountProfilePanel {
358359
}
359360
}
360361

361-
private desktopDevices(): CloudAccountDevice[] {
362-
return this.accountDevices.filter((device: CloudAccountDevice): boolean =>
362+
private desktopDevices(): DeviceDirectoryEntry[] {
363+
return this.directoryState.devices.filter((device: DeviceDirectoryEntry): boolean =>
363364
device.deviceId !== this.deviceId && device.deviceName !== 'HarmonyOS Phone');
364365
}
365366

@@ -488,7 +489,7 @@ export struct AccountProfilePanel {
488489

489490
@Monitor('accountUserId')
490491
private handleAccountUserChanged(): void {
491-
if (this.isAccountAuthenticated() && this.accountDevices.length === 0) {
492+
if (this.isAccountAuthenticated() && this.directoryState.devices.length === 0) {
492493
this.refreshAccountDevices();
493494
}
494495
}

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/AppRootOverlaySurfaces.ets‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export struct AppSidebarSurface {
128128

129129
@ComponentV2
130130
export struct AppSettingsSurface {
131+
@Param deviceDirectoryState: DeviceDirectoryState = new DeviceDirectoryState();
131132
@Param shellState: AppShellState = new AppShellState();
132133
@Param remotePageState: RemotePageState = new RemotePageState();
133134
@Param generalPageState: GeneralChatPageState = new GeneralChatPageState();
@@ -145,6 +146,7 @@ export struct AppSettingsSurface {
145146
})
146147
} else {
147148
SettingsSheet({
149+
directoryState: this.deviceDirectoryState,
148150
sheetState: this.shellState.settingsSheet,
149151
accountUsername: this.remotePageState.accountUsername,
150152
accountAvatarUrl: this.remotePageState.accountAvatarUrl,
@@ -179,12 +181,14 @@ export struct AppSettingsSurface {
179181

180182
@ComponentV2
181183
export struct AppConnectSurface {
184+
@Param deviceDirectoryState: DeviceDirectoryState = new DeviceDirectoryState();
182185
@Param remotePageState: RemotePageState = new RemotePageState();
183186
@Param deviceId: string = '';
184187
@Param actions: AppRootPresentationActions = emptyAppRootPresentationActions();
185188

186189
build() {
187190
ConnectView({
191+
directoryState: this.deviceDirectoryState,
188192
remoteUrl: this.remotePageState.remoteUrl,
189193
userId: this.remotePageState.userId,
190194
statusText: this.remotePageState.conversation.statusText,

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/AppRootPresentation.ets‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ export struct AppRootPresentation {
635635

636636
@Builder SettingsContent() {
637637
AppSettingsSurface({
638+
deviceDirectoryState: this.deviceDirectoryState,
638639
shellState: this.shellState,
639640
remotePageState: this.remotePageState,
640641
generalPageState: this.generalPageState,
@@ -645,6 +646,7 @@ export struct AppRootPresentation {
645646

646647
@Builder ConnectContent() {
647648
AppConnectSurface({
649+
deviceDirectoryState: this.deviceDirectoryState,
648650
remotePageState: this.remotePageState,
649651
deviceId: this.deviceId,
650652
actions: this.actions

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/ConnectAccountDevicePage.ets‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { RemoteI18n } from '../../i18n/RemoteI18n';
33
import { accountDeviceDisplayName, CloudAccountDevice } from '../../services/CloudAccountClient';
44
import { ACCENT, CARD, STATUS_SUCCESS, INK, LINE, MUTED, PAGE_BG, STATUS_DANGER, SOFT } from './Theme';
55
import { AccountDeviceSelectionPolicy } from '../policy/AccountDeviceSelectionPolicy';
6+
import { DeviceDirectoryEntry, DeviceDirectoryState } from '../state/DeviceDirectoryState';
67
import { NavigationBackButton } from './NavigationBackButton';
78

89
@ComponentV2
910
export struct ConnectAccountDevicePage {
11+
@Param directoryState: DeviceDirectoryState = new DeviceDirectoryState();
1012
@Param deviceId: string = '';
1113
@Param controlTargetDeviceId: string = '';
1214
@Param connectionState: string = 'idle';
@@ -15,7 +17,6 @@ export struct ConnectAccountDevicePage {
1517
async (): Promise<CloudAccountDevice[]> => [];
1618
@Event cloudSelectDevice: (device: CloudAccountDevice) => Promise<void> =
1719
async (_device: CloudAccountDevice): Promise<void> => {};
18-
@Local accountDevices: CloudAccountDevice[] = [];
1920
@Local accountDevicesBusy: boolean = false;
2021
@Local accountDevicesError: string = '';
2122
@Local switchingDeviceId: string = '';
@@ -77,7 +78,7 @@ export struct ConnectAccountDevicePage {
7778
}
7879
.width('100%').height(38)
7980

80-
if (this.accountDevicesBusy && this.accountDevices.length === 0) {
81+
if (this.accountDevicesBusy && this.directoryState.devices.length === 0) {
8182
Column() {
8283
this.AccountDeviceSkeletonRow()
8384
this.AccountDeviceSkeletonRow()
@@ -171,7 +172,7 @@ export struct ConnectAccountDevicePage {
171172
this.accountDevicesBusy = true;
172173
this.accountDevicesError = '';
173174
try {
174-
this.accountDevices = await this.cloudListDevices();
175+
await this.cloudListDevices();
175176
} catch (err) {
176177
this.accountDevicesError = err instanceof Error ? err.message :
177178
RemoteI18n.t('remote.settings.deviceLoadFailed');
@@ -180,8 +181,8 @@ export struct ConnectAccountDevicePage {
180181
}
181182
}
182183

183-
private desktopDevices(): CloudAccountDevice[] {
184-
return this.accountDevices.filter((device: CloudAccountDevice): boolean =>
184+
private desktopDevices(): DeviceDirectoryEntry[] {
185+
return this.directoryState.devices.filter((device: DeviceDirectoryEntry): boolean =>
185186
device.deviceId !== this.deviceId && device.deviceName !== 'HarmonyOS Phone');
186187
}
187188

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/ConnectView.ets‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MobileDesignTypography } from '../../generated/MobileDesignTokens';
22
import { RemoteI18n } from '../../i18n/RemoteI18n';
33
import { CloudAccountDevice } from '../../services/CloudAccountClient';
4+
import { DeviceDirectoryState } from '../state/DeviceDirectoryState';
45
import { ConnectAccountDevicePage } from './ConnectAccountDevicePage';
56
import { AppActionButton, AppActionStyle } from './AppActionButton';
67
import { NavigationBackButton } from './NavigationBackButton';
@@ -21,6 +22,7 @@ import { WindowInsetsBinding } from '../../services/WindowSystemBarService';
2122
*/
2223
@ComponentV2
2324
export struct ConnectView {
25+
@Param directoryState: DeviceDirectoryState = new DeviceDirectoryState();
2426
@Param remoteUrl: string = '';
2527
@Param userId: string = '';
2628
@Param statusText: string = RemoteI18n.t('status.waitingConnection');
@@ -72,6 +74,7 @@ export struct ConnectView {
7274
@Builder
7375
AccountDeviceSelectionPage() {
7476
ConnectAccountDevicePage({
77+
directoryState: this.directoryState,
7578
deviceId: this.deviceId,
7679
controlTargetDeviceId: this.controlTargetDeviceId,
7780
connectionState: this.connectionState,

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/SettingsSheet.ets‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MobileDesignGeometry, MobileDesignTypography } from '../../generated/MobileDesignTokens';
22
import { RemoteI18n } from '../../i18n/RemoteI18n';
33
import { SettingsSheetState } from '../state/SettingsSheetState';
4+
import { DeviceDirectoryState } from '../state/DeviceDirectoryState';
45
import { DeviceModelDefaultsRequestPath } from '../state/DeviceModelDefaultsSheetState';
56
import { CARD, INK, LINE, MUTED, PAGE_BG } from './Theme';
67
import { CurrentControlSection } from './CurrentControlSection';
@@ -17,6 +18,7 @@ import { RemotePermissionSection } from './RemotePermissionSection';
1718

1819
@ComponentV2
1920
export struct SettingsSheet {
21+
@Param directoryState: DeviceDirectoryState = new DeviceDirectoryState();
2022
@Param sheetState: SettingsSheetState = new SettingsSheetState();
2123
@Param accountUsername: string = '';
2224
@Param accountAvatarUrl: string = '';
@@ -162,6 +164,7 @@ export struct SettingsSheet {
162164
Scroll() {
163165
Column({ space: 0 }) {
164166
AccountProfilePanel({
167+
directoryState: this.directoryState,
165168
accountUsername: this.accountUsername,
166169
accountAvatarUrl: this.accountAvatarUrl,
167170
accountUserId: this.authenticatedUserId,

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/preview/ImmersiveBottomPreview.ets‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,13 @@ function gallery(): MiniAppSummary[] {
115115
return apps;
116116
}
117117

118-
function settingsDevices(): CloudAccountDevice[] {
119-
const devices: CloudAccountDevice[] = [];
118+
function settingsDevices(): DeviceDirectoryEntry[] {
119+
const devices: DeviceDirectoryEntry[] = [];
120120
for (let index = 0; index < SETTINGS_DEVICES; index++) {
121121
const device = index + 1;
122-
devices.push({
123-
deviceId: `account-desktop-${device}`,
124-
deviceName: `Account desktop ${device}`,
125-
online: device % 3 !== 0,
126-
lastSeenAt: device
127-
});
122+
const entry = emptyDeviceDirectoryEntry(`account-desktop-${device}`, `Account desktop ${device}`, device % 3 !== 0);
123+
entry.lastSeenAt = device;
124+
devices.push(entry);
128125
}
129126
return devices;
130127
}
@@ -351,7 +348,9 @@ export struct ImmersiveBottomPreview {
351348
// be the greyed state. This fixture is the connected desktop's, so it
352349
// advertises the command and the sheet shows the entry it really shows.
353350
this.remotePageState.setHostCapabilities([REMOTE_CAPABILITY_DEVICE_MODEL_DEFAULTS_V1]);
354-
this.previewActions = previewActionsWithDevices(settingsDevices());
351+
const devices = settingsDevices();
352+
this.directoryState.replace(devices);
353+
this.previewActions = previewActionsWithDevices(devices);
355354
return;
356355
}
357356
if (scenarioId.endsWith('-connect')) {
@@ -469,6 +468,7 @@ export struct ImmersiveBottomPreview {
469468
@Builder
470469
private Settings() {
471470
AppSettingsSurface({
471+
deviceDirectoryState: this.directoryState,
472472
shellState: this.shellState,
473473
remotePageState: this.remotePageState,
474474
actions: this.previewActions
@@ -478,6 +478,7 @@ export struct ImmersiveBottomPreview {
478478
@Builder
479479
private Connect() {
480480
AppConnectSurface({
481+
deviceDirectoryState: this.directoryState,
481482
remotePageState: this.remotePageState,
482483
actions: this.previewActions
483484
})

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/state/DeviceDirectoryState.ets‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class WorkspaceDirectoryState {
2929
}
3030

3131
/**
32-
* One account desktop in the sidebar. Nested @Trace fields are what
32+
* One account desktop shared by the sidebar, settings and device picker. Nested @Trace fields are what
3333
* SidebarDeviceGroup actually watches — replacing a plain object on
3434
* the parent array does not update a reused ForEach child.
3535
*/
@@ -38,6 +38,9 @@ export class DeviceDirectoryEntry {
3838
@Trace deviceId: string = '';
3939
@Trace deviceName: string = '';
4040
@Trace deviceAlias: string | null | undefined = undefined;
41+
@Trace deviceKind: string | undefined = undefined;
42+
@Trace lastSeenAt: number | undefined = undefined;
43+
@Trace compatible: boolean | undefined = undefined;
4144
@Trace online: boolean = false;
4245
@Trace status: DeviceDirectoryStatus = 'idle';
4346
@Trace errorText: string = '';
@@ -51,7 +54,7 @@ export class DeviceDirectoryEntry {
5154
}
5255
}
5356

54-
/** Snapshot of every account desktop the sidebar can observe. */
57+
/** Shared observable directory of account desktops. */
5558
@ObservedV2
5659
export class DeviceDirectoryState {
5760
@Trace refreshing: boolean = false;

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/viewmodel/DeviceDirectoryViewModel.ets‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export class DeviceDirectoryViewModel {
9191
deviceId: item.deviceId,
9292
deviceName: item.deviceName,
9393
deviceAlias: item.deviceAlias,
94+
deviceKind: item.deviceKind,
95+
lastSeenAt: item.lastSeenAt,
96+
compatible: item.compatible,
9497
online: item.online
9598
};
9699
}));
@@ -258,6 +261,9 @@ export class DeviceDirectoryViewModel {
258261
if (existing) {
259262
existing.deviceName = device.deviceName || existing.deviceName;
260263
existing.deviceAlias = device.deviceAlias;
264+
existing.deviceKind = device.deviceKind;
265+
existing.lastSeenAt = device.lastSeenAt;
266+
existing.compatible = device.compatible;
261267
if (existing.online && !online) {
262268
this.inFlight.delete(device.deviceId);
263269
if (existing.status === 'loading') existing.status = 'idle';
@@ -271,6 +277,9 @@ export class DeviceDirectoryViewModel {
271277
online
272278
);
273279
created.deviceAlias = device.deviceAlias;
280+
created.deviceKind = device.deviceKind;
281+
created.lastSeenAt = device.lastSeenAt;
282+
created.compatible = device.compatible;
274283
return created;
275284
});
276285
this.state.replace(next);

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/viewmodel/SettingsController.ets‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ export class SettingsController {
297297
this.accountDevices = this.accountDevices.map((device: CloudAccountDevice): CloudAccountDevice => ({
298298
deviceId: device.deviceId, deviceName: device.deviceName, deviceKind: device.deviceKind,
299299
deviceAlias: device.deviceAlias,
300+
compatible: device.compatible,
300301
lastSeenAt: device.lastSeenAt, online: ids.includes(device.deviceId)
301302
}));
302303
const cloud = this.requireCloud();

0 commit comments

Comments
 (0)