Skip to content

feat: add general settings to core PreferencesController #6111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/preferences-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add general settings properties for cross-platform synchronization ([#6111](https://github.com/MetaMask/core/pull/6111))

### Deprecated

- Deprecate preference `smartAccountOptInForAccounts` and function `setSmartAccountOptInForAccounts` ([#6087](https://github.com/MetaMask/core/pull/6087))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('PreferencesController', () => {
},
privacyMode: false,
dismissSmartAccountSuggestionEnabled: false,
currentLocale: 'en',
theme: 'auto',
useBlockie: false,
currentCurrency: 'USD',
showNativeTokenAsMainBalance: false,
hideZeroBalanceTokens: false,
});
});

Expand Down Expand Up @@ -572,6 +578,48 @@ describe('PreferencesController', () => {
controller.setSmartAccountOptInForAccounts(['0x1', '0x2']);
expect(controller.state.smartAccountOptInForAccounts[0]).toBe('0x1');
});

it('should set currentLocale', () => {
const controller = setupPreferencesController();
expect(controller.state.currentLocale).toBe('en');
controller.setCurrentLocale('es');
expect(controller.state.currentLocale).toBe('es');
});

it('should set theme', () => {
const controller = setupPreferencesController();
expect(controller.state.theme).toBe('auto');
controller.setTheme('dark');
expect(controller.state.theme).toBe('dark');
});

it('should set useBlockie', () => {
const controller = setupPreferencesController();
expect(controller.state.useBlockie).toBe(false);
controller.setUseBlockie(true);
expect(controller.state.useBlockie).toBe(true);
});

it('should set currentCurrency', () => {
const controller = setupPreferencesController();
expect(controller.state.currentCurrency).toBe('USD');
controller.setCurrentCurrency('EUR');
expect(controller.state.currentCurrency).toBe('EUR');
});

it('should set showNativeTokenAsMainBalance', () => {
const controller = setupPreferencesController();
expect(controller.state.showNativeTokenAsMainBalance).toBe(false);
controller.setShowNativeTokenAsMainBalance(true);
expect(controller.state.showNativeTokenAsMainBalance).toBe(true);
});

it('should set hideZeroBalanceTokens', () => {
const controller = setupPreferencesController();
expect(controller.state.hideZeroBalanceTokens).toBe(false);
controller.setHideZeroBalanceTokens(true);
expect(controller.state.hideZeroBalanceTokens).toBe(true);
});
});

/**
Expand Down
102 changes: 102 additions & 0 deletions packages/preferences-controller/src/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@ export type PreferencesState = {
* @deprecated This preference is deprecated and will be removed in the future.
*/
smartAccountOptInForAccounts: Hex[];
/**
* The user's preferred language locale
*/
currentLocale: string;
/**
* The user's preferred theme (light, dark, or auto)
*/
theme: string;
/**
* Whether to use blockie identicons instead of jazzicons
*/
useBlockie: boolean;
/**
* The user's preferred currency for conversion rates
*/
currentCurrency: string;
/**
* Whether to show native token as the main balance
*/
showNativeTokenAsMainBalance: boolean;
/**
* Whether to hide tokens with zero balance
*/
hideZeroBalanceTokens: boolean;
};

const metadata = {
Expand All @@ -172,6 +196,12 @@ const metadata = {
dismissSmartAccountSuggestionEnabled: { persist: true, anonymous: true },
smartAccountOptIn: { persist: true, anonymous: true },
smartAccountOptInForAccounts: { persist: true, anonymous: true },
currentLocale: { persist: true, anonymous: true },
theme: { persist: true, anonymous: true },
useBlockie: { persist: true, anonymous: true },
currentCurrency: { persist: true, anonymous: true },
showNativeTokenAsMainBalance: { persist: true, anonymous: true },
hideZeroBalanceTokens: { persist: true, anonymous: true },
};

const name = 'PreferencesController';
Expand Down Expand Up @@ -255,6 +285,12 @@ export function getDefaultPreferencesState(): PreferencesState {
dismissSmartAccountSuggestionEnabled: false,
smartAccountOptIn: true,
smartAccountOptInForAccounts: [],
currentLocale: 'en',
theme: 'auto',
useBlockie: false,
currentCurrency: 'USD',
showNativeTokenAsMainBalance: false,
hideZeroBalanceTokens: false,
Comment on lines +288 to +293
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are those default values coming from?

};
}

Expand Down Expand Up @@ -645,6 +681,72 @@ export class PreferencesController extends BaseController<
state.smartAccountOptInForAccounts = accounts;
});
}

/**
* Sets the user's preferred language locale
*
* @param currentLocale - The language locale (e.g., 'en', 'es', 'fr')
*/
setCurrentLocale(currentLocale: string): void {
this.update((state) => {
state.currentLocale = currentLocale;
});
}

/**
* Sets the user's preferred theme
*
* @param theme - The theme preference ('light', 'dark', or 'auto')
*/
setTheme(theme: string): void {
this.update((state) => {
state.theme = theme;
});
}

/**
* Sets whether to use blockie identicons instead of jazzicons
*
* @param useBlockie - Whether to use blockie identicons
*/
setUseBlockie(useBlockie: boolean): void {
this.update((state) => {
state.useBlockie = useBlockie;
});
}

/**
* Sets the user's preferred currency for conversion rates
*
* @param currentCurrency - The currency code (e.g., 'USD', 'EUR', 'GBP')
*/
setCurrentCurrency(currentCurrency: string): void {
this.update((state) => {
state.currentCurrency = currentCurrency;
});
}

/**
* Sets whether to show native token as the main balance
*
* @param showNativeTokenAsMainBalance - Whether to show native token as main balance
*/
setShowNativeTokenAsMainBalance(showNativeTokenAsMainBalance: boolean): void {
this.update((state) => {
state.showNativeTokenAsMainBalance = showNativeTokenAsMainBalance;
});
}

/**
* Sets whether to hide tokens with zero balance
*
* @param hideZeroBalanceTokens - Whether to hide tokens with zero balance
*/
setHideZeroBalanceTokens(hideZeroBalanceTokens: boolean): void {
this.update((state) => {
state.hideZeroBalanceTokens = hideZeroBalanceTokens;
});
}
}

export default PreferencesController;
Loading