From 205bf14ecf86a660b4353083bf68db4e284c3209 Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Mon, 3 Dec 2018 22:09:15 +0300 Subject: [PATCH] Changed async storage with real --- src/constants/initial.js | 4 -- src/providers/steem/dsteem.js | 1 - src/realm/realm.js | 38 +++++++++++++++++++ .../container/applicationContainer.js | 8 ++-- .../pinCode/container/pinCodeContainer.js | 15 +++----- src/screens/root/container/rootContainer.js | 11 +++--- .../settings/container/settingsContainer.js | 17 +++++---- 7 files changed, 61 insertions(+), 33 deletions(-) delete mode 100644 src/constants/initial.js diff --git a/src/constants/initial.js b/src/constants/initial.js deleted file mode 100644 index 02f573d3e..000000000 --- a/src/constants/initial.js +++ /dev/null @@ -1,4 +0,0 @@ -export default { - IS_EXIST_USER: 'IS_EXIST_USER', - PUSH_TOKEN_SAVED: 'PUSH_TOKEN_SAVED', -}; diff --git a/src/providers/steem/dsteem.js b/src/providers/steem/dsteem.js index 225ce943b..397716243 100644 --- a/src/providers/steem/dsteem.js +++ b/src/providers/steem/dsteem.js @@ -2,7 +2,6 @@ import { Client, PrivateKey } from 'dsteem'; import { AsyncStorage } from 'react-native'; import { getUnreadActivityCount } from '../esteem/esteem'; -import sc2 from './steemConnectAPI'; // Utils import { decryptKey } from '../../utils/crypto'; diff --git a/src/realm/realm.js b/src/realm/realm.js index 1d6ef8cae..947c12b99 100644 --- a/src/realm/realm.js +++ b/src/realm/realm.js @@ -46,6 +46,7 @@ const applicationSchema = { name: APPLICATION_SCHEMA, properties: { isPushTokenSaved: { type: 'bool', default: false }, + isExistUser: { type: 'bool', default: false }, }, }; @@ -404,3 +405,40 @@ export const setPushTokenSaved = pushTokenSaved => new Promise((resolve, reject) 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); + } +}); diff --git a/src/screens/application/container/applicationContainer.js b/src/screens/application/container/applicationContainer.js index e9a84c660..e12e4eabb 100644 --- a/src/screens/application/container/applicationContainer.js +++ b/src/screens/application/container/applicationContainer.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { AsyncStorage, Platform } from 'react-native'; +import { Platform } from 'react-native'; import { connect } from 'react-redux'; import { addLocaleData } from 'react-intl'; import Config from 'react-native-config'; @@ -8,7 +8,6 @@ import AppCenter from 'appcenter'; // Constants import en from 'react-intl/locale-data/en'; import tr from 'react-intl/locale-data/tr'; -import INITIAL from '../../../constants/initial'; // Services import { @@ -17,6 +16,7 @@ import { getSettings, getPushTokenSaved, setPushTokenSaved, + getExistUser, } from '../../../realm/realm'; import { getUser } from '../../../providers/steem/dsteem'; import { setPushToken } from '../../../providers/esteem/esteem'; @@ -141,8 +141,8 @@ class ApplicationContainer extends Component { const { notificationSettings } = this.props; const token = await AppCenter.getInstallId(); - AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { - if (JSON.parse(result)) { + getExistUser().then((isExistUser) => { + if (isExistUser) { getPushTokenSaved().then((isPushTokenSaved) => { if (!isPushTokenSaved) { const data = { diff --git a/src/screens/pinCode/container/pinCodeContainer.js b/src/screens/pinCode/container/pinCodeContainer.js index fe90076fd..02f10a704 100644 --- a/src/screens/pinCode/container/pinCodeContainer.js +++ b/src/screens/pinCode/container/pinCodeContainer.js @@ -1,20 +1,15 @@ import React, { Component } from 'react'; -import { AsyncStorage } from 'react-native'; import { connect } from 'react-redux'; import { injectIntl } from 'react-intl'; import { setUserDataWithPinCode, verifyPinCode } from '../../../providers/steem/auth'; -// Actions +// Actions & Services import { closePinCodeModal } from '../../../redux/actions/applicationActions'; - -// Constants -import { default as INITIAL } from '../../../constants/initial'; +import { getExistUser, setExistUser } from '../../../realm/realm'; import { PinCodeScreen } from '..'; -const DEFAULT_IMAGE = require('../../../assets/esteem.png'); - class PinCodeContainer extends Component { constructor(props) { super(props); @@ -48,10 +43,10 @@ class PinCodeContainer extends Component { } _getDataFromStorage = () => new Promise((resolve) => { - AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { + getExistUser().then((isExistUser) => { this.setState( { - isExistUser: JSON.parse(result), + isExistUser, }, resolve, ); @@ -106,7 +101,7 @@ class PinCodeContainer extends Component { accessToken, }; setUserDataWithPinCode(pinData).then(() => { - AsyncStorage.setItem(INITIAL.IS_EXIST_USER, JSON.stringify(true), () => { + setExistUser(true).then(() => { dispatch(closePinCodeModal()); if (navigateTo) { navigation.navigate(navigateTo); diff --git a/src/screens/root/container/rootContainer.js b/src/screens/root/container/rootContainer.js index b3e6d87a4..7e6ca4245 100644 --- a/src/screens/root/container/rootContainer.js +++ b/src/screens/root/container/rootContainer.js @@ -1,10 +1,11 @@ import React, { Component, Fragment } from 'react'; -import { AppState, AsyncStorage } from 'react-native'; +import { AppState } from 'react-native'; import { connect } from 'react-redux'; import Push from 'appcenter-push'; -// Actions +// Actions & Services import { openPinCodeModal } from '../../../redux/actions/applicationActions'; +import { getExistUser } from '../../../realm/realm'; // Components import { Modal } from '../../../components'; @@ -12,7 +13,6 @@ import { PinCode } from '../..'; // Constants import ROUTES from '../../../constants/routeNames'; -import INITIAL from '../../../constants/initial'; const RootContainer = () => (WrappedComponent) => { class RootComponent extends Component { @@ -45,9 +45,9 @@ const RootContainer = () => (WrappedComponent) => { const { appState } = this.state; const { dispatch } = this.props; - AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { + getExistUser().then((isExistUser) => { if ( - JSON.parse(result) + isExistUser && appState.match(/inactive|background/) && nextAppState === 'active' && __DEV__ === false @@ -55,7 +55,6 @@ const RootContainer = () => (WrappedComponent) => { dispatch(openPinCodeModal()); } }); - this.setState({ appState: nextAppState }); }; diff --git a/src/screens/settings/container/settingsContainer.js b/src/screens/settings/container/settingsContainer.js index e65deee84..95fafdbbf 100644 --- a/src/screens/settings/container/settingsContainer.js +++ b/src/screens/settings/container/settingsContainer.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { AsyncStorage, Platform } from 'react-native'; +import { Platform } from 'react-native'; import { connect } from 'react-redux'; import AppCenter from 'appcenter'; @@ -10,6 +10,7 @@ import { setCurrency as setCurrency2DB, setServer, setNotificationIsOpen, + getExistUser, } from '../../../realm/realm'; // 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 LANGUAGE_VALUE } from '../../../constants/options/language'; import API_VALUE from '../../../constants/options/api'; -import INITIAL from '../../../constants/initial'; // Utilities @@ -36,10 +36,10 @@ import INITIAL from '../../../constants/initial'; import { SettingsScreen } from '..'; /* - * Props Name Description Value - *@props --> props name here description here Value Type Here - * - */ + * Props Name Description Value + *@props --> props name here description here Value Type Here + * + */ class SettingsContainer extends Component { constructor(props) { @@ -116,8 +116,9 @@ class SettingsContainer extends Component { const { notificationSettings, isLoggedIn, username } = this.props; if (isLoggedIn) { const token = await AppCenter.getInstallId(); - AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { - if (JSON.parse(result)) { + + getExistUser().then((isExistUser) => { + if (isExistUser) { const data = { username, token,