From 2584fd5ad2c8d95fd57e34034e1bf6ad1e263fe9 Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Mon, 3 Dec 2018 13:20:22 +0300 Subject: [PATCH] Changed asyncstorage with realm --- src/realm/realm.js | 47 ++++++++++++++++++- .../container/applicationContainer.js | 36 +++++++++----- 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/src/realm/realm.js b/src/realm/realm.js index 84bc71a6f..2cbc6a5bc 100644 --- a/src/realm/realm.js +++ b/src/realm/realm.js @@ -5,6 +5,7 @@ const USER_SCHEMA = 'user'; const AUTH_SCHEMA = 'auth'; const DRAFT_SCHEMA = 'draft'; const SETTINGS_SCHEMA = 'settings'; +const APPLICATION_SCHEMA = 'application'; const userSchema = { name: USER_SCHEMA, @@ -41,6 +42,13 @@ const settingsSchema = { }, }; +const applicationSchema = { + name: APPLICATION_SCHEMA, + properties: { + isPushTokenSaved: { type: 'bool', default: false }, + }, +}; + const authSchema = { name: AUTH_SCHEMA, properties: { @@ -52,7 +60,7 @@ const authSchema = { const realm = new Realm({ path: 'esteem.realm', - schema: [userSchema, authSchema, draftSchema, settingsSchema], + schema: [userSchema, authSchema, draftSchema, settingsSchema, applicationSchema], }); const settings = realm.objects(SETTINGS_SCHEMA); @@ -359,3 +367,40 @@ export const getSettings = () => new Promise((resolve, reject) => { reject(error); } }); + +export const getPushTokenSaved = () => new Promise((resolve, reject) => { + try { + const application = realm.objects(APPLICATION_SCHEMA); + if (!application[0]) { + setPushTokenSaved(JSON.stringify(false)); + resolve(false); + } + if (application[0].isPushTokenSaved) { + resolve((application[0].isPushTokenSaved)); + } else { + resolve(false); + } + } catch (error) { + reject(error); + } +}); + +export const setPushTokenSaved = pushTokenSaved => new Promise((resolve, reject) => { + try { + const application = realm.objects(APPLICATION_SCHEMA); + realm.write(() => { + if (Array.from(application).length > 0) { + application[0].isPushTokenSaved = pushTokenSaved; + resolve(application[0]); + } else { + const applicationData = { + pushTokenSaved: 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 48b22563d..477c126c3 100644 --- a/src/screens/application/container/applicationContainer.js +++ b/src/screens/application/container/applicationContainer.js @@ -11,7 +11,13 @@ import tr from 'react-intl/locale-data/tr'; import INITIAL from '../../../constants/initial'; // Services -import { getUserData, getAuthStatus, getSettings } from '../../../realm/realm'; +import { + getUserData, + getAuthStatus, + getSettings, + getPushTokenSaved, + setPushTokenSaved, +} from '../../../realm/realm'; import { getUser } from '../../../providers/steem/dsteem'; import { setPushToken } from '../../../providers/esteem/esteem'; @@ -133,18 +139,22 @@ class ApplicationContainer extends Component { _setPushToken = async (username) => { const { notificationSettings } = this.props; const token = await AppCenter.getInstallId(); - AsyncStorage.multiGet([INITIAL.PUSH_TOKEN_SAVED, INITIAL.IS_EXIST_USER], (err, result) => { - if (!JSON.parse(result[0][1]) && JSON.parse(result[1][1])) { - const data = { - username, - token, - system: Platform.OS, - allows_notify: notificationSettings, - }; - setPushToken(data) - .then(() => { - AsyncStorage.setItem(INITIAL.PUSH_TOKEN_SAVED, JSON.stringify(true)); - }); + + AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => { + if (JSON.parse(result)) { + getPushTokenSaved().then((isPushTokenSaved) => { + if (!isPushTokenSaved) { + const data = { + username, + token, + system: Platform.OS, + allows_notify: notificationSettings, + }; + setPushToken(data).then(() => { + setPushTokenSaved(JSON.stringify(true)); + }); + } + }); } }); };