Changed asyncstorage with realm

This commit is contained in:
Mustafa Buyukcelebi 2018-12-03 13:20:22 +03:00
parent 310bed4047
commit 2584fd5ad2
2 changed files with 69 additions and 14 deletions

View File

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

View File

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