removed getSettings method from applicationContainer in favour of MigrationHelpers

This commit is contained in:
Nouman Tahir 2022-07-04 21:57:59 +05:00
parent f19320f90d
commit f00bbff0d3
2 changed files with 88 additions and 53 deletions

View File

@ -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
}

View File

@ -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;