From a84f861d1f62b07df596ab91964477f2cb921f63 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Mon, 10 Feb 2014 14:27:00 -0800 Subject: [PATCH] fixes #56: iOS network activity indicator --- ios/MBXViewController.mm | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index accd8bf9407..d27a1aad32a 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -18,10 +18,12 @@ #include NSString *const MBXNeedsRenderNotification = @"MBXNeedsRenderNotification"; +NSString *const MBXUpdateActivityNotification = @"MBXUpdateActivityNotification"; @interface MBXViewController () @property (nonatomic) EAGLContext *context; +@property (nonatomic) NSUInteger activityCount; @property (nonatomic) CGPoint center; @property (nonatomic) CGFloat scale; @property (nonatomic) CGFloat angle; @@ -87,10 +89,6 @@ - (void)viewDidLoad { [super viewDidLoad]; - mapView = new MBXMapView(); - - mapView->init(); - UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; pan.delegate = self; [self.view addGestureRecognizer:pan]; @@ -131,6 +129,11 @@ - (void)viewDidLoad } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startRender:) name:MBXNeedsRenderNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateNetworkActivity:) name:MBXUpdateActivityNotification object:nil]; + + mapView = new MBXMapView(); + + mapView->init(); displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)]; [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; @@ -141,6 +144,15 @@ - (void)startRender:(NSNotification *)notification [self startRender]; } +- (void)updateNetworkActivity:(NSNotification *)notification +{ + NSInteger input = [[notification userInfo][@"count"] integerValue]; + + self.activityCount += input; + + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(self.activityCount > 0)]; +} + - (void)startRender { [NSObject cancelPreviousPerformRequestsWithTarget:self]; @@ -381,6 +393,8 @@ void async(std::function fn, std::function cb) void request_http(std::string url, std::function func) { + [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@1 forKey:@"count"]]; + NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]]; [NSURLConnection sendAsynchronousRequest:urlRequest @@ -403,12 +417,15 @@ void request_http(std::string url, std::function func) func(res); } + [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@(-1) forKey:@"count"]]; [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; }]; } void request_http(std::string url, std::function func, std::function cb) { + [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@1 forKey:@"count"]]; + NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]]; [NSURLConnection sendAsynchronousRequest:urlRequest @@ -427,6 +444,7 @@ void request_http(std::string url, std::function func, std::fun cb(); + [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@(-1) forKey:@"count"]]; [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; }]; }