diff --git a/ReactNativeClient/src/components/folder-list.js b/ReactNativeClient/src/components/folder-list.js index 09322afb2..b4170da6a 100644 --- a/ReactNativeClient/src/components/folder-list.js +++ b/ReactNativeClient/src/components/folder-list.js @@ -6,28 +6,12 @@ import { ItemListComponent } from 'src/components/item-list.js'; import { Note } from 'src/models/note.js'; import { Folder } from 'src/models/folder.js'; import { _ } from 'src/locale.js'; +import { NoteFolderService } from 'src/services/note-folder-service.js'; class FolderListComponent extends ItemListComponent { listView_itemPress = (folderId) => { - Folder.load(folderId).then((folder) => { - Log.info('Current folder', folder); - - Note.previews(folderId).then((notes) => { - this.props.dispatch({ - type: 'NOTES_UPDATE_ALL', - notes: notes, - }); - - this.props.dispatch({ - type: 'Navigation/NAVIGATE', - routeName: 'Notes', - folderId: folderId, - }); - }).catch((error) => { - Log.warn('Cannot load notes', error); - }); - }); + NoteFolderService.openNoteList(folderId); } } diff --git a/ReactNativeClient/src/components/screen-header.js b/ReactNativeClient/src/components/screen-header.js index 09e2edaa0..df2d13113 100644 --- a/ReactNativeClient/src/components/screen-header.js +++ b/ReactNativeClient/src/components/screen-header.js @@ -25,7 +25,7 @@ class ScreenHeaderComponent extends Component { // Note: this is hardcoded for now because navigation.state doesn't tell whether // it's possible to go back or not. Maybe it's possible to get this information // from somewhere else. - return this.props.navState.routeName != 'Folders'; + return this.props.navState.routeName != 'Notes'; } sideMenuButton_press = () => { diff --git a/ReactNativeClient/src/components/screens/loading.js b/ReactNativeClient/src/components/screens/loading.js new file mode 100644 index 000000000..e169333d0 --- /dev/null +++ b/ReactNativeClient/src/components/screens/loading.js @@ -0,0 +1,31 @@ +import React, { Component } from 'react'; +import { View, Text } from 'react-native'; +import { connect } from 'react-redux' +import { Log } from 'src/log.js' +import { Folder } from 'src/models/folder.js' +import { ScreenHeader } from 'src/components/screen-header.js'; +import { NoteFolderService } from 'src/services/note-folder-service.js'; + +class LoadingScreenComponent extends React.Component { + + static navigationOptions = (options) => { + return { header: null }; + } + + render() { + return ( + + Loading... + + ); + } + +} + +const LoadingScreen = connect( + (state) => { + return {}; + } +)(LoadingScreenComponent) + +export { LoadingScreen }; \ No newline at end of file diff --git a/ReactNativeClient/src/components/side-menu-content.js b/ReactNativeClient/src/components/side-menu-content.js index 8409395e8..653767298 100644 --- a/ReactNativeClient/src/components/side-menu-content.js +++ b/ReactNativeClient/src/components/side-menu-content.js @@ -1,6 +1,8 @@ import { connect } from 'react-redux' import { Button } from 'react-native'; import { Log } from 'src/log.js'; +import { Note } from 'src/models/note.js'; +import { NoteFolderService } from 'src/services/note-folder-service.js'; const React = require('react'); const { @@ -40,15 +42,11 @@ const styles = StyleSheet.create({ class SideMenuContentComponent extends Component { folder_press(folder) { - this.props.dispatch({ - type: 'Navigation/NAVIGATE', - routeName: 'Notes', - folderId: folder.id, - }); - this.props.dispatch({ type: 'SIDE_MENU_CLOSE', }); + + NoteFolderService.openNoteList(folder.id); } render() { diff --git a/ReactNativeClient/src/root.js b/ReactNativeClient/src/root.js index 419dc644b..0c5cad27c 100644 --- a/ReactNativeClient/src/root.js +++ b/ReactNativeClient/src/root.js @@ -18,12 +18,13 @@ import { NoteScreen } from 'src/components/screens/note.js' import { FolderScreen } from 'src/components/screens/folder.js' import { FoldersScreen } from 'src/components/screens/folders.js' import { LoginScreen } from 'src/components/screens/login.js' +import { LoadingScreen } from 'src/components/screens/loading.js' import { Setting } from 'src/models/setting.js' import { Synchronizer } from 'src/synchronizer.js' import { MenuContext } from 'react-native-popup-menu'; -//import SideMenu from 'react-native-side-menu'; import { SideMenu } from 'src/components/side-menu.js'; import { SideMenuContent } from 'src/components/side-menu-content.js'; +import { NoteFolderService } from 'src/services/note-folder-service.js'; let defaultState = { notes: [], @@ -45,19 +46,14 @@ const reducer = (state = defaultState, action) => { case 'Navigation/NAVIGATE': case 'Navigation/BACK': - // If the current screen is already the requested screen, don't do anything const r = state.nav.routes; - if (r.length && r[r.length - 1].routeName == action.routeName) { - return state - } + const currentRoute = r.length ? r[r.length - 1] : null; + const currentRouteName = currentRoute ? currentRoute.routeName : ''; - action.params = { listMode: 'view' }; + Log.info('Current route name', currentRouteName); + Log.info('New route name', action.routeName); - const nextStateNav = AppNavigator.router.getStateForAction(action, state.nav); newState = Object.assign({}, state); - if (nextStateNav) { - newState.nav = nextStateNav; - } if ('noteId' in action) { newState.selectedNoteId = action.noteId; @@ -67,6 +63,17 @@ const reducer = (state = defaultState, action) => { newState.selectedFolderId = action.folderId; } + if (currentRouteName == action.routeName) { + // If the current screen is already the requested screen, don't do anything + } else { + action.params = { listMode: 'view' }; + + const nextStateNav = AppNavigator.router.getStateForAction(action, currentRouteName != 'Loading' ? state.nav : null); + if (nextStateNav) { + newState.nav = nextStateNav; + } + } + break; // Replace all the notes with the provided array @@ -168,17 +175,20 @@ const reducer = (state = defaultState, action) => { } + // Log.info('newState.selectedFolderId', newState.selectedFolderId); + return newState; } let store = createStore(reducer); const AppNavigator = StackNavigator({ - Notes: {screen: NotesScreen}, - Note: {screen: NoteScreen}, - Folder: {screen: FolderScreen}, - Folders: {screen: FoldersScreen}, - Login: {screen: LoginScreen}, + Notes: { screen: NotesScreen }, + Note: { screen: NoteScreen }, + Folder: { screen: FolderScreen }, + Folders: { screen: FoldersScreen }, + Login: { screen: LoginScreen }, + Loading: { screen: LoadingScreen }, }); class AppComponent extends React.Component { @@ -190,6 +200,7 @@ class AppComponent extends React.Component { BaseModel.dispatch = this.props.dispatch; BaseModel.db_ = db; + NoteFolderService.dispatch = this.props.dispatch; db.open().then(() => { Log.info('Database is ready.'); @@ -211,14 +222,27 @@ class AppComponent extends React.Component { Log.info('Loading folders...'); - Folder.all().then((folders) => { + return Folder.all().then((folders) => { this.props.dispatch({ type: 'FOLDERS_UPDATE_ALL', folders: folders, }); + return folders; }).catch((error) => { Log.warn('Cannot load folders', error); }); + }).then((folders) => { + let folder = folders[0]; + + if (!folder) throw new Error('No default folder is defined'); + + return NoteFolderService.openNoteList(folder.id); + + // this.props.dispatch({ + // type: 'Navigation/NAVIGATE', + // routeName: 'Notes', + // folderId: folder.id, + // }); }).then(() => { let synchronizer = new Synchronizer(db, Registry.api()); Registry.setSynchronizer(synchronizer); @@ -254,7 +278,7 @@ class AppComponent extends React.Component { defaultState.nav = AppNavigator.router.getStateForAction({ type: 'Navigation/NAVIGATE', - routeName: 'Folders', + routeName: 'Loading', params: {listMode: 'view'} }); diff --git a/ReactNativeClient/src/services/note-folder-service.js b/ReactNativeClient/src/services/note-folder-service.js index 6a7f5acdd..5a10e047e 100644 --- a/ReactNativeClient/src/services/note-folder-service.js +++ b/ReactNativeClient/src/services/note-folder-service.js @@ -36,6 +36,23 @@ class NoteFolderService extends BaseService { }); } + static openNoteList(folderId) { + return Note.previews(folderId).then((notes) => { + this.dispatch({ + type: 'NOTES_UPDATE_ALL', + notes: notes, + }); + + this.dispatch({ + type: 'Navigation/NAVIGATE', + routeName: 'Notes', + folderId: folderId, + }); + }).catch((error) => { + Log.warn('Cannot load notes', error); + }); + } + } export { NoteFolderService }; \ No newline at end of file