fix backHandler in Editor

This commit is contained in:
ammarahm-ed
2020-04-19 17:19:47 +05:00
parent 2353243bd6
commit a66fdd1a6d
3 changed files with 87 additions and 185 deletions

View File

@@ -98,7 +98,8 @@ export const NotebookPlaceHolder = ({colors, animation}) => {
height: 8, height: 8,
backgroundColor: 'white', backgroundColor: 'white',
marginBottom: 15, marginBottom: 15,
}}></View> }}
/>
<Text <Text
style={{ style={{
fontFamily: WEIGHT.bold, fontFamily: WEIGHT.bold,
@@ -153,7 +154,8 @@ export const NotebookPlaceHolder = ({colors, animation}) => {
height: 8, height: 8,
backgroundColor: 'white', backgroundColor: 'white',
marginBottom: 15, marginBottom: 15,
}}></View> }}
/>
<Text <Text
style={{ style={{
fontFamily: WEIGHT.bold, fontFamily: WEIGHT.bold,
@@ -489,104 +491,7 @@ export const TrashPlaceHolder = ({colors, animation}) => {
style={{ style={{
alignItems: 'center', alignItems: 'center',
}}> }}>
<Animatable.View <View
animation={deleteItems(-50, -100)}
iterationCount="infinite"
duration={3000}
iterationDelay={7000}
easing="ease-in"
useNativeDriver={true}
style={{
width: 30,
height: 30,
backgroundColor: colors.errorBg,
borderRadius: 5,
transform: [
{
translateX: -50,
},
{
translateY: -100,
},
],
}}
/>
<Animatable.View
animation={deleteItems(-80, -70)}
iterationCount="infinite"
duration={3000}
iterationDelay={7000}
easing="ease-in"
useNativeDriver={true}
style={{
width: 25,
height: 25,
backgroundColor: colors.errorBg,
borderRadius: 5,
transform: [
{
translateX: -80,
},
{
translateY: -70,
},
],
}}
/>
<Animatable.View
animation={deleteItems(-120, -50)}
iterationCount="infinite"
duration={3000}
iterationDelay={7000}
easing="ease-in"
useNativeDriver={true}
style={{
width: 40,
height: 40,
backgroundColor: colors.errorBg,
borderRadius: 5,
transform: [
{
translateX: -120,
},
{
translateY: -50,
},
],
}}
/>
<Animatable.View
animation={deleteItems(-120, -140)}
iterationCount="infinite"
duration={3000}
iterationDelay={7000}
easing="ease-in"
useNativeDriver={true}
style={{
width: 20,
height: 20,
backgroundColor: colors.errorBg,
borderRadius: 5,
transform: [
{
translateX: -120,
},
{
translateY: -140,
},
],
}}
/>
<Animatable.View
animation={rotate}
iterationCount="infinite"
duration={3000}
iterationDelay={3500}
direction="alternate"
easing="ease-in"
useNativeDriver={true}
style={{ style={{
width: 100, width: 100,
height: 15, height: 15,
@@ -607,7 +512,7 @@ export const TrashPlaceHolder = ({colors, animation}) => {
marginTop: -9, marginTop: -9,
}} }}
/> />
</Animatable.View> </View>
<View <View
style={{ style={{
@@ -623,14 +528,7 @@ export const TrashPlaceHolder = ({colors, animation}) => {
borderRadius: 5, borderRadius: 5,
zIndex: 10, zIndex: 10,
}}> }}>
<Animatable.View <View
animation={opacity}
iterationCount="infinite"
duration={3000}
iterationDelay={3500}
direction="alternate"
easing="ease-in"
useNativeDriver={true}
style={{ style={{
flexDirection: 'row', flexDirection: 'row',
width: '90%', width: '90%',
@@ -666,7 +564,7 @@ export const TrashPlaceHolder = ({colors, animation}) => {
zIndex: 10, zIndex: 10,
}} }}
/> />
</Animatable.View> </View>
</View> </View>
</View> </View>
</View> </View>

View File

@@ -49,6 +49,8 @@ let timer = null;
let saveCounter = 0; let saveCounter = 0;
let tapCount = 0; let tapCount = 0;
let canSave = false; let canSave = false;
let handleBack;
const Editor = ({noMenu}) => { const Editor = ({noMenu}) => {
// Global State // Global State
const [state, dispatch] = useTracked(); const [state, dispatch] = useTracked();
@@ -80,6 +82,7 @@ const Editor = ({ noMenu }) => {
const loadNote = async item => { const loadNote = async item => {
//EditorWebView.current?.requestFocus(); //EditorWebView.current?.requestFocus();
editing.currentlyEditing = true;
noMenu ? null : sideMenuRef.current?.setGestureEnabled(false); noMenu ? null : sideMenuRef.current?.setGestureEnabled(false);
if (note && note.id) { if (note && note.id) {
dispatch({type: ACTIONS.NOTES}); dispatch({type: ACTIONS.NOTES});
@@ -142,22 +145,7 @@ const Editor = ({ noMenu }) => {
const onCallClear = () => { const onCallClear = () => {
canSave = false; canSave = false;
exitEditorAnimation(); exitEditorAnimation();
setDateEdited(0); clearEditor();
title = null;
content = null;
note = null;
id = null;
tapCount = 0;
saveCounter = 0;
post({
type: 'clearEditor',
});
post({
type: 'clearTitle',
});
post({
type: 'blur',
});
}; };
useEffect(() => { useEffect(() => {
@@ -251,7 +239,13 @@ const Editor = ({ noMenu }) => {
const saveNote = async (lockNote = true) => { const saveNote = async (lockNote = true) => {
if (!canSave) return; if (!canSave) return;
if (!title && !content) return; if (!title && !content) return;
if (content && content.text.trim().length === 0 && title && title.trim().length == 0) return; if (
content &&
content.text.trim().length === 0 &&
title &&
title.trim().length == 0
)
return;
if (!content && title && title.trim().length === 0) return; if (!content && title && title.trim().length === 0) return;
if (!title && content && content.text.trim.length === 0) return; if (!title && content && content.text.trim.length === 0) return;
if (!content) { if (!content) {
@@ -425,12 +419,17 @@ const Editor = ({ noMenu }) => {
}); });
const _onHardwareBackPress = async () => { const _onHardwareBackPress = async () => {
if (editing.currentlyEditing) {
if (tapCount > 0) { if (tapCount > 0) {
exitEditorAnimation(); exitEditorAnimation();
if (note && note.id) { if (note && note.id) {
ToastEvent.show('Note Saved!', 'success'); ToastEvent.show('Note Saved!', 'success');
} }
await clearEditor(); await clearEditor();
if (handleBack) {
handleBack.remove();
handleBack = null;
}
if (noMenu) return true; if (noMenu) return true;
DDS.isTab ? sideMenuRef.current?.openMenu(true) : null; DDS.isTab ? sideMenuRef.current?.openMenu(true) : null;
sideMenuRef.current?.setGestureEnabled(true); sideMenuRef.current?.setGestureEnabled(true);
@@ -443,26 +442,16 @@ const Editor = ({ noMenu }) => {
ToastEvent.show('Press back again to exit editor', 'success'); ToastEvent.show('Press back again to exit editor', 'success');
return true; return true;
} }
}
}; };
useEffect(() => { useEffect(() => {
let handleBack;
if (!noMenu && DDS.isTab) { if (!noMenu && DDS.isTab) {
handleBack = BackHandler.addEventListener('hardwareBackPress', () => { handleBack = BackHandler.addEventListener('hardwareBackPress', () => {
simpleDialogEvent(TEMPLATE_EXIT_FULLSCREEN()); simpleDialogEvent(TEMPLATE_EXIT_FULLSCREEN());
editing.isFullscreen = false; editing.isFullscreen = false;
return true; return true;
}); });
} else if (!DDS.isTab) {
handleBack = BackHandler.addEventListener(
'hardwareBackPress',
_onHardwareBackPress,
);
} else {
if (handleBack) {
handleBack.remove();
handleBack = null;
}
} }
return () => { return () => {
@@ -470,14 +459,24 @@ const Editor = ({ noMenu }) => {
handleBack.remove(); handleBack.remove();
handleBack = null; handleBack = null;
} }
title = null;
content = null;
id = null;
timer = null;
note = {};
}; };
}, [noMenu]); }, [noMenu]);
useEffect(() => {
if (!DDS.isTab) {
handleBack = BackHandler.addEventListener(
'hardwareBackPress',
_onHardwareBackPress,
);
}
return () => {
if (handleBack) {
handleBack.remove();
handleBack = null;
}
};
}, []);
return ( return (
<SafeAreaView <SafeAreaView
style={{ style={{
@@ -497,11 +496,13 @@ const Editor = ({ noMenu }) => {
<View <View
style={{ style={{
marginTop: Platform.OS === 'ios' ? 0 : StatusBar.currentHeight, marginTop: Platform.OS === 'ios' ? 0 : StatusBar.currentHeight,
}}></View> }}
/>
{noMenu ? null : ( {noMenu ? null : (
<TouchableOpacity <TouchableOpacity
onPress={async () => { onPress={async () => {
editing.currentlyEditing = true;
if (DDS.isTab) { if (DDS.isTab) {
simpleDialogEvent(TEMPLATE_EXIT_FULLSCREEN()); simpleDialogEvent(TEMPLATE_EXIT_FULLSCREEN());
} else { } else {
@@ -512,6 +513,10 @@ const Editor = ({ noMenu }) => {
await clearEditor(); await clearEditor();
DDS.isTab ? sideMenuRef.current?.openMenu(true) : null; DDS.isTab ? sideMenuRef.current?.openMenu(true) : null;
sideMenuRef.current?.setGestureEnabled(true); sideMenuRef.current?.setGestureEnabled(true);
if (handleBack) {
handleBack.remove();
handleBack = null;
}
} }
}} }}
style={{ style={{
@@ -597,8 +602,7 @@ const Editor = ({ noMenu }) => {
<View <View
style={{ style={{
paddingHorizontal: 12, paddingHorizontal: 12,
marginTop: marginTop: Platform.OS === 'ios' ? 45 : insets.top + 45,
Platform.OS === 'ios' ? 45 : insets.top + 45,
width: '100%', width: '100%',
position: 'absolute', position: 'absolute',
justifyContent: 'flex-start', justifyContent: 'flex-start',

View File

@@ -167,14 +167,14 @@ export const Notes = ({navigation}) => {
return ( return (
<Container <Container
bottomButtonText="Create a new note" bottomButtonText="Create a new note"
canGoBack={false} canGoBack={params.type === 'color' ? false : true}
menu={params.type === 'color' ? true : false}
heading={ heading={
params.type == 'tag' params.type == 'tag'
? '#' + params.title ? '#' + params.title
: params.title.slice(0, 1).toUpperCase() + params.title.slice(1) : params.title.slice(0, 1).toUpperCase() + params.title.slice(1)
} }
headerColor={params.type == 'color' ? params.title : null} headerColor={params.type == 'color' ? params.title : null}
canGoBack={true}
data={notes} data={notes}
type="notes" type="notes"
placeholder={`Search in ${ placeholder={`Search in ${