joplin/ReactNativeClient/root.js

426 lines
11 KiB
JavaScript
Raw Normal View History

2017-05-09 19:59:14 +00:00
import React, { Component } from 'react';
2017-07-10 21:44:55 +00:00
import { BackHandler, Keyboard } from 'react-native';
2017-07-10 21:00:41 +00:00
import { connect, Provider } from 'react-redux'
2017-05-09 19:59:14 +00:00
import { createStore } from 'redux';
2017-07-10 18:09:58 +00:00
import { shimInit } from 'lib/shim-init-react.js';
2017-06-24 18:06:28 +00:00
import { Log } from 'lib/log.js'
2017-07-14 18:49:14 +00:00
import { AppNav } from 'lib/components/app-nav.js'
2017-07-07 17:19:24 +00:00
import { Logger } from 'lib/logger.js'
2017-06-24 18:06:28 +00:00
import { Note } from 'lib/models/note.js'
import { Folder } from 'lib/models/folder.js'
2017-07-15 15:54:19 +00:00
import { FoldersScreenUtils } from 'lib/components/screens/folders-utils.js';
2017-07-06 19:48:17 +00:00
import { Resource } from 'lib/models/resource.js'
import { Tag } from 'lib/models/tag.js'
import { NoteTag } from 'lib/models/note-tag.js'
import { BaseItem } from 'lib/models/base-item.js'
2017-06-24 18:06:28 +00:00
import { BaseModel } from 'lib/base-model.js'
2017-07-06 19:48:17 +00:00
import { JoplinDatabase } from 'lib/joplin-database.js'
2017-07-07 17:19:24 +00:00
import { Database } from 'lib/database.js'
2017-07-05 20:34:25 +00:00
import { ItemList } from 'lib/components/item-list.js'
import { NotesScreen } from 'lib/components/screens/notes.js'
2017-07-05 21:29:00 +00:00
import { NotesScreenUtils } from 'lib/components/screens/notes-utils.js'
2017-07-05 20:34:25 +00:00
import { NoteScreen } from 'lib/components/screens/note.js'
import { FolderScreen } from 'lib/components/screens/folder.js'
import { FoldersScreen } from 'lib/components/screens/folders.js'
2017-07-07 17:19:24 +00:00
import { LogScreen } from 'lib/components/screens/log.js'
2017-07-10 19:16:59 +00:00
import { StatusScreen } from 'lib/components/screens/status.js'
import { WelcomeScreen } from 'lib/components/screens/welcome.js'
2017-07-06 19:29:09 +00:00
import { OneDriveLoginScreen } from 'lib/components/screens/onedrive-login.js'
2017-06-24 18:06:28 +00:00
import { Setting } from 'lib/models/setting.js'
2017-05-16 20:25:19 +00:00
import { MenuContext } from 'react-native-popup-menu';
2017-07-05 20:34:25 +00:00
import { SideMenu } from 'lib/components/side-menu.js';
import { SideMenuContent } from 'lib/components/side-menu-content.js';
2017-06-24 18:06:28 +00:00
import { DatabaseDriverReactNative } from 'lib/database-driver-react-native';
2017-07-06 19:48:17 +00:00
import { reg } from 'lib/registry.js';
2017-07-06 19:48:17 +00:00
import RNFetchBlob from 'react-native-fetch-blob';
2017-05-16 20:25:19 +00:00
2017-05-09 20:46:54 +00:00
let defaultState = {
2017-05-12 19:54:06 +00:00
notes: [],
2017-05-15 20:50:14 +00:00
folders: [],
2017-05-09 20:46:54 +00:00
selectedNoteId: null,
2017-05-24 20:51:50 +00:00
selectedItemType: 'note',
2017-05-15 19:10:00 +00:00
selectedFolderId: null,
2017-05-24 19:27:13 +00:00
showSideMenu: false,
2017-07-08 22:57:09 +00:00
screens: {},
loading: true,
2017-07-08 23:46:25 +00:00
historyCanGoBack: false,
2017-07-15 18:13:31 +00:00
notesOrder: {
orderBy: 'updated_time',
orderByDir: 'DESC',
},
2017-05-09 20:46:54 +00:00
};
2017-07-13 21:50:21 +00:00
const initialRoute = {
type: 'Navigation/NAVIGATE',
routeName: 'Welcome',
params: {}
};
2017-07-14 18:49:14 +00:00
defaultState.route = initialRoute;
2017-07-08 23:46:25 +00:00
let navHistory = [];
2017-07-13 21:50:21 +00:00
navHistory.push(initialRoute);
2017-07-08 23:46:25 +00:00
2017-07-15 17:08:54 +00:00
function historyCanGoBackTo(route) {
if (route.routeName == 'Note' && !route.noteId) return false;
if (route.routeName == 'Folder' && !route.folderId) return false;
return true;
}
2017-05-09 20:46:54 +00:00
const reducer = (state = defaultState, action) => {
2017-07-07 17:19:24 +00:00
reg.logger().info('Reducer action', action.type);
2017-05-09 20:46:54 +00:00
2017-05-10 19:21:09 +00:00
let newState = state;
2017-07-14 23:12:32 +00:00
try {
switch (action.type) {
2017-05-09 20:46:54 +00:00
2017-07-14 23:12:32 +00:00
case 'Navigation/BACK':
2017-05-09 20:46:54 +00:00
2017-07-14 23:12:32 +00:00
if (navHistory.length < 2) break;
2017-07-08 23:57:30 +00:00
2017-07-14 23:12:32 +00:00
action = navHistory.pop(); // Current page
action = navHistory.pop(); // Previous page
2017-07-08 23:46:25 +00:00
2017-07-15 17:08:54 +00:00
while (!historyCanGoBackTo(action)) {
if (!navHistory.length) {
action = null;
break;
}
action = navHistory.pop();
}
if (!action) action = Object.assign({}, initialRoute);
2017-07-14 23:12:32 +00:00
// Fall throught
2017-07-08 23:46:25 +00:00
2017-07-14 23:12:32 +00:00
case 'Navigation/NAVIGATE':
2017-07-08 23:46:25 +00:00
2017-07-14 23:12:32 +00:00
const currentRoute = state.route;
const currentRouteName = currentRoute ? currentRoute.routeName : '';
2017-05-12 19:54:06 +00:00
2017-07-14 23:12:32 +00:00
reg.logger().info('Route: ' + currentRouteName + ' => ' + action.routeName);
2017-05-15 20:50:14 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
2017-05-10 19:21:09 +00:00
2017-07-14 23:12:32 +00:00
if ('noteId' in action) {
newState.selectedNoteId = action.noteId;
}
2017-05-15 19:46:34 +00:00
2017-07-14 23:12:32 +00:00
if ('folderId' in action) {
newState.selectedFolderId = action.folderId;
}
if ('itemType' in action) {
newState.selectedItemType = action.itemType;
}
2017-05-10 19:21:09 +00:00
2017-07-14 23:12:32 +00:00
if (currentRouteName == action.routeName) {
// If the current screen is already the requested screen, don't do anything
} else {
newState.route = action;
2017-07-15 22:47:11 +00:00
if (action.routeName == 'Welcome') navHistory = [];
2017-07-14 23:12:32 +00:00
navHistory.push(action);
}
2017-05-24 20:51:50 +00:00
2017-07-14 23:12:32 +00:00
newState.historyCanGoBack = navHistory.length >= 2;
2017-05-24 20:11:37 +00:00
2017-07-14 23:12:32 +00:00
if (newState.route.routeName == 'Notes') {
Setting.setValue('activeFolderId', newState.selectedFolderId);
}
2017-07-10 21:44:55 +00:00
2017-07-14 23:12:32 +00:00
Keyboard.dismiss(); // TODO: should probably be in some middleware
break;
2017-05-09 20:46:54 +00:00
2017-07-14 23:12:32 +00:00
// Replace all the notes with the provided array
case 'APPLICATION_LOADING_DONE':
2017-07-08 22:57:09 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.loading = false;
break;
2017-07-08 22:57:09 +00:00
2017-07-14 23:12:32 +00:00
// Replace all the notes with the provided array
case 'NOTES_UPDATE_ALL':
2017-05-09 20:46:54 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.notes = action.notes;
break;
2017-05-11 20:14:01 +00:00
2017-07-14 23:12:32 +00:00
// Insert the note into the note list if it's new, or
// update it within the note array if it already exists.
case 'NOTES_UPDATE_ONE':
2017-07-14 23:12:32 +00:00
if (action.note.parent_id != state.selectedFolderId) break;
2017-07-07 22:25:03 +00:00
2017-07-14 23:12:32 +00:00
let newNotes = state.notes.splice(0);
var found = false;
for (let i = 0; i < newNotes.length; i++) {
let n = newNotes[i];
if (n.id == action.note.id) {
2017-07-15 23:09:04 +00:00
newNotes[i] = Object.assign(newNotes[i], action.note);
2017-07-14 23:12:32 +00:00
found = true;
break;
}
}
2017-05-10 19:21:09 +00:00
2017-07-14 23:12:32 +00:00
if (!found) newNotes.push(action.note);
2017-07-15 18:13:31 +00:00
newNotes = Note.sortNotes(newNotes, state.notesOrder);
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.notes = newNotes;
break;
case 'NOTES_DELETE':
var newNotes = [];
for (let i = 0; i < state.notes.length; i++) {
let f = state.notes[i];
if (f.id == action.noteId) continue;
newNotes.push(f);
}
2017-05-11 20:14:01 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.notes = newNotes;
break;
2017-05-09 20:46:54 +00:00
2017-07-14 23:12:32 +00:00
case 'FOLDERS_UPDATE_ALL':
2017-05-15 19:46:34 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.folders = action.folders;
break;
2017-05-15 19:46:34 +00:00
2017-07-14 23:12:32 +00:00
case 'FOLDERS_UPDATE_ONE':
2017-05-15 19:10:00 +00:00
2017-07-14 23:12:32 +00:00
var newFolders = state.folders.splice(0);
var found = false;
for (let i = 0; i < newFolders.length; i++) {
let n = newFolders[i];
if (n.id == action.folder.id) {
newFolders[i] = Object.assign(newFolders[i], action.folder);
2017-07-14 23:12:32 +00:00
found = true;
break;
}
2017-05-15 19:10:00 +00:00
}
2017-07-14 23:12:32 +00:00
if (!found) newFolders.push(action.folder);
2017-05-15 19:10:00 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.folders = newFolders;
break;
2017-05-15 19:10:00 +00:00
2017-07-14 23:12:32 +00:00
case 'FOLDER_DELETE':
2017-05-16 20:25:19 +00:00
2017-07-14 23:12:32 +00:00
var newFolders = [];
for (let i = 0; i < state.folders.length; i++) {
let f = state.folders[i];
if (f.id == action.folderId) continue;
newFolders.push(f);
}
2017-05-16 20:25:19 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.folders = newFolders;
break;
2017-05-16 20:25:19 +00:00
2017-07-14 23:12:32 +00:00
case 'SIDE_MENU_TOGGLE':
2017-05-24 19:27:13 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.showSideMenu = !newState.showSideMenu
break;
2017-05-24 19:27:13 +00:00
2017-07-14 23:12:32 +00:00
case 'SIDE_MENU_OPEN':
2017-05-24 19:27:13 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.showSideMenu = true
break;
2017-05-24 19:27:13 +00:00
2017-07-14 23:12:32 +00:00
case 'SIDE_MENU_CLOSE':
2017-05-24 19:27:13 +00:00
2017-07-14 23:12:32 +00:00
newState = Object.assign({}, state);
newState.showSideMenu = false
break;
2017-05-24 19:27:13 +00:00
2017-07-14 23:12:32 +00:00
}
} catch (error) {
error.message = 'In reducer: ' + error.message;
throw error;
2017-05-09 20:46:54 +00:00
}
2017-05-10 19:21:09 +00:00
return newState;
2017-05-09 20:46:54 +00:00
}
let store = createStore(reducer);
2017-07-09 22:57:15 +00:00
let initializationState_ = 'waiting';
2017-07-06 21:30:45 +00:00
2017-07-10 21:00:41 +00:00
async function initialize(dispatch, backButtonHandler) {
2017-07-09 22:57:15 +00:00
if (initializationState_ != 'waiting') return;
2017-07-07 17:19:24 +00:00
2017-07-10 18:09:58 +00:00
shimInit();
2017-07-07 17:19:24 +00:00
2017-07-10 18:09:58 +00:00
initializationState_ = 'in_progress';
2017-07-06 21:30:45 +00:00
2017-07-09 22:57:15 +00: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-14 23:12:32 +00:00
const mainLogger = new Logger();
mainLogger.addTarget('database', { database: logDatabase, source: 'm' });
2017-07-15 17:08:54 +00:00
if (Setting.value('env') == 'dev') mainLogger.addTarget('console');
2017-07-14 23:12:32 +00:00
mainLogger.setLevel(Logger.LEVEL_DEBUG);
reg.setLogger(mainLogger);
2017-07-09 22:57:15 +00:00
reg.logger().info('====================================');
reg.logger().info('Starting application ' + Setting.value('appId') + ' (' + Setting.value('env') + ')');
2017-07-14 23:12:32 +00:00
const dbLogger = new Logger();
dbLogger.addTarget('database', { database: logDatabase, source: 'm' });
2017-07-15 17:08:54 +00:00
if (Setting.value('env') == 'dev') dbLogger.addTarget('console');
if (Setting.value('env') == 'dev') {
dbLogger.setLevel(Logger.LEVEL_INFO); // Set to LEVEL_DEBUG for full SQL queries
2017-07-15 17:08:54 +00:00
} else {
dbLogger.setLevel(Logger.LEVEL_INFO);
}
2017-07-14 23:12:32 +00:00
2017-07-09 22:57:15 +00:00
let db = new JoplinDatabase(new DatabaseDriverReactNative());
2017-07-14 23:12:32 +00:00
db.setLogger(dbLogger);
2017-07-09 22:57:15 +00:00
reg.setDb(db);
BaseModel.dispatch = dispatch;
NotesScreenUtils.dispatch = dispatch;
2017-07-15 18:13:31 +00:00
NotesScreenUtils.store = store;
2017-07-15 15:54:19 +00:00
FoldersScreenUtils.dispatch = dispatch;
2017-07-09 22:57:15 +00: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 {
2017-07-12 22:32:08 +00:00
await db.open({ name: 'joplin-55.sqlite' })
2017-07-09 22:57:15 +00:00
2017-07-10 20:48:17 +00: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 15:54:19 +00:00
// await db.exec('UPDATE notes SET is_conflict = 1 where id like "546f%"');
2017-07-09 22:57:15 +00:00
}
2017-05-16 21:46:21 +00:00
2017-07-09 22:57:15 +00:00
reg.logger().info('Database is ready.');
reg.logger().info('Loading settings...');
await Setting.load();
2017-07-06 21:30:45 +00:00
2017-07-09 22:57:15 +00:00
reg.logger().info('Loading folders...');
2017-07-06 21:30:45 +00:00
2017-07-15 15:54:19 +00:00
await FoldersScreenUtils.refreshFolders();
2017-07-08 22:57:09 +00:00
2017-07-09 22:57:15 +00:00
dispatch({
type: 'APPLICATION_LOADING_DONE',
});
2017-07-14 23:12:32 +00:00
let folderId = Setting.value('activeFolderId');
let folder = await Folder.load(folderId);
if (folder) {
await NotesScreenUtils.openNoteList(folderId);
} else {
await NotesScreenUtils.openDefaultNoteList();
}
2017-07-09 22:57:15 +00:00
} catch (error) {
reg.logger().error('Initialization error:', error);
}
2017-07-10 21:00:41 +00:00
BackHandler.addEventListener('hardwareBackPress', () => {
return backButtonHandler();
});
2017-07-09 22:57:15 +00:00
initializationState_ = 'done';
reg.logger().info('Application initialized');
}
class AppComponent extends React.Component {
async componentDidMount() {
2017-07-10 21:00:41 +00:00
await initialize(this.props.dispatch, this.backButtonHandler.bind(this));
}
backButtonHandler() {
if (this.props.showSideMenu) {
this.props.dispatch({ type: 'SIDE_MENU_CLOSE' });
return true;
}
if (this.props.historyCanGoBack) {
this.props.dispatch({ type: 'Navigation/BACK' });
return true;
}
return false;
2017-05-11 20:14:01 +00:00
}
sideMenu_change(isOpen) {
2017-05-24 19:27:13 +00: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 20:14:01 +00:00
render() {
2017-05-24 19:27:13 +00:00
const sideMenuContent = <SideMenuContent/>;
2017-05-24 19:09:46 +00:00
2017-07-14 18:49:14 +00: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-05-11 20:14:01 +00:00
return (
<SideMenu menu={sideMenuContent} onChange={(isOpen) => this.sideMenu_change(isOpen)}>
2017-05-24 19:09:46 +00:00
<MenuContext style={{ flex: 1 }}>
2017-07-14 18:49:14 +00:00
<AppNav screens={appNavInit} />
2017-05-24 19:09:46 +00:00
</MenuContext>
</SideMenu>
2017-05-11 20:14:01 +00:00
);
}
2017-05-10 18:58:02 +00:00
}
const mapStateToProps = (state) => {
return {
2017-07-10 21:00:41 +00:00
historyCanGoBack: state.historyCanGoBack,
showSideMenu: state.showSideMenu,
2017-05-10 18:58:02 +00:00
};
};
const App = connect(mapStateToProps)(AppComponent);
2017-05-09 20:46:54 +00:00
class Root extends React.Component {
render() {
return (
<Provider store={store}>
2017-07-14 18:49:14 +00:00
<App/>
2017-05-09 20:46:54 +00:00
</Provider>
);
}
2017-05-09 19:59:14 +00:00
}
export { Root };