1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-26 18:58:21 +02:00

296 lines
8.6 KiB
JavaScript
Raw Normal View History

2017-05-09 19:59:14 +00:00
import React, { Component } from 'react';
import { Keyboard, NativeModules } from 'react-native';
2017-07-10 22:00:41 +01:00
import { connect, Provider } from 'react-redux'
import { BackButtonService } from 'lib/services/back-button.js';
import { createStore, applyMiddleware } from 'redux';
2017-07-10 18:09:58 +00:00
import { shimInit } from 'lib/shim-init-react.js';
2017-06-24 19:06:28 +01: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 18:19:24 +01:00
import { Logger } from 'lib/logger.js'
2017-06-24 19:06:28 +01:00
import { Note } from 'lib/models/note.js'
import { Folder } from 'lib/models/folder.js'
2017-10-07 23:17:10 +01:00
import { FoldersScreenUtils } from 'lib/folders-screen-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 19:06:28 +01: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 18:19:24 +01:00
import { Database } from 'lib/database.js'
2017-07-05 21:34:25 +01:00
import { NotesScreen } from 'lib/components/screens/notes.js'
import { NoteScreen } from 'lib/components/screens/note.js'
2017-07-23 19:26:50 +01:00
import { ConfigScreen } from 'lib/components/screens/config.js'
2017-07-05 21:34:25 +01:00
import { FolderScreen } from 'lib/components/screens/folder.js'
2017-07-07 18:19:24 +01:00
import { LogScreen } from 'lib/components/screens/log.js'
2017-07-10 20:16:59 +01:00
import { StatusScreen } from 'lib/components/screens/status.js'
import { WelcomeScreen } from 'lib/components/screens/welcome.js'
2017-07-22 23:52:24 +01:00
import { SearchScreen } from 'lib/components/screens/search.js'
2017-07-06 19:29:09 +00:00
import { OneDriveLoginScreen } from 'lib/components/screens/onedrive-login.js'
2017-06-24 19:06:28 +01: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 21:34:25 +01:00
import { SideMenu } from 'lib/components/side-menu.js';
import { SideMenuContent } from 'lib/components/side-menu-content.js';
2017-06-24 19:06:28 +01: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-25 18:57:06 +00:00
import { _, setLocale, closestSupportedLocale, defaultLocale } from 'lib/locale.js';
2017-07-06 19:48:17 +00:00
import RNFetchBlob from 'react-native-fetch-blob';
2017-07-22 18:35:39 +01:00
import { PoorManIntervals } from 'lib/poor-man-intervals.js';
2017-10-07 17:30:27 +01:00
import { reducer, defaultState } from 'lib/reducer.js';
2017-05-09 20:46:54 +00: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();
}
if (action.type == 'NAV_GO' && action.routeName == 'Notes') {
Setting.setValue('activeFolderId', newState.selectedFolderId);
}
return result;
}
let store = createStore(reducer, applyMiddleware(generalMiddleware));
2017-05-09 20:46:54 +00:00
2017-07-10 22:00:41 +01:00
async function initialize(dispatch, backButtonHandler) {
2017-07-10 18:09:58 +00:00
shimInit();
2017-07-07 18:19:24 +01:00
2017-07-09 23:57:15 +01: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 00:12:32 +01:00
const mainLogger = new Logger();
mainLogger.addTarget('database', { database: logDatabase, source: 'm' });
2017-07-15 18:08:54 +01:00
if (Setting.value('env') == 'dev') mainLogger.addTarget('console');
2017-07-15 00:12:32 +01:00
mainLogger.setLevel(Logger.LEVEL_DEBUG);
reg.setLogger(mainLogger);
2017-07-09 23:57:15 +01:00
reg.logger().info('====================================');
reg.logger().info('Starting application ' + Setting.value('appId') + ' (' + Setting.value('env') + ')');
2017-07-15 00:12:32 +01:00
const dbLogger = new Logger();
2017-07-17 21:22:05 +01:00
dbLogger.addTarget('database', { database: logDatabase, source: 'm' });
2017-07-15 18:08:54 +01:00
if (Setting.value('env') == 'dev') {
2017-07-17 21:22:05 +01:00
dbLogger.addTarget('console');
dbLogger.setLevel(Logger.LEVEL_INFO); // Set to LEVEL_DEBUG for full SQL queries
2017-07-15 18:08:54 +01:00
} else {
dbLogger.setLevel(Logger.LEVEL_INFO);
}
2017-07-15 00:12:32 +01:00
2017-07-09 23:57:15 +01:00
let db = new JoplinDatabase(new DatabaseDriverReactNative());
2017-07-15 00:12:32 +01:00
db.setLogger(dbLogger);
2017-07-09 23:57:15 +01:00
reg.setDb(db);
reg.dispatch = dispatch;
2017-07-09 23:57:15 +01:00
BaseModel.dispatch = dispatch;
2017-07-15 16:54:19 +01:00
FoldersScreenUtils.dispatch = dispatch;
2017-07-09 23:57:15 +01: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 21:00:16 +01:00
//await db.open({ name: 'joplin-67.sqlite' })
2017-07-09 23:57:15 +01:00
2017-07-10 21:48:17 +01: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 16:54:19 +01:00
// await db.exec('UPDATE notes SET is_conflict = 1 where id like "546f%"');
2017-07-09 23:57:15 +01:00
}
2017-05-16 23:46:21 +02:00
2017-07-09 23:57:15 +01:00
reg.logger().info('Database is ready.');
reg.logger().info('Loading settings...');
await Setting.load();
2017-07-25 18:57:06 +00: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 22:29:40 +01:00
setLocale(Setting.value('locale'));
2017-07-19 22:26:30 +01:00
2017-07-09 23:57:15 +01:00
reg.logger().info('Loading folders...');
2017-07-06 22:30:45 +01:00
2017-07-15 16:54:19 +01:00
await FoldersScreenUtils.refreshFolders();
2017-07-08 23:57:09 +01:00
2017-07-25 19:36:52 +01:00
const tags = await Tag.all();
dispatch({
type: 'TAGS_UPDATE_ALL',
tags: tags,
});
2017-07-15 00:12:32 +01:00
let folderId = Setting.value('activeFolderId');
let folder = await Folder.load(folderId);
2017-07-25 18:09:01 +00:00
if (!folder) folder = await Folder.defaultFolder();
2017-07-23 19:26:50 +01:00
2017-07-25 18:09:01 +00:00
if (!folder) {
dispatch({
type: 'NAV_GO',
2017-07-25 18:09:01 +00:00
routeName: 'Welcome',
});
2017-07-24 22:52:30 +01:00
} else {
2017-07-25 18:09:01 +00:00
dispatch({
type: 'NAV_GO',
2017-07-25 18:09:01 +00:00
routeName: 'Notes',
folderId: folder.id,
});
2017-07-24 22:52:30 +01:00
}
2017-07-09 23:57:15 +01:00
} catch (error) {
reg.logger().error('Initialization error:', error);
}
BackButtonService.initialize(backButtonHandler);
2017-07-10 22:00:41 +01:00
reg.setupRecurrentSync();
2017-07-22 18:35:39 +01:00
2017-07-22 23:52:24 +01:00
if (Setting.value('env') == 'dev') {
2017-07-30 22:22:57 +02:00
// reg.scheduleSync();
2017-07-22 23:52:24 +01:00
} else {
reg.scheduleSync();
}
2017-07-09 23:57:15 +01:00
reg.logger().info('Application initialized');
}
class AppComponent extends React.Component {
constructor() {
super();
this.lastSyncStarted_ = defaultState.syncStarted;
}
2017-07-09 23:57:15 +01:00
async componentDidMount() {
2017-08-01 17:41:58 +00: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 22:00:41 +01:00
}
async backButtonHandler() {
2017-07-10 22:00:41 +01: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 22:00:41 +01:00
return true;
}
return false;
2017-05-11 20:14:01 +00:00
}
componentWillReceiveProps(newProps) {
if (newProps.syncStarted != this.lastSyncStarted_) {
if (!newProps.syncStarted) FoldersScreenUtils.refreshFolders();
this.lastSyncStarted_ = newProps.syncStarted;
}
}
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-08-01 17:41:58 +00:00
if (this.props.appState != 'ready') return null;
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-07-22 23:52:24 +01:00
Search: { screen: SearchScreen },
2017-07-23 19:26:50 +01:00
Config: { screen: ConfigScreen },
2017-07-14 18:49:14 +00:00
};
2017-05-11 20:14:01 +00: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 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 22:00:41 +01:00
historyCanGoBack: state.historyCanGoBack,
showSideMenu: state.showSideMenu,
syncStarted: state.syncStarted,
2017-08-01 17:41:58 +00:00
appState: state.appState,
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 };