fix state

This commit is contained in:
ammarahm-ed
2020-01-17 16:23:13 +05:00
parent 85d669ac7a
commit a879469ca4
35 changed files with 444 additions and 419 deletions

View File

@@ -10,17 +10,18 @@ import {Menu} from './src/components/Menu';
import SideMenu from './src/components/SideMenu'; import SideMenu from './src/components/SideMenu';
import Storage from 'notes-core/api/database'; import Storage from 'notes-core/api/database';
import StorageInterface from './src/utils/storage'; import StorageInterface from './src/utils/storage';
import {Provider, useTracked} from './src/provider';
import {DeviceDetectionService} from './src/utils/deviceDetection'; import {DeviceDetectionService} from './src/utils/deviceDetection';
import {DialogManager} from './src/components/DialogManager'; import {DialogManager} from './src/components/DialogManager';
import {useAppContext} from './src/provider/useAppContext';
import {AppProvider} from './src/provider';
import {COLOR_SCHEME_LIGHT} from './src/common/common';
export const DDS = new DeviceDetectionService(); export const DDS = new DeviceDetectionService();
export const db = new Storage(StorageInterface); export const db = new Storage(StorageInterface);
let sideMenuRef; let sideMenuRef;
const App = () => { const App = () => {
const [state, dispatch] = useTracked(); const [colors, setColors] = useState(COLOR_SCHEME_LIGHT);
const {colors} = state;
// Global State // Global State
// Local State // Local State
@@ -73,15 +74,15 @@ const App = () => {
db.init().then(() => { db.init().then(() => {
setInit(true); setInit(true);
}); });
}, []); });
// Render // Render
if (!init) { if (!init) {
return <></>; return <></>;
} }
return ( return (
<Provider> <AppProvider>
<View <View
style={{ style={{
width: '100%', width: '100%',
@@ -144,14 +145,9 @@ const App = () => {
</SideMenu> </SideMenu>
)} )}
<Toast /> <Toast />
<DialogManager
colors={colors}
update={type => {
dispatch({type: 'updateNotes'});
}}
/>
</View> </View>
</Provider> <DialogManager colors={colors} />
</AppProvider>
); );
}; };

View File

@@ -5,15 +5,5 @@
import {AppRegistry} from 'react-native'; import {AppRegistry} from 'react-native';
import App, {db} from './App'; import App, {db} from './App';
import {name as appName} from './app.json'; import {name as appName} from './app.json';
import React, {useEffect} from 'react';
import {Provider} from './src/provider';
const AppProvider = () => { AppRegistry.registerComponent(appName, () => App);
return (
<Provider>
<App />
</Provider>
);
};
AppRegistry.registerComponent(appName, () => AppProvider);

View File

@@ -19,7 +19,8 @@ import NavigationService from '../../services/NavigationService';
import {db} from '../../../App'; import {db} from '../../../App';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import FastStorage from 'react-native-fast-storage'; import FastStorage from 'react-native-fast-storage';
import {useTracked} from '../../provider';
import {_recieveEvent, _unSubscribeEvent} from '../DialogManager';
const w = Dimensions.get('window').width; const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height; const h = Dimensions.get('window').height;
@@ -33,13 +34,10 @@ export const ActionSheetComponent = ({
rowItems = [], rowItems = [],
columnItems = [], columnItems = [],
}) => { }) => {
const [state, dispatch] = useTracked(); const {colors, updateDB, changeColorScheme} = useAppContext();
const {colors} = state;
// Todo // Todo
const changeColorScheme = () => {};
const [focused, setFocused] = useState(false); const [focused, setFocused] = useState(false);
const [note, setNote] = useState( const [note, setNote] = useState(
item item
@@ -154,7 +152,7 @@ export const ActionSheetComponent = ({
} }
} }
dispatch({type: type}); updateDB();
setNote({...toAdd}); setNote({...toAdd});
}; };

View File

