crated set and get realm object for settings

This commit is contained in:
ue 2018-11-23 15:05:51 +03:00
parent 2fb5ded517
commit 72322639f4
2 changed files with 101 additions and 12 deletions

View File

@ -4,6 +4,7 @@ import Realm from 'realm';
const USER_SCHEMA = 'user';
const AUTH_SCHEMA = 'auth';
const DRAFT_SCHEMA = 'draft';
const SETTINGS_SCHEMA = 'settings';
const userSchema = {
name: USER_SCHEMA,
@ -29,6 +30,17 @@ const draftSchema = {
},
};
const settingsSchema = {
name: SETTINGS_SCHEMA,
properties: {
language: { type: 'string', default: null },
isDarkTheme: { type: 'bool', default: false },
currency: { type: 'string' },
notification: { type: 'string', default: true },
server: { type: 'string' },
},
};
const authSchema = {
name: AUTH_SCHEMA,
properties: {
@ -38,7 +50,10 @@ const authSchema = {
},
};
const realm = new Realm({ path: 'esteem.realm', schema: [userSchema, authSchema, draftSchema] });
const realm = new Realm({
path: 'esteem.realm',
schema: [userSchema, authSchema, draftSchema, settingsSchema],
});
// TODO: This is getting ALL user data, we should change this method with getUserDataWithUsername
export const getUserData = () => new Promise((resolve, reject) => {
@ -222,3 +237,71 @@ export const getPinCode = () => new Promise((resolve, reject) => {
reject(error);
}
});
// SETTINGS
export const setTheme = isDarkTheme => new Promise((resolve, reject) => {
try {
const settings = realm.objects(SETTINGS_SCHEMA);
console.log('realma tema kayit ediiyor ');
realm.write(() => {
settings[0].isDarkTheme = isDarkTheme;
resolve(settings[0]);
});
} catch (error) {
reject(error);
}
});
export const getTheme = () => new Promise((resolve, reject) => {
try {
const settings = realm.objects(SETTINGS_SCHEMA);
if (settings[0]) {
resolve(settings[0].isDarkTheme);
} else {
resolve(false);
}
} catch (error) {
reject(error);
}
});
export const setLanguage = selectedLanguage => new Promise((resolve, reject) => {
try {
const settings = realm.objects(SETTINGS_SCHEMA);
console.log('realma tema kayit ediiyor ');
realm.write(() => {
settings[0].language = selectedLanguage;
resolve(settings[0]);
});
} catch (error) {
reject(error);
}
});
export const getLanguage = () => new Promise((resolve, reject) => {
try {
const settings = realm.objects(SETTINGS_SCHEMA);
if (settings[0]) {
resolve(settings[0].language);
} else {
resolve(false);
}
} catch (error) {
reject(error);
}
});
export const getSettings = () => new Promise((resolve, reject) => {
try {
const settings = realm.objects(SETTINGS_SCHEMA);
if (settings[0]) {
resolve(settings[0]);
} else {
resolve(false);
}
} catch (error) {
reject(error);
}
});

View File

@ -1,6 +1,9 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
// Realm
import { setTheme } from '../../../realm/realm';
// Services and Actions
import {
setLanguage,
@ -41,43 +44,43 @@ class SettingsContainer extends Component {
switch (actionType) {
case 'currency':
dispatch( setCurrency(CURRENCY_VALUE[action]) );
dispatch(setCurrency(CURRENCY_VALUE[action]));
break;
case 'language':
dispatch( setLanguage(LANGUAGE_VALUE[action]) );
dispatch(setLanguage(LANGUAGE_VALUE[action]));
break;
case 'api':
dispatch( setApi(API_VALUE[action]) );
dispatch(setApi(API_VALUE[action]));
break;
default:
break;
}
}
};
_handleToggleChanged = (action, actionType) => {
const { dispatch } = this.props;
switch (actionType) {
case 'notification':
dispatch( isNotificationOpen(action) );
dispatch(isNotificationOpen(action));
break;
case 'theme':
dispatch( isDarkTheme(action) );
dispatch(isDarkTheme(action));
setTheme(true);
break;
default:
break;
}
}
};
_handleOnChange = (action, type, actionType = null) => {
const { dispatch } = this.props;
this.props.navigation.setParams({ cardStyle: {backgroundColor: "red" } });
const { dispatch, navigation } = this.props;
switch (type) {
case 'dropdown':
@ -91,13 +94,16 @@ class SettingsContainer extends Component {
case 'button':
console.log(action + type);
break;
default:
break;
}
};
// _handleThemeChange = () => {
// const { dispatch } = this.props;
// };
render() {
return <SettingsScreen handleOnChange={this._handleOnChange} {...this.props} />;
}