Fixed realm to asyncstorage issues

This commit is contained in:
Mustafa Buyukcelebi 2019-11-10 23:18:00 +03:00
parent d6b4a9e840
commit 09f72278f6
7 changed files with 62 additions and 39 deletions

View File

@ -4,6 +4,7 @@ import { injectIntl } from 'react-intl';
import LinearGradient from 'react-native-linear-gradient'; import LinearGradient from 'react-native-linear-gradient';
import ActionSheet from 'react-native-actionsheet'; import ActionSheet from 'react-native-actionsheet';
import VersionNumber from 'react-native-version-number'; import VersionNumber from 'react-native-version-number';
import { getStorageType } from '../../../realm/realm';
// Components // Components
import { IconButton } from '../../buttons'; import { IconButton } from '../../buttons';
@ -31,10 +32,16 @@ class SideMenuView extends Component {
this.state = { this.state = {
menuItems: props.isLoggedIn ? MENU.AUTH_MENU_ITEMS : MENU.NO_AUTH_MENU_ITEMS, menuItems: props.isLoggedIn ? MENU.AUTH_MENU_ITEMS : MENU.NO_AUTH_MENU_ITEMS,
isAddAccountIconActive: false, isAddAccountIconActive: false,
storageT: 'R',
}; };
} }
// Component Life Cycles // Component Life Cycles
componentDidMount() {
getStorageType().then(item => {
this.setState({ storageT: item });
});
}
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
const { isLoggedIn, accounts } = this.props; const { isLoggedIn, accounts } = this.props;
@ -87,7 +94,7 @@ class SideMenuView extends Component {
render() { render() {
const { currentAccount, isLoggedIn, intl, handleLogout } = this.props; const { currentAccount, isLoggedIn, intl, handleLogout } = this.props;
const { menuItems, isAddAccountIconActive } = this.state; const { menuItems, isAddAccountIconActive, storageT } = this.state;
const { version } = PackageJson; const { version } = PackageJson;
const { buildVersion } = VersionNumber; const { buildVersion } = VersionNumber;
@ -169,7 +176,7 @@ class SideMenuView extends Component {
)} )}
/> />
</View> </View>
<Text style={styles.versionText}>{`v${version}, ${buildVersion}`}</Text> <Text style={styles.versionText}>{`v${version}, ${buildVersion}${storageT}`}</Text>
<ActionSheet <ActionSheet
ref={o => (this.ActionSheet = o)} ref={o => (this.ActionSheet = o)}
options={[ options={[

View File

@ -107,7 +107,7 @@ class TransferContainer extends Component {
return validUsers; return validUsers;
}; };
_transferToAccount = (from, destination, amount, memo) => { _transferToAccount = async (from, destination, amount, memo) => {
const { pinCode, navigation, dispatch, intl } = this.props; const { pinCode, navigation, dispatch, intl } = this.props;
let { currentAccount } = this.props; let { currentAccount } = this.props;
const { selectedAccount } = this.state; const { selectedAccount } = this.state;
@ -160,7 +160,7 @@ class TransferContainer extends Component {
break; break;
} }
if (!currentAccount.local) { if (!currentAccount.local) {
const realmData = getUserDataWithUsername(currentAccount.name); const realmData = await getUserDataWithUsername(currentAccount.name);
currentAccount.local = realmData[0]; currentAccount.local = realmData[0];
} }

View File

@ -32,7 +32,7 @@ export const login = async (username, password, isPinCodeOpen) => {
if (!account) { if (!account) {
return Promise.reject(new Error('auth.invalid_username')); return Promise.reject(new Error('auth.invalid_username'));
} }
if (isLoggedInUser(username)) { if (await isLoggedInUser(username)) {
return Promise.reject(new Error('auth.already_logged')); return Promise.reject(new Error('auth.already_logged'));
} }
// Public keys of user // Public keys of user
@ -144,7 +144,7 @@ export const loginWithSC2 = async (code, isPinCodeOpen) => {
account.local.avatar = avatar; account.local.avatar = avatar;
} }
if (isLoggedInUser(account.name)) { if (await isLoggedInUser(account.name)) {
reject(new Error('auth.already_logged')); reject(new Error('auth.already_logged'));
} }
@ -167,7 +167,7 @@ export const loginWithSC2 = async (code, isPinCodeOpen) => {
export const setUserDataWithPinCode = async data => { export const setUserDataWithPinCode = async data => {
try { try {
const result = getUserDataWithUsername(data.username); const result = await getUserDataWithUsername(data.username);
const userData = result[0]; const userData = result[0];
if (!data.password) { if (!data.password) {
@ -234,7 +234,7 @@ export const updatePinCode = data =>
export const verifyPinCode = async data => { export const verifyPinCode = async data => {
const pinHash = await getPinCode(); const pinHash = await getPinCode();
const result = getUserDataWithUsername(data.username); const result = await getUserDataWithUsername(data.username);
const userData = result[0]; const userData = result[0];
// This is migration for new pin structure, it will remove v2.2 // 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 isLoggedInUser = async username => {
const result = getUserDataWithUsername(username); const result = await getUserDataWithUsername(username);
if (result.length > 0) { if (result.length > 0) {
return true; return true;
} }

View File

@ -9,6 +9,7 @@ const AUTH_SCHEMA = 'auth';
const DRAFT_SCHEMA = 'draft'; const DRAFT_SCHEMA = 'draft';
const SETTINGS_SCHEMA = 'settings'; const SETTINGS_SCHEMA = 'settings';
const APPLICATION_SCHEMA = 'application'; const APPLICATION_SCHEMA = 'application';
const STORAGE_SCHEMA = 'storage';
const userSchema = { const userSchema = {
name: USER_SCHEMA, name: USER_SCHEMA,
@ -174,6 +175,7 @@ export const getAllData = async () => {
[DRAFT_SCHEMA, JSON.stringify(draft)], [DRAFT_SCHEMA, JSON.stringify(draft)],
[SETTINGS_SCHEMA, JSON.stringify(setting)], [SETTINGS_SCHEMA, JSON.stringify(setting)],
[APPLICATION_SCHEMA, JSON.stringify(application)], [APPLICATION_SCHEMA, JSON.stringify(application)],
[STORAGE_SCHEMA, 'A'],
]; ];
AsyncStorage.multiSet(data); AsyncStorage.multiSet(data);
} }
@ -388,8 +390,8 @@ export const getPinCode = async () => {
export const getPinCodeOpen = async () => { export const getPinCodeOpen = async () => {
try { try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA); const setting = await getItemFromStorage(SETTINGS_SCHEMA);
if (setting[0]) { if (setting) {
return setting[0].isPinCodeOpen; return setting.isPinCodeOpen;
} }
return false; return false;
} catch (error) { } catch (error) {
@ -426,8 +428,8 @@ export const setTheme = async isDarkTheme => {
export const getTheme = async () => { export const getTheme = async () => {
try { try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA); const setting = await getItemFromStorage(SETTINGS_SCHEMA);
if (setting[0]) { if (setting) {
return setting[0].isDarkTheme; return setting.isDarkTheme;
} }
return false; return false;
} catch (error) { } catch (error) {
@ -464,8 +466,8 @@ export const setUpvotePercent = async percent => {
export const getUpvotePercent = async () => { export const getUpvotePercent = async () => {
try { try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA); const setting = await getItemFromStorage(SETTINGS_SCHEMA);
if (setting[0]) { if (setting) {
return setting[0].upvotePercent; return setting.upvotePercent;
} }
return false; return false;
} catch (error) { } catch (error) {
@ -476,8 +478,8 @@ export const getUpvotePercent = async () => {
export const getNsfw = async () => { export const getNsfw = async () => {
try { try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA); const setting = await getItemFromStorage(SETTINGS_SCHEMA);
if (setting[0]) { if (setting) {
return setting[0].nsfw; return setting.nsfw;
} }
return false; return false;
} catch (error) { } catch (error) {
@ -577,8 +579,8 @@ export const setCurrency = async currencyProps => {
export const getLanguage = async () => { export const getLanguage = async () => {
try { try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA); const setting = await getItemFromStorage(SETTINGS_SCHEMA);
if (setting[0]) { if (setting) {
return setting[0].language; return setting.language;
} }
return false; return false;
} catch (error) { } catch (error) {
@ -589,8 +591,8 @@ export const getLanguage = async () => {
export const getServer = async () => { export const getServer = async () => {
try { try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA); const setting = await getItemFromStorage(SETTINGS_SCHEMA);
if (setting[0]) { if (setting) {
return setting[0].server; return setting.server;
} }
return false; return false;
} catch (error) { } catch (error) {
@ -735,3 +737,15 @@ export const removeSCAccount = async username => {
return error; 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;
}
};

View File

@ -14,6 +14,7 @@ const persistConfig = {
storage: AsyncStorage, storage: AsyncStorage,
// Blacklist (Don't Save Specific Reducers) // Blacklist (Don't Save Specific Reducers)
blacklist: ['nav', 'application'], blacklist: ['nav', 'application'],
timeout: 0,
}; };
// Middleware: Redux Persist Persisted Reducer // Middleware: Redux Persist Persisted Reducer

View File

@ -638,7 +638,7 @@ class ApplicationContainer extends Component {
const accountData = await switchAccount(targetAccountUsername); const accountData = await switchAccount(targetAccountUsername);
const realmData = getUserDataWithUsername(targetAccountUsername); const realmData = await getUserDataWithUsername(targetAccountUsername);
const _currentAccount = accountData; const _currentAccount = accountData;
_currentAccount.username = accountData.name; _currentAccount.username = accountData.name;
[_currentAccount.local] = realmData; [_currentAccount.local] = realmData;

View File

@ -207,21 +207,22 @@ class PinCodeContainer extends Component {
verifyPinCode(pinData) verifyPinCode(pinData)
.then(() => { .then(() => {
this._savePinCode(pin); this._savePinCode(pin);
const realmData = getUserDataWithUsername(currentAccount.name); getUserDataWithUsername(currentAccount.name).then(realmData => {
const _currentAccount = currentAccount; const _currentAccount = currentAccount;
_currentAccount.username = _currentAccount.name; _currentAccount.username = _currentAccount.name;
[_currentAccount.local] = realmData; [_currentAccount.local] = realmData;
dispatch(updateCurrentAccount({ ..._currentAccount })); dispatch(updateCurrentAccount({ ..._currentAccount }));
dispatch(closePinCodeModal()); dispatch(closePinCodeModal());
if (callback) callback(pin, oldPinCode); if (callback) callback(pin, oldPinCode);
if (navigateTo) { if (navigateTo) {
const navigateAction = NavigationActions.navigate({ const navigateAction = NavigationActions.navigate({
routeName: navigateTo, routeName: navigateTo,
params: navigateParams, params: navigateParams,
action: NavigationActions.navigate({ routeName: navigateTo }), action: NavigationActions.navigate({ routeName: navigateTo }),
}); });
dispatch(navigateAction); dispatch(navigateAction);
} }
});
}) })
.catch(err => { .catch(err => {
Alert.alert( Alert.alert(
@ -246,7 +247,7 @@ class PinCodeContainer extends Component {
const { intl, currentAccount, applicationPinCode } = this.props; const { intl, currentAccount, applicationPinCode } = this.props;
const { isExistUser, pinCode } = this.state; const { isExistUser, pinCode } = this.state;
const realmData = getUserDataWithUsername(currentAccount.name); const realmData = await getUserDataWithUsername(currentAccount.name);
const userData = realmData[0]; const userData = realmData[0];
// For exist users // For exist users