From f00bbff0d3922e9acf29fcb3ab93081251c955d2 Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Mon, 4 Jul 2022 21:57:59 +0500 Subject: [PATCH] removed getSettings method from applicationContainer in favour of MigrationHelpers --- .../application/children/migrationHelpers.ts | 83 +++++++++++++++++++ .../container/applicationContainer.tsx | 58 ++----------- 2 files changed, 88 insertions(+), 53 deletions(-) create mode 100644 src/screens/application/children/migrationHelpers.ts diff --git a/src/screens/application/children/migrationHelpers.ts b/src/screens/application/children/migrationHelpers.ts new file mode 100644 index 000000000..4fa87aebb --- /dev/null +++ b/src/screens/application/children/migrationHelpers.ts @@ -0,0 +1,83 @@ +import { Appearance } from 'react-native'; + +// Constants +import THEME_OPTIONS from '../../../constants/options/theme'; + +// Services +import { + getSettings, +} from '../../../realm/realm'; + +import { + isDarkTheme, + changeNotificationSettings, + changeAllNotificationSettings, + setApi, + setCurrency, + setLanguage, + setUpvotePercent, + setNsfw, + isDefaultFooter, + isPinCodeOpen, + setColorTheme, + setSettingsMigrated, +} from '../../../redux/actions/applicationActions'; +import { + hideActionModal, + hideProfileModal, + setRcOffer, + toastNotification, +} from '../../../redux/actions/uiAction'; + + +//migrates settings from realm to redux once and do no user realm for settings again; +export const migrateSettings = async (dispatch: any, settingsMigrated: boolean) => { + + if (settingsMigrated) { + return; + } + + //reset certain properties + dispatch(hideActionModal()); + dispatch(hideProfileModal()); + dispatch(toastNotification('')); + dispatch(setRcOffer(false)); + + + const settings = await getSettings(); + + if (settings) { + const isDarkMode = Appearance.getColorScheme() === 'dark'; + dispatch(isDarkTheme(settings.isDarkTheme !== null ? settings.isDarkTheme : isDarkMode)); + dispatch(setColorTheme(THEME_OPTIONS.findIndex(item => item.value === settings.isDarkTheme))); + if (settings.isPinCodeOpen !== '') await dispatch(isPinCodeOpen(settings.isPinCodeOpen)); + if (settings.language !== '') dispatch(setLanguage(settings.language)); + if (settings.server !== '') dispatch(setApi(settings.server)); + if (settings.upvotePercent !== '') { + dispatch(setUpvotePercent(Number(settings.upvotePercent))); + } + if (settings.isDefaultFooter !== '') dispatch(isDefaultFooter(settings.isDefaultFooter)); //TODO: remove as not being used + + + if (settings.nsfw !== '') dispatch(setNsfw(settings.nsfw)); + + dispatch(setCurrency(settings.currency !== '' ? settings.currency : 'usd')); + + if (settings.notification !== '') { + dispatch( + changeNotificationSettings({ + type: 'notification', + action: settings.notification, + }), + ); + + dispatch(changeAllNotificationSettings(settings)); + } + + await dispatch(setSettingsMigrated(true)) + } +} + +export default { + migrateSettings +} \ No newline at end of file diff --git a/src/screens/application/container/applicationContainer.tsx b/src/screens/application/container/applicationContainer.tsx index 0ca3ff7d6..ebb64cb9b 100644 --- a/src/screens/application/container/applicationContainer.tsx +++ b/src/screens/application/container/applicationContainer.tsx @@ -104,6 +104,7 @@ import { setMomentLocale } from '../../../utils/time'; import parseAuthUrl from '../../../utils/parseAuthUrl'; import { purgeExpiredCache } from '../../../redux/actions/cacheActions'; import { fetchSubscribedCommunities } from '../../../redux/actions/communitiesAction'; +import MigrationHelpers from '../children/migrationHelpers'; // Workaround let previousAppState = 'background'; @@ -432,12 +433,14 @@ class ApplicationContainer extends Component { _fetchApp = async () => { - await this._getSettings(); + const {dispatch, settingsMigrated} = this.props; + + await MigrationHelpers.migrateSettings(dispatch, settingsMigrated) this._refreshGlobalProps(); await this._getUserDataFromRealm(); this._compareAndPromptForUpdate(); this._registerDeviceForNotifications(); - this.props.dispatch(purgeExpiredCache()); + dispatch(purgeExpiredCache()); }; _pushNavigate = (notification) => { @@ -753,57 +756,6 @@ class ApplicationContainer extends Component { - //TODO rename to migrateSettings and move out of application container - _getSettings = async () => { - const { dispatch, settingsMigrated } = this.props; - - if(settingsMigrated){ - return; - } - - //reset certain properties - dispatch(hideActionModal()); - dispatch(hideProfileModal()); - dispatch(toastNotification('')); - dispatch(setRcOffer(false)); - - - const settings = await getSettings(); - - if (settings) { - const isDarkMode = Appearance.getColorScheme() === 'dark'; - dispatch(isDarkTheme(settings.isDarkTheme !== null ? settings.isDarkTheme : isDarkMode)); - dispatch(setColorTheme(THEME_OPTIONS.findIndex(item=>item.value===settings.isDarkTheme))); - if (settings.isPinCodeOpen !== '') await dispatch(isPinCodeOpen(settings.isPinCodeOpen)); - if (settings.language !== '') dispatch(setLanguage(settings.language)); - if (settings.server !== '') dispatch(setApi(settings.server)); - if (settings.upvotePercent !== '') { - dispatch(setUpvotePercent(Number(settings.upvotePercent))); - } - if (settings.isDefaultFooter !== '') dispatch(isDefaultFooter(settings.isDefaultFooter)); //TODO: remove as not being used - - - if (settings.nsfw !== '') dispatch(setNsfw(settings.nsfw)); - - dispatch(setCurrency(settings.currency !== '' ? settings.currency : 'usd')); - - if (settings.notification !== '') { - dispatch( - changeNotificationSettings({ - type: 'notification', - action: settings.notification, - }), - ); - - dispatch(changeAllNotificationSettings(settings)); - } - - await dispatch(setSettingsMigrated(true)) - } - }; - - - //update notification settings and update push token for each signed accoutn useing access tokens _registerDeviceForNotifications = (settings?:any) => { const { otherAccounts, notificationDetails, isNotificationsEnabled } = this.props;