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 = await getItemFromStorage(AUTH_SCHEMA);
const auth = realm.objects(AUTH_SCHEMA);
realm.write(() => { if (auth) {
if (convertToArray(auth).length > 0) { auth.currentUsername = username;
auth[0].currentUsername = username; await setItemToStorage(AUTH_SCHEMA, auth);
resolve(auth[0]); return auth;
} else {
const authData = {
isLoggedIn: false,
pinCode: '',
currentUsername: username,
};
realm.create(AUTH_SCHEMA, { ...authData });
resolve(authData);
}
});
} catch (error) {
reject(error);
} }
}); const authData = {
isLoggedIn: false,
pinCode: '',
currentUsername: username,
};
export const setPinCode = pinCode => await setItemToStorage(AUTH_SCHEMA, { ...authData });
new Promise((resolve, reject) => { return authData;
try { } catch (error) {
const auth = realm.objects(AUTH_SCHEMA); return error;
const pinHash = sha256(pinCode); }
};
realm.write(() => { export const setPinCode = async pinCode => {
auth[0].pinCode = pinHash.toString(); try {
resolve(auth[0]); const auth = await getItemFromStorage(AUTH_SCHEMA);
}); const pinHash = sha256(pinCode);
} catch (error) {
reject(error); auth.pinCode = pinHash.toString();
await setItemToStorage(AUTH_SCHEMA, auth);
return auth;
} catch (error) {
return error;
}
};
export const removePinCode = async () => {
try {
const auth = await getItemFromStorage(AUTH_SCHEMA);
auth.pinCode = '';
await setItemToStorage(AUTH_SCHEMA, auth);
return auth;
} catch (error) {
return error;
}
};
export const getPinCode = async () => {
try {
const auth = await getItemFromStorage(AUTH_SCHEMA);
if (auth) {
return auth.pinCode;
} }
}); return '';
} catch (error) {
export const removePinCode = () => return error;
new Promise((resolve, reject) => { }
try { };
const auth = realm.objects(AUTH_SCHEMA);
realm.write(() => {
auth[0].pinCode = '';
resolve(auth[0]);
});
} catch (error) {
reject(error);
}
});
export const getPinCode = () =>
new Promise((resolve, reject) => {
try {
const auth = realm.objects(AUTH_SCHEMA);
if (auth[0]) {
resolve(auth[0].pinCode);
}
} catch (error) {
reject(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 { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
realm.write(() => {
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; } catch (error) {
resolve(true); return error;
}); }
} catch (error) { };
reject(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 { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
realm.write(() => {
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; } catch (error) {
resolve(true); return error;
}); }
} catch (error) { };
reject(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 { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
realm.write(() => {
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) => { } catch (error) {
try { return error;
realm.write(() => { }
settings[0].server = selectedServer; };
resolve(true);
});
} catch (error) {
reject(error);
}
});
export const setNotificationSettings = ({ type, action }) => export const setLanguage = async selectedLanguage => {
new Promise((resolve, reject) => { try {
try { const setting = await getItemFromStorage(SETTINGS_SCHEMA);
realm.write(() => {
switch (type) {
case 'notification.follow':
settings[0].followNotification = action;
break;
case 'notification.vote':
settings[0].voteNotification = action;
break;
case 'notification.comment':
settings[0].commentNotification = action;
break;
case 'notification.mention':
settings[0].mentionNotification = action;
break;
case 'notification.reblog':
settings[0].reblogNotification = action;
break;
case 'notification.transfers':
settings[0].transfersNotification = action;
break;
case 'notification':
settings[0].notification = action;
break;
default:
break;
}
resolve(true);
});
} catch (error) {
reject(error);
}
});
export const setCurrency = currencyProps => setting.language = selectedLanguage;
new Promise((resolve, reject) => { await setItemToStorage(SETTINGS_SCHEMA, setting);
try {
realm.write(() => { return true;
settings[0].currency = currencyProps; } catch (error) {
resolve(true); return error;
}); }
} catch (error) { };
reject(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) {
case 'notification.follow':
setting.followNotification = action;
break;
case 'notification.vote':
setting.voteNotification = action;
break;
case 'notification.comment':
setting.commentNotification = action;
break;
case 'notification.mention':
setting.mentionNotification = action;
break;
case 'notification.reblog':
setting.reblogNotification = action;
break;
case 'notification.transfers':
setting.transfersNotification = action;
break;
case 'notification':
setting.notification = action;
break;
default:
break;
} }
});
await setItemToStorage(SETTINGS_SCHEMA, setting);
return true;
} catch (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 = await getItemFromStorage(APPLICATION_SCHEMA);
const application = realm.objects(APPLICATION_SCHEMA); if (application) {
realm.write(() => { application.isPushTokenSaved = pushTokenSaved;
if (convertToArray(application).length > 0) { await setItemToStorage(APPLICATION_SCHEMA, application);
application[0].isPushTokenSaved = pushTokenSaved; return application;
resolve(application[0]);
} else {
const applicationData = {
pushTokenSaved: false,
};
realm.create(APPLICATION_SCHEMA, { ...applicationData });
resolve(applicationData);
}
});
} catch (error) {
reject(error);
} }
}); const applicationData = {
pushTokenSaved: false,
};
await setItemToStorage(APPLICATION_SCHEMA, { ...applicationData });
return applicationData;
} catch (error) {
return error;
}
};
export const getExistUser = async () => { export const getExistUser = async () => {
try { try {
@ -659,53 +661,50 @@ export const getExistUser = async () => {
} }
}; };
export const setExistUser = existUser => export const setExistUser = async existUser => {
new Promise((resolve, reject) => { try {
try { const application = await getItemFromStorage(APPLICATION_SCHEMA);
const application = realm.objects(APPLICATION_SCHEMA); if (application) {
realm.write(() => { application.isExistUser = existUser;
if (convertToArray(application).length > 0) { await setItemToStorage(APPLICATION_SCHEMA, application);
application[0].isExistUser = existUser; return application;
resolve(application[0]);
} else {
const applicationData = {
existUser: false,
};
realm.create(APPLICATION_SCHEMA, { ...applicationData });
resolve(applicationData);
}
});
} catch (error) {
reject(error);
} }
}); const applicationData = {
existUser: false,
};
await setItemToStorage(APPLICATION_SCHEMA, { ...applicationData });
return applicationData;
} catch (error) {
return error;
}
};
export const setSCAccount = data => export const setSCAccount = async data => {
new Promise((resolve, reject) => { try {
try { let scAccount = await getItemFromStorage(SC_ACCOUNTS);
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', data.username); const date = new Date();
const date = new Date(); date.setSeconds(date.getSeconds() + data.expires_in);
date.setSeconds(date.getSeconds() + data.expires_in); if (scAccount.some(e => e.username === data.username)) {
realm.write(() => { scAccount = scAccount.map(item =>
if (convertToArray(scAccount).length > 0) { item.username === data.username
scAccount[0].refreshToken = data.refresh_token; ? { ...item, refreshToken: data.refresh_token, expireDate: date.toString() }
scAccount[0].expireDate = date.toString(); : item,
resolve(); );
} else { } else {
const account = { const account = {
username: data.username, username: data.username,
accessToken: data.access_token, accessToken: data.access_token,
refreshToken: data.refresh_token, refreshToken: data.refresh_token,
expireDate: date.toString(), expireDate: date.toString(),
}; };
realm.create(SC_ACCOUNTS, { ...account }); scAccount.push(account);
resolve();
}
});
} catch (error) {
reject(error);
} }
}); await setItemToStorage(SC_ACCOUNTS, scAccount);
return scAccount;
} catch (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 { let scAccount = await getItemFromStorage(SC_ACCOUNTS);
const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', username);
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'));
}
} catch (error) {
reject(error);
} }
}); return new Error('Could not remove selected user');
} catch (error) {
return error;
}
};

