2017-05-09 21:59:14 +02:00
|
|
|
import React, { Component } from 'react';
|
2017-07-10 23:44:55 +02:00
|
|
|
import { BackHandler, Keyboard } from 'react-native';
|
2017-07-10 23:00:41 +02:00
|
|
|
import { connect, Provider } from 'react-redux'
|
2017-05-09 21:59:14 +02:00
|
|
|
import { createStore } from 'redux';
|
2017-07-10 20:09:58 +02:00
|
|
|
import { shimInit } from 'lib/shim-init-react.js';
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Log } from 'lib/log.js'
|
2017-07-14 20:49:14 +02:00
|
|
|
import { AppNav } from 'lib/components/app-nav.js'
|
2017-07-07 19:19:24 +02:00
|
|
|
import { Logger } from 'lib/logger.js'
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Note } from 'lib/models/note.js'
|
|
|
|
import { Folder } from 'lib/models/folder.js'
|
2017-07-06 21:48:17 +02: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 20:06:28 +02:00
|
|
|
import { BaseModel } from 'lib/base-model.js'
|
2017-07-06 21:48:17 +02:00
|
|
|
import { JoplinDatabase } from 'lib/joplin-database.js'
|
2017-07-07 19:19:24 +02:00
|
|
|
import { Database } from 'lib/database.js'
|
2017-07-05 22:34:25 +02:00
|
|
|
import { ItemList } from 'lib/components/item-list.js'
|
|
|
|
import { NotesScreen } from 'lib/components/screens/notes.js'
|
2017-07-05 23:29:00 +02:00
|
|
|
import { NotesScreenUtils } from 'lib/components/screens/notes-utils.js'
|
2017-07-05 22:34:25 +02: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 19:19:24 +02:00
|
|
|
import { LogScreen } from 'lib/components/screens/log.js'
|
2017-07-10 21:16:59 +02:00
|
|
|
import { StatusScreen } from 'lib/components/screens/status.js'
|
2017-07-13 20:47:31 +02:00
|
|
|
import { WelcomeScreen } from 'lib/components/screens/welcome.js'
|
2017-07-06 21:29:09 +02:00
|
|
|
import { OneDriveLoginScreen } from 'lib/components/screens/onedrive-login.js'
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Setting } from 'lib/models/setting.js'
|
|
|
|
import { Synchronizer } from 'lib/synchronizer.js'
|
2017-05-16 22:25:19 +02:00
|
|
|
import { MenuContext } from 'react-native-popup-menu';
|
2017-07-05 22:34:25 +02:00
|
|
|
import { SideMenu } from 'lib/components/side-menu.js';
|
|
|
|
import { SideMenuContent } from 'lib/components/side-menu-content.js';
|
2017-06-24 20:06:28 +02:00
|
|
|
import { DatabaseDriverReactNative } from 'lib/database-driver-react-native';
|
2017-07-06 21:48:17 +02:00
|
|
|
import { reg } from 'lib/registry.js';
|
2017-07-06 21:48:17 +02:00
|
|
|
import RNFetchBlob from 'react-native-fetch-blob';
|
2017-05-16 22:25:19 +02:00
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
let defaultState = {
|
2017-05-12 21:54:06 +02:00
|
|
|
notes: [],
|
2017-05-15 22:50:14 +02:00
|
|
|
folders: [],
|
2017-05-09 22:46:54 +02:00
|
|
|
selectedNoteId: null,
|
2017-05-24 22:51:50 +02:00
|
|
|
selectedItemType: 'note',
|
2017-05-15 21:10:00 +02:00
|
|
|
selectedFolderId: null,
|
2017-05-24 21:27:13 +02:00
|
|
|
showSideMenu: false,
|
2017-07-09 00:57:09 +02:00
|
|
|
screens: {},
|
|
|
|
loading: true,
|
2017-07-09 01:46:25 +02:00
|
|
|
historyCanGoBack: false,
|
2017-05-09 22:46:54 +02:00
|
|
|
};
|
|
|
|
|
2017-07-13 23:50:21 +02:00
|
|
|
const initialRoute = {
|
|
|
|
type: 'Navigation/NAVIGATE',
|
|
|
|
routeName: 'Welcome',
|
|
|
|
params: {}
|
|
|
|
};
|
|
|
|
|
2017-07-14 20:49:14 +02:00
|
|
|
defaultState.route = initialRoute;
|
|
|
|
|
2017-07-09 01:46:25 +02:00
|
|
|
let navHistory = [];
|
2017-07-13 23:50:21 +02:00
|
|
|
navHistory.push(initialRoute);
|
2017-07-09 01:46:25 +02:00
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
const reducer = (state = defaultState, action) => {
|
2017-07-07 19:19:24 +02:00
|
|
|
reg.logger().info('Reducer action', action.type);
|
2017-05-09 22:46:54 +02:00
|
|
|
|
2017-05-10 21:21:09 +02:00
|
|
|
let newState = state;
|
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
switch (action.type) {
|
|
|
|
|
2017-05-10 20:58:02 +02:00
|
|
|
case 'Navigation/BACK':
|
2017-05-09 22:46:54 +02:00
|
|
|
|
2017-07-09 01:57:30 +02:00
|
|
|
if (navHistory.length < 2) break;
|
|
|
|
|
2017-07-09 01:46:25 +02:00
|
|
|
action = navHistory.pop(); // Current page
|
|
|
|
action = navHistory.pop(); // Previous page
|
|
|
|
|
|
|
|
// Fall throught
|
|
|
|
|
|
|
|
case 'Navigation/NAVIGATE':
|
|
|
|
|
2017-07-14 20:49:14 +02:00
|
|
|
const currentRoute = state.route;
|
2017-05-24 22:11:37 +02:00
|
|
|
const currentRouteName = currentRoute ? currentRoute.routeName : '';
|
2017-05-12 21:54:06 +02:00
|
|
|
|
2017-07-09 01:46:25 +02:00
|
|
|
reg.logger().info('Route: ' + currentRouteName + ' => ' + action.routeName);
|
2017-05-15 22:50:14 +02:00
|
|
|
|
2017-05-10 21:21:09 +02:00
|
|
|
newState = Object.assign({}, state);
|
|
|
|
|
2017-05-15 22:50:14 +02:00
|
|
|
if ('noteId' in action) {
|
2017-05-15 21:46:34 +02:00
|
|
|
newState.selectedNoteId = action.noteId;
|
|
|
|
}
|
|
|
|
|
2017-05-15 22:50:14 +02:00
|
|
|
if ('folderId' in action) {
|
2017-05-15 21:46:34 +02:00
|
|
|
newState.selectedFolderId = action.folderId;
|
2017-05-10 21:21:09 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 22:51:50 +02:00
|
|
|
if ('itemType' in action) {
|
|
|
|
newState.selectedItemType = action.itemType;
|
|
|
|
}
|
|
|
|
|
2017-05-24 22:11:37 +02:00
|
|
|
if (currentRouteName == action.routeName) {
|
|
|
|
// If the current screen is already the requested screen, don't do anything
|
|
|
|
} else {
|
2017-07-14 20:49:14 +02:00
|
|
|
newState.route = action;
|
|
|
|
navHistory.push(action);
|
2017-05-24 22:11:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 01:57:30 +02:00
|
|
|
newState.historyCanGoBack = navHistory.length >= 2;
|
2017-07-10 23:44:55 +02:00
|
|
|
|
|
|
|
Keyboard.dismiss(); // TODO: should probably be in some middleware
|
2017-05-10 21:21:09 +02:00
|
|
|
break;
|
2017-05-09 22:46:54 +02:00
|
|
|
|
2017-07-09 00:57:09 +02:00
|
|
|
// Replace all the notes with the provided array
|
|
|
|
case 'APPLICATION_LOADING_DONE':
|
|
|
|
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.loading = false;
|
|
|
|
break;
|
|
|
|
|
2017-05-11 23:02:47 +02:00
|
|
|
// Replace all the notes with the provided array
|
|
|
|
case 'NOTES_UPDATE_ALL':
|
2017-05-09 22:46:54 +02:00
|
|
|
|
2017-05-10 21:21:09 +02:00
|
|
|
newState = Object.assign({}, state);
|
2017-05-11 22:14:01 +02:00
|
|
|
newState.notes = action.notes;
|
|
|
|
break;
|
|
|
|
|
2017-05-11 23:02:47 +02:00
|
|
|
// Insert the note into the note list if it's new, or
|
2017-05-12 21:54:06 +02:00
|
|
|
// update it within the note array if it already exists.
|
2017-05-11 23:02:47 +02:00
|
|
|
case 'NOTES_UPDATE_ONE':
|
|
|
|
|
2017-07-08 00:25:03 +02:00
|
|
|
if (action.note.parent_id != state.selectedFolderId) break;
|
|
|
|
|
2017-05-11 23:02:47 +02:00
|
|
|
let newNotes = state.notes.splice(0);
|
2017-05-15 21:10:00 +02:00
|
|
|
var found = false;
|
2017-05-11 23:02:47 +02:00
|
|
|
for (let i = 0; i < newNotes.length; i++) {
|
|
|
|
let n = newNotes[i];
|
|
|
|
if (n.id == action.note.id) {
|
|
|
|
newNotes[i] = action.note;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 21:21:09 +02:00
|
|
|
|
2017-05-11 23:02:47 +02:00
|
|
|
if (!found) newNotes.push(action.note);
|
2017-05-11 22:14:01 +02:00
|
|
|
|
2017-05-11 23:02:47 +02:00
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.notes = newNotes;
|
2017-05-11 22:14:01 +02:00
|
|
|
break;
|
2017-05-09 22:46:54 +02:00
|
|
|
|
2017-05-15 21:46:34 +02:00
|
|
|
case 'FOLDERS_UPDATE_ALL':
|
|
|
|
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.folders = action.folders;
|
|
|
|
break;
|
|
|
|
|
2017-05-15 21:10:00 +02:00
|
|
|
case 'FOLDERS_UPDATE_ONE':
|
|
|
|
|
2017-05-16 22:25:19 +02:00
|
|
|
var newFolders = state.folders.splice(0);
|
2017-05-15 21:10:00 +02:00
|
|
|
var found = false;
|
|
|
|
for (let i = 0; i < newFolders.length; i++) {
|
|
|
|
let n = newFolders[i];
|
|
|
|
if (n.id == action.folder.id) {
|
|
|
|
newFolders[i] = action.folder;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) newFolders.push(action.folder);
|
|
|
|
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.folders = newFolders;
|
|
|
|
break;
|
|
|
|
|
2017-05-16 22:25:19 +02:00
|
|
|
case 'FOLDER_DELETE':
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.folders = newFolders;
|
|
|
|
break;
|
|
|
|
|
2017-05-24 21:27:13 +02:00
|
|
|
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;
|
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 21:21:09 +02:00
|
|
|
return newState;
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let store = createStore(reducer);
|
|
|
|
|
2017-07-10 00:57:15 +02:00
|
|
|
let initializationState_ = 'waiting';
|
2017-07-06 23:30:45 +02:00
|
|
|
|
2017-07-10 23:00:41 +02:00
|
|
|
async function initialize(dispatch, backButtonHandler) {
|
2017-07-10 00:57:15 +02:00
|
|
|
if (initializationState_ != 'waiting') return;
|
2017-07-07 19:19:24 +02:00
|
|
|
|
2017-07-10 20:09:58 +02:00
|
|
|
shimInit();
|
2017-07-07 19:19:24 +02:00
|
|
|
|
2017-07-10 20:09:58 +02:00
|
|
|
initializationState_ = 'in_progress';
|
2017-07-06 23:30:45 +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());
|
|
|
|
reg.logger().addTarget('database', { database: logDatabase, source: 'm' });
|
|
|
|
|
|
|
|
reg.logger().info('====================================');
|
|
|
|
reg.logger().info('Starting application ' + Setting.value('appId') + ' (' + Setting.value('env') + ')');
|
|
|
|
|
|
|
|
let db = new JoplinDatabase(new DatabaseDriverReactNative());
|
|
|
|
reg.setDb(db);
|
|
|
|
|
|
|
|
BaseModel.dispatch = dispatch;
|
|
|
|
NotesScreenUtils.dispatch = dispatch;
|
|
|
|
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-13 00:32:08 +02:00
|
|
|
await db.open({ name: 'joplin-55.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-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-06 23:30:45 +02:00
|
|
|
|
2017-07-10 00:57:15 +02:00
|
|
|
reg.logger().info('Loading folders...');
|
|
|
|
let initialFolders = await Folder.all();
|
2017-07-06 23:30:45 +02:00
|
|
|
|
2017-07-10 00:57:15 +02:00
|
|
|
dispatch({
|
|
|
|
type: 'FOLDERS_UPDATE_ALL',
|
|
|
|
folders: initialFolders,
|
|
|
|
});
|
2017-07-09 00:57:09 +02:00
|
|
|
|
2017-07-10 00:57:15 +02:00
|
|
|
dispatch({
|
|
|
|
type: 'APPLICATION_LOADING_DONE',
|
|
|
|
});
|
|
|
|
|
2017-07-13 01:01:15 +02:00
|
|
|
await NotesScreenUtils.openDefaultNoteList();
|
2017-07-10 00:57:15 +02:00
|
|
|
} catch (error) {
|
|
|
|
reg.logger().error('Initialization error:', error);
|
|
|
|
}
|
|
|
|
|
2017-07-10 23:00:41 +02:00
|
|
|
BackHandler.addEventListener('hardwareBackPress', () => {
|
|
|
|
return backButtonHandler();
|
|
|
|
});
|
|
|
|
|
2017-07-10 00:57:15 +02:00
|
|
|
initializationState_ = 'done';
|
|
|
|
|
|
|
|
reg.logger().info('Application initialized');
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppComponent extends React.Component {
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2017-07-10 23:00:41 +02: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 22:14:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:58:19 +02:00
|
|
|
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-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-05-11 22:14:01 +02:00
|
|
|
return (
|
2017-06-06 22:58:19 +02:00
|
|
|
<SideMenu menu={sideMenuContent} onChange={(isOpen) => this.sideMenu_change(isOpen)}>
|
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-07-10 23:00:41 +02:00
|
|
|
historyCanGoBack: state.historyCanGoBack,
|
|
|
|
showSideMenu: state.showSideMenu,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export { Root };
|