Files
notesnook/apps/mobile/src/utils/functions.js

127 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-06-05 21:10:20 +05:00
import { Linking } from 'react-native';
import { InAppBrowser } from 'react-native-inappbrowser-reborn';
import { history } from '.';
import { useMenuStore, useSelectionStore } from '../provider/stores';
import { eSendEvent, ToastEvent } from '../services/EventManager';
2021-02-16 16:11:10 +05:00
import Navigation from '../services/Navigation';
2021-06-05 21:10:20 +05:00
import { db } from './DB';
import { eClearEditor } from './Events';
2020-11-14 10:02:38 +05:00
2020-12-31 14:18:38 +05:00
export const deleteItems = async (item) => {
2020-12-31 19:54:08 +05:00
if (item && item.id && history.selectedItemsList.length === 0) {
2020-11-14 12:25:57 +05:00
history.selectedItemsList = [];
history.selectedItemsList.push(item);
}
2020-11-14 10:02:38 +05:00
2021-06-07 11:53:27 +05:00
2020-12-16 12:29:36 +05:00
let notes = history.selectedItemsList.filter((i) => i.type === 'note');
let notebooks = history.selectedItemsList.filter(
(i) => i.type === 'notebook',
);
let topics = history.selectedItemsList.filter((i) => i.type === 'topic');
if (notes?.length > 0) {
let ids = notes.map((i) => i.id);
2021-06-07 11:53:27 +05:00
console.log(ids);
2020-12-31 14:18:38 +05:00
await db.notes.delete(...ids);
2021-02-16 16:11:10 +05:00
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notes,
Navigation.routeNames.NotesPage,
]);
2020-12-16 12:29:36 +05:00
eSendEvent(eClearEditor);
}
if (topics?.length > 0) {
for (var i = 0; i < topics.length; i++) {
let it = topics[i];
await db.notebooks.notebook(it.notebookId).topics.delete(it.id);
2020-11-14 12:25:57 +05:00
}
2021-02-16 16:11:10 +05:00
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
Navigation.routeNames.Notebook,
]);
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
2021-02-20 15:03:02 +05:00
ToastEvent.show({
heading: 'Topics deleted',
type: 'success',
});
2020-12-16 12:29:36 +05:00
}
if (notebooks?.length > 0) {
let ids = notebooks.map((i) => i.id);
2020-12-31 14:18:38 +05:00
await db.notebooks.delete(...ids);
2021-02-16 16:11:10 +05:00
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
Navigation.routeNames.Notes,
]);
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
2020-11-14 12:25:57 +05:00
}
2020-11-14 10:02:38 +05:00
2020-11-14 12:25:57 +05:00
let msgPart = history.selectedItemsList.length === 1 ? ' item' : ' items';
let message = history.selectedItemsList.length + msgPart + ' moved to trash.';
2020-11-14 10:02:38 +05:00
2020-11-14 12:25:57 +05:00
let itemsCopy = [...history.selectedItemsList];
2021-01-01 15:29:10 +05:00
if (topics.length === 0 && (notes.length > 0 || notebooks.length > 0)) {
2021-02-20 15:03:02 +05:00
ToastEvent.show({
heading: message,
type: 'success',
func: async () => {
2020-11-14 12:25:57 +05:00
let trash = db.trash;
2020-12-16 12:29:36 +05:00
let ids = [];
2020-11-14 12:25:57 +05:00
for (var i = 0; i < itemsCopy.length; i++) {
let it = itemsCopy[i];
let trashItem = trash.all.find((item) => item.itemId === it.id);
2020-12-16 12:29:36 +05:00
ids.push(trashItem.id);
2020-11-14 12:25:57 +05:00
}
2020-12-31 14:18:58 +05:00
await db.trash.restore(...ids);
2021-02-16 16:11:10 +05:00
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
Navigation.routeNames.Notes,
Navigation.routeNames.Trash,
Navigation.routeNames.NotesPage,
Navigation.routeNames.Notebook,
Navigation.routeNames.Trash,
]);
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
useMenuStore.getState().setColorNotes();
2020-11-14 12:25:57 +05:00
ToastEvent.hide();
},
2021-02-20 15:03:02 +05:00
actionText: 'Undo',
});
2020-11-14 12:25:57 +05:00
}
2021-06-07 11:53:27 +05:00
history.selectedItemsList = [];
2021-02-16 16:11:10 +05:00
Navigation.setRoutesToUpdate([Navigation.routeNames.Trash]);
2021-06-05 21:10:20 +05:00
useSelectionStore.getState().clearSelection();
useMenuStore.getState().setColorNotes();
2021-01-01 15:29:10 +05:00
};
2021-02-10 10:09:48 +05:00
2021-02-20 15:03:02 +05:00
2021-02-10 10:09:48 +05:00
export const openLinkInBrowser = async (link, colors) => {
try {
const url = link;
if (await InAppBrowser.isAvailable()) {
await InAppBrowser.open(url, {
// iOS Properties
dismissButtonStyle: 'cancel',
preferredBarTintColor: colors.accent,
preferredControlTintColor: 'white',
readerMode: false,
animated: true,
modalPresentationStyle: 'fullScreen',
modalTransitionStyle: 'coverVertical',
modalEnabled: true,
enableBarCollapsing: false,
// Android Properties
showTitle: true,
toolbarColor: colors.accent,
secondaryToolbarColor: 'black',
enableUrlBarHiding: true,
enableDefaultShare: true,
forceCloseOnRedirection: false,
});
} else Linking.openURL(url);
} catch (error) {
console.log(error.message);
}
2021-02-16 16:11:10 +05:00
};