iOS: Update diagnostic screen on state update

Summary: Update the diagnostic screen whenever the sonar state changes.

Reviewed By: priteshrnandgaonkar

Differential Revision: D9218765

fbshipit-source-id: 654a35e309cb43df18bc8c8a645d469eddc1031d
This commit is contained in:
John Knox
2018-08-20 05:06:23 -07:00
committed by Facebook Github Bot
parent 21888157df
commit d8e5e31c9a
7 changed files with 84 additions and 4 deletions

View File

@@ -7,11 +7,13 @@
*/ */
#ifdef FB_SONARKIT_ENABLED #ifdef FB_SONARKIT_ENABLED
#include "FlipperStateUpdateListener.h"
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@interface FlipperDiagnosticsViewController : UIViewController @interface FlipperDiagnosticsViewController : UIViewController <FlipperStateUpdateListener>
@property(strong, nonatomic) UIScrollView *scrollView; @property(strong, nonatomic) UIScrollView *scrollView;
@property(strong, nonatomic) UILabel *stateLabel; @property(strong, nonatomic) UILabel *stateLabel;
- (void)onUpdate;
@end @end
#endif #endif

View File

@@ -12,8 +12,9 @@
text.text = @"Flipper Diagnostics"; text.text = @"Flipper Diagnostics";
[self.view addSubview:text]; [self.view addSubview:text];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50)]; self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 100)];
self.stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1000)]; self.stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1000)];
self.stateLabel.numberOfLines = 0; self.stateLabel.numberOfLines = 0;
self.stateLabel.text = [[SonarClient sharedClient] getState]; self.stateLabel.text = [[SonarClient sharedClient] getState];
[self.scrollView addSubview:self.stateLabel]; [self.scrollView addSubview:self.stateLabel];
@@ -22,8 +23,23 @@
} }
- (void)onUpdate { - (void)onUpdate {
self.stateLabel.text = [[SonarClient sharedClient] getState]; FlipperDiagnosticsViewController __weak *weakSelf = self;
self.scrollView.contentSize = self.stateLabel.frame.size; dispatch_async(dispatch_get_main_queue(), ^{
FlipperDiagnosticsViewController *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
NSString *state = [[SonarClient sharedClient] getState];
strongSelf.stateLabel.text = state;
[strongSelf.stateLabel sizeToFit];
strongSelf.scrollView.contentSize = strongSelf.stateLabel.frame.size;
});
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
id<FlipperStateUpdateListener> weakSelf = self;
[[SonarClient sharedClient] subscribeForUpdates:weakSelf];
} }
- (UIInterfaceOrientationMask)supportedInterfaceOrientations { - (UIInterfaceOrientationMask)supportedInterfaceOrientations {

View File

@@ -0,0 +1,14 @@
/*
* 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.
*
*/
#ifdef FB_SONARKIT_ENABLED
@protocol FlipperStateUpdateListener
- (void)onUpdate;
@end
#endif

View File

@@ -0,0 +1,19 @@
#ifdef FB_SONARKIT_ENABLED
#include <Sonar/SonarStateUpdateListener.h>
#import "FlipperStateUpdateListener.h"
/*
* This class exists to bridge the gap between Objective C and C++.
* A SKStateUpdateCPPWrapper instance allows for wrapping an Objective-C object
* and passing it to the pure C++ SonarClient, so it can be triggered when updates occur.
*/
class SKStateUpdateCPPWrapper : public SonarStateUpdateListener {
public:
SKStateUpdateCPPWrapper(id<FlipperStateUpdateListener> delegate_);
void onUpdate();
private:
__weak id<FlipperStateUpdateListener> delegate_;
};
#endif

View File

@@ -0,0 +1,16 @@
#ifdef FB_SONARKIT_ENABLED
#include "SKStateUpdateCPPWrapper.h"
SKStateUpdateCPPWrapper::SKStateUpdateCPPWrapper(id<FlipperStateUpdateListener> controller) {
delegate_ = controller;
}
void SKStateUpdateCPPWrapper::onUpdate() {
if (!delegate_) {
return;
}
[delegate_ onUpdate];
}
#endif

View File

@@ -8,6 +8,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "SonarPlugin.h" #import "SonarPlugin.h"
#import "FlipperStateUpdateListener.h"
/** /**
Represents a connection between the Sonar desktop och client side. Manages the lifecycle of attached Represents a connection between the Sonar desktop och client side. Manages the lifecycle of attached
@@ -50,6 +51,11 @@ Get the current state of the sonar client
*/ */
- (NSString *)getState; - (NSString *)getState;
/**
Subscribe a ViewController to state update change notifications
*/
- (void)subscribeForUpdates:(id<FlipperStateUpdateListener>)controller;
// initializers are disabled. You must use `+[SonarClient sharedClient]` instance. // initializers are disabled. You must use `+[SonarClient sharedClient]` instance.
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE;

View File

@@ -13,6 +13,8 @@
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#include <folly/io/async/ScopedEventBaseThread.h> #include <folly/io/async/ScopedEventBaseThread.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "SKStateUpdateCPPWrapper.h"
#import "FlipperDiagnosticsViewController.h"
#if !TARGET_OS_SIMULATOR #if !TARGET_OS_SIMULATOR
//#import "SKPortForwardingServer.h" //#import "SKPortForwardingServer.h"
@@ -125,6 +127,11 @@ using WrapperPlugin = facebook::sonar::SonarCppWrapperPlugin;
return @(_cppClient->getState().c_str()); return @(_cppClient->getState().c_str());
} }
- (void)subscribeForUpdates:(id<FlipperStateUpdateListener>)controller {
auto stateListener = std::make_shared<SKStateUpdateCPPWrapper>(controller);
_cppClient->setStateListener(stateListener);
}
@end @end
#endif #endif