From fefa1e87feb1f321e4bf28ae24114251bf444546 Mon Sep 17 00:00:00 2001 From: Renan Ferreira Date: Mon, 15 Nov 2021 15:16:08 -0300 Subject: [PATCH] [DDW-813] Move discreet mode behavior to its own feature --- source/renderer/app/actions/app-actions.js | 4 -- source/renderer/app/api/utils/localStorage.js | 9 ---- .../app/features/discreet-mode/api/index.js | 14 ++++++ .../app/features/discreet-mode/context.js | 13 +++++- .../app/features/discreet-mode/feature.js | 43 ++++++++++++++++++- .../app/features/discreet-mode/index.js | 2 +- source/renderer/app/features/index.js | 3 ++ source/renderer/app/index.js | 5 ++- source/renderer/app/stores/AppStore.js | 39 ----------------- 9 files changed, 76 insertions(+), 56 deletions(-) create mode 100644 source/renderer/app/features/discreet-mode/api/index.js create mode 100644 source/renderer/app/features/index.js diff --git a/source/renderer/app/actions/app-actions.js b/source/renderer/app/actions/app-actions.js index c19aed84f3..c7d6bac45a 100644 --- a/source/renderer/app/actions/app-actions.js +++ b/source/renderer/app/actions/app-actions.js @@ -19,8 +19,4 @@ export default class AppActions { // Daedalus Diagnostics dialog actions closeDaedalusDiagnosticsDialog: Action = new Action(); openDaedalusDiagnosticsDialog: Action = new Action(); - - // Discreet mode actions - toggleDiscreetMode: Action = new Action(); - toggleOpenInDiscreetMode: Action = new Action(); } diff --git a/source/renderer/app/api/utils/localStorage.js b/source/renderer/app/api/utils/localStorage.js index 0ac354ac61..1245aee667 100644 --- a/source/renderer/app/api/utils/localStorage.js +++ b/source/renderer/app/api/utils/localStorage.js @@ -492,13 +492,4 @@ export default class LocalStorageApi { reset = async () => { await LocalStorageApi.reset(); }; - - getDiscreetModeSettings = (): Promise => - LocalStorageApi.get(keys.DISCREET_MODE_ENABLED, false); - - setDiscreetModeSettings = (enabled: boolean): Promise => - LocalStorageApi.set(keys.DISCREET_MODE_ENABLED, enabled); - - unsetDiscreetModeSettings = (): Promise => - LocalStorageApi.unset(keys.DISCREET_MODE_ENABLED); } diff --git a/source/renderer/app/features/discreet-mode/api/index.js b/source/renderer/app/features/discreet-mode/api/index.js new file mode 100644 index 0000000000..1163e67ef0 --- /dev/null +++ b/source/renderer/app/features/discreet-mode/api/index.js @@ -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 => + LocalStorageApi.get(storageKeys.DISCREET_MODE_ENABLED, false); + + setDiscreetModeSettings = (enabled: boolean): Promise => + LocalStorageApi.set(storageKeys.DISCREET_MODE_ENABLED, enabled); + + unsetDiscreetModeSettings = (): Promise => + LocalStorageApi.unset(storageKeys.DISCREET_MODE_ENABLED); +} diff --git a/source/renderer/app/features/discreet-mode/context.js b/source/renderer/app/features/discreet-mode/context.js index 57af9a4041..b5deadf235 100644 --- a/source/renderer/app/features/discreet-mode/context.js +++ b/source/renderer/app/features/discreet-mode/context.js @@ -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( null @@ -18,7 +20,16 @@ interface Props { } export const DiscreetModeFeatureProvider = ({ children }: Props) => { - const [discreetModeFeature] = useState(new DiscreetMode()); + const [discreetModeFeature] = useState(() => { + const feature = new DiscreetMode(new DiscreetModeApi()); + window.daedalus = merge(window.daedalus, { + features: { + discreetModeFeature: feature, + }, + }); + + return feature; + }); useFeature(discreetModeFeature); diff --git a/source/renderer/app/features/discreet-mode/feature.js b/source/renderer/app/features/discreet-mode/feature.js index a5a99425fb..82f2615e89 100644 --- a/source/renderer/app/features/discreet-mode/feature.js +++ b/source/renderer/app/features/discreet-mode/feature.js @@ -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 + > = new Request(this.api.getDiscreetModeSettings); + + @observable setDiscreetModeSettingsRequest: Request< + Promise + > = 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; + }); + }; } diff --git a/source/renderer/app/features/discreet-mode/index.js b/source/renderer/app/features/discreet-mode/index.js index f52d92cb47..d03b28d3ac 100644 --- a/source/renderer/app/features/discreet-mode/index.js +++ b/source/renderer/app/features/discreet-mode/index.js @@ -1,4 +1,4 @@ // @flow -export { useDiscreetModeFeature } from './context'; +export { useDiscreetModeFeature, DiscreetModeFeatureProvider } from './context'; export { DiscreetValue } from './ui'; diff --git a/source/renderer/app/features/index.js b/source/renderer/app/features/index.js new file mode 100644 index 0000000000..fa01d4977f --- /dev/null +++ b/source/renderer/app/features/index.js @@ -0,0 +1,3 @@ +// @flow + +export * from './discreet-mode'; diff --git a/source/renderer/app/index.js b/source/renderer/app/index.js index f9da99fe5c..4df43bf5a3 100755 --- a/source/renderer/app/index.js +++ b/source/renderer/app/index.js @@ -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({ @@ -50,7 +51,9 @@ const initializeDaedalus = () => { const rootElement = document.getElementById('root'); if (!rootElement) throw new Error('No #root element found.'); render( - , + + + , rootElement ); }; diff --git a/source/renderer/app/stores/AppStore.js b/source/renderer/app/stores/AppStore.js index 0a3e0b2f0e..dc9743f814 100644 --- a/source/renderer/app/stores/AppStore.js +++ b/source/renderer/app/stores/AppStore.js @@ -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'; @@ -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 - > = new Request(this.api.localStorage.getDiscreetModeSettings); - - @observable setDiscreetModeSettingsRequest: Request< - Promise - > = new Request(this.api.localStorage.setDiscreetModeSettings); setup() { this.actions.router.goToRoute.listen(this._updateRouteLocation); @@ -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 { @@ -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, @@ -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; - }); - }; }