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

279 lines
7.1 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-06-24 20:06:28 +02:00
import { Log } from 'lib/log.js'
import { Note } from 'lib/models/note.js'
import { Folder } from 'lib/models/folder.js'
import { BaseModel } from 'lib/base-model.js'
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'
import { LoginScreen } from 'lib/components/screens/login.js'
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-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-16 23:46:21 +02:00
user: { email: 'laurent@cozic.net', session: null },
2017-05-24 21:27:13 +02:00
showSideMenu: false,
2017-05-09 22:46:54 +02:00
};
const reducer = (state = defaultState, action) => {
2017-05-19 21:12:09 +02:00
Log.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-05-24 22:11:37 +02:00
Log.info('Current route name', currentRouteName);
Log.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-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 {
const nextStateNav = AppNavigator.router.getStateForAction(action, currentRouteName != 'Loading' ? state.nav : null);
if (nextStateNav) {
newState.nav = nextStateNav;
}
}
2017-05-10 21:21:09 +02:00
break;
2017-05-09 22:46:54 +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;
// 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':
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-16 23:46:21 +02:00
case 'USER_SET':
newState = Object.assign({}, state);
newState.user = action.user;
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-24 22:11:37 +02:00
// Log.info('newState.selectedFolderId', newState.selectedFolderId);
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 },
Folders: { screen: FoldersScreen },
Login: { screen: LoginScreen },
Loading: { screen: LoadingScreen },
2017-07-06 21:29:09 +02:00
OneDriveLogin: { screen: OneDriveLoginScreen },
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
componentDidMount() {
2017-06-11 23:11:14 +02:00
let db = new Database(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-05 23:29:00 +02:00
db.open({ name: '/storage/emulated/0/Download/joplin-43.sqlite' }).then(() => {
2017-05-12 22:17:23 +02:00
Log.info('Database is ready.');
}).then(() => {
Log.info('Loading settings...');
return Setting.load();
}).then(() => {
2017-07-05 23:29:00 +02:00
Setting.setConstant('appId', 'net.cozic.joplin-android');
2017-05-16 23:46:21 +02:00
2017-05-15 21:46:34 +02:00
Log.info('Loading folders...');
2017-05-24 22:11:37 +02:00
return Folder.all().then((folders) => {
2017-05-12 22:17:23 +02:00
this.props.dispatch({
2017-05-15 21:46:34 +02:00
type: 'FOLDERS_UPDATE_ALL',
folders: folders,
2017-05-12 22:17:23 +02:00
});
2017-05-24 22:11:37 +02:00
return folders;
2017-05-12 22:17:23 +02:00
}).catch((error) => {
2017-05-15 21:46:34 +02:00
Log.warn('Cannot load folders', error);
2017-05-11 22:14:01 +02:00
});
2017-05-24 22:11:37 +02:00
}).then((folders) => {
2017-07-05 23:29:00 +02:00
this.props.dispatch({
type: 'Navigation/NAVIGATE',
routeName: 'Folders',
});
2017-05-11 22:14:01 +02:00
}).catch((error) => {
2017-05-18 21:58:01 +02:00
Log.error('Initialization error:', error);
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 };