Changed all realm functions with ayncstorage

This commit is contained in:
Mustafa Buyukcelebi 2019-11-07 15:18:05 +03:00
parent 30ffcb12ab
commit 4b5b96b452
3 changed files with 306 additions and 310 deletions

View File

@ -73,8 +73,8 @@ class SideMenuContainer extends Component {
const { dispatch, currentAccount, navigation } = this.props; const { dispatch, currentAccount, navigation } = this.props;
if (anchor !== currentAccount.name) { if (anchor !== currentAccount.name) {
switchAccount(anchor).then(accountData => { switchAccount(anchor).then(async accountData => {
const realmData = getUserDataWithUsername(anchor); const realmData = await getUserDataWithUsername(anchor);
const _currentAccount = accountData; const _currentAccount = accountData;
_currentAccount.username = _currentAccount.name; _currentAccount.username = _currentAccount.name;

View File

@ -141,6 +141,7 @@ export const setItemToStorage = async (key, data) => {
export const getAllData = async () => { export const getAllData = async () => {
try { try {
const keys = await AsyncStorage.getAllKeys(); const keys = await AsyncStorage.getAllKeys();
const isMigrated = [ const isMigrated = [
USER_SCHEMA, USER_SCHEMA,
SC_ACCOUNTS, SC_ACCOUNTS,
@ -151,30 +152,31 @@ export const getAllData = async () => {
].some(el => keys.includes(el)); ].some(el => keys.includes(el));
if (!isMigrated) { if (!isMigrated) {
const users = convertToArray(realm.objects(USER_SCHEMA)); const users = convertToArray(realm.objects(USER_SCHEMA));
AsyncStorage.setItem(USER_SCHEMA, JSON.stringify(users));
const scAccount = convertToArray(realm.objects(SC_ACCOUNTS)); const scAccount = convertToArray(realm.objects(SC_ACCOUNTS));
AsyncStorage.setItem(SC_ACCOUNTS, JSON.stringify(scAccount)); const draft = convertToArray(realm.objects(DRAFT_SCHEMA));
const auth = const auth =
convertToArray(realm.objects(AUTH_SCHEMA)).length === 1 convertToArray(realm.objects(AUTH_SCHEMA)).length === 1
? convertToArray(realm.objects(AUTH_SCHEMA))[0] ? convertToArray(realm.objects(AUTH_SCHEMA))[0]
: convertToArray(realm.objects(AUTH_SCHEMA)); : convertToArray(realm.objects(AUTH_SCHEMA));
AsyncStorage.setItem(AUTH_SCHEMA, JSON.stringify(auth));
const draft = convertToArray(realm.objects(DRAFT_SCHEMA));
AsyncStorage.setItem(DRAFT_SCHEMA, JSON.stringify(draft));
const setting = const setting =
convertToArray(realm.objects(SETTINGS_SCHEMA)).length === 1 convertToArray(realm.objects(SETTINGS_SCHEMA)).length === 1
? convertToArray(realm.objects(SETTINGS_SCHEMA))[0] ? convertToArray(realm.objects(SETTINGS_SCHEMA))[0]
: convertToArray(realm.objects(SETTINGS_SCHEMA)); : convertToArray(realm.objects(SETTINGS_SCHEMA));
AsyncStorage.setItem(SETTINGS_SCHEMA, JSON.stringify(setting));
const application = const application =
convertToArray(realm.objects(APPLICATION_SCHEMA)).length === 1 convertToArray(realm.objects(APPLICATION_SCHEMA)).length === 1
? convertToArray(realm.objects(APPLICATION_SCHEMA))[0] ? convertToArray(realm.objects(APPLICATION_SCHEMA))[0]
: convertToArray(realm.objects(APPLICATION_SCHEMA)); : convertToArray(realm.objects(APPLICATION_SCHEMA));
AsyncStorage.setItem(APPLICATION_SCHEMA, JSON.stringify(application));
}
// AsyncStorage.setItem('temp-reply', ''); const data = [
// AsyncStorage.getItem('temp-reply'); [USER_SCHEMA, JSON.stringify(users)],
[SC_ACCOUNTS, JSON.stringify(scAccount)],
[AUTH_SCHEMA, JSON.stringify(auth)],
[DRAFT_SCHEMA, JSON.stringify(draft)],
[SETTINGS_SCHEMA, JSON.stringify(setting)],
[APPLICATION_SCHEMA, JSON.stringify(application)],
];
AsyncStorage.multiSet(data);
}
} catch (error) { } catch (error) {
return error; return error;
} }
@ -203,13 +205,13 @@ export const getUserDataWithUsername = async username => {
}; };
export const setUserData = async userData => { export const setUserData = async userData => {
console.log('userData 111 :', userData);
try { try {
const account = getUserDataWithUsername(userData.username); const account = await getUserDataWithUsername(userData.username);
const user = await getItemFromStorage(USER_SCHEMA);
if (account.length === 0) { if (account.length === 0) {
account.push(userData); user.push(userData);
await setItemToStorage(USER_SCHEMA, account); await setItemToStorage(USER_SCHEMA, user);
} }
return userData; return userData;
} catch (error) { } catch (error) {
@ -218,7 +220,6 @@ export const setUserData = async userData => {
}; };
export const updateUserData = async userData => { export const updateUserData = async userData => {
console.log('userData 222 :', userData);
try { try {
let account = await getItemFromStorage(USER_SCHEMA); let account = await getItemFromStorage(USER_SCHEMA);
@ -238,13 +239,10 @@ export const updateUserData = async userData => {
export const removeUserData = async username => { export const removeUserData = async username => {
try { try {
let account = await getItemFromStorage(USER_SCHEMA); let account = await getItemFromStorage(USER_SCHEMA);
console.log('account :', account);
if (account.some(e => e.username === username)) { if (account.some(e => e.username === username)) {
account = account.filter(item => item.username !== username); account = account.filter(item => item.username !== username);
console.log('account :', account);
await setItemToStorage(USER_SCHEMA, account); await setItemToStorage(USER_SCHEMA, account);
console.log('return :');
return true; return true;
} }
return new Error('Could not remove selected user'); return new Error('Could not remove selected user');
@ -323,69 +321,67 @@ export const setAuthStatus = async authStatus => {
} }
}; };
export const updateCurrentUsername = username => export const updateCurrentUsername = async username => {
new Promise((resolve, reject) => {
try { try {
const auth = realm.objects(AUTH_SCHEMA); const auth = await getItemFromStorage(AUTH_SCHEMA);
realm.write(() => {
if (convertToArray(auth).length > 0) { if (auth) {
auth[0].currentUsername = username; auth.currentUsername = username;
resolve(auth[0]); await setItemToStorage(AUTH_SCHEMA, auth);
} else { return auth;
}
const authData = { const authData = {
isLoggedIn: false, isLoggedIn: false,
pinCode: '', pinCode: '',
currentUsername: username, currentUsername: username,
}; };
realm.create(AUTH_SCHEMA, { ...authData });
resolve(authData);
}
});
} catch (error) {
reject(error);
}
});
export const setPinCode = pinCode => await setItemToStorage(AUTH_SCHEMA, { ...authData });
new Promise((resolve, reject) => { return authData;
} catch (error) {
return error;
}
};
export const setPinCode = async pinCode => {
try { try {
const auth = realm.objects(AUTH_SCHEMA); const auth = await getItemFromStorage(AUTH_SCHEMA);
const pinHash = sha256(pinCode); const pinHash = sha256(pinCode);
realm.write(() => { auth.pinCode = pinHash.toString();
auth[0].pinCode = pinHash.toString(); await setItemToStorage(AUTH_SCHEMA, auth);
resolve(auth[0]);
});
} catch (error) {
reject(error);
}
});
export const removePinCode = () => return auth;
new Promise((resolve, reject) => { } catch (error) {
return error;
}
};
export const removePinCode = async () => {
try { try {
const auth = realm.objects(AUTH_SCHEMA); const auth = await getItemFromStorage(AUTH_SCHEMA);
realm.write(() => { auth.pinCode = '';
auth[0].pinCode = ''; await setItemToStorage(AUTH_SCHEMA, auth);
resolve(auth[0]);
}); return auth;
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const getPinCode = () => export const getPinCode = async () => {
new Promise((resolve, reject) => {
try { try {
const auth = realm.objects(AUTH_SCHEMA); const auth = await getItemFromStorage(AUTH_SCHEMA);
if (auth[0]) {
resolve(auth[0].pinCode); if (auth) {
return auth.pinCode;
} }
return '';
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
// SETTINGS // SETTINGS
@ -401,29 +397,31 @@ export const getPinCodeOpen = async () => {
} }
}; };
export const setPinCodeOpen = status => export const setPinCodeOpen = async status => {
new Promise((resolve, reject) => {
try { try {
realm.write(() => { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
settings[0].isPinCodeOpen = status;
resolve(true);
});
} catch (error) {
reject(error);
}
});
export const setTheme = isDarkTheme => setting.isPinCodeOpen = status;
new Promise((resolve, reject) => { await setItemToStorage(SETTINGS_SCHEMA, setting);
try {
realm.write(() => { return true;
settings[0].isDarkTheme = isDarkTheme;
resolve(true);
});
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const setTheme = async isDarkTheme => {
try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA);
setting.isDarkTheme = isDarkTheme;
await setItemToStorage(SETTINGS_SCHEMA, setting);
return true;
} catch (error) {
return error;
}
};
export const getTheme = async () => { export const getTheme = async () => {
try { try {
@ -437,29 +435,31 @@ export const getTheme = async () => {
} }
}; };
export const setDefaultFooter = isDefaultFooter => export const setDefaultFooter = async isDefaultFooter => {
new Promise((resolve, reject) => {
try { try {
realm.write(() => { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
settings[0].isDefaultFooter = isDefaultFooter;
resolve(true);
});
} catch (error) {
reject(error);
}
});
export const setUpvotePercent = percent => setting.isDefaultFooter = isDefaultFooter;
new Promise((resolve, reject) => { await setItemToStorage(SETTINGS_SCHEMA, setting);
try {
realm.write(() => { return true;
settings[0].upvotePercent = percent;
resolve(true);
});
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const setUpvotePercent = async percent => {
try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA);
setting.upvotePercent = percent;
await setItemToStorage(SETTINGS_SCHEMA, setting);
return true;
} catch (error) {
return error;
}
};
export const getUpvotePercent = async () => { export const getUpvotePercent = async () => {
try { try {
@ -485,89 +485,94 @@ export const getNsfw = async () => {
} }
}; };
export const setNsfw = nsfw => export const setNsfw = async nsfw => {
new Promise((resolve, reject) => {
try { try {
realm.write(() => { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
settings[0].nsfw = nsfw;
resolve(true);
});
} catch (error) {
reject(error);
}
});
export const setLanguage = selectedLanguage => setting.nsfw = nsfw;
new Promise((resolve, reject) => { await setItemToStorage(SETTINGS_SCHEMA, setting);
try {
realm.write(() => {
settings[0].language = selectedLanguage;
resolve(true);
});
} catch (error) {
reject(error);
}
});
export const setServer = selectedServer => return true;
new Promise((resolve, reject) => {
try {
realm.write(() => {
settings[0].server = selectedServer;
resolve(true);
});
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const setNotificationSettings = ({ type, action }) => export const setLanguage = async selectedLanguage => {
new Promise((resolve, reject) => {
try { try {
realm.write(() => { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
setting.language = selectedLanguage;
await setItemToStorage(SETTINGS_SCHEMA, setting);
return true;
} catch (error) {
return error;
}
};
export const setServer = async selectedServer => {
try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA);
setting.server = selectedServer;
await setItemToStorage(SETTINGS_SCHEMA, setting);
return true;
} catch (error) {
return error;
}
};
export const setNotificationSettings = async ({ type, action }) => {
try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA);
switch (type) { switch (type) {
case 'notification.follow': case 'notification.follow':
settings[0].followNotification = action; setting.followNotification = action;
break; break;
case 'notification.vote': case 'notification.vote':
settings[0].voteNotification = action; setting.voteNotification = action;
break; break;
case 'notification.comment': case 'notification.comment':
settings[0].commentNotification = action; setting.commentNotification = action;
break; break;
case 'notification.mention': case 'notification.mention':
settings[0].mentionNotification = action; setting.mentionNotification = action;
break; break;
case 'notification.reblog': case 'notification.reblog':
settings[0].reblogNotification = action; setting.reblogNotification = action;
break; break;
case 'notification.transfers': case 'notification.transfers':
settings[0].transfersNotification = action; setting.transfersNotification = action;
break; break;
case 'notification': case 'notification':
settings[0].notification = action; setting.notification = action;
break; break;
default: default:
break; break;
} }
resolve(true);
});
} catch (error) {
reject(error);
}
});
export const setCurrency = currencyProps => await setItemToStorage(SETTINGS_SCHEMA, setting);
new Promise((resolve, reject) => { return true;
try {
realm.write(() => {
settings[0].currency = currencyProps;
resolve(true);
});
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const setCurrency = async currencyProps => {
try {
const setting = await getItemFromStorage(SETTINGS_SCHEMA);
setting.currency = currencyProps;
await setItemToStorage(SETTINGS_SCHEMA, setting);
return true;
} catch (error) {
return error;
}
};
export const getLanguage = async () => { export const getLanguage = async () => {
try { try {
@ -622,26 +627,23 @@ export const getPushTokenSaved = async () => {
} }
}; };
export const setPushTokenSaved = pushTokenSaved => export const setPushTokenSaved = async pushTokenSaved => {
new Promise((resolve, reject) => {
try { try {
const application = realm.objects(APPLICATION_SCHEMA); const application = await getItemFromStorage(APPLICATION_SCHEMA);
realm.write(() => { if (application) {
if (convertToArray(application).length > 0) { application.isPushTokenSaved = pushTokenSaved;
application[0].isPushTokenSaved = pushTokenSaved; await setItemToStorage(APPLICATION_SCHEMA, application);
resolve(application[0]); return application;
} else { }
const applicationData = { const applicationData = {
pushTokenSaved: false, pushTokenSaved: false,
}; };
realm.create(APPLICATION_SCHEMA, { ...applicationData }); await setItemToStorage(APPLICATION_SCHEMA, { ...applicationData });
resolve(applicationData); return applicationData;
}
});
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const getExistUser = async () => { export const getExistUser = async () => {
try { try {
@ -659,38 +661,35 @@ export const getExistUser = async () => {
} }
}; };
export const setExistUser = existUser => export const setExistUser = async existUser => {
new Promise((resolve, reject) => {
try { try {
const application = realm.objects(APPLICATION_SCHEMA); const application = await getItemFromStorage(APPLICATION_SCHEMA);
realm.write(() => { if (application) {
if (convertToArray(application).length > 0) { application.isExistUser = existUser;
application[0].isExistUser = existUser; await setItemToStorage(APPLICATION_SCHEMA, application);
resolve(application[0]); return application;
} else { }
const applicationData = { const applicationData = {
existUser: false, existUser: false,
}; };
realm.create(APPLICATION_SCHEMA, { ...applicationData }); await setItemToStorage(APPLICATION_SCHEMA, { ...applicationData });
resolve(applicationData); return applicationData;
}
});
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const setSCAccount = data => export const setSCAccount = async data => {
new Promise((resolve, reject) => {
try { try {
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', data.username); let scAccount = await getItemFromStorage(SC_ACCOUNTS);
const date = new Date(); const date = new Date();
date.setSeconds(date.getSeconds() + data.expires_in); date.setSeconds(date.getSeconds() + data.expires_in);
realm.write(() => { if (scAccount.some(e => e.username === data.username)) {
if (convertToArray(scAccount).length > 0) { scAccount = scAccount.map(item =>
scAccount[0].refreshToken = data.refresh_token; item.username === data.username
scAccount[0].expireDate = date.toString(); ? { ...item, refreshToken: data.refresh_token, expireDate: date.toString() }
resolve(); : item,
);
} else { } else {
const account = { const account = {
username: data.username, username: data.username,
@ -698,14 +697,14 @@ export const setSCAccount = data =>
refreshToken: data.refresh_token, refreshToken: data.refresh_token,
expireDate: date.toString(), expireDate: date.toString(),
}; };
realm.create(SC_ACCOUNTS, { ...account }); scAccount.push(account);
resolve();
} }
}); await setItemToStorage(SC_ACCOUNTS, scAccount);
return scAccount;
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };
export const getSCAccount = async username => { export const getSCAccount = async username => {
try { try {
@ -720,20 +719,19 @@ export const getSCAccount = async username => {
} }
}; };
export const removeSCAccount = username => export const removeSCAccount = async username => {
new Promise((resolve, reject) => {
try { try {
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', username); let scAccount = await getItemFromStorage(SC_ACCOUNTS);
if (convertToArray(scAccount).length > 0) { if (scAccount.some(e => e.username === username)) {
realm.write(() => { scAccount = scAccount.filter(item => item.username !== username);
realm.delete(scAccount);
resolve(); await setItemToStorage(SC_ACCOUNTS, scAccount);
});
} else { return true;
reject(new Error('Could not remove selected user'));
} }
return new Error('Could not remove selected user');
} catch (error) { } catch (error) {
reject(error); return error;
} }
}); };

View File

@ -438,13 +438,13 @@ class ApplicationContainer extends Component {
_getUserDataFromRealm = async () => { _getUserDataFromRealm = async () => {
const { dispatch, pinCode, isPinCodeOpen: _isPinCodeOpen } = this.props; const { dispatch, pinCode, isPinCodeOpen: _isPinCodeOpen } = this.props;
let realmData = []; let realmData = [];
let currentUsername;
await getAuthStatus().then(res => { const res = await getAuthStatus();
({ currentUsername } = res); const { currentUsername } = res;
if (res) { if (res) {
getUserData().then(async userData => { const userData = await getUserData();
if (userData.length > 0) { if (userData.length > 0) {
realmData = userData; realmData = userData;
userData.forEach((accountData, index) => { userData.forEach((accountData, index) => {
@ -472,9 +472,7 @@ class ApplicationContainer extends Component {
} }
}); });
} }
});
} }
});
if (realmData.length > 0) { if (realmData.length > 0) {
const realmObject = realmData.filter(data => data.username === currentUsername); const realmObject = realmData.filter(data => data.username === currentUsername);