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 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 {
)}
/>
</View>
<Text style={styles.versionText}>{`v${version}, ${buildVersion}`}</Text>
<Text style={styles.versionText}>{`v${version}, ${buildVersion}${storageT}`}</Text>
<ActionSheet
ref={o => (this.ActionSheet = o)}
options={[

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -207,7 +207,7 @@ class PinCodeContainer extends Component {
verifyPinCode(pinData)
.then(() => {
this._savePinCode(pin);
const realmData = getUserDataWithUsername(currentAccount.name);
getUserDataWithUsername(currentAccount.name).then(realmData => {
const _currentAccount = currentAccount;
_currentAccount.username = _currentAccount.name;
[_currentAccount.local] = realmData;
@ -222,6 +222,7 @@ class PinCodeContainer extends Component {
});
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