From f5bd46f605fdb430c9c466f623903de7ec61d87e Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Sun, 10 Nov 2019 23:18:00 +0300 Subject: [PATCH] Fixed realm to asyncstorage issues --- src/components/sideMenu/view/sideMenuView.js | 11 +++++- src/containers/transferContainer.js | 4 +- src/providers/steem/auth.js | 12 +++--- src/realm/realm.js | 38 +++++++++++++------ src/redux/store/store.js | 1 + .../container/applicationContainer.js | 2 +- .../pinCode/container/pinCodeContainer.js | 33 ++++++++-------- 7 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/components/sideMenu/view/sideMenuView.js b/src/components/sideMenu/view/sideMenuView.js index c5a89a31e..9f315ae9f 100644 --- a/src/components/sideMenu/view/sideMenuView.js +++ b/src/components/sideMenu/view/sideMenuView.js @@ -4,6 +4,7 @@ import { injectIntl } from 'react-intl'; import LinearGradient from 'react-native-linear-gradient'; import ActionSheet from 'react-native-actionsheet'; import VersionNumber from 'react-native-version-number'; +import { getStorageType } from '../../../realm/realm'; // Components import { IconButton } from '../../buttons'; @@ -31,10 +32,16 @@ class SideMenuView extends Component { this.state = { menuItems: props.isLoggedIn ? MENU.AUTH_MENU_ITEMS : MENU.NO_AUTH_MENU_ITEMS, isAddAccountIconActive: false, + storageT: 'R', }; } // Component Life Cycles + componentDidMount() { + getStorageType().then(item => { + this.setState({ storageT: item }); + }); + } componentWillReceiveProps(nextProps) { const { isLoggedIn, accounts } = this.props; @@ -87,7 +94,7 @@ class SideMenuView extends Component { render() { const { currentAccount, isLoggedIn, intl, handleLogout } = this.props; - const { menuItems, isAddAccountIconActive } = this.state; + const { menuItems, isAddAccountIconActive, storageT } = this.state; const { version } = PackageJson; const { buildVersion } = VersionNumber; @@ -169,7 +176,7 @@ class SideMenuView extends Component { )} /> - {`v${version}, ${buildVersion}`} + {`v${version}, ${buildVersion}${storageT}`} (this.ActionSheet = o)} options={[ diff --git a/src/containers/transferContainer.js b/src/containers/transferContainer.js index e4562c6b0..149f3fc54 100644 --- a/src/containers/transferContainer.js +++ b/src/containers/transferContainer.js @@ -107,7 +107,7 @@ class TransferContainer extends Component { return validUsers; }; - _transferToAccount = (from, destination, amount, memo) => { + _transferToAccount = async (from, destination, amount, memo) => { const { pinCode, navigation, dispatch, intl } = this.props; let { currentAccount } = this.props; const { selectedAccount } = this.state; @@ -160,7 +160,7 @@ class TransferContainer extends Component { break; } if (!currentAccount.local) { - const realmData = getUserDataWithUsername(currentAccount.name); + const realmData = await getUserDataWithUsername(currentAccount.name); currentAccount.local = realmData[0]; } diff --git a/src/providers/steem/auth.js b/src/providers/steem/auth.js index 4b2c5676e..594ac64c6 100644 --- a/src/providers/steem/auth.js +++ b/src/providers/steem/auth.js @@ -32,7 +32,7 @@ export const login = async (username, password, isPinCodeOpen) => { if (!account) { return Promise.reject(new Error('auth.invalid_username')); } - if (isLoggedInUser(username)) { + if (await isLoggedInUser(username)) { return Promise.reject(new Error('auth.already_logged')); } // Public keys of user @@ -144,7 +144,7 @@ export const loginWithSC2 = async (code, isPinCodeOpen) => { account.local.avatar = avatar; } - if (isLoggedInUser(account.name)) { + if (await isLoggedInUser(account.name)) { reject(new Error('auth.already_logged')); } @@ -167,7 +167,7 @@ export const loginWithSC2 = async (code, isPinCodeOpen) => { export const setUserDataWithPinCode = async data => { try { - const result = getUserDataWithUsername(data.username); + const result = await getUserDataWithUsername(data.username); const userData = result[0]; if (!data.password) { @@ -234,7 +234,7 @@ export const updatePinCode = data => export const verifyPinCode = async data => { const pinHash = await getPinCode(); - const result = getUserDataWithUsername(data.username); + const result = await getUserDataWithUsername(data.username); const userData = result[0]; // This is migration for new pin structure, it will remove v2.2 @@ -338,8 +338,8 @@ export const getUpdatedUserData = (userData, data) => { }; }; -const isLoggedInUser = username => { - const result = getUserDataWithUsername(username); +const isLoggedInUser = async username => { + const result = await getUserDataWithUsername(username); if (result.length > 0) { return true; } diff --git a/src/realm/realm.js b/src/realm/realm.js index 275f644d5..f797b8594 100644 --- a/src/realm/realm.js +++ b/src/realm/realm.js @@ -9,6 +9,7 @@ const AUTH_SCHEMA = 'auth'; const DRAFT_SCHEMA = 'draft'; const SETTINGS_SCHEMA = 'settings'; const APPLICATION_SCHEMA = 'application'; +const STORAGE_SCHEMA = 'storage'; const userSchema = { name: USER_SCHEMA, @@ -174,6 +175,7 @@ export const getAllData = async () => { [DRAFT_SCHEMA, JSON.stringify(draft)], [SETTINGS_SCHEMA, JSON.stringify(setting)], [APPLICATION_SCHEMA, JSON.stringify(application)], + [STORAGE_SCHEMA, 'A'], ]; AsyncStorage.multiSet(data); } @@ -388,8 +390,8 @@ export const getPinCode = async () => { export const getPinCodeOpen = async () => { try { const setting = await getItemFromStorage(SETTINGS_SCHEMA); - if (setting[0]) { - return setting[0].isPinCodeOpen; + if (setting) { + return setting.isPinCodeOpen; } return false; } catch (error) { @@ -426,8 +428,8 @@ export const setTheme = async isDarkTheme => { export const getTheme = async () => { try { const setting = await getItemFromStorage(SETTINGS_SCHEMA); - if (setting[0]) { - return setting[0].isDarkTheme; + if (setting) { + return setting.isDarkTheme; } return false; } catch (error) { @@ -464,8 +466,8 @@ export const setUpvotePercent = async percent => { export const getUpvotePercent = async () => { try { const setting = await getItemFromStorage(SETTINGS_SCHEMA); - if (setting[0]) { - return setting[0].upvotePercent; + if (setting) { + return setting.upvotePercent; } return false; } catch (error) { @@ -476,8 +478,8 @@ export const getUpvotePercent = async () => { export const getNsfw = async () => { try { const setting = await getItemFromStorage(SETTINGS_SCHEMA); - if (setting[0]) { - return setting[0].nsfw; + if (setting) { + return setting.nsfw; } return false; } catch (error) { @@ -577,8 +579,8 @@ export const setCurrency = async currencyProps => { export const getLanguage = async () => { try { const setting = await getItemFromStorage(SETTINGS_SCHEMA); - if (setting[0]) { - return setting[0].language; + if (setting) { + return setting.language; } return false; } catch (error) { @@ -589,8 +591,8 @@ export const getLanguage = async () => { export const getServer = async () => { try { const setting = await getItemFromStorage(SETTINGS_SCHEMA); - if (setting[0]) { - return setting[0].server; + if (setting) { + return setting.server; } return false; } catch (error) { @@ -735,3 +737,15 @@ export const removeSCAccount = async username => { return error; } }; + +export const getStorageType = async () => { + try { + const storageType = await AsyncStorage.getItem(STORAGE_SCHEMA); + if (storageType !== null) { + return storageType; + } + return 'R'; + } catch (error) { + return error; + } +}; diff --git a/src/redux/store/store.js b/src/redux/store/store.js index ad38eefa4..b15047a7f 100644 --- a/src/redux/store/store.js +++ b/src/redux/store/store.js @@ -14,6 +14,7 @@ const persistConfig = { storage: AsyncStorage, // Blacklist (Don't Save Specific Reducers) blacklist: ['nav', 'application'], + timeout: 0, }; // Middleware: Redux Persist Persisted Reducer diff --git a/src/screens/application/container/applicationContainer.js b/src/screens/application/container/applicationContainer.js index 4e3acb206..fe0c534d7 100644 --- a/src/screens/application/container/applicationContainer.js +++ b/src/screens/application/container/applicationContainer.js @@ -638,7 +638,7 @@ class ApplicationContainer extends Component { const accountData = await switchAccount(targetAccountUsername); - const realmData = getUserDataWithUsername(targetAccountUsername); + const realmData = await getUserDataWithUsername(targetAccountUsername); const _currentAccount = accountData; _currentAccount.username = accountData.name; [_currentAccount.local] = realmData; diff --git a/src/screens/pinCode/container/pinCodeContainer.js b/src/screens/pinCode/container/pinCodeContainer.js index 4548cec71..689c733e5 100644 --- a/src/screens/pinCode/container/pinCodeContainer.js +++ b/src/screens/pinCode/container/pinCodeContainer.js @@ -207,21 +207,22 @@ class PinCodeContainer extends Component { verifyPinCode(pinData) .then(() => { this._savePinCode(pin); - const realmData = getUserDataWithUsername(currentAccount.name); - const _currentAccount = currentAccount; - _currentAccount.username = _currentAccount.name; - [_currentAccount.local] = realmData; - dispatch(updateCurrentAccount({ ..._currentAccount })); - dispatch(closePinCodeModal()); - if (callback) callback(pin, oldPinCode); - if (navigateTo) { - const navigateAction = NavigationActions.navigate({ - routeName: navigateTo, - params: navigateParams, - action: NavigationActions.navigate({ routeName: navigateTo }), - }); - dispatch(navigateAction); - } + getUserDataWithUsername(currentAccount.name).then(realmData => { + const _currentAccount = currentAccount; + _currentAccount.username = _currentAccount.name; + [_currentAccount.local] = realmData; + dispatch(updateCurrentAccount({ ..._currentAccount })); + dispatch(closePinCodeModal()); + if (callback) callback(pin, oldPinCode); + if (navigateTo) { + const navigateAction = NavigationActions.navigate({ + routeName: navigateTo, + params: navigateParams, + action: NavigationActions.navigate({ routeName: navigateTo }), + }); + dispatch(navigateAction); + } + }); }) .catch(err => { Alert.alert( @@ -246,7 +247,7 @@ class PinCodeContainer extends Component { const { intl, currentAccount, applicationPinCode } = this.props; const { isExistUser, pinCode } = this.state; - const realmData = getUserDataWithUsername(currentAccount.name); + const realmData = await getUserDataWithUsername(currentAccount.name); const userData = realmData[0]; // For exist users