Skip to content

Commit

Permalink
Merge pull request Expensify#50493 from c3024/write-mute-settings-to-…
Browse files Browse the repository at this point in the history
…backend

Write platform specific mute settings to backend
  • Loading branch information
tgolen authored Oct 29, 2024
2 parents bc7b855 + cb4e58e commit 57d5a0c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ const CONST = {
ANDROID: 'android',
WEB: 'web',
DESKTOP: 'desktop',
MOBILEWEB: 'mobileweb',
},
PLATFORM_SPECIFIC_KEYS: {
CTRL: {
Expand Down
5 changes: 5 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {ValueOf} from 'type-fest';
import type CONST from './CONST';
import type {OnboardingCompanySizeType, OnboardingPurposeType} from './CONST';
import type Platform from './libs/getPlatform/types';
import type * as FormTypes from './types/form';
import type * as OnyxTypes from './types/onyx';
import type {Attendee} from './types/onyx/IOU';
Expand Down Expand Up @@ -122,6 +123,9 @@ const ONYXKEYS = {
/** This NVP contains data associated with HybridApp */
NVP_TRYNEWDOT: 'nvp_tryNewDot',

/** Contains the platforms for which the user muted the sounds */
NVP_MUTED_PLATFORMS: 'nvp_mutedPlatforms',

/** Contains the user preference for the LHN priority mode */
NVP_PRIORITY_MODE: 'nvp_priorityMode',

Expand Down Expand Up @@ -903,6 +907,7 @@ type OnyxValuesMapping = {
[ONYXKEYS.USER_METADATA]: OnyxTypes.UserMetadata;
[ONYXKEYS.STASHED_SESSION]: OnyxTypes.Session;
[ONYXKEYS.BETAS]: OnyxTypes.Beta[];
[ONYXKEYS.NVP_MUTED_PLATFORMS]: Partial<Record<Platform, true>>;
[ONYXKEYS.NVP_PRIORITY_MODE]: ValueOf<typeof CONST.PRIORITY_MODE>;
[ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE]: OnyxTypes.BlockedFromConcierge;

Expand Down
7 changes: 7 additions & 0 deletions src/libs/API/parameters/TogglePlatformMuteParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type Platform from '@libs/getPlatform/types';

type TogglePlatformMuteParams = {
platformToMute: Platform;
};

export default TogglePlatformMuteParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,4 @@ export type {default as UpdateInvoiceCompanyNameParams} from './UpdateInvoiceCom
export type {default as UpdateInvoiceCompanyWebsiteParams} from './UpdateInvoiceCompanyWebsiteParams';
export type {default as UpdateQuickbooksDesktopExpensesExportDestinationTypeParams} from './UpdateQuickbooksDesktopExpensesExportDestinationTypeParams';
export type {default as UpdateQuickbooksDesktopCompanyCardExpenseAccountTypeParams} from './UpdateQuickbooksDesktopCompanyCardExpenseAccountTypeParams';
export type {default as TogglePlatformMuteParams} from './TogglePlatformMuteParams';
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const WRITE_COMMANDS = {
VALIDATE_SECONDARY_LOGIN: 'ValidateSecondaryLogin',
UPDATE_PREFERRED_EMOJI_SKIN_TONE: 'UpdatePreferredEmojiSkinTone',
UPDATE_CHAT_PRIORITY_MODE: 'UpdateChatPriorityMode',
TOGGLE_PLATFORM_MUTE: 'TogglePlatformMute',
SET_CONTACT_METHOD_AS_DEFAULT: 'SetContactMethodAsDefault',
UPDATE_THEME: 'UpdateTheme',
UPDATE_STATUS: 'UpdateStatus',
Expand Down Expand Up @@ -488,6 +489,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.UPDATE_PREFERRED_EMOJI_SKIN_TONE]: Parameters.UpdatePreferredEmojiSkinToneParams;
[WRITE_COMMANDS.UPDATE_CHAT_PRIORITY_MODE]: Parameters.UpdateChatPriorityModeParams;
[WRITE_COMMANDS.SET_CONTACT_METHOD_AS_DEFAULT]: Parameters.SetContactMethodAsDefaultParams;
[WRITE_COMMANDS.TOGGLE_PLATFORM_MUTE]: Parameters.TogglePlatformMuteParams;
[WRITE_COMMANDS.UPDATE_THEME]: Parameters.UpdateThemeParams;
[WRITE_COMMANDS.UPDATE_STATUS]: Parameters.UpdateStatusParams;
[WRITE_COMMANDS.CLEAR_STATUS]: null;
Expand Down
32 changes: 29 additions & 3 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
RequestContactMethodValidateCodeParams,
SetContactMethodAsDefaultParams,
SetNameValuePairParams,
TogglePlatformMuteParams,
UpdateChatPriorityModeParams,
UpdateNewsletterSubscriptionParams,
UpdatePreferredEmojiSkinToneParams,
Expand All @@ -23,6 +24,7 @@ import type {
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import DateUtils from '@libs/DateUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import type Platform from '@libs/getPlatform/types';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import * as SequentialQueue from '@libs/Network/SequentialQueue';
Expand Down Expand Up @@ -978,8 +980,32 @@ function clearUserErrorMessage() {
Onyx.merge(ONYXKEYS.USER, {error: ''});
}

function setMuteAllSounds(isMutedAllSounds: boolean) {
Onyx.merge(ONYXKEYS.USER, {isMutedAllSounds});
function togglePlatformMute(platform: Platform, mutedPlatforms: Partial<Record<Platform, true>>) {
const newMutedPlatforms = mutedPlatforms?.[platform]
? {...mutedPlatforms, [platform]: undefined} // Remove platform if it's muted
: {...mutedPlatforms, [platform]: true}; // Add platform if it's not muted

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.NVP_MUTED_PLATFORMS,
value: newMutedPlatforms,
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.NVP_MUTED_PLATFORMS,
value: mutedPlatforms,
},
];

const parameters: TogglePlatformMuteParams = {platformToMute: platform};

API.write(WRITE_COMMANDS.TOGGLE_PLATFORM_MUTE, parameters, {
optimisticData,
failureData,
});
}

/**
Expand Down Expand Up @@ -1354,7 +1380,7 @@ export {
subscribeToUserEvents,
updatePreferredSkinTone,
setShouldUseStagingServer,
setMuteAllSounds,
togglePlatformMute,
clearUserErrorMessage,
joinScreenShare,
clearScreenShareRequest,
Expand Down
10 changes: 8 additions & 2 deletions src/pages/settings/Preferences/PreferencesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Browser from '@libs/Browser';
import getPlatform from '@libs/getPlatform';
import LocaleUtils from '@libs/LocaleUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as User from '@userActions/User';
Expand All @@ -22,6 +24,10 @@ import ROUTES from '@src/ROUTES';

function PreferencesPage() {
const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE);

const platform = Browser.isMobile() ? CONST.PLATFORM.MOBILEWEB : getPlatform();
const [mutedPlatforms = {}] = useOnyx(ONYXKEYS.NVP_MUTED_PLATFORMS);
const isPlatformMuted = mutedPlatforms[platform];
const [user] = useOnyx(ONYXKEYS.USER);
const [preferredTheme] = useOnyx(ONYXKEYS.PREFERRED_THEME);

Expand Down Expand Up @@ -79,8 +85,8 @@ function PreferencesPage() {
<View style={[styles.flex1, styles.alignItemsEnd]}>
<Switch
accessibilityLabel={translate('preferencesPage.muteAllSounds')}
isOn={user?.isMutedAllSounds ?? false}
onToggle={User.setMuteAllSounds}
isOn={isPlatformMuted ?? false}
onToggle={() => User.togglePlatformMute(platform, mutedPlatforms)}
/>
</View>
</View>
Expand Down

0 comments on commit 57d5a0c

Please sign in to comment.