1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00
joplin/ReactNativeClient/root.js

431 lines
12 KiB
JavaScript
Raw Normal View History

const React = require('react'); const Component = React.Component;
const { Keyboard, NativeModules } = require('react-native');
const { connect, Provider } = require('react-redux');
const { BackButtonService } = require('lib/services/back-button.js');
const { createStore, applyMiddleware } = require('redux');
const { shimInit } = require('lib/shim-init-react.js');
const { Log } = require('lib/log.js');
const { AppNav } = require('lib/components/app-nav.js');
const { Logger } = require('lib/logger.js');
const { Note } = require('lib/models/note.js');
const { Folder } = require('lib/models/folder.js');
const { FoldersScreenUtils } = require('lib/folders-screen-utils.js');
const { Resource } = require('lib/models/resource.js');
const { Tag } = require('lib/models/tag.js');
const { NoteTag } = require('lib/models/note-tag.js');
const { BaseItem } = require('lib/models/base-item.js');
const { BaseModel } = require('lib/base-model.js');
const { JoplinDatabase } = require('lib/joplin-database.js');
const { Database } = require('lib/database.js');
const { NotesScreen } = require('lib/components/screens/notes.js');
const { NoteScreen } = require('lib/components/screens/note.js');
const { ConfigScreen } = require('lib/components/screens/config.js');
const { FolderScreen } = require('lib/components/screens/folder.js');
const { LogScreen } = require('lib/components/screens/log.js');
const { StatusScreen } = require('lib/components/screens/status.js');
const { WelcomeScreen } = require('lib/components/screens/welcome.js');
const { SearchScreen } = require('lib/components/screens/search.js');
const { OneDriveLoginScreen } = require('lib/components/screens/onedrive-login.js');
const { Setting } = require('lib/models/setting.js');
const { MenuContext } = require('react-native-popup-menu');
const { SideMenu } = require('lib/components/side-menu.js');
const { SideMenuContent } = require('lib/components/side-menu-content.js');
const { DatabaseDriverReactNative } = require('lib/database-driver-react-native');
const { reg } = require('lib/registry.js');
const { _, setLocale, closestSupportedLocale, defaultLocale } = require('lib/locale.js');
const RNFetchBlob = require('react-native-fetch-blob').default;
const { PoorManIntervals } = require('lib/poor-man-intervals.js');
const { reducer, defaultState } = require('lib/reducer.js');
2017-05-09 22:46:54 +02:00
const generalMiddleware = store => next => async (action) => {
if (action.type !== 'SIDE_MENU_OPEN_PERCENT') reg.logger().info('Reducer action', action.type);
PoorManIntervals.update(); // This function needs to be called regularly so put it here
const result = next(action);
const newState = store.getState();
if (action.type == 'NAV_GO') Keyboard.dismiss();
if (['NOTES_UPDATE_ONE', 'NOTES_DELETE', 'FOLDERS_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
if (!await reg.syncStarted()) reg.scheduleSync();
}
if (action.type == 'SETTINGS_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTINGS_UPDATE_ALL') {
reg.setupRecurrentSync();
}
2017-11-03 20:51:13 +02:00
if (action.type == 'SETTINGS_UPDATE_ONE' && action.key == 'locale' || action.type == 'SETTINGS_UPDATE_ALL') {
setLocale(Setting.value('locale'));
}
if (action.type == 'NAV_GO' && action.routeName == 'Notes') {
Setting.setValue('activeFolderId', newState.selectedFolderId);
}
return result;
}
let navHistory = [];
function historyCanGoBackTo(route) {
if (route.routeName == 'Note') return false;
if (route.routeName == 'Folder') return false;
return true;
}
const appDefaultState = Object.assign({}, defaultState, {
sideMenuOpenPercent: 0,
route: {
type: 'NAV_GO',
routeName: 'Welcome',
params: {},
},
});
const appReducer = (state = appDefaultState, action) => {
let newState = state;
let historyGoingBack = false;
try {
switch (action.type) {
case 'NAV_BACK':
if (!navHistory.length) break;
let newAction = null;
while (navHistory.length) {
newAction = navHistory.pop();
if (newAction.routeName != state.route.routeName) break;
}
action = newAction ? newAction : navHistory.pop();
historyGoingBack = true;
// Fall throught
case 'NAV_GO':
const currentRoute = state.route;
const currentRouteName = currentRoute ? currentRoute.routeName : '';
if (!historyGoingBack && historyCanGoBackTo(currentRoute)) {
// If the route *name* is the same (even if the other parameters are different), we
// overwrite the last route in the history with the current one. If the route name
// is different, we push a new history entry.
if (currentRoute.routeName == action.routeName) {
// nothing
} else {
navHistory.push(currentRoute);
}
}
// HACK: whenever a new screen is loaded, all the previous screens of that type
// are overwritten with the new screen parameters. This is because the way notes
// are currently loaded is not optimal (doesn't retain history properly) so
// this is a simple fix without doing a big refactoring to change the way notes
// are loaded. Might be good enough since going back to different folders
// is probably not a common workflow.
for (let i = 0; i < navHistory.length; i++) {
let n = navHistory[i];
if (n.routeName == action.routeName) {
navHistory[i] = Object.assign({}, action);
}
}
if (action.routeName == 'Welcome') navHistory = [];
//reg.logger().info('Route: ' + currentRouteName + ' => ' + action.routeName);
newState = Object.assign({}, state);
if ('noteId' in action) {
newState.selectedNoteId = action.noteId;
}
if ('folderId' in action) {
newState.selectedFolderId = action.folderId;
newState.notesParentType = 'Folder';
}
if ('tagId' in action) {
newState.selectedTagId = action.tagId;
newState.notesParentType = 'Tag';
}
if ('itemType' in action) {
newState.selectedItemType = action.itemType;
}
newState.route = action;
newState.historyCanGoBack = !!navHistory.length;
break;
case 'SIDE_MENU_TOGGLE':
newState = Object.assign({}, state);
newState.showSideMenu = !newState.showSideMenu
break;
case 'SIDE_MENU_OPEN':
newState = Object.assign({}, state);
newState.showSideMenu = true
break;
case 'SIDE_MENU_CLOSE':
newState = Object.assign({}, state);
newState.showSideMenu = false
break;
case 'SIDE_MENU_OPEN_PERCENT':
newState = Object.assign({}, state);
newState.sideMenuOpenPercent = action.value;
break;
}
} catch (error) {
error.message = 'In reducer: ' + error.message + ' Action: ' + JSON.stringify(action);
throw error;
}
return reducer(newState, action);
}
let store = createStore(appReducer, applyMiddleware(generalMiddleware));
2017-05-09 22:46:54 +02:00
2017-07-10 23:00:41 +02:00
async function initialize(dispatch, backButtonHandler) {
2017-07-10 20:09:58 +02:00
shimInit();
2017-07-07 19:19:24 +02:00
2017-07-10 00:57:15 +02:00
Setting.setConstant('env', __DEV__ ? 'dev' : 'prod');
Setting.setConstant('appId', 'net.cozic.joplin');
Setting.setConstant('appType', 'mobile');
Setting.setConstant('resourceDir', RNFetchBlob.fs.dirs.DocumentDir);
const logDatabase = new Database(new DatabaseDriverReactNative());
await logDatabase.open({ name: 'log.sqlite' });
await logDatabase.exec(Logger.databaseCreateTableSql());
2017-07-15 01:12:32 +02:00
const mainLogger = new Logger();
mainLogger.addTarget('database', { database: logDatabase, source: 'm' });
2017-07-15 19:08:54 +02:00
if (Setting.value('env') == 'dev') mainLogger.addTarget('console');
2017-07-15 01:12:32 +02:00
mainLogger.setLevel(Logger.LEVEL_DEBUG);
reg.setLogger(mainLogger);
2017-07-10 00:57:15 +02:00
reg.logger().info('====================================');
reg.logger().info('Starting application ' + Setting.value('appId') + ' (' + Setting.value('env') + ')');
2017-07-15 01:12:32 +02:00
const dbLogger = new Logger();
2017-07-17 22:22:05 +02:00
dbLogger.addTarget('database', { database: logDatabase, source: 'm' });
2017-07-15 19:08:54 +02:00
if (Setting.value('env') == 'dev') {
2017-07-17 22:22:05 +02:00
dbLogger.addTarget('console');
dbLogger.setLevel(Logger.LEVEL_INFO); // Set to LEVEL_DEBUG for full SQL queries
2017-07-15 19:08:54 +02:00
} else {
dbLogger.setLevel(Logger.LEVEL_INFO);
}
2017-07-15 01:12:32 +02:00
2017-07-10 00:57:15 +02:00
let db = new JoplinDatabase(new DatabaseDriverReactNative());
2017-07-15 01:12:32 +02:00
db.setLogger(dbLogger);
2017-07-10 00:57:15 +02:00
reg.setDb(db);
reg.dispatch = dispatch;
2017-07-10 00:57:15 +02:00
BaseModel.dispatch = dispatch;
2017-07-15 17:54:19 +02:00
FoldersScreenUtils.dispatch = dispatch;
2017-07-10 00:57:15 +02:00
BaseModel.db_ = db;
BaseItem.loadClass('Note', Note);
BaseItem.loadClass('Folder', Folder);
BaseItem.loadClass('Resource', Resource);
BaseItem.loadClass('Tag', Tag);
BaseItem.loadClass('NoteTag', NoteTag);
try {
if (Setting.value('env') == 'prod') {
await db.open({ name: 'joplin.sqlite' })
} else {
await db.open({ name: 'joplin-66.sqlite' })
2017-07-26 22:00:16 +02:00
//await db.open({ name: 'joplin-67.sqlite' })
2017-07-10 00:57:15 +02:00
2017-07-10 22:48:17 +02:00
// await db.exec('DELETE FROM notes');
// await db.exec('DELETE FROM folders');
// await db.exec('DELETE FROM tags');
// await db.exec('DELETE FROM note_tags');
// await db.exec('DELETE FROM resources');
// await db.exec('DELETE FROM deleted_items');
2017-07-15 17:54:19 +02:00
// await db.exec('UPDATE notes SET is_conflict = 1 where id like "546f%"');
2017-07-10 00:57:15 +02:00
}
2017-05-16 23:46:21 +02:00
2017-07-10 00:57:15 +02:00
reg.logger().info('Database is ready.');
reg.logger().info('Loading settings...');
await Setting.load();
2017-07-25 20:57:06 +02:00
if (Setting.value('firstStart')) {
const locale = NativeModules.I18nManager.localeIdentifier
if (!locale) locale = defaultLocale();
Setting.setValue('locale', closestSupportedLocale(locale));
Setting.setValue('firstStart', 0)
}
2017-07-24 23:29:40 +02:00
setLocale(Setting.value('locale'));
2017-07-19 23:26:30 +02:00
2017-07-10 00:57:15 +02:00
reg.logger().info('Loading folders...');
2017-07-06 23:30:45 +02:00
2017-07-15 17:54:19 +02:00
await FoldersScreenUtils.refreshFolders();
2017-07-09 00:57:09 +02:00
2017-07-25 20:36:52 +02:00
const tags = await Tag.all();
dispatch({
type: 'TAGS_UPDATE_ALL',
tags: tags,
});
2017-07-15 01:12:32 +02:00
let folderId = Setting.value('activeFolderId');
let folder = await Folder.load(folderId);
2017-07-25 20:09:01 +02:00
if (!folder) folder = await Folder.defaultFolder();
2017-07-23 20:26:50 +02:00
2017-07-25 20:09:01 +02:00
if (!folder) {
dispatch({
type: 'NAV_GO',
2017-07-25 20:09:01 +02:00
routeName: 'Welcome',
});
2017-07-24 23:52:30 +02:00
} else {
2017-07-25 20:09:01 +02:00
dispatch({
type: 'NAV_GO',
2017-07-25 20:09:01 +02:00
routeName: 'Notes',
folderId: folder.id,
});
2017-07-24 23:52:30 +02:00
}
2017-07-10 00:57:15 +02:00
} catch (error) {
reg.logger().error('Initialization error:', error);
}
BackButtonService.initialize(backButtonHandler);
2017-07-10 23:00:41 +02:00
reg.setupRecurrentSync();
2017-07-22 19:35:39 +02:00
2017-07-23 00:52:24 +02:00
if (Setting.value('env') == 'dev') {
2017-07-30 22:22:57 +02:00
// reg.scheduleSync();
2017-07-23 00:52:24 +02:00
} else {
reg.scheduleSync();
}
2017-07-10 00:57:15 +02:00
reg.logger().info('Application initialized');
}
class AppComponent extends React.Component {
constructor() {
super();
this.lastSyncStarted_ = defaultState.syncStarted;
}
2017-07-10 00:57:15 +02:00
async componentDidMount() {
2017-08-01 19:41:58 +02:00
if (this.props.appState == 'starting') {
this.props.dispatch({
type: 'SET_APP_STATE',
state: 'initializing',
});
await initialize(this.props.dispatch, this.backButtonHandler.bind(this));
this.props.dispatch({
type: 'SET_APP_STATE',
state: 'ready',
});
}
2017-07-10 23:00:41 +02:00
}
async backButtonHandler() {
2017-07-10 23:00:41 +02:00
if (this.props.showSideMenu) {
this.props.dispatch({ type: 'SIDE_MENU_CLOSE' });
return true;
}
if (this.props.historyCanGoBack) {
this.props.dispatch({ type: 'NAV_BACK' });
2017-07-10 23:00:41 +02:00
return true;
}
return false;
2017-05-11 22:14:01 +02:00
}
componentWillReceiveProps(newProps) {
if (newProps.syncStarted != this.lastSyncStarted_) {
if (!newProps.syncStarted) FoldersScreenUtils.refreshFolders();
this.lastSyncStarted_ = newProps.syncStarted;
}
}
sideMenu_change(isOpen) {
2017-05-24 21:27:13 +02:00
// Make sure showSideMenu property of state is updated
// when the menu is open/closed.
this.props.dispatch({
type: isOpen ? 'SIDE_MENU_OPEN' : 'SIDE_MENU_CLOSE',
});
}
2017-05-11 22:14:01 +02:00
render() {
2017-08-01 19:41:58 +02:00
if (this.props.appState != 'ready') return null;
2017-05-24 21:27:13 +02:00
const sideMenuContent = <SideMenuContent/>;
2017-05-24 21:09:46 +02:00
2017-07-14 20:49:14 +02:00
const appNavInit = {
Welcome: { screen: WelcomeScreen },
Notes: { screen: NotesScreen },
Note: { screen: NoteScreen },
Folder: { screen: FolderScreen },
OneDriveLogin: { screen: OneDriveLoginScreen },
Log: { screen: LogScreen },
Status: { screen: StatusScreen },
2017-07-23 00:52:24 +02:00
Search: { screen: SearchScreen },
2017-07-23 20:26:50 +02:00
Config: { screen: ConfigScreen },
2017-07-14 20:49:14 +02:00
};
2017-05-11 22:14:01 +02:00
return (
<SideMenu
menu={sideMenuContent}
onChange={(isOpen) => this.sideMenu_change(isOpen)}
onSliding={(percent) => {
this.props.dispatch({
type: 'SIDE_MENU_OPEN_PERCENT',
value: percent,
});
}}
>
2017-05-24 21:09:46 +02:00
<MenuContext style={{ flex: 1 }}>
2017-07-14 20:49:14 +02:00
<AppNav screens={appNavInit} />
2017-05-24 21:09:46 +02:00
</MenuContext>
</SideMenu>
2017-05-11 22:14:01 +02:00
);
}
2017-05-10 20:58:02 +02:00
}
const mapStateToProps = (state) => {
return {
2017-11-04 18:40:34 +02:00
historyCanGoBack: state.historyCanGoBack,
showSideMenu: state.showSideMenu,
syncStarted: state.syncStarted,
appState: state.appState,
};
2017-05-10 20:58:02 +02:00
};
const App = connect(mapStateToProps)(AppComponent);
2017-05-09 22:46:54 +02:00
class Root extends React.Component {
render() {
return (
<Provider store={store}>
2017-07-14 20:49:14 +02:00
<App/>
2017-05-09 22:46:54 +02:00
</Provider>
);
}
2017-05-09 21:59:14 +02:00
}
2017-11-03 02:13:17 +02:00
module.exports = { Root };