From 060b6b330348c6785426a70797d802d5a9117ad0 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Thu, 3 May 2018 14:18:18 -0400 Subject: [PATCH 1/7] Queueing alerts --- MVMCore/MVMCore.xcodeproj/project.pbxproj | 8 ++++ MVMCore/MVMCore/MVMCore.h | 1 + .../MVMCoreNavigationHandler.h | 4 ++ .../MVMCoreNavigationHandler.m | 7 +++- .../MVMCore/Utility/MVMCoreBlockOperation.h | 16 ++++++++ .../MVMCore/Utility/MVMCoreBlockOperation.m | 37 +++++++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 MVMCore/MVMCore/Utility/MVMCoreBlockOperation.h create mode 100644 MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index 6845002..f1d48bc 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -123,6 +123,8 @@ AFBB96ED1FBA4A260008D868 /* MFHardCodedServerResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = AFBB96EA1FBA4A260008D868 /* MFHardCodedServerResponse.m */; }; AFC5FA161FFC2E2A00C244CF /* MVMCoreGlobalTopAlertDelegateProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = AFC5FA141FFC2E2A00C244CF /* MVMCoreGlobalTopAlertDelegateProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; AFC5FA1D1FFC39C700C244CF /* MVMCoreTopAlertViewProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = AFC5FA1C1FFC39C700C244CF /* MVMCoreTopAlertViewProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; + AFEA17A8209B6A1C00BC6740 /* MVMCoreBlockOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = AFEA17A6209B6A1C00BC6740 /* MVMCoreBlockOperation.h */; settings = {ATTRIBUTES = (Public, ); }; }; + AFEA17A9209B6A1C00BC6740 /* MVMCoreBlockOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = AFEA17A7209B6A1C00BC6740 /* MVMCoreBlockOperation.m */; }; AFED77A11FCCA29400BAE689 /* MVMCoreViewControllerNibMappingObject.h in Headers */ = {isa = PBXBuildFile; fileRef = AFED77991FCCA29300BAE689 /* MVMCoreViewControllerNibMappingObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; AFED77A21FCCA29400BAE689 /* MVMCoreViewControllerMappingObject.h in Headers */ = {isa = PBXBuildFile; fileRef = AFED779A1FCCA29300BAE689 /* MVMCoreViewControllerMappingObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; AFED77A31FCCA29400BAE689 /* MVMCoreViewControllerNibMappingObject.m in Sources */ = {isa = PBXBuildFile; fileRef = AFED779B1FCCA29300BAE689 /* MVMCoreViewControllerNibMappingObject.m */; }; @@ -251,6 +253,8 @@ AFBB96EA1FBA4A260008D868 /* MFHardCodedServerResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MFHardCodedServerResponse.m; sourceTree = ""; }; AFC5FA141FFC2E2A00C244CF /* MVMCoreGlobalTopAlertDelegateProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVMCoreGlobalTopAlertDelegateProtocol.h; sourceTree = ""; }; AFC5FA1C1FFC39C700C244CF /* MVMCoreTopAlertViewProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVMCoreTopAlertViewProtocol.h; sourceTree = ""; }; + AFEA17A6209B6A1C00BC6740 /* MVMCoreBlockOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVMCoreBlockOperation.h; sourceTree = ""; }; + AFEA17A7209B6A1C00BC6740 /* MVMCoreBlockOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MVMCoreBlockOperation.m; sourceTree = ""; }; AFED77991FCCA29300BAE689 /* MVMCoreViewControllerNibMappingObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MVMCoreViewControllerNibMappingObject.h; sourceTree = ""; }; AFED779A1FCCA29300BAE689 /* MVMCoreViewControllerMappingObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MVMCoreViewControllerMappingObject.h; sourceTree = ""; }; AFED779B1FCCA29300BAE689 /* MVMCoreViewControllerNibMappingObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MVMCoreViewControllerNibMappingObject.m; sourceTree = ""; }; @@ -327,6 +331,8 @@ 881D268F1FCC9D180079C521 /* MVMCoreErrorObject.m */, 881D26921FCC9D180079C521 /* MVMCoreOperation.h */, 881D26901FCC9D180079C521 /* MVMCoreOperation.m */, + AFEA17A6209B6A1C00BC6740 /* MVMCoreBlockOperation.h */, + AFEA17A7209B6A1C00BC6740 /* MVMCoreBlockOperation.m */, AFBB96E71FBA4A260008D868 /* HardCodedServerResponse */, AFBB96AB1FBA3B590008D868 /* Helpers */, ); @@ -647,6 +653,7 @@ AFBB968B1FBA3A9A0008D868 /* MVMCoreDismissViewControllerOperation.h in Headers */, AF43A70B1FC4F415008E9347 /* MVMCoreCache.h in Headers */, AFBB965C1FBA3A570008D868 /* MFFreebeeHandler.h in Headers */, + AFEA17A8209B6A1C00BC6740 /* MVMCoreBlockOperation.h in Headers */, AF43A5831FBB66DE008E9347 /* MVMCoreConstants.h in Headers */, AFBB969A1FBA3A9A0008D868 /* MVMCoreAlertDelegateProtocol.h in Headers */, AFED77A81FCCA29400BAE689 /* MVMCoreViewControllerStoryBoardMappingObject.h in Headers */, @@ -795,6 +802,7 @@ AF43A7071FC4D7A2008E9347 /* MVMCoreObject.m in Sources */, AFBB96B11FBA3B590008D868 /* MVMCoreDispatchUtility.m in Sources */, AFBB965B1FBA3A570008D868 /* FreeBeeUrlObject.m in Sources */, + AFEA17A9209B6A1C00BC6740 /* MVMCoreBlockOperation.m in Sources */, AF43A70A1FC4F415008E9347 /* MVMCoreCache.m in Sources */, AF43A6FF1FBE3252008E9347 /* Reachability.m in Sources */, AFBB96921FBA3A9A0008D868 /* MVMCoreNavigationOperation.m in Sources */, diff --git a/MVMCore/MVMCore/MVMCore.h b/MVMCore/MVMCore/MVMCore.h index 923a1bc..e3402cf 100644 --- a/MVMCore/MVMCore/MVMCore.h +++ b/MVMCore/MVMCore/MVMCore.h @@ -35,6 +35,7 @@ FOUNDATION_EXPORT const unsigned char MVMCoreVersionString[]; #import #import #import +#import // Mapping #import diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h index a3e5bcd..32789d0 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h @@ -10,6 +10,7 @@ #import #import +@class MVMCoreOperation; @class MVMCoreNavigationObject; @class MVMCoreLoadObject; @@ -67,6 +68,9 @@ // Use this to pop to the root of the stack. - (void)popToRootAnimated:(BOOL)animated; +// Adds the navigation operation to the queue. +- (void)addNavigationOperation:(nonnull MVMCoreOperation *)operation; + // Removes all queued up items. - (void)cancelNavigation; diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m index 727d97b..4f89c71 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m @@ -165,7 +165,12 @@ - (void)popToRootAnimated:(BOOL)animated { MVMCoreNavigationObject *navigationObject = [[MVMCoreNavigationObject alloc] initWithViewController:nil navigationController:nil viewControllers:nil animated:animated tryToReplaceFirst:NO navigationType:NavigationTypePopToRoot]; - [self startNavigationWithNavigationObject:navigationObject delegate:nil completionHandler:NULL];} + [self startNavigationWithNavigationObject:navigationObject delegate:nil completionHandler:NULL]; +} + +- (void)addNavigationOperation:(nonnull MVMCoreOperation *)operation { + [self.navigationQueue addOperation:operation]; +} - (void)cancelNavigation { [self.navigationQueue cancelAllOperations]; diff --git a/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.h b/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.h new file mode 100644 index 0000000..5096a20 --- /dev/null +++ b/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.h @@ -0,0 +1,16 @@ +// +// MVMCoreBlockOperation.h +// MVMCore +// +// Created by Pfeil, Scott Robert on 5/3/18. +// Copyright © 2018 myverizon. All rights reserved. +// + +#import + +@interface MVMCoreBlockOperation : MVMCoreOperation + +// Start this operation with the block. This block needs to call mark as finished when finished. ++ (nullable instancetype)blockOperationWithBlock:(nonnull void (^)(MVMCoreBlockOperation * _Nonnull operation))block; + +@end diff --git a/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m b/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m new file mode 100644 index 0000000..85a223a --- /dev/null +++ b/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m @@ -0,0 +1,37 @@ +// +// MVMCoreBlockOperation.m +// MVMCore +// +// Created by Pfeil, Scott Robert on 5/3/18. +// Copyright © 2018 myverizon. All rights reserved. +// + +#import "MVMCoreBlockOperation.h" + +@interface MVMCoreBlockOperation () + +@property (nonatomic, copy) void (^block)(MVMCoreBlockOperation * _Nonnull operation); + +@end + +@implementation MVMCoreBlockOperation + ++ (nullable instancetype)blockOperationWithBlock:(nonnull void (^)(MVMCoreBlockOperation * _Nonnull operation))block { + MVMCoreBlockOperation *operation = [[MVMCoreBlockOperation alloc] init]; + operation.block = block; + return operation; +} + + +- (void)main { + + // Always check for cancellation before launching the task. + if ([self checkAndHandleForCancellation]) { + return; + } + + __weak typeof(self) weakSelf = self; + self.block(weakSelf); +} + +@end From 50fb716f99215b586b471b5feea11ff481117783 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 7 May 2018 15:48:59 -0400 Subject: [PATCH 2/7] Top alert queue fix --- .../MVMCore/PresentationHandling/MVMCoreNavigationHandler.m | 5 +++++ .../MVMCore/PresentationHandling/MVMCoreNavigationObject.h | 1 + .../PresentationHandling/MVMCoreNavigationOperation.m | 6 +++++- MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m | 1 - 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m index 4f89c71..b6aa0aa 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m @@ -16,6 +16,7 @@ #import "MVMCoreRequestParameters.h" #import "MVMCoreErrorConstants.h" #import "MVMCoreLoggingHandler.h" +#import "MVMCoreLoadingOverlayHandler.h" @interface MVMCoreNavigationHandler () @@ -90,6 +91,10 @@ - (void)startNavigationWithNavigationObject:(MVMCoreNavigationObject *)navigationObject delegate:(nullable NSObject*)delegate completionHandler:(nullable void (^)(void))completionBlock { + // In case it takes some time to happen. + [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] startLoading]; + navigationObject.stopLoadingOverlay = YES; + MVMCoreNavigationOperation *navigationOperation = [[MVMCoreNavigationOperation alloc] initWithNavigationObject:navigationObject]; navigationOperation.delegate = delegate; navigationOperation.completionBlock = completionBlock; diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationObject.h b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationObject.h index 71e7c72..527110f 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationObject.h +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationObject.h @@ -20,6 +20,7 @@ @property (nonatomic) NavigationType navigationType; @property (nonatomic) BOOL animated; @property (nonatomic) BOOL tryToReplaceFirst; +@property (nonatomic) BOOL stopLoadingOverlay; - (nullable instancetype)initWithViewController:(nullable UIViewController *)viewController navigationController:(nullable UINavigationController *)navigationController viewControllers:(nullable NSArray *)viewControllers animated:(BOOL)animated tryToReplaceFirst:(BOOL)tryToReplaceFirst navigationType:(NavigationType)navigationType; diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m index 45d8647..19f2743 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m @@ -11,6 +11,7 @@ #import "MVMCoreViewManagerProtocol.h" #import "MVMCoreViewControllerProtocol.h" #import "MVMCoreLoggingHandler.h" +#import "MVMCoreLoadingOverlayHandler.h" @interface MVMCoreNavigationOperation () @@ -209,7 +210,10 @@ } - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { - + if (self.navigationObject.stopLoadingOverlay) { + [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:YES]; + } + if (self.delegate && [self.delegate respondsToSelector:@selector(navigationController:willDisplayViewController:)]) { [self.delegate navigationController:navigationController willDisplayViewController:viewController]; } diff --git a/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m b/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m index 85a223a..f0fc8ce 100644 --- a/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m +++ b/MVMCore/MVMCore/Utility/MVMCoreBlockOperation.m @@ -22,7 +22,6 @@ return operation; } - - (void)main { // Always check for cancellation before launching the task. From d91e8a836540f772d0ad48a1649adaf06f61779b Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 7 May 2018 16:12:39 -0400 Subject: [PATCH 3/7] to catch all scenarios --- .../PresentationHandling/MVMCoreNavigationOperation.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m index 19f2743..e53f5b2 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m @@ -206,12 +206,17 @@ - (void)markAsFinished { self.navigationObject.navigationController.delegate = nil; + if (self.navigationObject.stopLoadingOverlay) { + [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:YES]; + self.navigationObject.stopLoadingOverlay = NO; + } [super markAsFinished]; } - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.navigationObject.stopLoadingOverlay) { [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:YES]; + self.navigationObject.stopLoadingOverlay = NO; } if (self.delegate && [self.delegate respondsToSelector:@selector(navigationController:willDisplayViewController:)]) { @@ -225,7 +230,6 @@ [self.delegate navigationController:navigationController displayedViewController:viewController]; } [self markAsFinished]; - } - (nullable id )navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { From 56c8a961eb1573091ea6da511a6aa24efd6a03c5 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Tue, 8 May 2018 11:26:32 -0400 Subject: [PATCH 4/7] fixing major navigation operation defect. dispatch after instead of nstimer --- .../AlertHandling/MVMCoreTopAlertOperation.m | 44 ++++--------------- .../MVMCoreNavigationOperation.m | 8 +++- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertOperation.m b/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertOperation.m index a4cbfb8..222a56e 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertOperation.m +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertOperation.m @@ -16,22 +16,16 @@ __block BOOL _paused; __block BOOL _displayed; __block BOOL _animating; - __block NSTimer *_dismissTimer; } @property (readwrite, getter=isPaused) BOOL paused; - @property (readwrite, getter=isDisplayed) BOOL displayed; - @property (readwrite, getter=isAnimating) BOOL animating; -@property (strong, nonatomic) NSTimer *dismissTimer; - // For thread safety @property (strong, nonatomic) dispatch_queue_t pausedQueue; @property (strong, nonatomic) dispatch_queue_t displayedQueue; @property (strong, nonatomic) dispatch_queue_t animatingQueue; -@property (strong, nonatomic) dispatch_queue_t timerQueue; @end @@ -44,7 +38,6 @@ self.pausedQueue = dispatch_queue_create("paused", DISPATCH_QUEUE_CONCURRENT); self.displayedQueue = dispatch_queue_create("displayed", DISPATCH_QUEUE_CONCURRENT); self.animatingQueue = dispatch_queue_create("animating", DISPATCH_QUEUE_CONCURRENT); - self.timerQueue = dispatch_queue_create("timer", DISPATCH_QUEUE_CONCURRENT); } return self; } @@ -102,20 +95,6 @@ }); } -- (NSTimer *)dismissTimer { - __block NSTimer *timer; - dispatch_sync(self.timerQueue, ^{ - timer = _dismissTimer; - }); - return timer; -} - -- (void)setDismissTimer:(NSTimer *)dismissTimer { - dispatch_barrier_async(self.timerQueue, ^{ - _dismissTimer = dismissTimer; - }); -} - - (void)main { // Always check for cancellation before launching the task. @@ -156,7 +135,14 @@ } else { dismissTime = TopAlertDismissTime; } - self.dismissTimer = [NSTimer scheduledTimerWithTimeInterval:dismissTime target:self selector:@selector(timerFinished) userInfo:nil repeats:NO]; + + __weak typeof(self) weakSelf = self; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(dismissTime * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + if (weakSelf.isFinished || [weakSelf checkAndHandleForCancellation]) { + return; + } + [weakSelf dismissAlertView:YES]; + }); } }]; } @@ -166,19 +152,9 @@ } } -- (void)timerFinished { - [self dismissAlertView:YES]; -} - - (void)cancel { [super cancel]; - // Kill the dismiss timer. - if (self.dismissTimer) { - [self.dismissTimer invalidate]; - self.dismissTimer = nil; - } - // Do nothing if animating. if (!self.isAnimating) { @@ -191,9 +167,7 @@ } - (void)dismissAlertView:(BOOL)andFinish { - - self.dismissTimer = nil; - + if (self.isDisplayed && !self.isAnimating) { // Dismisses. diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m index e53f5b2..a1fda67 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m @@ -134,6 +134,8 @@ { if (self.navigationObject.navigationController.viewControllers.count > 1) { [self.navigationObject.navigationController popViewControllerAnimated:self.navigationObject.animated]; + } else { + [self markAsFinished]; } } break; @@ -182,7 +184,11 @@ break; case NavigationTypePopToRoot: { - [self.navigationObject.navigationController popToRootViewControllerAnimated:self.navigationObject.animated]; + if (self.navigationObject.navigationController.viewControllers.count > 1) { + [self.navigationObject.navigationController popToRootViewControllerAnimated:self.navigationObject.animated]; + } else { + [self markAsFinished]; + } } break; default: From 1e7fb38f17bdae9e1c39925af80a489ce1c3e002 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 11:32:08 -0400 Subject: [PATCH 5/7] update freebee method --- .../LoadHandling/FreeBee/MFFreebeeHandler.h | 3 +- .../LoadHandling/FreeBee/MFFreebeeHandler.m | 46 +++++-------------- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 12 ++--- 3 files changed, 19 insertions(+), 42 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h index 9ab7393..fb148d5 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h @@ -44,8 +44,7 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe - (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url; - (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId; -- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; -- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL; +- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; - (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset; @end diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m index 698043b..e52684e 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m @@ -230,53 +230,31 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) { } #pragma mark FreeBee Helper for NSData -- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url { +- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *)url { NSData* data = nil; NSDictionary* proxyDict = [self proxyDictionaryforUrl:url]; + NSURLResponse* response = nil; + NSError* error = nil; if ([self isFreeBeeEnabled] && [self isValidCampaign] && proxyDict && ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { - MVMCoreLog(@"Free Data Url, %@", url); - NSURLResponse* response = nil; - NSError* error = nil; - data = [self sendSynchronousRequest:url returningResponse:&response error:&error]; - - if (error) - data = nil; - - MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); - } else { - data = [NSData dataWithContentsOfURL:url]; - } - return data; -} - -- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *)imageURL { - - NSData* data = nil; - NSDictionary* proxyDict = [self proxyDictionaryforUrl:imageURL]; - - if ([self isFreeBeeEnabled] && - [self isValidCampaign] && proxyDict && - ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { - - MVMCoreLog(@"Free Image Data Url, %@", imageURL); - NSURLResponse* response = nil; - NSError* error = nil; - data = [self sendSynchronousRequest:imageURL returningResponse:&response error:&error]; - + MVMCoreLog(@"Free Data Url, %@", url); + data = [self sendSynchronousRequest:url returningResponse:&response error:&error]; if (error) { data = nil; } + MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); - MVMCoreLog(@"freebee_dataWithImageURL:Free Image Data, %lu", (unsigned long)data.length); - NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; - return cachedResponse; + } else { + //create response for identify + response = [[NSURLResponse alloc] initWithURL:url MIMEType:nil expectedContentLength:100 textEncodingName:nil]; + data = [NSData dataWithContentsOfURL:url]; } - return nil; + NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; + return cachedResponse; } #pragma mark - FreeBee Registration diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 45e9603..aea1f46 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -459,11 +459,12 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *imageData = nil; if (pathOrName.length > 0) { - if(pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { + if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { // Gets the full path NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO]; if (s7URL) { [self downloadImage:s7URL isGif:YES fallbackImageName:fallbackImageName completionHandler:completionHandler]; + return; } } else { // Try Local. Check in memory cache @@ -514,16 +515,15 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } //download image -- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ +- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { + NSURLCache *sharedCache = [NSURLCache sharedURLCache]; - NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; - configure.URLCache = sharedCache; - NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; + NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut]; //use Freebee to download image first, then store image to cache NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request]; if (!cachedResponse.data) { - cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL]; + cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL]; if (cachedResponse) { //system stores cache based datatask and check cache based on response, //need to manually store cache for response From ebb5303711c8899b78e5dea8d5570f4823f84988 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 11:39:09 -0400 Subject: [PATCH 6/7] fix typo --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index aea1f46..68f2f00 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -533,10 +533,10 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; //call nsurlsession again, if image is cached by Freebee, the second session will grab image data from cache instead of making another server call NSURLSessionDownloadTask *downloadImageTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSData *data = [NSData dataWithContentsOfURL:location]; - NSCachedURLResponse *cachedreponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; + NSCachedURLResponse *cachedReponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; //system stores cache based datatask and check cache based on response, //need to manually store cache for response - [sharedCache storeCachedResponse:cachedreponse forRequest:request]; + [sharedCache storeCachedResponse:cachedReponse forRequest:request]; if (isGif) { [self checkImage:nil imageData:data fallbackImage:fallbackImageName completionHandler:completionHandler]; } else { @@ -544,12 +544,11 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; //check fallback image [self checkImage:image imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler]; } - }]; [downloadImageTask resume]; } -//check whehter get image or imageData, if not, return fallbackImage +//check whether get image or imageData, if not, return fallbackImage - (void)checkImage:(UIImage *)img imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ // Set to the fallback image. BOOL isFallBackImage = NO; From 995b3119ba42613483b685bb6b82471a82a038eb Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 11:46:48 -0400 Subject: [PATCH 7/7] revert freebee changes --- .../LoadHandling/FreeBee/MFFreebeeHandler.h | 3 +- .../LoadHandling/FreeBee/MFFreebeeHandler.m | 44 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h index fb148d5..9ab7393 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h @@ -44,7 +44,8 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe - (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url; - (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId; -- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; +- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; +- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL; - (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset; @end diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m index e52684e..0b6c153 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m @@ -230,31 +230,53 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) { } #pragma mark FreeBee Helper for NSData -- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *)url { +- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url { NSData* data = nil; NSDictionary* proxyDict = [self proxyDictionaryforUrl:url]; - NSURLResponse* response = nil; - NSError* error = nil; if ([self isFreeBeeEnabled] && [self isValidCampaign] && proxyDict && ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { - + MVMCoreLog(@"Free Data Url, %@", url); + NSURLResponse* response = nil; + NSError* error = nil; data = [self sendSynchronousRequest:url returningResponse:&response error:&error]; + + if (error) + data = nil; + + MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); + } else { + data = [NSData dataWithContentsOfURL:url]; + } + return data; +} + +- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *)imageURL { + + NSData* data = nil; + NSDictionary* proxyDict = [self proxyDictionaryforUrl:imageURL]; + + if ([self isFreeBeeEnabled] && + [self isValidCampaign] && proxyDict && + ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { + + MVMCoreLog(@"Free Image Data Url, %@", imageURL); + NSURLResponse* response = nil; + NSError* error = nil; + data = [self sendSynchronousRequest:imageURL returningResponse:&response error:&error]; + if (error) { data = nil; } - MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); - } else { - //create response for identify - response = [[NSURLResponse alloc] initWithURL:url MIMEType:nil expectedContentLength:100 textEncodingName:nil]; - data = [NSData dataWithContentsOfURL:url]; + MVMCoreLog(@"freebee_dataWithImageURL:Free Image Data, %lu", (unsigned long)data.length); + NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; + return cachedResponse; } - NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; - return cachedResponse; + return nil; } #pragma mark - FreeBee Registration