From 12d2af38f7ae9216e2491e602aef2e0c474f8f35 Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 23 Oct 2018 09:11:11 -0700 Subject: [PATCH] Added screens in sample app for notification Summary: Added a basic communication flows and a button to trigger notification {F142016937} Reviewed By: jknoxville Differential Revision: D10492428 fbshipit-source-id: b65fc46b3be695852f9197771a253d9e8596f328 --- .../FlipperKitExamplePlugin.h | 5 + .../FlipperKitExamplePlugin.mm | 5 +- iOS/Sample/CommunicationDemoViewController.h | 13 ++ iOS/Sample/CommunicationDemoViewController.mm | 57 +++++++ iOS/Sample/MainStoryBoard.storyboard | 157 +++++++++++++++++- iOS/Sample/MainViewController.m | 7 + iOS/Sample/Sample.xcodeproj/project.pbxproj | 6 + src/plugins/example/index.js | 46 +++-- 8 files changed, 276 insertions(+), 20 deletions(-) create mode 100644 iOS/Sample/CommunicationDemoViewController.h create mode 100644 iOS/Sample/CommunicationDemoViewController.mm diff --git a/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.h b/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.h index a2817e61f..e09d0820a 100644 --- a/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.h +++ b/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.h @@ -8,7 +8,12 @@ #import #import +@protocol FlipperKitExampleCommunicationResponderDelegate +- (void)messageReceived:(NSString *)msg; +@end + @interface FlipperKitExamplePlugin : NSObject +@property (weak, nonatomic) id delegate; - (instancetype)init NS_UNAVAILABLE; - (void)sendMessage:(NSString *)msg; diff --git a/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.mm b/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.mm index 5c67fc57d..9c170a72a 100644 --- a/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.mm +++ b/iOS/Plugins/FlipperKitExamplePlugin/FlipperKitExamplePlugin/FlipperKitExamplePlugin.mm @@ -13,7 +13,6 @@ #import @interface FlipperKitExamplePlugin() -@property (strong, nonatomic) NSMutableArray *messagesToDisplay; @property (strong, nonatomic) id connection; @property (nonatomic) NSInteger triggerCount; @@ -23,7 +22,6 @@ - (instancetype)init { if (self = [super init]) { - _messagesToDisplay = @[].mutableCopy; _triggerCount = 0; } return self; @@ -44,7 +42,8 @@ __weak FlipperKitExamplePlugin *weakSelf = self; self.connection = connection; [connection receive:@"displayMessage" withBlock:^(NSDictionary *params, id responder) { - [weakSelf.messagesToDisplay addObject:params[@"message"]]; + [weakSelf.delegate messageReceived:params[@"message"]]; + [responder success:@{@"greeting": @"Hello"}]; }]; } diff --git a/iOS/Sample/CommunicationDemoViewController.h b/iOS/Sample/CommunicationDemoViewController.h new file mode 100644 index 000000000..5a7d464c4 --- /dev/null +++ b/iOS/Sample/CommunicationDemoViewController.h @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + * + */ + +#import + +@interface CommunicationDemoViewController : UIViewController + +@end diff --git a/iOS/Sample/CommunicationDemoViewController.mm b/iOS/Sample/CommunicationDemoViewController.mm new file mode 100644 index 000000000..91ead68ef --- /dev/null +++ b/iOS/Sample/CommunicationDemoViewController.mm @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + * + */ + +#import "CommunicationDemoViewController.h" +#import + +@interface CommunicationDemoViewController() +@property (strong, nonatomic) NSMutableArray *messagesToDisplay; +@property (weak, nonatomic) IBOutlet UITextField *messageTextField; +@property (weak, nonatomic) IBOutlet UITableView *tableView; +@end + +@implementation CommunicationDemoViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + [FlipperKitExamplePlugin sharedInstance].delegate = self; +} + +- (IBAction)tappedTriggerNotification:(UIButton *)sender { + [[FlipperKitExamplePlugin sharedInstance] triggerNotification]; +} + +- (IBAction)tappedSendMessage:(UIButton *)sender { + if (self.messageTextField.text.length > 0) { + [[FlipperKitExamplePlugin sharedInstance] sendMessage:self.messageTextField.text]; + } +} + +- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusableCell"]; + cell.textLabel.text = self.messagesToDisplay[indexPath.row]; + return cell; +} + +- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return self.messagesToDisplay.count; +} + +- (void)messageReceived:(NSString *)msg { + if (msg) { + if (!self.messagesToDisplay) { + self.messagesToDisplay = @[].mutableCopy; + } + [self.messagesToDisplay addObject:msg]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self.tableView reloadData]; + }); + } +} + +@end diff --git a/iOS/Sample/MainStoryBoard.storyboard b/iOS/Sample/MainStoryBoard.storyboard index 41a5307dd..d69963614 100644 --- a/iOS/Sample/MainStoryBoard.storyboard +++ b/iOS/Sample/MainStoryBoard.storyboard @@ -10,6 +10,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -62,6 +194,20 @@ + @@ -72,19 +218,22 @@ + + + - + @@ -173,7 +322,7 @@ - + @@ -244,10 +393,10 @@ - + - + diff --git a/iOS/Sample/MainViewController.m b/iOS/Sample/MainViewController.m index 27de034fd..2db0fe6f7 100644 --- a/iOS/Sample/MainViewController.m +++ b/iOS/Sample/MainViewController.m @@ -11,6 +11,7 @@ #import "NetworkViewController.h" #import "RootViewController.h" #import "UserDefaultsViewController.h" +#import "CommunicationDemoViewController.h" @interface MainViewController () @@ -43,4 +44,10 @@ [self.navigationController pushViewController:userDefaultsViewController animated:true]; } +- (IBAction)tappedCommunicationDemo:(UIButton *)sender { + UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil]; + CommunicationDemoViewController *communicationDemoViewController = [storyboard instantiateViewControllerWithIdentifier:@"CommunicationDemoViewController"]; + [self.navigationController pushViewController:communicationDemoViewController animated:true]; +} + @end diff --git a/iOS/Sample/Sample.xcodeproj/project.pbxproj b/iOS/Sample/Sample.xcodeproj/project.pbxproj index 930c53e4b..b8beca34e 100644 --- a/iOS/Sample/Sample.xcodeproj/project.pbxproj +++ b/iOS/Sample/Sample.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 0D0B1CF0E859D91C55CC453B /* libPods-Sample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F97F4E8C3E28D8BFAAEE60F4 /* libPods-Sample.a */; }; 4E102341216AD7B400160734 /* UserDefaultsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E102340216AD7B400160734 /* UserDefaultsViewController.m */; }; + 53B4A36B217E2B6200B36A53 /* CommunicationDemoViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 53B4A36A217E2B6200B36A53 /* CommunicationDemoViewController.mm */; }; 53D59DB320ABA18400207065 /* NetworkViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D59DAA20ABA18300207065 /* NetworkViewController.m */; }; 53D59DB420ABA18400207065 /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 53D59DAB20ABA18300207065 /* AppDelegate.mm */; }; 53D59DB520ABA18400207065 /* MainViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D59DAD20ABA18300207065 /* MainViewController.m */; }; @@ -22,6 +23,8 @@ 081A9FC23643CD21C7D61AA1 /* Pods-Sample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Sample.release.xcconfig"; path = "Pods/Target Support Files/Pods-Sample/Pods-Sample.release.xcconfig"; sourceTree = ""; }; 4E10233F216AD7B400160734 /* UserDefaultsViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UserDefaultsViewController.h; sourceTree = ""; }; 4E102340216AD7B400160734 /* UserDefaultsViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserDefaultsViewController.m; sourceTree = ""; }; + 534252A9217DECCD0092D02B /* CommunicationDemoViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CommunicationDemoViewController.h; sourceTree = ""; }; + 53B4A36A217E2B6200B36A53 /* CommunicationDemoViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CommunicationDemoViewController.mm; sourceTree = ""; }; 53D59DAA20ABA18300207065 /* NetworkViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NetworkViewController.m; sourceTree = SOURCE_ROOT; }; 53D59DAB20ABA18300207065 /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = AppDelegate.mm; sourceTree = SOURCE_ROOT; }; 53D59DAC20ABA18300207065 /* NetworkViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkViewController.h; sourceTree = SOURCE_ROOT; }; @@ -54,6 +57,7 @@ 53D59DB920ABA19900207065 /* Sample */ = { isa = PBXGroup; children = ( + 53B4A36A217E2B6200B36A53 /* CommunicationDemoViewController.mm */, 53D59DBA20ABA20300207065 /* AppDelegate.h */, 53D59DAB20ABA18300207065 /* AppDelegate.mm */, 53D59DB120ABA18400207065 /* Icons.xcassets */, @@ -68,6 +72,7 @@ 53D59DAF20ABA18300207065 /* RootViewController.mm */, 53E0DE5220ABA0E4005682E1 /* Info.plist */, 53E0DE5320ABA0E4005682E1 /* main.m */, + 534252A9217DECCD0092D02B /* CommunicationDemoViewController.h */, ); name = Sample; sourceTree = ""; @@ -202,6 +207,7 @@ 4E102341216AD7B400160734 /* UserDefaultsViewController.m in Sources */, 53D59DB320ABA18400207065 /* NetworkViewController.m in Sources */, 53D59DB420ABA18400207065 /* AppDelegate.mm in Sources */, + 53B4A36B217E2B6200B36A53 /* CommunicationDemoViewController.mm in Sources */, 53D59DB520ABA18400207065 /* MainViewController.m in Sources */, 53D59DB620ABA18400207065 /* RootViewController.mm in Sources */, ); diff --git a/src/plugins/example/index.js b/src/plugins/example/index.js index 5d233bdf5..a5985c581 100644 --- a/src/plugins/example/index.js +++ b/src/plugins/example/index.js @@ -7,7 +7,7 @@ */ import {Button, Input, FlipperPlugin, FlexColumn, styled, Text} from 'flipper'; - +import type {Notification} from '../../plugin'; type DisplayMessageResponse = { greeting: string, }; @@ -18,7 +18,8 @@ type State = { }; type PersistedState = { - currentNotificationId: number, + currentNotificationIds: Array, + receivedMessage: ?string, }; const Container = styled(FlexColumn)({ @@ -32,6 +33,11 @@ export default class extends FlipperPlugin<*, State, PersistedState> { static id = 'Example'; static icon = 'apps'; + static defaultPersistedState = { + currentNotificationIds: [], + receivedMessage: null, + }; + state = { prompt: 'Type a message below to see it displayed on the mobile app', message: '', @@ -47,7 +53,16 @@ export default class extends FlipperPlugin<*, State, PersistedState> { ): PersistedState => { if (method === 'triggerNotification') { return { - currentNotificationId: payload.id, + ...persistedState, + currentNotificationIds: persistedState.currentNotificationIds.concat([ + payload.id, + ]), + }; + } + if (method === 'displayMessage') { + return { + ...persistedState, + receivedMessage: payload.msg, }; } return persistedState || {}; @@ -56,15 +71,17 @@ export default class extends FlipperPlugin<*, State, PersistedState> { /* * Callback to provide the currently active notifications. */ - static getActiveNotifications = (persistedState: PersistedState) => { - return [ - { - id: 'test-notification:' + persistedState.currentNotificationId, + static getActiveNotifications = ( + persistedState: PersistedState, + ): Array => { + return persistedState.currentNotificationIds.map((x: number) => { + return { + id: 'test-notification:' + x, message: 'Example Notification', severity: 'warning', - title: 'Notification: ' + persistedState.currentNotificationId, - }, - ]; + title: 'Notification: ' + x, + }; + }); }; /* @@ -83,14 +100,17 @@ export default class extends FlipperPlugin<*, State, PersistedState> { render() { return ( - {this.state.prompt}, + {this.state.prompt} { this.setState({message: event.target.value}); }} - />, - , + /> + + {this.props.persistedState.receivedMessage && ( + {this.props.persistedState.receivedMessage} + )} ); }