View File

@ -438,43 +438,41 @@ 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) {
realmData = userData; if (userData.length > 0) {
userData.forEach((accountData, index) => { realmData = userData;
if ( userData.forEach((accountData, index) => {
!accountData.accessToken && if (
!accountData.masterKey && !accountData.accessToken &&
!accountData.postingKey && !accountData.masterKey &&
!accountData.activeKey && !accountData.postingKey &&
!accountData.memoKey !accountData.activeKey &&
) { !accountData.memoKey
realmData.splice(index, 1); ) {
if (realmData.length === 0) { realmData.splice(index, 1);
dispatch(login(false)); if (realmData.length === 0) {
dispatch(logoutDone()); dispatch(login(false));
removePinCode(); dispatch(logoutDone());
setAuthStatus({ isLoggedIn: false }); removePinCode();
setExistUser(false); setAuthStatus({ isLoggedIn: false });
if (accountData.authType === AUTH_TYPE.STEEM_CONNECT) { setExistUser(false);
removeSCAccount(accountData.username); if (accountData.authType === AUTH_TYPE.STEEM_CONNECT) {
} removeSCAccount(accountData.username);
}
removeUserData(accountData.username);
} else {
dispatch(addOtherAccount({ username: accountData.username }));
} }
}); }
removeUserData(accountData.username);
} else {
dispatch(addOtherAccount({ username: accountData.username }));
} }
}); });
} }
}); }
if (realmData.length > 0) { if (realmData.length > 0) {
const realmObject = realmData.filter(data => data.username === currentUsername); const realmObject = realmData.filter(data => data.username === currentUsername);