show dialog only when necessary

This commit is contained in:
ammarahm-ed
2020-11-14 10:02:38 +05:00
parent d2d7f4ed3f
commit 9b33e9ba95
2 changed files with 73 additions and 134 deletions

View File

@@ -1,21 +1,21 @@
import React, { Component } from 'react';
import { View } from 'react-native';
import { Actions } from '../../provider/Actions';
import { DDS } from '../../services/DeviceDetection';
import { eSendEvent, ToastEvent } from '../../services/EventManager';
import React, {Component} from 'react';
import {View} from 'react-native';
import {Actions} from '../../provider/Actions';
import {DDS} from '../../services/DeviceDetection';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
import NavigationService from '../../services/Navigation';
import { getElevation, history } from '../../utils';
import { db } from '../../utils/DB';
import {getElevation, history} from '../../utils';
import {db} from '../../utils/DB';
import {
eApplyChanges,
eClearEditor,
eCloseFullscreenEditor,
eOnLoadNote,
eOnNewTopicAdded
eOnNewTopicAdded,
} from '../../utils/Events';
import { ph, pv } from '../../utils/SizeUtils';
import { dialogActions } from '../DialogManager/DialogActions';
import { updateEvent } from '../DialogManager/recievers';
import {ph, pv} from '../../utils/SizeUtils';
import {dialogActions} from '../DialogManager/DialogActions';
import {updateEvent} from '../DialogManager/recievers';
import Seperator from '../Seperator';
import BaseDialog from './base-dialog';
import DialogButtons from './dialog-buttons';
@@ -32,87 +32,7 @@ export class Dialog extends Component {
_onPress = async () => {
let {template, item} = this.props;
switch (template.action) {
case dialogActions.ACTION_DELETE: {
if (item.dateCreated && history.selectedItemsList.length === 0) {
history.selectedItemsList = [];
history.selectedItemsList.push(item);
}
for (var i = 0; i < history.selectedItemsList.length; i++) {
let it = history.selectedItemsList[i];
if (it.type === 'note') {
await db.notes.delete(it.id);
updateEvent({type: it.type});
eSendEvent(eClearEditor);
} else if (it.type === 'topic') {
await db.notebooks.notebook(it.notebookId).topics.delete(it.title);
eSendEvent(eOnNewTopicAdded);
updateEvent({type: 'notebook'});
ToastEvent.show('Topics deleted', 'success');
} else if (it.type === 'notebook') {
await db.notebooks.delete(it.id);
updateEvent({type: it.type});
}
}
updateEvent({type: Actions.PINNED});
let message;
let notes = history.selectedItemsList.filter((o) => o.type === 'note');
let notebooks = history.selectedItemsList.filter(
(o) => o.type === 'notebook',
);
let topics = history.selectedItemsList.filter(
(o) => o.type === 'topic',
);
if (notes.length > 0 && notebooks.length === 0 && topics.length === 0) {
let msgPart = notes.length > 1 ? ' notes' : ' note';
message = notes.length + msgPart + ' moved to trash';
} else if (
notes.length === 0 &&
notebooks.length > 0 &&
topics.length === 0
) {
let msgPart = notebooks.length > 1 ? ' notebooks' : ' notebook';
message = notebooks.length + msgPart + ' moved to trash';
} else if (
notes.length === 0 &&
notebooks.length === 0 &&
topics.length > 0
) {
let msgPart = topics.length > 1 ? ' topics' : ' topic';
message = topics.length + msgPart + ' moved to trash';
}
let itemsCopy = [...history.selectedItemsList];
if (history.selectedItemsList[0].type !== 'topic')
ToastEvent.show(
message,
'success',
'global',
6000,
async () => {
let trash = db.trash;
for (var i = 0; i < itemsCopy.length; i++) {
let it = itemsCopy[i];
let trashItem = trash.all.find((item) => item.itemId === it.id);
await db.trash.restore(trashItem.id);
updateEvent({type: it.type});
}
updateEvent({type: Actions.TRASH});
updateEvent({type: Actions.PINNED});
ToastEvent.hide();
},
'Undo',
);
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
this.hide();
break;
}
case dialogActions.ACTION_PERMANANT_DELETE: {
if (item.dateCreated && history.selectedItemsList.length === 0) {
history.selectedItemsList = [];
@@ -120,63 +40,21 @@ export class Dialog extends Component {
}
let ids = [];
history.selectedItemsList.forEach((item) => ids.push(item.id));
await db.trash.delete(...ids);
updateEvent({type: Actions.TRASH});
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
ToastEvent.show('Item permanantly deleted');
this.hide();
break;
}
case dialogActions.ACTION_EXIT: {
this.setState({
visible: false,
});
NavigationService.goBack();
this.hide();
break;
}
case dialogActions.ACTION_NEW_NOTE: {
eSendEvent(eOnLoadNote, {type: 'new'});
ToastEvent.show('Items permanantly deleted');
this.hide();
break;
}
case dialogActions.ACTION_EMPTY_TRASH: {
await db.trash.clear();
updateEvent({type: Actions.TRASH});
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
ToastEvent.show('Trash cleared', 'error');
this.hide();
break;
}
case dialogActions.ACTION_EXIT_FULLSCREEN: {
updateEvent({type: Actions.NOTES});
eSendEvent(eCloseFullscreenEditor);
this.hide();
break;
}
case dialogActions.ACTION_TRASH: {
await db.trash.restore(i.id);
this.hide();
ToastEvent.show(
item.type.slice(0, 1).toUpperCase() +
item.type.slice(1) +
' restored',
'success',
);
updateEvent({type: Actions.TRASH});
this.hide();
break;
}
case dialogActions.ACTION_APPLY_CHANGES: {
eSendEvent(eApplyChanges);
this.hide();
break;
}
}

View File

@@ -0,0 +1,61 @@
import { history } from ".";
import { updateEvent } from "../components/DialogManager/Recievers";
import { Actions } from "../provider/Actions";
import { eSendEvent, ToastEvent } from "../services/EventManager";
import { db } from "./DB";
import { eClearEditor, eOnNewTopicAdded } from "./Events";
export async function deleteItems(item) {
if (item && item.dateCreated && history.selectedItemsList.length === 0) {
history.selectedItemsList = [];
history.selectedItemsList.push(item);
}
for (var i = 0; i < history.selectedItemsList.length; i++) {
let it = history.selectedItemsList[i];
if (it.type === 'note') {
await db.notes.delete(it.id);
updateEvent({type: it.type});
eSendEvent(eClearEditor);
} else if (it.type === 'topic') {
await db.notebooks.notebook(it.notebookId).topics.delete(it.title);
eSendEvent(eOnNewTopicAdded);
updateEveny({type: 'notebook'});
ToastEvent.show('Topics deleted', 'success');
} else if (it.type === 'notebook') {
await db.notebooks.delete(it.id);
updateEvent({type: it.type});
}
}
let msgPart = history.selectedItemsList.length === 1? ' item' : " items"
let message = history.selectedItemsList.length + msgPart + " moved to trash."
let itemsCopy = [...history.selectedItemsList];
if (history.selectedItemsList[0].type !== 'topic'){
ToastEvent.show(
message,
'success',
'global',
6000,
async () => {
let trash = db.trash;
for (var i = 0; i < itemsCopy.length; i++) {
let it = itemsCopy[i];
let trashItem = trash.all.find((item) => item.itemId === it.id);
await db.trash.restore(trashItem.id);
console.log(it.type,'type')
updateEvent({type: it.type});
}
updateEvent({type: Actions.TRASH});
ToastEvent.hide();
},
'Undo',
);
}
updateEvent({type: Actions.CLEAR_SELECTION});
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
}