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

368 lines
10 KiB
JavaScript
Raw Normal View History

2017-05-09 21:59:14 +02:00
import React, { Component } from 'react';
2017-05-09 22:46:54 +02:00
import { View, Button, TextInput } from 'react-native';
2017-05-09 21:59:14 +02:00
import { connect } from 'react-redux'
import { Provider } from 'react-redux'
import { createStore } from 'redux';
import { combineReducers } from 'redux';
import { StackNavigator } from 'react-navigation';
import { addNavigationHelpers } from 'react-navigation';
2017-07-06 23:30:45 +02:00
import { shim } from 'lib/shim.js';
2017-06-24 20:06:28 +02:00
import { Log } from 'lib/log.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-05 22:34:25 +02:00
import { LoadingScreen } from 'lib/components/screens/loading.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-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/NAVIGATE':
case 'Navigation/BACK':
2017-05-09 22:46:54 +02:00
2017-05-12 21:54:06 +02:00
const r = state.nav.routes;
2017-05-24 22:11:37 +02:00
const currentRoute = r.length ? r[r.length - 1] : null;
const currentRouteName = currentRoute ? currentRoute.routeName : '';
2017-05-12 21:54:06 +02:00
2017-07-07 19:19:24 +02:00
reg.logger().info('Current route name', currentRouteName);
reg.logger().info('New route name', 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-07-09 00:57:09 +02:00
if ('screens' in action) {
for (let n in action.screens) {
if (!action.screens.hasOwnProperty(n)) continue;
newState.screens[n] = action.screens[n];
}
}
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-09 00:57:09 +02:00
//const nextStateNav = AppNavigator.router.getStateForAction(action, currentRouteName != 'Loading' ? state.nav : null);
const nextStateNav = AppNavigator.router.getStateForAction(action, state.nav);
2017-05-24 22:11:37 +02:00
if (nextStateNav) {
newState.nav = nextStateNav;
}
}
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;
// 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;
// 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.
case 'NOTES_UPDATE_ONE':
2017-07-08 00:25:03 +02:00
if (action.note.parent_id != state.selectedFolderId) break;
let newNotes = state.notes.splice(0);
2017-05-15 21:10:00 +02:00
var found = false;
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
if (!found) newNotes.push(action.note);
2017-05-11 22:14:01 +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-07-07 19:19:24 +02:00
// reg.logger().info('newState.selectedFolderId', newState.selectedFolderId);
2017-05-24 22:11:37 +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-05-09 21:59:14 +02:00
const AppNavigator = StackNavigator({
2017-05-24 22:11:37 +02:00
Notes: { screen: NotesScreen },
Note: { screen: NoteScreen },
Folder: { screen: FolderScreen },
2017-07-09 00:57:09 +02:00
//Folders: { screen: FoldersScreen },
2017-05-24 22:11:37 +02:00
Loading: { screen: LoadingScreen },
2017-07-06 21:29:09 +02:00
OneDriveLogin: { screen: OneDriveLoginScreen },
2017-07-07 19:19:24 +02:00
Log: { screen: LogScreen },
2017-05-09 21:59:14 +02:00
});
2017-05-10 20:58:02 +02:00
class AppComponent extends React.Component {
2017-05-11 22:14:01 +02:00
2017-07-06 23:30:45 +02:00
async componentDidMount() {
shim.fetchBlob = async function(url, options) {
if (!options || !options.path) throw new Error('fetchBlob: target file path is missing');
if (!options.method) options.method = 'GET';
let headers = options.headers ? options.headers : {};
let method = options.method ? options.method : 'GET';
let dirs = RNFetchBlob.fs.dirs;
let localFilePath = options.path;
if (localFilePath.indexOf('/') !== 0) localFilePath = dirs.DocumentDir + '/' + localFilePath;
delete options.path;
try {
let response = await RNFetchBlob.config({
path: localFilePath
}).fetch(method, url, headers);
// Returns an object that roughtly compatible with a standard Response object
let output = {
ok: response.respInfo.status < 400,
path: response.data,
text: response.text,
json: response.json,
status: response.respInfo.status,
headers: response.respInfo.headers,
};
return output;
} catch (error) {
throw new Error('fetchBlob: ' + method + ' ' + url + ': ' + error.toString());
}
}
2017-07-08 00:25:03 +02:00
Setting.setConstant('env', __DEV__ ? 'dev' : 'prod');
Setting.setConstant('appId', 'net.cozic.joplin');
2017-07-07 19:19:24 +02:00
Setting.setConstant('appType', 'mobile');
2017-07-08 00:25:03 +02:00
Setting.setConstant('resourceDir', RNFetchBlob.fs.dirs.DocumentDir);
2017-07-07 19:19:24 +02:00
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' });
2017-07-08 00:25:03 +02:00
reg.logger().info('Starting application ' + Setting.value('appId') + ' (' + Setting.value('env') + ')');
2017-07-07 19:19:24 +02:00
2017-07-06 21:48:17 +02:00
let db = new JoplinDatabase(new DatabaseDriverReactNative());
2017-07-06 21:48:17 +02:00
reg.setDb(db);
2017-05-20 00:16:50 +02:00
2017-05-18 21:58:01 +02:00
BaseModel.dispatch = this.props.dispatch;
2017-07-05 23:29:00 +02:00
NotesScreenUtils.dispatch = this.props.dispatch;
2017-05-20 00:16:50 +02:00
BaseModel.db_ = db;
2017-05-18 21:58:01 +02:00
2017-07-06 21:48:17 +02:00
BaseItem.loadClass('Note', Note);
BaseItem.loadClass('Folder', Folder);
BaseItem.loadClass('Resource', Resource);
BaseItem.loadClass('Tag', Tag);
BaseItem.loadClass('NoteTag', NoteTag);
2017-07-06 23:30:45 +02:00
try {
2017-07-08 00:25:03 +02:00
if (Setting.value('env') == 'prod') {
await db.open({ name: 'joplin.sqlite' })
} else {
2017-07-09 00:57:09 +02:00
await db.open({ name: 'joplin-53.sqlite' })
2017-07-08 01:25:10 +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-08 00:25:03 +02:00
}
2017-07-06 23:30:45 +02:00
2017-07-08 00:25:03 +02:00
reg.logger().info('Database is ready.');
2017-07-07 19:19:24 +02:00
reg.logger().info('Loading settings...');
2017-07-06 23:30:45 +02:00
await Setting.load();
2017-05-16 23:46:21 +02:00
2017-07-07 19:19:24 +02:00
reg.logger().info('Loading folders...');
2017-07-09 00:57:09 +02:00
let initialFolders = await Folder.all();
2017-07-06 23:30:45 +02:00
this.props.dispatch({
type: 'FOLDERS_UPDATE_ALL',
2017-07-09 00:57:09 +02:00
folders: initialFolders,
2017-05-11 22:14:01 +02:00
});
2017-07-06 23:30:45 +02:00
2017-07-05 23:29:00 +02:00
this.props.dispatch({
2017-07-09 00:57:09 +02:00
type: 'APPLICATION_LOADING_DONE',
2017-07-05 23:29:00 +02:00
});
2017-07-09 00:57:09 +02:00
// console.info(initialFolders);
// if (initialFolders.length) {
// // const selectedFolder = await Folder.defaultFolder();
// // this.props.dispatch({
// // type: 'Navigation/NAVIGATE',
// // routeName: 'Notes',
// // params: selectedFolder.id,
// // });
// }
2017-07-06 23:30:45 +02:00
} catch (error) {
2017-05-18 21:58:01 +02:00
Log.error('Initialization error:', error);
2017-07-06 23:30:45 +02:00
}
2017-05-11 22:14:01 +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-05-11 22:14:01 +02:00
return (
<SideMenu menu={sideMenuContent} onChange={(isOpen) => this.sideMenu_change(isOpen)}>
2017-05-24 21:09:46 +02:00
<MenuContext style={{ flex: 1 }}>
<AppNavigator navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav,
})} />
</MenuContext>
</SideMenu>
2017-05-11 22:14:01 +02:00
);
}
2017-05-10 20:58:02 +02:00
}
2017-05-15 22:50:14 +02:00
defaultState.nav = AppNavigator.router.getStateForAction({
type: 'Navigation/NAVIGATE',
2017-05-24 22:11:37 +02:00
routeName: 'Loading',
2017-05-24 22:18:09 +02:00
params: {}
2017-05-15 22:50:14 +02:00
});
2017-05-10 20:58:02 +02:00
const mapStateToProps = (state) => {
return {
nav: state.nav
};
};
const App = connect(mapStateToProps)(AppComponent);
2017-05-09 22:46:54 +02:00
class Root extends React.Component {
render() {
return (
<Provider store={store}>
2017-05-10 20:58:02 +02:00
<App />
2017-05-09 22:46:54 +02:00
</Provider>
);
}
2017-05-09 21:59:14 +02:00
}
export { Root };