Skip to content

Commit

Permalink
Adds LDTAppDelegate.bridge, gets eventDispatcher working - but profil…
Browse files Browse the repository at this point in the history
…e is blank on first load
  • Loading branch information
aaronschachter committed Feb 18, 2016
1 parent 0224141 commit 7fbdf20
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Lets Do This/Classes/LDTAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
// Copyright (c) 2015 Do Something. All rights reserved.
//
#import "LDTTabBarController.h"
#import <RCTBridgeModule.h>

@interface LDTAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic, readonly) LDTTabBarController *tabBarController;
@property (strong, nonatomic, readonly) RCTBridge *bridge;
@property (strong, nonatomic, readonly) NSURL *jsCodeLocation;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIView *statusBarBackgroundView;
Expand Down
3 changes: 3 additions & 0 deletions Lets Do This/Classes/LDTAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#import "GAI+LDT.h"
#import <Fabric/Fabric.h>
#import <Crashlytics/Crashlytics.h>
#import <RCTEventDispatcher.h>

@interface LDTAppDelegate()

@property (strong, nonatomic, readwrite) NSURL *jsCodeLocation;
@property (strong, nonatomic, readwrite) RCTBridge *bridge;

@end

Expand Down Expand Up @@ -64,6 +66,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
NSLog(@"[LDTAppDelegate] Running React Native from main.jsbundle.");
}
self.bridge = [[RCTBridge alloc] initWithBundleURL:self.jsCodeLocation moduleProvider:nil launchOptions:nil];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
Expand Down
6 changes: 6 additions & 0 deletions Lets Do This/Controllers/LDTReactBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "LDTReactBridge.h"
#import <RCTBridgeModule.h>
#import <RCTEventDispatcher.h>
#import "LDTAppDelegate.h"
#import "LDTTabBarController.h"
#import "LDTUserViewController.h"
Expand All @@ -30,6 +31,10 @@ -(LDTTabBarController *)tabBarController {
return appDelegate.tabBarController;
}

- (LDTAppDelegate *)appDelegate {
return ((LDTAppDelegate *)[UIApplication sharedApplication].delegate);
}

#pragma mark - RCTBridgeModule

RCT_EXPORT_MODULE();
Expand Down Expand Up @@ -83,6 +88,7 @@ - (dispatch_queue_t)methodQueue {
[SVProgressHUD showWithStatus:@"Signing up..."];
[[DSOUserManager sharedInstance] signupUserForCampaign:campaign completionHandler:^(DSOCampaignSignup *signup) {
[SVProgressHUD dismiss];
[[self appDelegate].bridge.eventDispatcher sendAppEventWithName:@"currentUserActivity" body:signup.dictionary];
} errorHandler:^(NSError *error) {
[SVProgressHUD dismiss];
[LDTMessage displayErrorMessageInViewController:self.tabBarController title:error.readableTitle];
Expand Down
18 changes: 1 addition & 17 deletions Lets Do This/Controllers/User/LDTUserViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ - (void)viewDidLoad {
if (self.isCurrentUserProfile) {
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Settings Icon"] style:UIBarButtonItemStylePlain target:self action:@selector(settingsTapped:)];
self.navigationItem.rightBarButtonItem = settingsButton;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"updateCurrentUser" object:nil];
}
[self setNavigationItemTitle];

NSURL *jsCodeLocation = ((LDTAppDelegate *)[UIApplication sharedApplication].delegate).jsCodeLocation;
self.reactRootView =[[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName: @"UserView" initialProperties:[self appProperties] launchOptions:nil];
self.reactRootView = [[RCTRootView alloc] initWithBridge:((LDTAppDelegate *)[UIApplication sharedApplication].delegate).bridge moduleName:@"UserView" initialProperties:[self appProperties]];

This comment has been minimized.

Copy link
@aaronschachter

aaronschachter Feb 18, 2016

Author Contributor

@DFurnes This is the magic sauce to get the NativeAppEventEmitter working within the React Components. facebook/react-native#1049

self.view = self.reactRootView;
}

Expand Down Expand Up @@ -110,30 +108,16 @@ - (NSDictionary *)appProperties {
userDict = self.user.dictionary;
sessionToken = [DSOUserManager sharedInstance].sessionToken;
}
NSMutableArray *signups = [[NSMutableArray alloc] init];
for (DSOCampaignSignup *signup in self.user.campaignSignups) {
[signups addObject:signup.dictionary];
}

appProperties = @{
@"user" : self.user.dictionary,
@"url" : profileURL,
@"signups" : [signups copy],
@"isSelfProfile" : [NSNumber numberWithBool:self.isCurrentUserProfile],
@"apiKey": [DSOAPI sharedInstance].apiKey,
@"sessionToken": sessionToken,
};
return appProperties;
}


- (void)receivedNotification:(NSNotification *)notification {
if ([[notification name] isEqualToString:@"updateCurrentUser"]) {
NSLog(@"heard it");
self.reactRootView.appProperties = [self appProperties];
}
}

- (IBAction)settingsTapped:(id)sender {
LDTSettingsViewController *destVC = [[LDTSettingsViewController alloc] initWithNibName:@"LDTSettingsView" bundle:nil];
UINavigationController *destNavVC = [[UINavigationController alloc] initWithRootViewController:destVC];
Expand Down
19 changes: 14 additions & 5 deletions ReactComponents/UserView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import React, {
RefreshControl,
TouchableHighlight,
ActivityIndicatorIOS,
View
View,
NativeAppEventEmitter
} from 'react-native';

var Style = require('./Style.js');
Expand Down Expand Up @@ -39,13 +40,19 @@ var UserView = React.createClass({
},
componentDidMount: function() {
if (this.props.isSelfProfile) {
this.loadSignups(this.props.signups);
}
else {
this.fetchData();
this.subscription = NativeAppEventEmitter.addListener(
'currentUserActivity',
(signup) => this.handleEvent(signup),
);
}
this.fetchData();
},
handleEvent: function(reminder) {
console.log('handleEvent');
this.fetchData();
},
fetchData: function() {
console.log('fetchData');
var options = {
method: 'GET',
headers: {
Expand All @@ -64,6 +71,7 @@ var UserView = React.createClass({
.done();
},
loadSignups: function(signups) {
console.log(signups);
var dataBlob = {},
sectionIDs = [],
rowIDs = [],
Expand Down Expand Up @@ -151,6 +159,7 @@ var UserView = React.createClass({
if (!this.state.loaded) {
return this.renderLoadingView();
}
console.log('listView');
return (
<ListView
dataSource={this.state.dataSource}
Expand Down

0 comments on commit 7fbdf20

Please sign in to comment.