Skip to content
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

Write platform specific mute settings to backend #50493

Merged
merged 16 commits into from
Oct 29, 2024
Merged
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
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,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 @@ -341,3 +341,4 @@ export type {default as SetInvoicingTransferBankAccountParams} from './SetInvoic
export type {default as ConnectPolicyToQuickBooksDesktopParams} from './ConnectPolicyToQuickBooksDesktopParams';
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 @@ -75,6 +75,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 @@ -484,6 +485,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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it better to use MERGE here without passing mutedPlatforms to this function togglePlatformMute?

Copy link
Contributor

Choose a reason for hiding this comment

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

For NVPs, the server uses SET, so it's probably best to use SET here as well. That will keep things consistent.

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
Loading