Commit d9b718c

mo khan <mo@mokhan.ca>
2016-12-25 17:02:25
inject registry into each screen.
1 parent 922645d
app/boot/wire-up-components-into.js
@@ -16,7 +16,8 @@ export default class WireUpComponentsInto {
     this.registry.register('eventAggregator', EventAggregator).asSingleton();
     this.registry.register('router', (container) => {
       return new Router({
-        eventAggregator: container.resolve('eventAggregator')
+        eventAggregator: container.resolve('eventAggregator'),
+        registry: this.registry
       });
     }).asSingleton();
     this.registry.register('applicationStorage', ApplicationStorage).asSingleton();
app/components/application-component.js
@@ -1,21 +0,0 @@
-import React, { Component } from 'react';
-
-export default class ApplicationComponent extends Component {
-  componentDidMount() {
-    if (this.state.eventsOfInterest == undefined) {
-      return;
-    }
-
-    this.state.eventsOfInterest.forEach((event) => {
-      this.props.eventAggregator.subscribe(event, this);
-    });
-  }
-
-  componentWillUnmount() {
-    this.props.eventAggregator.unsubscribe(this);
-  }
-
-  publish(event) {
-    this.props.eventAggregator.publish(event);
-  }
-}
app/screens/dashboard-screen.js
@@ -1,11 +1,12 @@
+import * as events from '../services/events';
+import NewWorkoutScreen from './new-workout-screen';
 import React, { Component } from 'react';
-import { Platform, View, Image } from 'react-native';
-import { Container, Header, Title, Content, Footer, FooterTab, Button, Icon, Spinner, DeckSwiper } from 'native-base';
-import ApplicationComponent from '../components/application-component';
+import Screen from './screen';
 import WorkoutSummary from '../components/workout-summary';
-import * as events from '../services/events';
+import { Container, Header, Title, Content, Footer, FooterTab, Button, Icon, Spinner, DeckSwiper } from 'native-base';
+import { Platform, View, Image } from 'react-native';
 
-export default class DashboardScreen extends ApplicationComponent {
+export default class DashboardScreen extends Screen {
   constructor(props) {
     super(props)
     this.state = {
@@ -80,6 +81,7 @@ export default class DashboardScreen extends ApplicationComponent {
 
   onStartWorkout() {
     console.log("start workout");
+    this.loadScreen(NewWorkoutScreen)
   }
 
   onLogout() {
app/screens/login-screen.js
@@ -1,16 +1,14 @@
 import React from 'react';
 import { View } from 'react-native';
 import { Container, Header, Title, Content, Spinner, List, ListItem, InputGroup, Input, Icon, Button, Text } from 'native-base';
-import Api from '../infrastructure/api'
-import DashboardScreen from './dashboard-screen'
-import ApplicationComponent from '../components/application-component'
-import Configuration from '../infrastructure/configuration'
 import * as events from '../services/events';
+import DashboardScreen from './dashboard-screen'
+import Screen from './screen'
 
-export default class LoginScreen extends ApplicationComponent {
+export default class LoginScreen extends Screen {
   constructor(props) {
     super(props)
-    this.configuration = new Configuration('development');
+    this.configuration = this.resolve('configuration');
     this.state = {
       account: { username: 'mokha', password: 'password' },
       eventsOfInterest: [events.LOGGED_IN],
@@ -99,8 +97,6 @@ export default class LoginScreen extends ApplicationComponent {
 
   notify(event) {
     this.setState({isLoading: false});
-    this.props.navigator.push({
-      component: DashboardScreen, params: { ...event }
-    });
+    this.loadScreen(DashboardScreen, { ...event })
   }
 }
app/screens/new-workout-screen.js
@@ -0,0 +1,17 @@
+import React, { Component } from 'react';
+import { View, Image } from 'react-native';
+import { Container, Header, Title, Content, Footer, FooterTab, Button, Icon, Spinner, DeckSwiper, Text } from 'native-base';
+import Screen from './screen';
+import * as events from '../services/events';
+
+export default class NewWorkoutScreen extends Screen {
+  render() {
+    return (
+      <Container>
+        <Content>
+          <Text>Yo!</Text>
+        </Content>
+      </Container>
+    );
+  }
+}
app/screens/screen.js
@@ -0,0 +1,32 @@
+import React, { Component } from 'react';
+
+export default class Screen extends Component {
+  componentDidMount() {
+    this.eventsOfInterest().forEach((event) => {
+      this.props.eventAggregator.subscribe(event, this);
+    });
+  }
+
+  componentWillUnmount() {
+    this.props.eventAggregator.unsubscribe(this);
+  }
+
+  publish(event) {
+    this.props.eventAggregator.publish(event);
+  }
+
+  loadScreen(screen, params = {}) {
+    this.props.navigator.push({ component: screen, params });
+  }
+
+  eventsOfInterest() {
+    if (this.state == null || this.state.eventsOfInterest == undefined) {
+      return [];
+    }
+    return this.state.eventsOfInterest;
+  }
+
+  resolve(component) {
+    return this.props.registry.resolve(component);
+  }
+}