diff --git a/packages/kit/src/background/services/ServiceAccount.ts b/packages/kit/src/background/services/ServiceAccount.ts index 984c9109417..9d0ade27a3a 100644 --- a/packages/kit/src/background/services/ServiceAccount.ts +++ b/packages/kit/src/background/services/ServiceAccount.ts @@ -128,6 +128,10 @@ class ServiceAccount extends ServiceBase { }, ); this.backgroundApi.walletConnect.notifySessionChanged(); + // emit at next tick + setTimeout(() => { + appEventBus.emit(AppEventBusNames.AccountChanged); + }, 10); } @backgroundMethod() diff --git a/packages/kit/src/background/services/ServiceNetwork.ts b/packages/kit/src/background/services/ServiceNetwork.ts index 2285aed8a7f..e96b232e7b1 100644 --- a/packages/kit/src/background/services/ServiceNetwork.ts +++ b/packages/kit/src/background/services/ServiceNetwork.ts @@ -5,6 +5,10 @@ import { Network, UpdateNetworkParams, } from '@onekeyhq/engine/src/types/network'; +import { + AppEventBusNames, + appEventBus, +} from '@onekeyhq/shared/src/eventBus/appEventBus'; import { GeneralInitialState, @@ -61,6 +65,7 @@ class ServiceNetwork extends ServiceBase { }, ); this.backgroundApi.walletConnect.notifySessionChanged(); + appEventBus.emit(AppEventBusNames.NetworkChanged); } @backgroundMethod() diff --git a/packages/kit/src/background/services/ServiceToken.ts b/packages/kit/src/background/services/ServiceToken.ts index 1b01f63aef5..f6f9778d5f0 100644 --- a/packages/kit/src/background/services/ServiceToken.ts +++ b/packages/kit/src/background/services/ServiceToken.ts @@ -1,3 +1,8 @@ +import { + AppEventBusNames, + appEventBus, +} from '@onekeyhq/shared/src/eventBus/appEventBus'; + import { TokenInitialState, setAccountTokens, @@ -7,10 +12,27 @@ import { } from '../../store/reducers/tokens'; import { backgroundClass, backgroundMethod } from '../decorators'; -import ServiceBase from './ServiceBase'; +import ServiceBase, { IServiceBaseProps } from './ServiceBase'; @backgroundClass() export default class ServiceToken extends ServiceBase { + constructor(props: IServiceBaseProps) { + super(props); + appEventBus.on( + AppEventBusNames.NetworkChanged, + this.refreshTokenBalance.bind(this), + ); + } + + refreshTokenBalance() { + const { appSelector } = this.backgroundApi; + const activeAccountId = appSelector((s) => s.general.activeAccountId); + const activeNetworkId = appSelector((s) => s.general.activeNetworkId); + if (activeAccountId && activeNetworkId) { + this.fetchTokenBalance({ activeAccountId, activeNetworkId }); + } + } + @backgroundMethod() async fetchTokens({ activeAccountId, diff --git a/packages/kit/src/views/ManagerAccount/AccountInfo/index.tsx b/packages/kit/src/views/ManagerAccount/AccountInfo/index.tsx index 7714cc05f71..5fbaebc89b9 100644 --- a/packages/kit/src/views/ManagerAccount/AccountInfo/index.tsx +++ b/packages/kit/src/views/ManagerAccount/AccountInfo/index.tsx @@ -159,6 +159,7 @@ const ManagerAccountModal: FC = () => { accountId, undefined, () => { + refreshAccounts?.(); if (navigation.canGoBack()) navigation.goBack(); }, ); @@ -172,6 +173,7 @@ const ManagerAccountModal: FC = () => { accountId, pwd, () => { + refreshAccounts?.(); if (navigation.canGoBack()) navigation.goBack(); }, ); diff --git a/packages/kit/src/views/ManagerAccount/ExportPrivate/index.tsx b/packages/kit/src/views/ManagerAccount/ExportPrivate/index.tsx index 54df90c79d4..eb7d2574253 100644 --- a/packages/kit/src/views/ManagerAccount/ExportPrivate/index.tsx +++ b/packages/kit/src/views/ManagerAccount/ExportPrivate/index.tsx @@ -113,7 +113,7 @@ const ExportPrivateViewModal = () => { onPress={copyDataToClipboard} > {intl.formatMessage({ - id: 'action__copy_address', + id: 'action__copy', })} diff --git a/packages/shared/src/eventBus/appEventBus.ts b/packages/shared/src/eventBus/appEventBus.ts index 52940a613d5..68123813341 100644 --- a/packages/shared/src/eventBus/appEventBus.ts +++ b/packages/shared/src/eventBus/appEventBus.ts @@ -4,6 +4,8 @@ const appEventBus = new CrossEventEmitter(); enum AppEventBusNames { AccountNameChanged = 'AccountNameChanged', + NetworkChanged = 'NetworkChanged', + AccountChanged = 'AccountChanged', } export { appEventBus, AppEventBusNames };