refactor dialogs

This commit is contained in:
ammarahm-ed
2020-09-27 13:05:26 +05:00
parent c67ad09c9a
commit ddb9ac0d0e
8 changed files with 431 additions and 451 deletions

View File

@@ -8,6 +8,9 @@ import {eSendEvent} from '../../services/eventManager';
import {eOnNewTopicAdded} from '../../services/events'; import {eOnNewTopicAdded} from '../../services/events';
import {Toast} from '../Toast'; import {Toast} from '../Toast';
import {Button} from '../Button'; import {Button} from '../Button';
import BaseDialog from '../Dialog/base-dialog';
import DialogHeader from '../Dialog/dialog-header';
import DialogButtons from '../Dialog/dialog-buttons';
export class AddTopicDialog extends React.Component { export class AddTopicDialog extends React.Component {
constructor(props) { constructor(props) {
@@ -44,6 +47,7 @@ export class AddTopicDialog extends React.Component {
}); });
} }
close() { close() {
refs = [];
this.title = null; this.title = null;
this.setState({ this.setState({
visible: false, visible: false,
@@ -55,35 +59,12 @@ export class AddTopicDialog extends React.Component {
const {colors, toEdit} = this.props; const {colors, toEdit} = this.props;
return ( return (
<Modal <BaseDialog
visible={visible}
animated
animationType="fade"
transparent={true}
onShow={() => { onShow={() => {
this.titleRef.current?.focus(); this.titleRef.current?.focus();
}} }}
onRequestClose={() => { visible={visible}
refs = []; onRequestClose={this.close}>
this.close();
}}>
<View
style={{
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
}}>
<TouchableOpacity
onPress={() => this.close()}
style={{
width: '100%',
height: '100%',
position: 'absolute',
}}
/>
<View <View
style={{ style={{
...getElevation(5), ...getElevation(5),
@@ -94,23 +75,10 @@ export class AddTopicDialog extends React.Component {
paddingHorizontal: ph, paddingHorizontal: ph,
paddingVertical: pv, paddingVertical: pv,
}}> }}>
<View <DialogHeader
style={{ icon="book-outline"
flexDirection: 'row', title={toEdit ? 'Edit Topic' : 'Add New Topic'}
justifyContent: 'center', />
alignItems: 'center',
}}>
<Icon name="book-outline" color={colors.accent} size={SIZE.lg} />
<Text
style={{
color: colors.accent,
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
}}>
{toEdit ? 'Edit Topic' : 'Add New Topic'}
</Text>
</View>
<TextInput <TextInput
ref={this.titleRef} ref={this.titleRef}
@@ -143,28 +111,14 @@ export class AddTopicDialog extends React.Component {
placeholderTextColor={colors.icon} placeholderTextColor={colors.icon}
/> />
<View <DialogButtons
style={{ positiveTitle={toEdit ? 'Save' : 'Add'}
justifyContent: 'space-around', onPressNegative={this.close}
alignItems: 'center', onPressPositive={this.addNewTopic}
flexDirection: 'row',
marginTop: 20,
}}>
<Button
activeOpacity={opacity}
onPress={async () => await this.addNewTopic()}
title={toEdit ? 'Save' : 'Add'}
/> />
<Button
activeOpacity={opacity}
onPress={() => this.close()}
title="Cancel"
/>
</View>
</View>
</View> </View>
<Toast context="local" /> <Toast context="local" />
</Modal> </BaseDialog>
); );
} }
} }

View File

@@ -0,0 +1,49 @@
import React from 'react';
import {Modal, StyleSheet, TouchableOpacity, View} from 'react-native';
import {useTracked} from '../../provider';
const BaseDialog = ({visible, onRequestClose, children, onShow}) => {
const [state, dispatch] = useTracked();
return (
<Modal
visible={visible}
transparent={true}
animated
statusBarTranslucent
onShow={onShow}
animationType="fade"
onRequestClose={onRequestClose}>
<View
style={[
{
backgroundColor: state.colors.night
? 'rgba(255,255,255,0.15)'
: 'rgba(0,0,0,0.3)',
},
styles.backdrop,
]}>
<TouchableOpacity
onPress={onRequestClose}
style={styles.overlayButton}
/>
{children}
</View>
</Modal>
);
};
const styles = StyleSheet.create({
backdrop: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
},
overlayButton: {
width: '100%',
height: '100%',
position: 'absolute',
},
});
export default BaseDialog;

View File

@@ -0,0 +1,31 @@
import React from 'react';
import {StyleSheet, Text, View} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {useTracked} from '../../provider';
import {Button} from '../Button';
const DialogButtons = ({
onPressPositive,
onPressNegative,
positiveTitle,
negativeTitle = 'Cancel',
}) => {
return (
<View
style={styles.container}>
<Button onPress={onPressPositive} title={positiveTitle} />
<Button onPress={onPressNegative} title={negativeTitle} />
</View>
);
};
export default DialogButtons;
const styles = StyleSheet.create({
container:{
justifyContent: 'space-around',
alignItems: 'center',
flexDirection: 'row',
marginTop: 20,
}
})

View File

@@ -0,0 +1,31 @@
import React from 'react';
import {Text, View} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {useTracked} from '../../provider';
const DialogHeader = ({icon, title}) => {
const [state, dispatch] = useTracked();
const colors = state.colors;
return (
<View
style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
}}>
{icon ? <Icon name={icon} color={colors.accent} size={SIZE.lg} /> : null}
<Text
style={{
color: colors.accent,
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
}}>
{title}
</Text>
</View>
);
};
export default DialogHeader

View File

@@ -1,20 +1,22 @@
import React, {Component} from 'react'; import React, {Component} from 'react';
import {Modal, Text, TouchableOpacity, View} from 'react-native'; import {Modal, Text, TouchableOpacity, View} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common'; import {ph, pv, SIZE, WEIGHT} from '../../common/common';
import {ACTIONS} from '../../provider/actions'; import {ACTIONS} from '../../provider/actions';
import {eSendEvent} from '../../services/eventManager'; import {eSendEvent} from '../../services/eventManager';
import { import {
eApplyChanges,
eClearEditor, eClearEditor,
eCloseFullscreenEditor, eCloseFullscreenEditor,
eOnLoadNote,
eOnNewTopicAdded, eOnNewTopicAdded,
eApplyChanges, eOnLoadNote
} from '../../services/events'; } from '../../services/events';
import NavigationService from '../../services/NavigationService'; import NavigationService from '../../services/NavigationService';
import {db, DDS, getElevation, history, ToastEvent} from '../../utils/utils'; import {db, DDS, getElevation, history, ToastEvent} from '../../utils/utils';
import {Button} from '../Button';
import {dialogActions} from '../DialogManager/dialogActions'; import {dialogActions} from '../DialogManager/dialogActions';
import {updateEvent} from '../DialogManager/recievers'; import {updateEvent} from '../DialogManager/recievers';
import {Button} from '../Button'; import BaseDialog from './base-dialog';
export class Dialog extends Component { export class Dialog extends Component {
constructor(props) { constructor(props) {
@@ -134,7 +136,7 @@ export class Dialog extends Component {
break; break;
} }
case dialogActions.ACTION_NEW_NOTE: { case dialogActions.ACTION_NEW_NOTE: {
eSendEvent(eOnLoadNote, {type:"new"}); eSendEvent(eOnLoadNote, {type: 'new'});
this.hide(); this.hide();
break; break;
} }
@@ -205,28 +207,7 @@ export class Dialog extends Component {
const {title, paragraph, positiveText, negativeText, icon} = template; const {title, paragraph, positiveText, negativeText, icon} = template;
const {visible} = this.state; const {visible} = this.state;
return ( return (
<Modal <BaseDialog visible={visible} onRequestClose={this.hide}>
visible={visible}
transparent={true}
animated
animationType="fade"
onRequestClose={() => this.setState({visible: false})}>
<View
style={{
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
}}>
<TouchableOpacity
onPress={this.hide}
style={{
width: '100%',
height: '100%',
position: 'absolute',
}}
/>
<View <View
style={{ style={{
...getElevation(5), ...getElevation(5),
@@ -265,9 +246,8 @@ export class Dialog extends Component {
style={{ style={{
color: colors.icon, color: colors.icon,
fontFamily: WEIGHT.regular, fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2, fontSize: SIZE.xs + 1,
textAlign: 'center', textAlign: 'center',
marginTop: 10,
}}> }}>
{this.state.selectedItemsLength > 0 {this.state.selectedItemsLength > 0
? 'Delete ' + ? 'Delete ' +
@@ -290,8 +270,7 @@ export class Dialog extends Component {
</View> </View>
)} )}
</View> </View>
</View> </BaseDialog>
</Modal>
); );
} }
} }

View File

@@ -14,6 +14,8 @@ import {useTracked} from '../../provider';
import storage from '../../utils/storage'; import storage from '../../utils/storage';
import {DDS, getElevation, ToastEvent} from '../../utils/utils'; import {DDS, getElevation, ToastEvent} from '../../utils/utils';
import {Button} from '../Button/index'; import {Button} from '../Button/index';
import BaseDialog from '../Dialog/base-dialog';
import DialogHeader from '../Dialog/dialog-header';
import {Loading} from '../Loading'; import {Loading} from '../Loading';
import Seperator from '../Seperator'; import Seperator from '../Seperator';
@@ -112,14 +114,7 @@ const ExportDialog = () => {
]; ];
return ( return (
<Modal <BaseDialog onRequestClose={close} visible={visible}>
visible={visible}
transparent={true}
animated
animationType="fade"
onRequestClose={close}>
<View style={styles.wrapper}>
<TouchableOpacity onPress={close} style={styles.overlay} />
<View <View
style={[ style={[
{ {
@@ -128,16 +123,7 @@ const ExportDialog = () => {
}, },
styles.container, styles.container,
]}> ]}>
<View style={styles.headingContainer}> <DialogHeader icon="export" title="Export Note" />
<Icon name="export" color={colors.accent} size={SIZE.lg} />
<Text style={[{color: colors.accent}, styles.heading]}>
Export
{notes.length === 0 || notes.length === 1
? ''
: ' ' + notes.length}{' '}
Note
</Text>
</View>
<Seperator half /> <Seperator half />
{exporting ? ( {exporting ? (
@@ -187,19 +173,11 @@ const ExportDialog = () => {
</> </>
)} )}
</View> </View>
</View> </BaseDialog>
</Modal>
); );
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({
wrapper: {
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
},
container: { container: {
...getElevation(5), ...getElevation(5),
maxHeight: 350, maxHeight: 350,
@@ -207,20 +185,10 @@ const styles = StyleSheet.create({
paddingHorizontal: ph, paddingHorizontal: ph,
paddingVertical: pv, paddingVertical: pv,
}, },
headingContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
buttonContainer: { buttonContainer: {
justifyContent: 'space-between', justifyContent: 'space-between',
alignItems: 'center', alignItems: 'center',
}, },
heading: {
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
},
button: { button: {
paddingVertical: pv, paddingVertical: pv,
paddingHorizontal: ph, paddingHorizontal: ph,

View File

@@ -19,6 +19,9 @@ import {
import {openEditorAnimation} from '../../utils/animations'; import {openEditorAnimation} from '../../utils/animations';
import {db, DDS, getElevation, ToastEvent} from '../../utils/utils'; import {db, DDS, getElevation, ToastEvent} from '../../utils/utils';
import {Button} from '../Button/index'; import {Button} from '../Button/index';
import BaseDialog from '../Dialog/base-dialog';
import DialogButtons from '../Dialog/dialog-buttons';
import DialogHeader from '../Dialog/dialog-header';
import {updateEvent} from '../DialogManager/recievers'; import {updateEvent} from '../DialogManager/recievers';
import {Toast} from '../Toast'; import {Toast} from '../Toast';
const passInputRef = createRef(); const passInputRef = createRef();
@@ -251,23 +254,12 @@ export class VaultDialog extends Component {
} = this.state; } = this.state;
return ( return (
<Modal <BaseDialog
onShow={() => { onShow={() => {
passInputRef.current?.focus(); passInputRef.current?.focus();
}} }}
visible={visible} onRequestClose={this.close}
transparent={true} visible={visible}>
onRequestClose={this.close}>
<View
style={{
width: '100%',
height: '100%',
backgroundColor: colors.night
? 'rgba(255,255,255,0.3)'
: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
}}>
<View <View
style={{ style={{
...getElevation(5), ...getElevation(5),
@@ -278,21 +270,9 @@ export class VaultDialog extends Component {
paddingHorizontal: ph, paddingHorizontal: ph,
paddingVertical: pv, paddingVertical: pv,
}}> }}>
<View <DialogHeader
style={{ title={
flexDirection: 'row', !novault
justifyContent: 'center',
alignItems: 'center',
}}>
<Icon name="shield" color={colors.accent} size={SIZE.lg} />
<Text
style={{
color: colors.accent,
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
}}>
{!novault
? 'Create vault' ? 'Create vault'
: note.locked : note.locked
? this.state.deleteNote ? this.state.deleteNote
@@ -302,9 +282,10 @@ export class VaultDialog extends Component {
: this.state.goToEditor : this.state.goToEditor
? 'Unlock note' ? 'Unlock note'
: 'Unlock note' : 'Unlock note'
: 'Lock note'} : 'Lock note'
</Text> }
</View> icon="shield"
/>
<Text <Text
style={{ style={{
@@ -411,18 +392,10 @@ export class VaultDialog extends Component {
</View> </View>
) : null} ) : null}
<View <DialogButtons
style={{ onPressNegative={this.close}
justifyContent: 'space-around', onPressPositive={this.onPress}
alignItems: 'center', positiveTitle={
flexDirection: 'row',
marginTop: 20,
}}>
<Button onPress={this.close} title="Cancel" />
<Button
onPress={this.onPress}
title={
note.locked note.locked
? this.state.deleteNote ? this.state.deleteNote
? 'Delete' ? 'Delete'
@@ -433,13 +406,10 @@ export class VaultDialog extends Component {
: 'Unlock' : 'Unlock'
: 'Lock' : 'Lock'
} }
grayed
/> />
</View> </View>
</View>
</View>
<Toast context="local" /> <Toast context="local" />
</Modal> </BaseDialog>
); );
} }
} }

View File

@@ -7,7 +7,9 @@ const {color, Value, timing} = Animated;
export const EditorPosition = new Value(Dimensions.get('window').width * 1.5); export const EditorPosition = new Value(Dimensions.get('window').width * 1.5);
export const EditorScale = new Value(1); export const EditorScale = new Value(1);
export const EditorOpacity = new Value(0); export const EditorOpacity = new Value(0);
export const EditorTranslateY = new Value(Dimensions.get('window').height * 0.75); export const EditorTranslateY = new Value(
Dimensions.get('window').height * 0.75,
);
export function openEditorAnimation() { export function openEditorAnimation() {
EditorPosition.setValue(Dimensions.get('window').width * 1.5); EditorPosition.setValue(Dimensions.get('window').width * 1.5);
@@ -28,7 +30,6 @@ export function openEditorAnimation() {
toValue: 1, toValue: 1,
easing: Easing.out(Easing.ease), easing: Easing.out(Easing.ease),
}).start(); }).start();
} }
export function exitEditorAnimation() { export function exitEditorAnimation() {
@@ -49,9 +50,6 @@ export function exitEditorAnimation() {
toValue: Dimensions.get('window').height * 0.75, toValue: Dimensions.get('window').height * 0.75,
easing: Easing.inOut(Easing.ease), easing: Easing.inOut(Easing.ease),
}).start(); }).start();
} }
export const slideRight = { export const slideRight = {