@@ -13,13 +13,12 @@ import {getElevation, ToastEvent} from '../../utils/utils';
import {FlatList, TextInput} from 'react-native-gesture-handler'; import {FlatList, TextInput} from 'react-native-gesture-handler';
import {db, DDS} from '../../../App'; import {db, DDS} from '../../../App';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
let refs = []; let refs = [];
export const AddNotebookDialog = ({visible, close, toEdit = null}) => { export const AddNotebookDialog = ({visible, close, toEdit = null}) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
const [topics, setTopics] = useState(['']); const [topics, setTopics] = useState(['']);
const [title, setTitle] = useState(null); const [title, setTitle] = useState(null);

View File

@@ -28,7 +28,7 @@ import {FlatList, TextInput} from 'react-native-gesture-handler';
import {useForceUpdate} from '../../views/ListsEditor'; import {useForceUpdate} from '../../views/ListsEditor';
import {db} from '../../../App'; import {db} from '../../../App';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
export const AddTopicDialog = ({ export const AddTopicDialog = ({
visible, visible,
@@ -36,8 +36,7 @@ export const AddTopicDialog = ({
notebookID, notebookID,
toEdit = null, toEdit = null,
}) => { }) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
const [titleFocused, setTitleFocused] = useState(false); const [titleFocused, setTitleFocused] = useState(false);
let title = null; let title = null;

View File

@@ -19,7 +19,7 @@ import NavigationService from '../../services/NavigationService';
import * as Animatable from 'react-native-animatable'; import * as Animatable from 'react-native-animatable';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {DDS} from '../../../App'; import {DDS} from '../../../App';
import {useTracked} from '../../provider';
export const AnimatedSafeAreaView = Animatable.createAnimatableComponent( export const AnimatedSafeAreaView = Animatable.createAnimatableComponent(
SafeAreaView, SafeAreaView,
); );
@@ -31,8 +31,7 @@ export const Container = ({
noBottomButton = false, noBottomButton = false,
}) => { }) => {
// State // State
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
const [buttonHide, setButtonHide] = useState(false); const [buttonHide, setButtonHide] = useState(false);

View File

@@ -13,7 +13,7 @@ import {
} from '../../common/common'; } from '../../common/common';
import Icon from 'react-native-vector-icons/Feather'; import Icon from 'react-native-vector-icons/Feather';
import {getElevation, ToastEvent} from '../../utils/utils'; import {getElevation, ToastEvent} from '../../utils/utils';
import {useTracked} from '../../provider';
import {dialogActions, updateEvent} from '../DialogManager'; import {dialogActions, updateEvent} from '../DialogManager';
import {db} from '../../../App'; import {db} from '../../../App';

View File

@@ -3,6 +3,7 @@ import {DeviceEventEmitter} from 'react-native';
import {ActionSheetComponent} from '../ActionSheetComponent'; import {ActionSheetComponent} from '../ActionSheetComponent';
import ActionSheet from '../ActionSheet'; import ActionSheet from '../ActionSheet';
import {Dialog} from '../Dialog'; import {Dialog} from '../Dialog';
import {VaultDialog} from '../VaultDialog';
export const ActionSheetEvent = (item, colors, tags, rowItems, columnItems) => { export const ActionSheetEvent = (item, colors, tags, rowItems, columnItems) => {
DeviceEventEmitter.emit('ActionSheetEvent', { DeviceEventEmitter.emit('ActionSheetEvent', {
item, item,
@@ -56,6 +57,8 @@ export class DialogManager extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.actionSheet; this.actionSheet;
this.opened = false;
this.state = { this.state = {
item: {}, item: {},
actionSheetData: { actionSheetData: {
@@ -72,9 +75,17 @@ export class DialogManager extends Component {
action: 0, action: 0,
icon: '', icon: '',
}, },
isPerm: false,
}; };
} }
shouldComponentUpdate(nextProps, nextState) {
return (
JSON.stringify(nextProps) !== JSON.stringify(this.props) ||
nextState !== this.state
);
}
_showActionSheet = data => { _showActionSheet = data => {
this.setState( this.setState(
{ {
@@ -120,6 +131,13 @@ export class DialogManager extends Component {
this.simpleDialog.hide(); this.simpleDialog.hide();
}; };
_showVaultDialog = () => {
this.vaultDialogRef.open();
};
_hideVaultDialog = () => {
this.vaultDialogRef.close();
};
onActionSheetHide = () => { onActionSheetHide = () => {
if (this.show) { if (this.show) {
if (this.show === 'delete') { if (this.show === 'delete') {
@@ -127,16 +145,13 @@ export class DialogManager extends Component {
this.show = null; this.show = null;
} else if (this.show == 'lock') { } else if (this.show == 'lock') {
this.setState({ this._showVaultDialog();
vaultDialog: true,
});
this.show = null; this.show = null;
} else if (this.show == 'unlock') { } else if (this.show == 'unlock') {
this.setState({ this.setState({
unlock: true,
isPerm: true, isPerm: true,
vaultDialog: true,
}); });
this._showVaultDialog();
this.show = null; this.show = null;
} }
} }
@@ -144,8 +159,15 @@ export class DialogManager extends Component {
}; };
render() { render() {
let {colors, update} = this.props; let {colors} = this.props;
let {actionSheetData, item, simpleDialog} = this.state; let {
actionSheetData,
item,
simpleDialog,
isPerm,
vaultDialog,
unlock,
} = this.state;
return ( return (
<> <>
<ActionSheet <ActionSheet
@@ -187,6 +209,15 @@ export class DialogManager extends Component {
colors={colors} colors={colors}
template={simpleDialog} template={simpleDialog}
/> />
<VaultDialog
ref={ref => (this.vaultDialogRef = ref)}
colors={colors}
note={item}
perm={isPerm}
openedToUnlock={false}
visible={vaultDialog}
/>
</> </>
); );
} }

View File

@@ -26,7 +26,7 @@ import {AnimatedSafeAreaView} from '../../views/Home';
import {TextInput} from 'react-native-gesture-handler'; import {TextInput} from 'react-native-gesture-handler';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {VaultDialog} from '../VaultDialog'; import {VaultDialog} from '../VaultDialog';
import {useTracked} from '../../provider';
let tagsInputRef; let tagsInputRef;
let tagsList; let tagsList;
@@ -39,11 +39,9 @@ export const EditorMenu = ({
note, note,
timestamp, timestamp,
}) => { }) => {
const [state, dispatch] = useTracked(); const {colors, changeColorScheme} = useAppContext();
const {colors} = state;
// Todo // Todo
const changeColorScheme = () => {};
/////// ///////
const [unlock, setUnlock] = useState(false); const [unlock, setUnlock] = useState(false);

View File

@@ -14,12 +14,11 @@ import Icon from 'react-native-vector-icons/Ionicons';
import {getElevation} from '../../utils/utils'; import {getElevation} from '../../utils/utils';
import {FlatList} from 'react-native-gesture-handler'; import {FlatList} from 'react-native-gesture-handler';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
const w = Dimensions.get('window').width; const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height; const h = Dimensions.get('window').height;
export const ListItem = props => { export const ListItem = props => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
return ( return (
<View <View

View File

@@ -21,10 +21,9 @@ import {h} from '../../utils/utils';
import {AnimatedSafeAreaView} from '../../views/Home'; import {AnimatedSafeAreaView} from '../../views/Home';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import FastStorage from 'react-native-fast-storage'; import FastStorage from 'react-native-fast-storage';
import {useTracked} from '../../provider';
export const Menu = ({close = () => {}, hide, update = () => {}}) => { export const Menu = ({close = () => {}, hide, update = () => {}}) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
// todo // todo

View File

@@ -49,8 +49,7 @@ export default class NoteItem extends React.Component {
update, update,
index, index,
} = this.props; } = this.props;
console.log('rerendering', index);
console.log('rerendering' + index);
return ( return (
<View <View
style={[ style={[
@@ -69,20 +68,6 @@ export default class NoteItem extends React.Component {
}, },
customStyle ? customStyle : {}, customStyle ? customStyle : {},
]}> ]}>
<VaultDialog
close={() => {
this.setState({
vaultDialog: false,
unlock: false,
isPerm: false,
});
}}
note={item}
perm={isPerm}
openedToUnlock={unlock}
visible={vaultDialog}
/>
{pinned ? ( {pinned ? (
<View <View
style={{ style={{

View File

@@ -27,11 +27,9 @@ export const NotebookItem = ({
customStyle, customStyle,
onLongPress, onLongPress,
}) => { }) => {
const [state, dispatch] = useTracked(); const {colors, updateDB} = useAppContext();
const {colors} = state;
/// ///
const updateDB = () => {};
const [isVisible, setVisible] = useState(false); const [isVisible, setVisible] = useState(false);
const [addTopic, setAddTopic] = useState(false); const [addTopic, setAddTopic] = useState(false);

View File

@@ -9,7 +9,7 @@ import {w} from '../../utils/utils';
import SelectionWrapper from '../SelectionWrapper'; import SelectionWrapper from '../SelectionWrapper';
import ActionSheet from '../ActionSheet'; import ActionSheet from '../ActionSheet';
import {ActionSheetComponent} from '../ActionSheetComponent'; import {ActionSheetComponent} from '../ActionSheetComponent';
import {useTracked} from '../../provider';
export const NotesList = ({ export const NotesList = ({
notes, notes,
@@ -18,12 +18,14 @@ export const NotesList = ({
isGrouped = false, isGrouped = false,
refresh = () => {}, refresh = () => {},
}) => { }) => {
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, pinned, selectedItemsList} = state; colors,
selectionMode,
/// pinned,
const updateSelectionList = () => {}; selectedItemsList,
const changeSelectionMode = () => {}; updateSelectionList,
changeSelectionMode,
} = useAppContext();
const _renderItem = ({item, index}) => ( const _renderItem = ({item, index}) => (
<SelectionWrapper item={item}> <SelectionWrapper item={item}>

View File

@@ -15,10 +15,9 @@ import {getElevation, w} from '../../utils/utils';
import * as Animatable from 'react-native-animatable'; import * as Animatable from 'react-native-animatable';
import {DDS} from '../../../App'; import {DDS} from '../../../App';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
export const Search = props => { export const Search = props => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
const [focus, setFocus] = useState(false); const [focus, setFocus] = useState(false);

View File

@@ -5,7 +5,7 @@ import {w} from '../../utils/utils';
import Icon from 'react-native-vector-icons/Feather'; import Icon from 'react-native-vector-icons/Feather';
import * as Animatable from 'react-native-animatable'; import * as Animatable from 'react-native-animatable';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
export const AnimatedSafeAreaView = Animatable.createAnimatableComponent( export const AnimatedSafeAreaView = Animatable.createAnimatableComponent(
SafeAreaView, SafeAreaView,
@@ -13,13 +13,14 @@ export const AnimatedSafeAreaView = Animatable.createAnimatableComponent(
export const SelectionHeader = ({navigation}) => { export const SelectionHeader = ({navigation}) => {
// State // State
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, selectedItemsList} = state; colors,
selectionMode,
/// selectedItemsList,
const updateDB = () => {}; updateDB,
const updateSelectionList = () => {}; updateSelectionList,
const changeSelectionMode = () => {}; changeSelectionMode,
} = useAppContext();
// Render // Render

View File

@@ -3,16 +3,18 @@ import {View, TouchableOpacity} from 'react-native';
import {SIZE} from '../../common/common'; import {SIZE} from '../../common/common';
import Icon from 'react-native-vector-icons/Feather'; import Icon from 'react-native-vector-icons/Feather';
import {w} from '../../utils/utils'; import {w} from '../../utils/utils';
import {useTracked} from '../../provider'; import {useAppContext} from '../../provider/useAppContext';
const SelectionWrapper = ({children, item}) => { const SelectionWrapper = ({children, item}) => {
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, selectedItemsList} = state; colors,
selectionMode,
selectedItemsList,
updateDB,
updateSelectionList,
changeSelectionMode,
} = useAppContext();
///
const updateDB = () => {};
const updateSelectionList = () => {};
const changeSelectionMode = () => {};
return ( return (
<View <View
style={{ style={{

View File

@@ -12,14 +12,13 @@ import Icon from 'react-native-vector-icons/Feather';
import * as Animatable from 'react-native-animatable'; import * as Animatable from 'react-native-animatable';
import {h, w} from '../../utils/utils'; import {h, w} from '../../utils/utils';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
const AnimatedTouchableOpacity = Animatable.createAnimatableComponent( const AnimatedTouchableOpacity = Animatable.createAnimatableComponent(
TouchableOpacity, TouchableOpacity,
); );
export const Toast = () => { export const Toast = () => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
const [toast, setToast] = useState(false); const [toast, setToast] = useState(false);
const [message, setMessage] = useState([]); const [message, setMessage] = useState([]);

View File

@@ -1,36 +1,71 @@
import React, {useState} from 'react'; import React, {Component} from 'react';
import {View, Text, TouchableOpacity, Modal} from 'react-native'; import {View, Text, TouchableOpacity, Modal} from 'react-native';
import {SIZE, ph, pv, opacity, WEIGHT} from '../../common/common'; import {SIZE, ph, pv, opacity, WEIGHT} from '../../common/common';
import Icon from 'react-native-vector-icons/Feather'; import Icon from 'react-native-vector-icons/Feather';
import {FlatList, TextInput} from 'react-native-gesture-handler'; import {TextInput} from 'react-native-gesture-handler';
import {useForceUpdate} from '../../views/ListsEditor';
import {db} from '../../../App'; import {db} from '../../../App';
import {useAppContext} from '../../provider/useAppContext';
import {getElevation} from '../../utils/utils'; import {getElevation} from '../../utils/utils';
import NavigationService from '../../services/NavigationService'; import NavigationService from '../../services/NavigationService';
import {useTracked} from '../../provider';
let refs = []; import {updateEvent} from '../DialogManager';
export const VaultDialog = ({ export class VaultDialog extends Component {
openedToUnlock = false, constructor(props) {
visible, super(props);
hasPassword = false, this.state = {
note, visible: false,
close = () => {}, };
perm = false, this.password = null;
timestamp = null, }
}) => {
const [state, dispatch] = useTracked(); open = () => {
const {colors} = state; this.setState({
visible: true,
});
};
close = () => {
this.setState(
{
visible: false,
},
() => {
updateEvent({type: this.props.note.type});
},
);
};
onPress = async () => {
if (this.props.note.locked) {
let n = db.getNote(this.props.note.dateCreated);
let item;
if (n.content.cipher) {
try {
item = await db.unlockNote(n.dateCreated, password, this.props.perm);
} catch (error) {
console.log(error);
}
} else {
item = n;
}
if (!this.props.perm) {
NavigationService.navigate('Editor', {
note: item,
});
}
this.close();
} else {
await db.lockNote(this.props.note.dateCreated, 'password');
this.close();
}
};
render() {
const {hasPassword, note, colors} = this.props;
const {visible} = this.state;
const [hidden, setHidden] = useState(false);
let password = null;
return ( return (
<Modal <Modal visible={visible} transparent={true} onRequestClose={this.close}>
visible={visible}
transparent={true}
onRequestClose={() => (refs = [])}>
<View <View
style={{ style={{
width: '100%', width: '100%',
@@ -65,7 +100,7 @@ export const VaultDialog = ({
marginLeft: 5, marginLeft: 5,
fontSize: SIZE.md, fontSize: SIZE.md,
}}> }}>
{openedToUnlock ? 'Unlock Note' : 'Lock Note'} {note.locked ? 'Unlock Note' : 'Lock Note'}
</Text> </Text>
</View> </View>
@@ -76,16 +111,16 @@ export const VaultDialog = ({
textAlign: 'center', textAlign: 'center',
fontSize: SIZE.sm - 1, fontSize: SIZE.sm - 1,
marginTop: 10, marginTop: 10,
marginBottom: hidden ? 20 : 0, //marginBottom: hidden ? 20 : 0,
}}> }}>
{hasPassword {hasPassword
? 'Set password for all your locked notes.' ? 'Set password for all your locked notes.'
: openedToUnlock : note.locked
? 'Enter vault password to unlock note.' ? 'Enter vault password to unlock note.'
: 'Do you want to lock this note?'} : 'Do you want to lock this note?'}
</Text> </Text>
{openedToUnlock ? ( {note.locked ? (
<TextInput <TextInput
style={{ style={{
padding: pv - 5, padding: pv - 5,
@@ -165,33 +200,7 @@ export const VaultDialog = ({
}}> }}>
<TouchableOpacity <TouchableOpacity
activeOpacity={opacity} activeOpacity={opacity}
onPress={async () => { onPress={this.onPress}
if (openedToUnlock) {
let n = db.getNote(note.dateCreated);
let item;
if (n.content.cipher) {
try {
item = await db.unlockNote(n.dateCreated, password, perm);
} catch (error) {
console.log(error);
}
} else {
item = n;
}
if (!perm) {
NavigationService.navigate('Editor', {
note: item,
});
}
close(item, false);
} else {
try {
await db.lockNote(note.dateCreated, 'password');
} catch (error) {}
close(null, true);
}
}}
style={{ style={{
paddingVertical: pv, paddingVertical: pv,
paddingHorizontal: ph, paddingHorizontal: ph,
@@ -209,15 +218,13 @@ export const VaultDialog = ({
color: 'white', color: 'white',
fontSize: SIZE.sm, fontSize: SIZE.sm,
}}> }}>
{openedToUnlock ? 'Unlock' : 'Lock'} {note.locked ? 'Unlock' : 'Lock'}
</Text> </Text>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity <TouchableOpacity
activeOpacity={opacity} activeOpacity={opacity}
onPress={() => { onPress={this.close}
close();
}}
style={{ style={{
paddingVertical: pv, paddingVertical: pv,
paddingHorizontal: ph, paddingHorizontal: ph,
@@ -241,4 +248,5 @@ export const VaultDialog = ({
</View> </View>
</Modal> </Modal>
); );
}; }
}

View File

@@ -7,7 +7,7 @@ import * as Animatable from 'react-native-animatable';
import NavigationService from '../../services/NavigationService'; import NavigationService from '../../services/NavigationService';
import {DDS} from '../../../App'; import {DDS} from '../../../App';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
let isOpen = false; let isOpen = false;
export const Header = ({ export const Header = ({
heading, heading,
@@ -18,8 +18,7 @@ export const Header = ({
verticalMenu = false, verticalMenu = false,
sendHeight = e => {}, sendHeight = e => {},
}) => { }) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
return ( return (
<Animatable.View <Animatable.View

View File

@@ -1,6 +1,9 @@
import React, {useReducer} from 'react'; import React, {createContext, useEffect, useState} from 'react';
import {View, Text} from 'react-native';
import {useImmer} from 'use-immer';
import {DDS, db} from '../../App'; import {DDS, db} from '../../App';
import {createContainer} from 'react-tracked'; import {getColorScheme} from '../common/common';
const defaultState = { const defaultState = {
isMenuOpen: { isMenuOpen: {
current: false, current: false,
@@ -32,26 +35,51 @@ const defaultState = {
warningBg: '#FEEFB3', warningBg: '#FEEFB3',
warningText: '#9F6000', warningText: '#9F6000',
}, },
selectionMode: false,
selectedItemsList: [],
}; };
export const ACTIONS = { const AppContext = createContext([defaultState, function() {}]);
NOTES: 'note',
};
const reducer = (state, action) => { const AppProvider = ({children}) => {
switch (action.type) { const [state, dispatch] = useImmer({...defaultState});
case 'note': const [loading, setLoading] = useState(true);
let notes = db.groupNotes();
return {
...state,
notes: notes,
};
default: async function init() {
throw new Error('unknown action type'); let newColors = await getColorScheme();
dispatch(draft => {
draft.colors = {...newColors};
draft.notes = db.groupNotes();
draft.notebooks = db.getNotebooks();
draft.trash = db.getTrash();
draft.favorites = db.getFavorites();
draft.pinned = db.getPinned();
});
} }
useEffect(() => {
init();
}, []);
return (
<AppContext.Provider value={[state, dispatch]}>
{children}
{loading ? (
<View
style={{
width: '100%',
height: '100%',
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
}}>
<Text>Loading...</Text>
</View>
) : (
children
)}
</AppContext.Provider>
);
}; };
const useValue = () => useReducer(reducer, defaultState); export {AppProvider, AppContext};
export const {Provider, useTracked} = createContainer(useValue);

View File

@@ -4,11 +4,10 @@ import {SIZE, pv, opacity, WEIGHT} from '../../common/common';
import {Header} from '../../components/header'; import {Header} from '../../components/header';
import {AnimatedSafeAreaView} from '../Home'; import {AnimatedSafeAreaView} from '../Home';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
export const AccountSettings = ({navigation}) => { export const AccountSettings = ({navigation}) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
return ( return (
<AnimatedSafeAreaView <AnimatedSafeAreaView

View File

@@ -25,7 +25,7 @@ import ActionSheet from '../../components/ActionSheet';
import {ActionSheetComponent} from '../../components/ActionSheetComponent'; import {ActionSheetComponent} from '../../components/ActionSheetComponent';
import {VaultDialog} from '../../components/VaultDialog'; import {VaultDialog} from '../../components/VaultDialog';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused} from 'react-navigation-hooks';
import {useTracked} from '../../provider';
const w = Dimensions.get('window').width; const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height; const h = Dimensions.get('window').height;
@@ -38,11 +38,7 @@ var title = null;
let timer = null; let timer = null;
const Editor = ({navigation}) => { const Editor = ({navigation}) => {
// Global State // Global State
const [state, dispatch] = useTracked(); const {colors, updateDB} = useAppContext();
const {colors} = state;
///
const updateDB = () => {};
// Local State // Local State

View File

@@ -10,15 +10,17 @@ import {NotebookItem} from '../../components/NotebookItem';
import {FavoritesPlaceHolder} from '../../components/ListPlaceholders'; import {FavoritesPlaceHolder} from '../../components/ListPlaceholders';
import Container from '../../components/Container'; import Container from '../../components/Container';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused} from 'react-navigation-hooks';
import {useTracked} from '../../provider';
export const Favorites = ({navigation}) => { export const Favorites = ({navigation}) => {
// Global State // Global State
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, pinned, selectedItemsList, favorites} = state; colors,
selectionMode,
const updateSelectionList = () => {}; favorites,
const changeSelectionMode = () => {}; updateSelectionList,
changeSelectionMode,
} = useAppContext();
// Local State // Local State
const [text, setText] = useState(''); const [text, setText] = useState('');

View File

@@ -14,16 +14,18 @@ import SelectionHeader from '../../components/SelectionHeader';
import SelectionWrapper from '../../components/SelectionWrapper'; import SelectionWrapper from '../../components/SelectionWrapper';
import {w} from '../../utils/utils'; import {w} from '../../utils/utils';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused} from 'react-navigation-hooks';
import {useTracked} from '../../provider';
export const Folders = ({navigation}) => { export const Folders = ({navigation}) => {
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, pinned, notebooks} = state; colors,
selectionMode,
/// pinned,
const updateDB = () => {}; notebooks,
const updateSelectionList = () => {}; updateDB,
const changeSelectionMode = () => {}; updateSelectionList,
changeSelectionMode,
} = useAppContext();
const [addNotebook, setAddNotebook] = useState(false); const [addNotebook, setAddNotebook] = useState(false);
const [hideHeader, setHideHeader] = useState(false); const [hideHeader, setHideHeader] = useState(false);

View File

@@ -33,16 +33,17 @@ import {NavigationEvents} from 'react-navigation';
import {Header} from '../../components/header'; import {Header} from '../../components/header';
import {useForceUpdate} from '../ListsEditor'; import {useForceUpdate} from '../ListsEditor';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
export const ForgotPassword = ({navigation}) => { export const ForgotPassword = ({navigation}) => {
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, pinned, selectedItemsList} = state; colors,
updateDB,
updateSelectionList,
changeSelectionMode,
} = useAppContext();
/// ///
const updateDB = () => {};
const updateSelectionList = () => {};
const changeSelectionMode = () => {};
useEffect(() => { useEffect(() => {
DeviceEventEmitter.emit('hide'); DeviceEventEmitter.emit('hide');

View File

@@ -1,5 +1,5 @@
import React, {useEffect, useState} from 'react'; import React, {useEffect, useState} from 'react';
import {SafeAreaView} from 'react-native'; import {SafeAreaView, Text} from 'react-native';
import {Search} from '../../components/SearchInput'; import {Search} from '../../components/SearchInput';
import {w, h, SideMenuEvent, ToastEvent} from '../../utils/utils'; import {w, h, SideMenuEvent, ToastEvent} from '../../utils/utils';
import {Header} from '../../components/header'; import {Header} from '../../components/header';
@@ -11,26 +11,28 @@ import {useAppContext} from '../../provider/useAppContext';
import {DDS} from '../../../App'; import {DDS} from '../../../App';
import Container from '../../components/Container'; import Container from '../../components/Container';
import SelectionHeader from '../../components/SelectionHeader'; import SelectionHeader from '../../components/SelectionHeader';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused, useFocusEffect} from 'react-navigation-hooks';
import {useTracked, ACTIONS} from '../../provider'; import {
import {_recieveEvent, _unSubscribeEvent} from '../../components/DialogManager'; _recieveEvent,
_unSubscribeEvent,
updateEvent,
} from '../../components/DialogManager';
export const AnimatedSafeAreaView = Animatable.createAnimatableComponent( export const AnimatedSafeAreaView = Animatable.createAnimatableComponent(
SafeAreaView, SafeAreaView,
); );
let intervals;
let counter = 0;
export const Home = ({navigation}) => { export const Home = ({navigation}) => {
// State // State
const {
const [state, dispatch] = useTracked(); colors,
const {colors, selectionMode, notes} = state; selectionMode,
pinned,
/// notes,
const updateDB = () => {}; updateDB,
const updateSelectionList = () => {}; updateSelectionList,
const changeSelectionMode = () => {}; changeSelectionMode,
} = useAppContext();
const [text, setText] = useState(''); const [text, setText] = useState('');
const [hideHeader, setHideHeader] = useState(false); const [hideHeader, setHideHeader] = useState(false);
@@ -48,19 +50,15 @@ export const Home = ({navigation}) => {
// Effects // Effects
useEffect(() => { useEffect(() => {
dispatch({type: ACTIONS.NOTES}); _recieveEvent('updateEvent', () => {
}, []); updateDB();
useEffect(() => {
_recieveEvent('updateEvent', type => {
dispatch(type);
}); });
return () => { return () => {
_unSubscribeEvent('updateEvent', type => { _unSubscribeEvent('updateEvent', () => {
dispatch(type); updateDB();
}); });
}; };
}); }, []);
// Functions // Functions
@@ -115,7 +113,7 @@ export const Home = ({navigation}) => {
}; };
// Render // Render
console.log('rerender');
return ( return (
<Container <Container
bottomButtonText="Add a new note" bottomButtonText="Add a new note"
@@ -141,6 +139,7 @@ export const Home = ({navigation}) => {
hide={hideHeader} hide={hideHeader}
verticalMenu verticalMenu
showSearch={() => { showSearch={() => {
let counter = updateCounter;
setHideHeader(false); setHideHeader(false);
countUp = 0; countUp = 0;
countDown = 0; countDown = 0;

View File

@@ -24,14 +24,13 @@ import Icon from 'react-native-vector-icons/Ionicons';
import {Reminder} from '../../components/Reminder'; import {Reminder} from '../../components/Reminder';
import {ListItem} from '../../components/ListItem'; import {ListItem} from '../../components/ListItem';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
const w = Dimensions.get('window').width; const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height; const h = Dimensions.get('window').height;
export const Lists = ({navigation}) => { export const Lists = ({navigation}) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
return ( return (
<SafeAreaView> <SafeAreaView>

View File

@@ -14,10 +14,9 @@ import {NavigationEvents} from 'react-navigation';
import {Header} from '../../components/header'; import {Header} from '../../components/header';
import {DDS} from '../../../App'; import {DDS} from '../../../App';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
export const Login = ({navigation}) => { export const Login = ({navigation}) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
useEffect(() => { useEffect(() => {
DeviceEventEmitter.emit('hide'); DeviceEventEmitter.emit('hide');

View File

@@ -9,16 +9,17 @@ import * as Animatable from 'react-native-animatable';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused} from 'react-navigation-hooks';
import Container from '../../components/Container'; import Container from '../../components/Container';
import {useTracked} from '../../provider';
export const Notebook = ({navigation}) => { export const Notebook = ({navigation}) => {
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, pinned, selectedItemsList} = state; colors,
selectionMode,
updateDB,
updateSelectionList,
changeSelectionMode,
} = useAppContext();
///
const updateDB = () => {};
const updateSelectionList = () => {};
const changeSelectionMode = () => {};
const params = navigation.state.params; const params = navigation.state.params;
const [hideHeader, setHideHeader] = useState(false); const [hideHeader, setHideHeader] = useState(false);
const [buttonHide, setButtonHide] = useState(false); const [buttonHide, setButtonHide] = useState(false);

View File

@@ -7,16 +7,17 @@ import * as Animatable from 'react-native-animatable';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import Container from '../../components/Container'; import Container from '../../components/Container';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused} from 'react-navigation-hooks';
import {useTracked} from '../../provider';
export const Notes = ({navigation}) => { export const Notes = ({navigation}) => {
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, pinned, selectedItemsList} = state; colors,
selectionMode,
updateDB,
updateSelectionList,
changeSelectionMode,
} = useAppContext();
///
const updateDB = () => {};
const updateSelectionList = () => {};
const changeSelectionMode = () => {};
const [hideHeader, setHideHeader] = useState(false); const [hideHeader, setHideHeader] = useState(false);
const [margin, setMargin] = useState(200); const [margin, setMargin] = useState(200);
const [buttonHide, setButtonHide] = useState(false); const [buttonHide, setButtonHide] = useState(false);

View File

@@ -22,14 +22,10 @@ import {useAppContext} from '../../provider/useAppContext';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused} from 'react-navigation-hooks';
import Container from '../../components/Container'; import Container from '../../components/Container';
import FastStorage from 'react-native-fast-storage'; import FastStorage from 'react-native-fast-storage';
import {useTracked} from '../../provider';
export const Settings = ({navigation}) => {
const [state, dispatch] = useTracked();
const {colors} = state;
/// export const Settings = ({navigation}) => {
const changeAccentColor = () => {}; const {colors, changeAccentColor, changeColorScheme} = useAppContext();
const changeColorScheme = () => {};
//const [colors, setColors] = useState(COLOR_SCHEME_LIGHT); //const [colors, setColors] = useState(COLOR_SCHEME_LIGHT);
let isFocused = useIsFocused(); let isFocused = useIsFocused();

View File

@@ -12,11 +12,10 @@ import {NavigationEvents} from 'react-navigation';
import {Header} from '../../components/header'; import {Header} from '../../components/header';
import {DDS} from '../../../App'; import {DDS} from '../../../App';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {useTracked} from '../../provider';
export const Signup = ({navigation}) => { export const Signup = ({navigation}) => {
const [state, dispatch] = useTracked(); const {colors} = useAppContext();
const {colors} = state;
useEffect(() => { useEffect(() => {
DeviceEventEmitter.emit('hide'); DeviceEventEmitter.emit('hide');
return () => { return () => {

View File

@@ -5,13 +5,13 @@ import {SIZE, pv, WEIGHT} from '../../common/common';
import {Header} from '../../components/header'; import {Header} from '../../components/header';
import {useAppContext} from '../../provider/useAppContext'; import {useAppContext} from '../../provider/useAppContext';
import {TagsPlaceHolder} from '../../components/ListPlaceholders'; import {TagsPlaceHolder} from '../../components/ListPlaceholders';
import {useTracked} from '../../provider';
const w = Dimensions.get('window').width; const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height; const h = Dimensions.get('window').height;
export const Tags = ({navigation}) => { export const Tags = ({navigation}) => {
const [state, dispatch] = useTracked(); const {colors, tags} = useAppContext();
const {colors} = state;
let isFocused = useIsFocused(); let isFocused = useIsFocused();
if (!isFocused) { if (!isFocused) {

View File

@@ -13,15 +13,17 @@ import {TrashPlaceHolder} from '../../components/ListPlaceholders';
import Container from '../../components/Container'; import Container from '../../components/Container';
import SelectionHeader from '../../components/SelectionHeader'; import SelectionHeader from '../../components/SelectionHeader';
import {useIsFocused} from 'react-navigation-hooks'; import {useIsFocused} from 'react-navigation-hooks';
import {useTracked} from '../../provider';
export const Trash = ({navigation}) => { export const Trash = ({navigation}) => {
const [state, dispatch] = useTracked(); const {
const {colors, selectionMode, trash} = state; colors,
selectionMode,
const updateDB = () => {}; updateDB,
const changeSelectionMode = () => {}; updateSelectionList,
const updateSelectionList = () => {}; changeSelectionMode,
trash,
} = useAppContext();
const [dialog, setDialog] = useState(false); const [dialog, setDialog] = useState(false);
let isFocused = useIsFocused(); let isFocused = useIsFocused();