Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
fixes #56: iOS network activity indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Feb 10, 2014
1 parent d9f4b07 commit a84f861
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions ios/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
#include <llmr/platform/platform.hpp>

NSString *const MBXNeedsRenderNotification = @"MBXNeedsRenderNotification";
NSString *const MBXUpdateActivityNotification = @"MBXUpdateActivityNotification";

@interface MBXViewController () <UIGestureRecognizerDelegate>

@property (nonatomic) EAGLContext *context;
@property (nonatomic) NSUInteger activityCount;
@property (nonatomic) CGPoint center;
@property (nonatomic) CGFloat scale;
@property (nonatomic) CGFloat angle;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -381,6 +393,8 @@ void async(std::function<void()> fn, std::function<void()> cb)

void request_http(std::string url, std::function<void(Response&)> 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
Expand All @@ -403,12 +417,15 @@ void request_http(std::string url, std::function<void(Response&)> 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<void(Response&)> func, std::function<void()> 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
Expand All @@ -427,6 +444,7 @@ void request_http(std::string url, std::function<void(Response&)> func, std::fun

cb();

[[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@(-1) forKey:@"count"]];
[[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil];
}];
}
Expand Down

0 comments on commit a84f861

Please sign in to comment.