2020-02-19 01:52:36 +02:00
|
|
|
import * as QuickActions from 'react-native-quick-actions';
|
2020-11-07 17:59:37 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2024-06-19 13:33:22 +02:00
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
import CommandService from '@joplin/lib/services/CommandService';
|
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
import { DeviceEventEmitter } from 'react-native';
|
2024-12-16 11:49:46 +02:00
|
|
|
import { GotoNoteOptions } from './commands/util/goToNote';
|
|
|
|
import { AttachFileAction } from './components/screens/Note/commands/attachFile';
|
2024-06-19 13:33:22 +02:00
|
|
|
|
|
|
|
const logger = Logger.create('setupQuickActions');
|
2020-02-19 01:52:36 +02:00
|
|
|
|
|
|
|
type TData = {
|
2020-11-12 21:29:22 +02:00
|
|
|
type: string;
|
|
|
|
};
|
2020-02-19 01:52:36 +02:00
|
|
|
|
2024-06-19 13:33:22 +02:00
|
|
|
export default async (dispatch: Dispatch) => {
|
2020-02-19 01:52:36 +02:00
|
|
|
const userInfo = { url: '' };
|
2024-08-02 15:51:49 +02:00
|
|
|
|
|
|
|
if (!QuickActions.setShortcutItems) {
|
|
|
|
logger.info('QuickActions unsupported');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-12-16 11:49:46 +02:00
|
|
|
// List of iOS icons:
|
|
|
|
// https://github.com/EvanBacon/expo-quick-actions?tab=readme-ov-file#system-icons
|
|
|
|
//
|
|
|
|
// Note: on Android, anything beyond the fourth menu item appears to be ignored, at least on
|
|
|
|
// emulator.
|
2020-02-19 01:52:36 +02:00
|
|
|
QuickActions.setShortcutItems([
|
2024-12-16 11:49:46 +02:00
|
|
|
{ type: 'newNote', title: _('New note'), icon: 'Compose', userInfo },
|
|
|
|
{ type: 'newTodo', title: _('New to-do'), icon: 'Add', userInfo },
|
|
|
|
{ type: 'newPhoto', title: _('New photo'), icon: 'CapturePhoto', userInfo },
|
|
|
|
{ type: 'newResource', title: _('New attachment'), icon: 'Bookmark', userInfo },
|
|
|
|
{ type: 'newDrawing', title: _('New drawing'), icon: 'Favorite', userInfo },
|
2020-02-19 01:52:36 +02:00
|
|
|
]);
|
|
|
|
|
2024-06-19 13:33:22 +02:00
|
|
|
try {
|
|
|
|
const data = await QuickActions.popInitialAction();
|
|
|
|
const handler = quickActionHandler(dispatch);
|
|
|
|
await handler(data);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Quick action command failed', error);
|
|
|
|
}
|
|
|
|
return DeviceEventEmitter.addListener('quickActionShortcut', quickActionHandler(dispatch));
|
2020-02-19 01:52:36 +02:00
|
|
|
};
|
2020-05-13 12:39:16 +02:00
|
|
|
|
2024-06-19 13:33:22 +02:00
|
|
|
const quickActionHandler = (dispatch: Dispatch) => async (data: TData) => {
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
// This dispatch is to momentarily go back to reset state, similar to what
|
|
|
|
// happens in onJoplinLinkClick_(). Easier to just go back, then go to the
|
|
|
|
// note since the Note screen doesn't handle reloading a different note.
|
|
|
|
//
|
|
|
|
// This hack is necessary because otherwise you get this problem:
|
|
|
|
// The first time you create a note from the quick-action menu, it works
|
|
|
|
// perfectly. But if you do it again immediately later, it re-opens the
|
|
|
|
// page to that first note you made rather than creating an entirely new
|
|
|
|
// note. If you navigate around enough (which I think changes the redux
|
|
|
|
// state sufficiently or something), then it'll work again.
|
|
|
|
dispatch({ type: 'NAV_BACK' });
|
|
|
|
dispatch({ type: 'SIDE_MENU_CLOSE' });
|
|
|
|
|
2024-12-16 11:49:46 +02:00
|
|
|
const isTodo = data.type === 'newTodo' ? 1 : 0;
|
|
|
|
const options: GotoNoteOptions = {
|
|
|
|
attachFileAction: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data.type === 'newPhoto') {
|
|
|
|
options.attachFileAction = AttachFileAction.TakePhoto;
|
|
|
|
} else if (data.type === 'newResource') {
|
|
|
|
options.attachFileAction = AttachFileAction.AttachFile;
|
|
|
|
} else if (data.type === 'newDrawing') {
|
|
|
|
options.attachFileAction = AttachFileAction.AttachDrawing;
|
|
|
|
}
|
|
|
|
|
|
|
|
await CommandService.instance().execute('newNote', '', isTodo, options);
|
2024-06-19 13:33:22 +02:00
|
|
|
};
|