Skip to content

Commit

Permalink
Update PBLiveLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
galenlin committed Mar 15, 2017
1 parent a8d1197 commit 80e9eed
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Example/PBLiveLoader/NSInputStream+Reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// NSInputStream+Reader.h
// Pchat
//
// Created by Galen Lin on 15/03/2017.
// Copyright © 2017 galen. All rights reserved.
//

#include <targetconditionals.h>

#if (DEBUG && !(TARGET_IPHONE_SIMULATOR))

#import <Foundation/Foundation.h>

@interface NSInputStream (Reader)

- (int)readInt;

- (NSString *)readString;

- (NSData *)readData;

@end

#endif
36 changes: 36 additions & 0 deletions Example/PBLiveLoader/NSInputStream+Reader.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// NSInputStream+Reader.m
// Pchat
//
// Created by Galen Lin on 15/03/2017.
// Copyright © 2017 galen. All rights reserved.
//

#import "NSInputStream+Reader.h"

#if (DEBUG && !(TARGET_IPHONE_SIMULATOR))

@implementation NSInputStream (Reader)

- (int)readInt {
uint8_t bytes[4];
[self read:bytes maxLength:4];
int intData = *((int *)bytes);
return NSSwapInt(intData);
}

- (NSData *)readData {
int length = [self readInt];
uint8_t *bytes = malloc(length);
NSInteger len = [self read:bytes maxLength:length];
return [NSData dataWithBytes:bytes length:len];
}

- (NSString *)readString {
NSData *data = [self readData];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

@end

#endif
23 changes: 23 additions & 0 deletions Example/PBLiveLoader/PBLLInspector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// PBLLInspector.h
// Pchat
//
// Created by Galen Lin on 15/03/2017.
// Copyright © 2017 galen. All rights reserved.
//

#include <targetconditionals.h>

#if (DEBUG && !(TARGET_IPHONE_SIMULATOR))

#import <UIKit/UIKit.h>

@interface PBLLInspector : UIButton

+ (instancetype)sharedInspector;

+ (void)addToWindow;

@end

#endif
52 changes: 52 additions & 0 deletions Example/PBLiveLoader/PBLLInspector.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// PBLLInspector.m
// Pbind
//
// Created by Galen Lin on 15/03/2017.
//

#import "PBLLInspector.h"

#if (DEBUG && !(TARGET_IPHONE_SIMULATOR))

#import "PBLLInspectorController.h"
#import <Pbind/Pbind.h>

@interface PBLLInspector () <UISearchBarDelegate>

@end

@implementation PBLLInspector

+ (instancetype)sharedInspector {
static PBLLInspector *o;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
o = [PBLLInspector buttonWithType:UIButtonTypeCustom];
});
return o;
}

+ (void)addToWindow {
PBLLInspector *inspector = [self sharedInspector];
CGRect frame = [UIScreen mainScreen].bounds;
frame.origin.x = frame.size.width - 68.f;
frame.origin.y = frame.size.height - 100.f;
frame.size.width = 60.f;
frame.size.height = 44.f;
inspector.frame = frame;
inspector.backgroundColor = [UIColor greenColor];
[inspector setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[inspector setTitle:@"Pbind" forState:UIControlStateNormal];
[inspector addTarget:inspector action:@selector(didClick:) forControlEvents:UIControlEventTouchUpInside];
[[[UIApplication sharedApplication].delegate window] addSubview:inspector];
}

- (void)didClick:(id)sender {
PBLLInspectorController *controller = [[PBLLInspectorController alloc] init];
[PBTopController().navigationController pushViewController:controller animated:YES];
}

@end

#endif
19 changes: 19 additions & 0 deletions Example/PBLiveLoader/PBLLInspectorController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// PBLLInspectorController.h
// Pchat
//
// Created by Galen Lin on 15/03/2017.
// Copyright © 2017 galen. All rights reserved.
//

#include <targetconditionals.h>

#if (DEBUG && !(TARGET_IPHONE_SIMULATOR))

#import <UIKit/UIKit.h>

@interface PBLLInspectorController : UIViewController

@end

#endif
96 changes: 96 additions & 0 deletions Example/PBLiveLoader/PBLLInspectorController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// PBLLInspectorController.m
// Pchat
//
// Created by Galen Lin on 15/03/2017.
//

#import "PBLLInspectorController.h"

#if (DEBUG && !(TARGET_IPHONE_SIMULATOR))

#import "PBLLInspector.h"
#import "PBLLRemoteWatcher.h"
#import <Pbind/Pbind.h>

@interface PBLLInspectorController () <UISearchBarDelegate>
{
UISearchBar *_searchBar;
}

@end

@implementation PBLLInspectorController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.edgesForExtendedLayout = UIRectEdgeNone;
self.view.backgroundColor = [UIColor whiteColor];
CGRect frame = self.view.bounds;
frame.size.height = 44.f;
frame.origin.y = 16.f;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:frame];
searchBar.text = [self defaultIP];
searchBar.delegate = self;
searchBar.returnKeyType = UIReturnKeyJoin;
[self.view addSubview:searchBar];
_searchBar = searchBar;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
[self.view addGestureRecognizer:tap];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[PBLLInspector sharedInspector].hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[PBLLInspector sharedInspector].hidden = NO;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSString *)defaultIP {
NSString *ip = [[NSUserDefaults standardUserDefaults] objectForKey:@"pbind.server.ip"];
if (ip == nil) {
ip = @"192.168.1.10";
}
return ip;
}

- (void)setDefaultIP:(NSString *)ip {
[[NSUserDefaults standardUserDefaults] setObject:ip forKey:@"pbind.server.ip"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark - UISearchBarDelegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *ip = searchBar.text;
if (ip.length == 0) {
return;
}

[self setDefaultIP:ip];
[[PBLLRemoteWatcher globalWatcher] connect:ip];
[self.navigationController popViewControllerAnimated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[PBTopController().view pb_reloadClient];
});
}

#pragma mark - Gesture

- (void)didTap:(id)sender {
[_searchBar resignFirstResponder];
}

@end

#endif
41 changes: 41 additions & 0 deletions Example/PBLiveLoader/PBLLRemoteWatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// PBLLRemoteWatcher.h
// Pchat
//
// Created by Galen Lin on 15/03/2017.
// Copyright © 2017 galen. All rights reserved.
//

#include <targetconditionals.h>

#if (DEBUG && !(TARGET_IPHONE_SIMULATOR))

#import <Foundation/Foundation.h>

@class PBLLRemoteWatcher;

@protocol PBLLRemoteWatcherDelegate <NSObject>

- (void)remoteWatcher:(PBLLRemoteWatcher *)watcher didUpdateFile:(NSString *)fileName withData:(NSData *)data;

@optional

- (void)remoteWatcher:(PBLLRemoteWatcher *)watcher didCreateFile:(NSString *)fileName;
- (void)remoteWatcher:(PBLLRemoteWatcher *)watcher didDeleteFile:(NSString *)fileName;

@end

@interface PBLLRemoteWatcher : NSObject

+ (instancetype)globalWatcher;

- (void)connect:(NSString *)ip;
- (void)connectDefaultIP;

- (void)requestAPI:(NSString *)api success:(void (^)(NSData *))success failure:(void (^)(NSError *))failure;

@property (nonatomic, assign) id<PBLLRemoteWatcherDelegate> delegate;

@end

#endif
Loading

0 comments on commit 80e9eed

Please sign in to comment.