Skip to content

Commit

Permalink
[DDW-813] Move discreet mode behavior to its own feature
Browse files Browse the repository at this point in the history
  • Loading branch information
renanvalentin committed Nov 15, 2021
1 parent 78ce7bf commit fefa1e8
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 56 deletions.
4 changes: 0 additions & 4 deletions source/renderer/app/actions/app-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ export default class AppActions {
// Daedalus Diagnostics dialog actions
closeDaedalusDiagnosticsDialog: Action<any> = new Action();
openDaedalusDiagnosticsDialog: Action<any> = new Action();

// Discreet mode actions
toggleDiscreetMode: Action<any> = new Action();
toggleOpenInDiscreetMode: Action<any> = new Action();
}
9 changes: 0 additions & 9 deletions source/renderer/app/api/utils/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,4 @@ export default class LocalStorageApi {
reset = async () => {
await LocalStorageApi.reset();
};

getDiscreetModeSettings = (): Promise<boolean> =>
LocalStorageApi.get(keys.DISCREET_MODE_ENABLED, false);

setDiscreetModeSettings = (enabled: boolean): Promise<void> =>
LocalStorageApi.set(keys.DISCREET_MODE_ENABLED, enabled);

unsetDiscreetModeSettings = (): Promise<void> =>
LocalStorageApi.unset(keys.DISCREET_MODE_ENABLED);
}
14 changes: 14 additions & 0 deletions source/renderer/app/features/discreet-mode/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @flow
import LocalStorageApi from '../../../api/utils/localStorage';
import { STORAGE_KEYS as storageKeys } from '../../../../../common/config/electron-store.config';

export class DiscreetModeApi {
getDiscreetModeSettings = (): Promise<boolean> =>
LocalStorageApi.get(storageKeys.DISCREET_MODE_ENABLED, false);

setDiscreetModeSettings = (enabled: boolean): Promise<void> =>
LocalStorageApi.set(storageKeys.DISCREET_MODE_ENABLED, enabled);

unsetDiscreetModeSettings = (): Promise<void> =>
LocalStorageApi.unset(storageKeys.DISCREET_MODE_ENABLED);
}
13 changes: 12 additions & 1 deletion source/renderer/app/features/discreet-mode/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import React, { useState } from 'react';
import type { Node } from 'react';
import { merge } from 'lodash/fp';
import {
getFeatureFromContext,
useFeature,
} from '../../utils/mobx-features/hooks';

import { DiscreetMode } from './feature';
import { DiscreetModeApi } from './api';

