From c20e36d29268b6b9ddb693af82e4ecd5cf5cbc17 Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Mon, 2 Mar 2020 14:28:40 +0500 Subject: [PATCH] fix views not updating --- apps/mobile/src/utils/storage.js | 24 +++++++++++++------ apps/mobile/src/views/Favorites/index.js | 17 +++++++++----- apps/mobile/src/views/Folders/index.js | 30 +++++++++--------------- apps/mobile/src/views/Notebook/index.js | 7 +++--- apps/mobile/src/views/Notes/index.js | 17 +++++++++----- apps/mobile/src/views/Tags/index.js | 16 ++++++++----- apps/mobile/src/views/Trash/index.js | 23 +++++++++++------- 7 files changed, 78 insertions(+), 56 deletions(-) diff --git a/apps/mobile/src/utils/storage.js b/apps/mobile/src/utils/storage.js index 31e4399b4..d98f34c21 100644 --- a/apps/mobile/src/utils/storage.js +++ b/apps/mobile/src/utils/storage.js @@ -1,5 +1,6 @@ import {NativeModules} from 'react-native'; import FastStorage from 'react-native-fast-storage'; + var Aes = NativeModules.Aes; async function read(key) { @@ -25,12 +26,16 @@ function encrypt(password, data) { let key; return Aes.pbkdf2('password', 'salt', 5000, 256).then(aes => { key = aes; - console.log(aes); return Aes.randomKey(16).then(iv => { - return Aes.encrypt(data, key, iv).then(cipher => ({ - cipher, - iv, - })); + return Aes.encrypt(data, key, iv).then(cipher => { + return Aes.hmac256(cipher, key).then(hash => { + return { + hash, + cipher, + iv, + }; + }); + }); }); }); } @@ -40,8 +45,13 @@ function decrypt(password, data) { return Aes.pbkdf2(password, 'salt', 5000, 256).then(aes => { key = aes; - return Aes.decrypt(data.cipher, key, data.iv).then(e => { - return e; + return Aes.hmac256(data.cipher, key).then(hash => { + if (hash !== data.hash) { + throw new Error('Wrong password'); + } + return Aes.decrypt(data.cipher, key, data.iv).then(e => { + return e; + }); }); }); } diff --git a/apps/mobile/src/views/Favorites/index.js b/apps/mobile/src/views/Favorites/index.js index e4ce492b1..ae2b3a925 100644 --- a/apps/mobile/src/views/Favorites/index.js +++ b/apps/mobile/src/views/Favorites/index.js @@ -11,18 +11,23 @@ import {ACTIONS} from '../../provider/actions'; import {eSendEvent} from '../../services/eventManager'; import {eScrollEvent} from '../../services/events'; import {ToastEvent, w} from '../../utils/utils'; +import {useIsFocused} from 'react-navigation-hooks'; export const Favorites = ({navigation}) => { const [state, dispatch] = useTracked(); const {colors, selectionMode, favorites} = state; const [refreshing, setRefreshing] = useState(false); + const isFocused = useIsFocused(); + useEffect(() => { - dispatch({ - type: ACTIONS.CURRENT_SCREEN, - screen: 'favorites', - }); - dispatch({type: ACTIONS.FAVORITES}); - }, []); + if (isFocused) { + dispatch({ + type: ACTIONS.CURRENT_SCREEN, + screen: 'favorites', + }); + dispatch({type: ACTIONS.FAVORITES}); + } + }, [isFocused]); const onScroll = event => { let y = event.nativeEvent.contentOffset.y; diff --git a/apps/mobile/src/views/Folders/index.js b/apps/mobile/src/views/Folders/index.js index 11c28b872..f8251b734 100644 --- a/apps/mobile/src/views/Folders/index.js +++ b/apps/mobile/src/views/Folders/index.js @@ -40,39 +40,31 @@ export const Folders = ({navigation}) => { }; useEffect(() => { - dispatch({type: ACTIONS.PINNED}); - }, []); - - useEffect(() => { - eSendEvent(eScrollEvent, 0); - dispatch({type: ACTIONS.NOTEBOOKS}); - if (isFocused) { + dispatch({type: ACTIONS.PINNED}); + dispatch({type: ACTIONS.NOTEBOOKS}); dispatch({ type: ACTIONS.CURRENT_SCREEN, screen: 'notebooks', }); } + }, [isFocused]); + useEffect(() => { + eSendEvent(eScrollEvent, 0); let backhandler; - if (isFocused) { - backhandler = BackHandler.addEventListener( - 'hardwareBackPress', - handleBackPress, - ); - } else { - if (backhandler) { - backhandler.remove(); - backhandler = null; - } - } + + backhandler = BackHandler.addEventListener( + 'hardwareBackPress', + handleBackPress, + ); return () => { if (!backhandler) return; backhandler.remove(); backhandler = null; }; - }, [isFocused]); + }, []); const params = navigation.state.params; diff --git a/apps/mobile/src/views/Notebook/index.js b/apps/mobile/src/views/Notebook/index.js index 127794dec..b107322e8 100644 --- a/apps/mobile/src/views/Notebook/index.js +++ b/apps/mobile/src/views/Notebook/index.js @@ -35,7 +35,6 @@ export const Notebook = ({navigation}) => { allTopics = db.notebooks.notebook(navigation.state.params.notebook.id).data .topics; - console.log(allTopics); notebook = db.notebooks.notebook(navigation.state.params.notebook.id); @@ -67,13 +66,15 @@ export const Notebook = ({navigation}) => { type: ACTIONS.CURRENT_SCREEN, screen: 'notebook', }); + onLoad(); } - + }, [isFocused]); + useEffect(() => { eSubscribeEvent(eMoveNoteDialogNavigateBack, handleBackPress); return () => { eUnSubscribeEvent(eMoveNoteDialogNavigateBack, handleBackPress); }; - }, [isFocused]); + }, []); const onScroll = event => { let y = event.nativeEvent.contentOffset.y; diff --git a/apps/mobile/src/views/Notes/index.js b/apps/mobile/src/views/Notes/index.js index c00787de7..85ca828b7 100644 --- a/apps/mobile/src/views/Notes/index.js +++ b/apps/mobile/src/views/Notes/index.js @@ -18,6 +18,7 @@ import {ToastEvent} from '../../utils/utils'; import {eSendEvent} from '../../services/eventManager'; import {eScrollEvent} from '../../services/events'; import {NotesPlaceHolder} from '../../components/ListPlaceholders'; +import {useIsFocused} from 'react-navigation-hooks'; export const Notes = ({navigation}) => { const [state, dispatch] = useTracked(); @@ -25,6 +26,7 @@ export const Notes = ({navigation}) => { const allNotes = state.notes; const [notes, setNotes] = useState([]); const [refreshing, setRefreshing] = useState(false); + const isFocused = useIsFocused(); let params = navigation.state ? navigation.state.params : null; useEffect(() => { @@ -34,6 +36,15 @@ export const Notes = ({navigation}) => { }; } }, []); + useEffect(() => { + if (isFocused) { + init(); + dispatch({ + type: ACTIONS.CURRENT_SCREEN, + screen: param.type, + }); + } + }, [isFocused, allNotes, colorNotes]); const init = () => { eSendEvent(eScrollEvent, 0); @@ -49,18 +60,12 @@ export const Notes = ({navigation}) => { .notebook(params.notebookId) .topics.topic(params.title).all; - console.log(allNotes, 'here getting topics'); - if (allNotes && allNotes.length > 0) { setNotes(allNotes); } } }; - useEffect(() => { - init(); - }, [allNotes, colorNotes]); - const _renderItem = ({item, index}) => ( { const [state, dispatch] = useTracked(); const {colors, tags, selectionMode} = state; const [refreshing, setRefreshing] = useState(false); + const isFocused = useIsFocused(); useEffect(() => { - dispatch({type: ACTIONS.TAGS}); - dispatch({ - type: ACTIONS.CURRENT_SCREEN, - screen: 'tags', - }); - }, []); + if (isFocused) { + dispatch({type: ACTIONS.TAGS}); + dispatch({ + type: ACTIONS.CURRENT_SCREEN, + screen: 'tags', + }); + } + }, [isFocused]); return ( { const [state, dispatch] = useTracked(); const {colors, selectionMode, trash} = state; const [refreshing, setRefreshing] = useState(false); - useEffect(() => { - dispatch({ - type: ACTIONS.TRASH, - }); + const isFocused = useIsFocused(); - dispatch({ - type: ACTIONS.CURRENT_SCREEN, - screen: 'trash', - }); - }, []); + useEffect(() => { + if (isFocused) { + dispatch({ + type: ACTIONS.TRASH, + }); + + dispatch({ + type: ACTIONS.CURRENT_SCREEN, + screen: 'trash', + }); + } + }, [isFocused]); const _renderItem = ({item, index}) => (