Changed async storage with real

This commit is contained in:
Mustafa Buyukcelebi 2018-12-03 22:09:15 +03:00
parent d54e8aaee2
commit 205bf14ecf
7 changed files with 61 additions and 33 deletions

View File

@ -1,4 +0,0 @@
export default {
IS_EXIST_USER: 'IS_EXIST_USER',
PUSH_TOKEN_SAVED: 'PUSH_TOKEN_SAVED',
};

View File

@ -2,7 +2,6 @@ import { Client, PrivateKey } from 'dsteem';
import { AsyncStorage } from 'react-native'; import { AsyncStorage } from 'react-native';
import { getUnreadActivityCount } from '../esteem/esteem'; import { getUnreadActivityCount } from '../esteem/esteem';
import sc2 from './steemConnectAPI';
// Utils // Utils
import { decryptKey } from '../../utils/crypto'; import { decryptKey } from '../../utils/crypto';

View File

@ -46,6 +46,7 @@ const applicationSchema = {
name: APPLICATION_SCHEMA, name: APPLICATION_SCHEMA,
properties: { properties: {
isPushTokenSaved: { type: 'bool', default: false }, isPushTokenSaved: { type: 'bool', default: false },
isExistUser: { type: 'bool', default: false },
}, },
}; };
@ -404,3 +405,40 @@ export const setPushTokenSaved = pushTokenSaved => new Promise((resolve, reject)
reject(error); reject(error);
} }
}); });
export const getExistUser = () => new Promise((resolve, reject) => {
try {
const application = realm.objects(APPLICATION_SCHEMA);
if (!application[0]) {
setExistUser(false);
resolve(false);
}
if (application[0].isExistUser) {
resolve((application[0].isExistUser));
} else {
resolve(false);
}
} catch (error) {
reject(error);
}
});
export const setExistUser = existUser => new Promise((resolve, reject) => {
try {
const application = realm.objects(APPLICATION_SCHEMA);
realm.write(() => {
if (Array.from(application).length > 0) {
application[0].isExistUser = existUser;
resolve(application[0]);
} else {
const applicationData = {
existUser: false,
};
realm.create(APPLICATION_SCHEMA, { ...applicationData });
resolve(applicationData);
}
});
} catch (error) {
reject(error);
}
});

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { AsyncStorage, Platform } from 'react-native'; import { Platform } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { addLocaleData } from 'react-intl'; import { addLocaleData } from 'react-intl';
import Config from 'react-native-config'; import Config from 'react-native-config';
@ -8,7 +8,6 @@ import AppCenter from 'appcenter';
// Constants // Constants
import en from 'react-intl/locale-data/en'; import en from 'react-intl/locale-data/en';
import tr from 'react-intl/locale-data/tr'; import tr from 'react-intl/locale-data/tr';
import INITIAL from '../../../constants/initial';
// Services // Services
import { import {
@ -17,6 +16,7 @@ import {
getSettings, getSettings,
getPushTokenSaved, getPushTokenSaved,
setPushTokenSaved, setPushTokenSaved,
getExistUser,
} from '../../../realm/realm'; } from '../../../realm/realm';
import { getUser } from '../../../providers/steem/dsteem'; import { getUser } from '../../../providers/steem/dsteem';
import { setPushToken } from '../../../providers/esteem/esteem'; import { setPushToken } from '../../../providers/esteem/esteem';
@ -141,8 +141,8 @@ class ApplicationContainer extends Component {
const { notificationSettings } = this.props; const { notificationSettings } = this.props;
const token = await AppCenter.getInstallId(); const token = await AppCenter.getInstallId();
AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { getExistUser().then((isExistUser) => {
if (JSON.parse(result)) { if (isExistUser) {
getPushTokenSaved().then((isPushTokenSaved) => { getPushTokenSaved().then((isPushTokenSaved) => {
if (!isPushTokenSaved) { if (!isPushTokenSaved) {
const data = { const data = {

View File

@ -1,20 +1,15 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { AsyncStorage } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { injectIntl } from 'react-intl'; import { injectIntl } from 'react-intl';
import { setUserDataWithPinCode, verifyPinCode } from '../../../providers/steem/auth'; import { setUserDataWithPinCode, verifyPinCode } from '../../../providers/steem/auth';
// Actions // Actions & Services
import { closePinCodeModal } from '../../../redux/actions/applicationActions'; import { closePinCodeModal } from '../../../redux/actions/applicationActions';
import { getExistUser, setExistUser } from '../../../realm/realm';
// Constants
import { default as INITIAL } from '../../../constants/initial';
import { PinCodeScreen } from '..'; import { PinCodeScreen } from '..';
const DEFAULT_IMAGE = require('../../../assets/esteem.png');
class PinCodeContainer extends Component { class PinCodeContainer extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
@ -48,10 +43,10 @@ class PinCodeContainer extends Component {
} }
_getDataFromStorage = () => new Promise((resolve) => { _getDataFromStorage = () => new Promise((resolve) => {
AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { getExistUser().then((isExistUser) => {
this.setState( this.setState(
{ {
isExistUser: JSON.parse(result), isExistUser,
}, },
resolve, resolve,
); );
@ -106,7 +101,7 @@ class PinCodeContainer extends Component {
accessToken, accessToken,
}; };
setUserDataWithPinCode(pinData).then(() => { setUserDataWithPinCode(pinData).then(() => {
AsyncStorage.setItem(INITIAL.IS_EXIST_USER, JSON.stringify(true), () => { setExistUser(true).then(() => {
dispatch(closePinCodeModal()); dispatch(closePinCodeModal());
if (navigateTo) { if (navigateTo) {
navigation.navigate(navigateTo); navigation.navigate(navigateTo);

View File

@ -1,10 +1,11 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import { AppState, AsyncStorage } from 'react-native'; import { AppState } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import Push from 'appcenter-push'; import Push from 'appcenter-push';
// Actions // Actions & Services
import { openPinCodeModal } from '../../../redux/actions/applicationActions'; import { openPinCodeModal } from '../../../redux/actions/applicationActions';
import { getExistUser } from '../../../realm/realm';
// Components // Components
import { Modal } from '../../../components'; import { Modal } from '../../../components';
@ -12,7 +13,6 @@ import { PinCode } from '../..';
// Constants // Constants
import ROUTES from '../../../constants/routeNames'; import ROUTES from '../../../constants/routeNames';
import INITIAL from '../../../constants/initial';
const RootContainer = () => (WrappedComponent) => { const RootContainer = () => (WrappedComponent) => {
class RootComponent extends Component { class RootComponent extends Component {
@ -45,9 +45,9 @@ const RootContainer = () => (WrappedComponent) => {
const { appState } = this.state; const { appState } = this.state;
const { dispatch } = this.props; const { dispatch } = this.props;
AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { getExistUser().then((isExistUser) => {
if ( if (
JSON.parse(result) isExistUser
&& appState.match(/inactive|background/) && appState.match(/inactive|background/)
&& nextAppState === 'active' && nextAppState === 'active'
&& __DEV__ === false && __DEV__ === false
@ -55,7 +55,6 @@ const RootContainer = () => (WrappedComponent) => {
dispatch(openPinCodeModal()); dispatch(openPinCodeModal());
} }
}); });
this.setState({ appState: nextAppState }); this.setState({ appState: nextAppState });
}; };

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { AsyncStorage, Platform } from 'react-native'; import { Platform } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import AppCenter from 'appcenter'; import AppCenter from 'appcenter';
@ -10,6 +10,7 @@ import {
setCurrency as setCurrency2DB, setCurrency as setCurrency2DB,
setServer, setServer,
setNotificationIsOpen, setNotificationIsOpen,
getExistUser,
} from '../../../realm/realm'; } from '../../../realm/realm';
// Services and Actions // Services and Actions
@ -28,7 +29,6 @@ import { setPushToken } from '../../../providers/esteem/esteem';
import { VALUE as CURRENCY_VALUE } from '../../../constants/options/currency'; import { VALUE as CURRENCY_VALUE } from '../../../constants/options/currency';
import { VALUE as LANGUAGE_VALUE } from '../../../constants/options/language'; import { VALUE as LANGUAGE_VALUE } from '../../../constants/options/language';
import API_VALUE from '../../../constants/options/api'; import API_VALUE from '../../../constants/options/api';
import INITIAL from '../../../constants/initial';
// Utilities // Utilities
@ -36,10 +36,10 @@ import INITIAL from '../../../constants/initial';
import { SettingsScreen } from '..'; import { SettingsScreen } from '..';
/* /*
* Props Name Description Value * Props Name Description Value
*@props --> props name here description here Value Type Here *@props --> props name here description here Value Type Here
* *
*/ */
class SettingsContainer extends Component { class SettingsContainer extends Component {
constructor(props) { constructor(props) {
@ -116,8 +116,9 @@ class SettingsContainer extends Component {
const { notificationSettings, isLoggedIn, username } = this.props; const { notificationSettings, isLoggedIn, username } = this.props;
if (isLoggedIn) { if (isLoggedIn) {
const token = await AppCenter.getInstallId(); const token = await AppCenter.getInstallId();
AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => {
if (JSON.parse(result)) { getExistUser().then((isExistUser) => {
if (isExistUser) {
const data = { const data = {
username, username,
token, token,