export const discreetModeContext = React.createContext<DiscreetMode | null>(
null
Expand All @@ -18,7 +20,16 @@ interface Props {
}

export const DiscreetModeFeatureProvider = ({ children }: Props) => {
const [discreetModeFeature] = useState<DiscreetMode>(new DiscreetMode());
const [discreetModeFeature] = useState<DiscreetMode>(() => {
const feature = new DiscreetMode(new DiscreetModeApi());
window.daedalus = merge(window.daedalus, {
features: {
discreetModeFeature: feature,
},
});

return feature;
});

useFeature(discreetModeFeature);

Expand Down
43 changes: 42 additions & 1 deletion source/renderer/app/features/discreet-mode/feature.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
// @flow

import { observable, action } from 'mobx';
import { observable, action, runInAction } from 'mobx';
import { Feature } from '../../utils/mobx-features/feature';
import Request from '../../stores/lib/LocalizedRequest';
import { DiscreetModeApi } from './api';

export class DiscreetMode extends Feature {
api: DiscreetModeApi;
@observable isDiscreetMode: boolean = false;
@observable openInDiscreetMode: boolean = false;

@observable getDiscreetModeSettingsRequest: Request<
Promise<boolean>
> = new Request(this.api.getDiscreetModeSettings);

@observable setDiscreetModeSettingsRequest: Request<
Promise<boolean>
> = new Request(this.api.setDiscreetModeSettings);

constructor(api: DiscreetModeApi) {
super();

this.api = api;
}

async start() {
super.start();

await this._setupDiscreetMode();
}

_setupDiscreetMode = async () => {
await this.getDiscreetModeSettingsRequest.execute();
const isDiscreetModeEnabled = this.getDiscreetModeSettingsRequest.result;
runInAction('Initialize discreet mode variables', () => {
this.openInDiscreetMode = isDiscreetModeEnabled;
this.isDiscreetMode = isDiscreetModeEnabled;
});
};

@action toggleDiscreetMode = () => {
this.isDiscreetMode = !this.isDiscreetMode;
};

@action toggleOpenInDiscreetMode = async () => {
const nextSetting = !this.openInDiscreetMode;
await this.setDiscreetModeSettingsRequest.execute(nextSetting);
runInAction('Update open in discreet mode settings', () => {
this.openInDiscreetMode = nextSetting;
});
};
}
2 changes: 1 addition & 1 deletion source/renderer/app/features/discreet-mode/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow

export { useDiscreetModeFeature } from './context';
export { useDiscreetModeFeature, DiscreetModeFeatureProvider } from './context';
export { DiscreetValue } from './ui';
3 changes: 3 additions & 0 deletions source/renderer/app/features/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow

export * from './discreet-mode';
5 changes: 4 additions & 1 deletion source/renderer/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Action from './actions/lib/Action';
import translations from './i18n/translations';
import '!style-loader!css-loader!sass-loader!./themes/index.global.scss'; // eslint-disable-line
import { setupApi } from './api/index';
import { DiscreetModeFeatureProvider } from './features';

// run MobX in strict mode
configure({
Expand Down Expand Up @@ -50,7 +51,9 @@ const initializeDaedalus = () => {
const rootElement = document.getElementById('root');
if (!rootElement) throw new Error('No #root element found.');
render(
<App stores={stores} actions={actions} history={history} />,
<DiscreetModeFeatureProvider>
<App stores={stores} actions={actions} history={history} />
</DiscreetModeFeatureProvider>,
rootElement
);
};
Expand Down
39 changes: 0 additions & 39 deletions source/renderer/app/stores/AppStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { observable, computed, action, runInAction } from 'mobx';
import path from 'path';
import Store from './lib/Store';
import Request from './lib/LocalizedRequest';
import LocalizableError from '../i18n/LocalizableError';
import { buildRoute } from '../utils/routing';
import { ROUTES } from '../routes-config';
Expand All @@ -24,16 +23,6 @@ export default class AppStore extends Store {
@observable gpuStatus: ?GpuStatus = null;
@observable activeDialog: ApplicationDialog = null;
@observable newsFeedIsOpen: boolean = false;
@observable isDiscreetMode: boolean = false;
@observable openInDiscreetMode: boolean = false;

@observable getDiscreetModeSettingsRequest: Request<
Promise<boolean>
> = new Request(this.api.localStorage.getDiscreetModeSettings);

@observable setDiscreetModeSettingsRequest: Request<
Promise<boolean>
> = new Request(this.api.localStorage.setDiscreetModeSettings);

setup() {
this.actions.router.goToRoute.listen(this._updateRouteLocation);
Expand Down Expand Up @@ -62,15 +51,8 @@ export default class AppStore extends Store {
this.actions.app.toggleNewsFeed.listen(this._toggleNewsFeed);
this.actions.app.closeNewsFeed.listen(this._closeNewsFeed);

this.actions.app.toggleDiscreetMode.listen(this._toggleDiscreetMode);
this.actions.app.toggleOpenInDiscreetMode.listen(
this._toggleOpenInDiscreetMode
);

toggleUiPartChannel.onReceive(this.toggleUiPart);
showUiPartChannel.onReceive(this.showUiPart);

this._setupDiscreetMode();
}

@computed get currentRoute(): string {
Expand Down Expand Up @@ -161,15 +143,6 @@ export default class AppStore extends Store {
});
};

_setupDiscreetMode = async () => {
await this.getDiscreetModeSettingsRequest.execute();
const isDiscreetModeEnabled = this.getDiscreetModeSettingsRequest.result;
runInAction('Initialize discreet mode variables', () => {
this.openInDiscreetMode = isDiscreetModeEnabled;
this.isDiscreetMode = isDiscreetModeEnabled;
});
};

@action _updateRouteLocation = (options: {
route: string,
params?: ?Object,
Expand Down Expand Up @@ -212,16 +185,4 @@ export default class AppStore extends Store {
@action _setIsDownloadingLogs = (isDownloadNotificationVisible: boolean) => {
this.isDownloadNotificationVisible = isDownloadNotificationVisible;
};

@action _toggleDiscreetMode = () => {
this.isDiscreetMode = !this.isDiscreetMode;
};

@action _toggleOpenInDiscreetMode = async () => {
const nextSetting = !this.openInDiscreetMode;
await this.setDiscreetModeSettingsRequest.execute(nextSetting);
runInAction('Update open in discreet mode settings', () => {
this.openInDiscreetMode = nextSetting;
});
};
}

0 comments on commit fefa1e8

Please sign in to comment.