mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
add toast messag queuing
This commit is contained in:
@@ -272,7 +272,7 @@ export const ActionSheetComponent = ({
|
|||||||
name: 'Restore',
|
name: 'Restore',
|
||||||
icon: 'delete-restore',
|
icon: 'delete-restore',
|
||||||
func: async () => {
|
func: async () => {
|
||||||
await db.trash.restore(note.id);
|
console.log(await db.trash.restore(note.id),"RESULT");
|
||||||
dispatch({type: ACTIONS.TRASH});
|
dispatch({type: ACTIONS.TRASH});
|
||||||
localRefresh(note.type);
|
localRefresh(note.type);
|
||||||
ToastEvent.show(
|
ToastEvent.show(
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import NavigationService from '../../services/NavigationService';
|
|||||||
import {db, DDS, getElevation, history, ToastEvent} from '../../utils/utils';
|
import {db, DDS, getElevation, history, ToastEvent} from '../../utils/utils';
|
||||||
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 {Button} from '../Button';
|
||||||
|
|
||||||
export class Dialog extends Component {
|
export class Dialog extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@@ -35,28 +35,73 @@ export class Dialog extends Component {
|
|||||||
history.selectedItemsList.push(item);
|
history.selectedItemsList.push(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
history.selectedItemsList.forEach(async i => {
|
for (var i = 0; i < history.selectedItemsList.length; i++) {
|
||||||
if (i.type === 'note') {
|
let it = history.selectedItemsList[i];
|
||||||
await db.notes.delete(i.id);
|
if (it.type === 'note') {
|
||||||
ToastEvent.show('Notes moved to trash', 'error');
|
await db.notes.delete(it.id);
|
||||||
updateEvent({type: i.type});
|
updateEvent({type: it.type});
|
||||||
updateEvent({type: ACTIONS.PINNED});
|
|
||||||
|
|
||||||
eSendEvent(eClearEditor);
|
eSendEvent(eClearEditor);
|
||||||
} else if (i.type === 'topic') {
|
} else if (it.type === 'topic') {
|
||||||
await db.notebooks.notebook(i.notebookId).topics.delete(i.title);
|
await db.notebooks.notebook(it.notebookId).topics.delete(it.title);
|
||||||
|
|
||||||
eSendEvent(eOnNewTopicAdded);
|
eSendEvent(eOnNewTopicAdded);
|
||||||
updateEvent({type: 'notebook'});
|
updateEvent({type: 'notebook'});
|
||||||
|
ToastEvent.show('Topics deleted', 'success');
|
||||||
ToastEvent.show('Topics deleted', 'error');
|
} else if (it.type === 'notebook') {
|
||||||
} else if (i.type === 'notebook') {
|
await db.notebooks.delete(it.id);
|
||||||
await db.notebooks.delete(i.id);
|
updateEvent({type: it.type});
|
||||||
updateEvent({type: i.type});
|
|
||||||
updateEvent({type: ACTIONS.PINNED});
|
|
||||||
ToastEvent.show('Notebooks moved to trash', 'error');
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
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 () => {
|
||||||
|
console.log('COPY');
|
||||||
|
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.CLEAR_SELECTION});
|
||||||
updateEvent({type: ACTIONS.SELECTION_MODE, enabled: false});
|
updateEvent({type: ACTIONS.SELECTION_MODE, enabled: false});
|
||||||
@@ -70,7 +115,7 @@ export class Dialog extends Component {
|
|||||||
history.selectedItemsList.push(item);
|
history.selectedItemsList.push(item);
|
||||||
}
|
}
|
||||||
let ids = [];
|
let ids = [];
|
||||||
history.selectedItemsList.forEach(item => ids.push(item.id));
|
history.selectedItemsList.forEach((item) => ids.push(item.id));
|
||||||
|
|
||||||
await db.trash.delete(...ids);
|
await db.trash.delete(...ids);
|
||||||
|
|
||||||
@@ -236,18 +281,8 @@ export class Dialog extends Component {
|
|||||||
flexDirection: 'row',
|
flexDirection: 'row',
|
||||||
marginTop: 20,
|
marginTop: 20,
|
||||||
}}>
|
}}>
|
||||||
<Button
|
<Button onPress={this._onClose} title={negativeText} grayed />
|
||||||
onPress={this._onClose}
|
<Button onPress={this._onPress} title={positiveText} />
|
||||||
title={negativeText}
|
|
||||||
grayed
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
onPress={this._onPress}
|
|
||||||
title={positiveText}
|
|
||||||
|
|
||||||
/>
|
|
||||||
|
|
||||||
|
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
@@ -9,19 +9,24 @@ import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
|||||||
import {SIZE} from '../../common/common';
|
import {SIZE} from '../../common/common';
|
||||||
const {spring, timing} = Animated;
|
const {spring, timing} = Animated;
|
||||||
|
|
||||||
|
const toastMessages = [];
|
||||||
export const Toast = ({context = 'global'}) => {
|
export const Toast = ({context = 'global'}) => {
|
||||||
const [state, dispatch] = useTracked();
|
const [state, dispatch] = useTracked();
|
||||||
const colors = state.colors;
|
const colors = state.colors;
|
||||||
const [keyboard, setKeyboard] = useState(false);
|
const [keyboard, setKeyboard] = useState(false);
|
||||||
const [data, setData] = useState([]);
|
const [data, setData] = useState({});
|
||||||
const [toastStyle, setToastStyle] = useState({
|
const [toastStyle, setToastStyle] = useState({
|
||||||
backgroundColor: colors.errorBg,
|
backgroundColor: colors.errorBg,
|
||||||
color: colors.errorText,
|
color: colors.errorText,
|
||||||
});
|
});
|
||||||
|
|
||||||
let toastTranslate = useValue(300);
|
let toastTranslate = useValue(300);
|
||||||
|
let toastOpacity = useValue(1);
|
||||||
|
|
||||||
const showToastFunc = (data) => {
|
const showToastFunc = (data) => {
|
||||||
|
toastMessages.push(data);
|
||||||
|
console.log(data, toastMessages.length);
|
||||||
|
if (toastMessages?.length > 1) return;
|
||||||
setData(data);
|
setData(data);
|
||||||
if (data.type === 'success') {
|
if (data.type === 'success') {
|
||||||
setToastStyle({
|
setToastStyle({
|
||||||
@@ -42,28 +47,60 @@ export const Toast = ({context = 'global'}) => {
|
|||||||
}, 100);
|
}, 100);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
hideToastFunc();
|
||||||
|
}, data.duration);
|
||||||
|
};
|
||||||
|
|
||||||
|
const showNext = (data) => {
|
||||||
|
setData(data);
|
||||||
|
if (data.type === 'success') {
|
||||||
|
setToastStyle({
|
||||||
|
color: colors.successText,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setToastStyle({
|
||||||
|
color: colors.errorText,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
setTimeout(() => {
|
||||||
|
hideToastFunc();
|
||||||
|
}, toastMessages[0].duration);
|
||||||
|
};
|
||||||
|
|
||||||
|
const hideToastFunc = (data) => {
|
||||||
|
console.log(toastMessages.length);
|
||||||
|
if (toastMessages.length > 1) {
|
||||||
|
toastMessages.shift();
|
||||||
|
|
||||||
|
timing(toastOpacity, {
|
||||||
|
toValue: 0,
|
||||||
|
duration: 300,
|
||||||
|
easing: Easing.in(Easing.ease),
|
||||||
|
}).start(() => {
|
||||||
|
showNext(toastMessages[0]);
|
||||||
|
timing(toastOpacity, {
|
||||||
|
toValue: 1,
|
||||||
|
duration: 300,
|
||||||
|
easing: Easing.in(Easing.ease),
|
||||||
|
}).start();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
timing(toastTranslate, {
|
timing(toastTranslate, {
|
||||||
toValue: 300,
|
toValue: 300,
|
||||||
duration: 200,
|
duration: 200,
|
||||||
easing: Easing.inOut(Easing.ease),
|
easing: Easing.inOut(Easing.ease),
|
||||||
}).start();
|
}).start(() => {
|
||||||
}, data.duration);
|
setData({});
|
||||||
};
|
});
|
||||||
|
}
|
||||||
const hideToastFunc = (data) => {
|
|
||||||
timing(toastTranslate, {
|
|
||||||
toValue: 300,
|
|
||||||
duration: 200,
|
|
||||||
easing: Easing.inOut(Easing.ease),
|
|
||||||
}).start();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const _onKeyboardShow = () => {
|
const _onKeyboardShow = () => {
|
||||||
setKeyboard(true);
|
//setKeyboard(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const _onKeyboardHide = () => {
|
const _onKeyboardHide = () => {
|
||||||
setKeyboard(false);
|
//setKeyboard(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -91,6 +128,7 @@ export const Toast = ({context = 'global'}) => {
|
|||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
zIndex: 999,
|
zIndex: 999,
|
||||||
elevation: 15,
|
elevation: 15,
|
||||||
|
opacity: toastOpacity,
|
||||||
transform: [
|
transform: [
|
||||||
{
|
{
|
||||||
translateY: toastTranslate,
|
translateY: toastTranslate,
|
||||||
@@ -138,6 +176,9 @@ export const Toast = ({context = 'global'}) => {
|
|||||||
</View>
|
</View>
|
||||||
|
|
||||||
<Text
|
<Text
|
||||||
|
onPress={() => {
|
||||||
|
hideToastFunc();
|
||||||
|
}}
|
||||||
style={{
|
style={{
|
||||||
color: 'white',
|
color: 'white',
|
||||||
backgroundColor: 'transparent',
|
backgroundColor: 'transparent',
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ export const h = Dimensions.get('window').height;
|
|||||||
export const ToastEvent = {
|
export const ToastEvent = {
|
||||||
show: (
|
show: (
|
||||||
message,
|
message,
|
||||||
type,
|
type = "error",
|
||||||
context = 'global',
|
context = 'global',
|
||||||
duration = 6000,
|
duration = 6000,
|
||||||
func = null,
|
func = null,
|
||||||
@@ -97,7 +97,7 @@ export const ToastEvent = {
|
|||||||
},
|
},
|
||||||
hide: (
|
hide: (
|
||||||
message,
|
message,
|
||||||
type,
|
type = "error",
|
||||||
context = 'global',
|
context = 'global',
|
||||||
duration = 6000,
|
duration = 6000,
|
||||||
func = null,
|
func = null,
|
||||||
|
|||||||
Reference in New Issue
Block a user