mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-11-24 17:09:13 +03:00
Changed asyncstorage with realm
This commit is contained in:
parent
310bed4047
commit
2584fd5ad2
@ -5,6 +5,7 @@ const USER_SCHEMA = 'user';
|
|||||||
const AUTH_SCHEMA = 'auth';
|
const AUTH_SCHEMA = 'auth';
|
||||||
const DRAFT_SCHEMA = 'draft';
|
const DRAFT_SCHEMA = 'draft';
|
||||||
const SETTINGS_SCHEMA = 'settings';
|
const SETTINGS_SCHEMA = 'settings';
|
||||||
|
const APPLICATION_SCHEMA = 'application';
|
||||||
|
|
||||||
const userSchema = {
|
const userSchema = {
|
||||||
name: USER_SCHEMA,
|
name: USER_SCHEMA,
|
||||||
@ -41,6 +42,13 @@ const settingsSchema = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const applicationSchema = {
|
||||||
|
name: APPLICATION_SCHEMA,
|
||||||
|
properties: {
|
||||||
|
isPushTokenSaved: { type: 'bool', default: false },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const authSchema = {
|
const authSchema = {
|
||||||
name: AUTH_SCHEMA,
|
name: AUTH_SCHEMA,
|
||||||
properties: {
|
properties: {
|
||||||
@ -52,7 +60,7 @@ const authSchema = {
|
|||||||
|
|
||||||
const realm = new Realm({
|
const realm = new Realm({
|
||||||
path: 'esteem.realm',
|
path: 'esteem.realm',
|
||||||
schema: [userSchema, authSchema, draftSchema, settingsSchema],
|
schema: [userSchema, authSchema, draftSchema, settingsSchema, applicationSchema],
|
||||||
});
|
});
|
||||||
|
|
||||||
const settings = realm.objects(SETTINGS_SCHEMA);
|
const settings = realm.objects(SETTINGS_SCHEMA);
|
||||||
@ -359,3 +367,40 @@ export const getSettings = () => new Promise((resolve, reject) => {
|
|||||||
reject(error);
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
@ -11,7 +11,13 @@ import tr from 'react-intl/locale-data/tr';
|
|||||||
import INITIAL from '../../../constants/initial';
|
import INITIAL from '../../../constants/initial';
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
import { getUserData, getAuthStatus, getSettings } from '../../../realm/realm';
|
import {
|
||||||
|
getUserData,
|
||||||
|
getAuthStatus,
|
||||||
|
getSettings,
|
||||||
|
getPushTokenSaved,
|
||||||
|
setPushTokenSaved,
|
||||||
|
} 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';
|
||||||
|
|
||||||
@ -133,18 +139,22 @@ class ApplicationContainer extends Component {
|
|||||||
_setPushToken = async (username) => {
|
_setPushToken = async (username) => {
|
||||||
const { notificationSettings } = this.props;
|
const { notificationSettings } = this.props;
|
||||||
const token = await AppCenter.getInstallId();
|
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])) {
|
AsyncStorage.getItem(INITIAL.IS_EXIST_USER, (err, result) => {
|
||||||
const data = {
|
if (JSON.parse(result)) {
|
||||||
username,
|
getPushTokenSaved().then((isPushTokenSaved) => {
|
||||||
token,
|
if (!isPushTokenSaved) {
|
||||||
system: Platform.OS,
|
const data = {
|
||||||
allows_notify: notificationSettings,
|
username,
|
||||||
};
|
token,
|
||||||
setPushToken(data)
|
system: Platform.OS,
|
||||||
.then(() => {
|
allows_notify: notificationSettings,
|
||||||
AsyncStorage.setItem(INITIAL.PUSH_TOKEN_SAVED, JSON.stringify(true));
|
};
|
||||||
});
|
setPushToken(data).then(() => {
|
||||||
|
setPushTokenSaved(JSON.stringify(true));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user