From 70bb318e7503ca136608d5632431793405bbab64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 15 Apr 2014 11:27:37 -0400 Subject: [PATCH] update activity indicator on ios --- common/foundation_request.h | 1 - common/foundation_request.mm | 50 ++++++++++++++++++++++++++++-------- ios/MBXViewController.mm | 9 ------- llmr.gyp | 16 ++++++++---- 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/common/foundation_request.h b/common/foundation_request.h index 70c383aa8..a6009983e 100644 --- a/common/foundation_request.h +++ b/common/foundation_request.h @@ -1,4 +1,3 @@ #import NSString *const MBXNeedsRenderNotification = @"MBXNeedsRenderNotification"; -NSString *const MBXUpdateActivityNotification = @"MBXUpdateActivityNotification"; diff --git a/common/foundation_request.mm b/common/foundation_request.mm index 297b4f380..ac0767bd6 100644 --- a/common/foundation_request.mm +++ b/common/foundation_request.mm @@ -1,5 +1,11 @@ #import "foundation_request.h" +#include "TargetConditionals.h" +#if TARGET_OS_IPHONE +#import +#include +#endif + #include #include #include @@ -12,6 +18,10 @@ dispatch_queue_t queue = nullptr; NSURLSession *session = nullptr; +#if TARGET_OS_IPHONE +std::atomic active_tasks; +#endif + void request_initialize_cb() { queue = dispatch_queue_create("Parsing", DISPATCH_QUEUE_CONCURRENT); @@ -21,6 +31,10 @@ void request_initialize_cb() { sessionConfig.requestCachePolicy = NSURLRequestUseProtocolCachePolicy; session = [NSURLSession sessionWithConfiguration:sessionConfig]; + +#if TARGET_OS_IPHONE + active_tasks = 0; +#endif } namespace llmr { @@ -28,7 +42,20 @@ namespace llmr { class platform::Request { public: Request(NSURLSessionDataTask *task, const std::string &original_url) - : task(task), original_url(original_url) {} + : task(task), original_url(original_url) { + #if TARGET_OS_IPHONE + active_tasks++; + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:active_tasks]; + #endif + } + +#if TARGET_OS_IPHONE + ~Request() { + active_tasks--; + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:active_tasks]; + } +#endif + NSURLSessionDataTask *task; std::string original_url; }; @@ -43,14 +70,13 @@ platform::request_http(const std::string &url, std::function b NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@(url.c_str())] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - // Make sure we clear the shared_ptr to resolve the circular reference. We're referencing - // this shared_ptr by value so that the object stays around until this completion handler is - // invoked. - req.reset(); - if ([error code] == NSURLErrorCancelled) { - // We intentionally cancelled this request. Do nothing. - [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil]; + // We intentionally cancelled this request. + // Make sure we clear the shared_ptr to resolve the circular reference. We're referencing + // this shared_ptr by value so that the object stays around until this completion handler is + // invoked. + req.reset(); + return; } @@ -68,16 +94,18 @@ platform::request_http(const std::string &url, std::function b background_function(&res); dispatch_async(dispatch_get_main_queue(), ^(void) { - foreground_callback(); + // Make sure we clear the shared_ptr to resolve the circular reference. We're referencing + // this shared_ptr by value so that the object stays around until this completion handler is + // invoked. + req.reset(); - [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil]; + foreground_callback(); }); }); }]; req = std::make_shared(task, url); [task resume]; - [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil]; return req; } diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index 2a0f64849..a951632f3 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -71,7 +71,6 @@ MBXViewController *view = nullptr; map->loadSettings(); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateRender:) name:MBXNeedsRenderNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateNetworkActivity:) name:MBXUpdateActivityNotification object:nil]; } @@ -159,14 +158,6 @@ MBXViewController *view = nullptr; self.paused = NO; } -- (void)updateNetworkActivity:(NSNotification *)notification -{ - [[NSURLSession sharedSession] getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) - { - [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:([downloadTasks count] > 0)]; - }]; -} - #pragma mark - Rendering delegates - (void)viewWillLayoutSubviews diff --git a/llmr.gyp b/llmr.gyp index 99bac358f..1f7e508ab 100644 --- a/llmr.gyp +++ b/llmr.gyp @@ -133,6 +133,7 @@ 'GCC_INPUT_FILETYPE':'sourcecode.cpp.cpp', 'OTHER_CPLUSPLUSFLAGS':[ '<@(png_cflags)', + '<@(uv_cflags)', '-I<(boost_root)/include', ] }, @@ -140,7 +141,8 @@ './include' ], 'cflags': [ - '<@(png_cflags)' + '<@(png_cflags)', + '<@(uv_cflags)', '-I<(boost_root)/include', ], 'direct_dependent_settings': { @@ -148,23 +150,27 @@ './include' ], 'cflags': [ - '<@(png_cflags)' + '<@(png_cflags)', + '<@(uv_cflags)', ], 'xcode_settings': { 'OTHER_CPLUSPLUSFLAGS':[ - '<@(png_cflags)' + '<@(png_cflags)', + '<@(uv_cflags)', ] }, 'conditions': [ ['OS == "mac"', { 'xcode_settings': { 'OTHER_LDFLAGS': [ - '<@(png_libraries)' + '<@(png_libraries)', + '<@(uv_libraries)', ] } }, { 'libraries': [ - '<@(png_libraries)' + '<@(png_libraries)', + '<@(uv_libraries)', ] }] ]