From f89bad1c7a3515588931c578597a3d1cf37a6cc7 Mon Sep 17 00:00:00 2001 From: Scott Pfeil Date: Tue, 26 Mar 2024 11:07:10 -0400 Subject: [PATCH] Digital PCT265 story MVAPCT-48 - Loading overlay cleanup --- .../MVMCore/LoadHandling/MVMCoreLoadHandler.m | 23 +++-------------- ...VMCoreLoadRequestOperation+Extension.swift | 8 +++--- .../MVMCoreLoadRequestOperation.h | 6 +++++ .../MVMCoreLoadRequestOperation.m | 25 ++++++++++++++----- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m index 940084f..3446798 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m @@ -383,28 +383,9 @@ if (requestParameters.backgroundRequest) { return [self loadBackgroundRequest:requestParameters dataForPage:dataForPage delegateObject:delegateObject]; } else { - BOOL loadingOverlay = NO; - if (!requestParameters.noloadingOverlay && [self.blockingLoadQueue.operations indexOfObjectPassingTest:^BOOL(__kindof NSOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - if (!obj.isExecuting) { - [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:@"CACHEDFEED: T No executor"]]; - return NO; } - if (![obj isKindOfClass:[MVMCoreLoadRequestOperation class]]) { - [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:@"CACHEDFEED: T Not load request"]]; - return NO; } - [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:@"CACHEDFEED: T %@ %i",((MVMCoreLoadRequestOperation *)obj).loadObject.requestParameters.pageType,((MVMCoreLoadRequestOperation *)obj).loadObject.requestParameters.noloadingOverlay]]; - return ((MVMCoreLoadRequestOperation *)obj).requestParameters.noloadingOverlay; - }] != NSNotFound) { - loadingOverlay = YES; - [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:@"CACHEDFEED: We need a loading indicator"]]; - [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] startLoading]; - } MVMCoreLoadRequestOperation *loadOperation = [[MVMCoreLoadRequestOperation alloc] initWithRequestParameters:requestParameters dataForPage:dataForPage delegateObject:delegateObject backgroundLoad:NO]; loadOperation.identifier = requestParameters.identifier; - [loadOperation setCompletionBlock:^{ - if (loadingOverlay) { - [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:YES]; - } - }]; + [loadOperation startLoadingAnimationIfNeeded]; [self.blockingLoadQueue addOperation:loadOperation]; return loadOperation; } @@ -419,6 +400,8 @@ - (MVMCoreLoadRequestOperation *)loadObject:(nonnull MVMCoreLoadObject *)loadObject { MVMCoreLoadRequestOperation *loadOperation = [[MVMCoreLoadRequestOperation alloc] initWithLoadObject:loadObject backgroundLoad:NO]; + loadOperation.identifier = loadObject.requestParameters.identifier; + [loadOperation startLoadingAnimationIfNeeded]; [self.blockingLoadQueue addOperation:loadOperation]; return loadOperation; } diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation+Extension.swift b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation+Extension.swift index a663b94..002e7d4 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation+Extension.swift +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation+Extension.swift @@ -118,11 +118,9 @@ public extension MVMCoreLoadRequestOperation { // stop any loading animation we may have started if we are about to display cancellable = NavigationHandler.shared().onNavigation .filter { $0.0 == .willNavigate } - .sink { (event, operation) in - if navigationOperation == operation, - !self.backgroundLoad, - !(loadObject?.requestParameters?.noloadingOverlay ?? false) { - MVMCoreLoadingOverlayHandler.sharedLoadingOverlay()?.stopLoading(false) + .sink { [weak self] (event, operation) in + if navigationOperation == operation { + self?.stopLoadingAnimationIfNeeded() } } await NavigationHandler.shared().navigate(with: navigationOperation) diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.h b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.h index fe334b3..fccbe82 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.h +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.h @@ -42,6 +42,12 @@ // Initializes the operation with the load object, data for page, and mvm view controller to handle the loading with. Can be used for loading a screen without going to the cache or server. - (nullable instancetype)initWithLoadObject:(nullable MVMCoreLoadObject *)loadObject backgroundLoad:(BOOL)backgroundLoad; +/// Begins the loading animation if needed. +- (void)startLoadingAnimationIfNeeded; + +/// Ends the loading animation if needed. +- (void)stopLoadingAnimationIfNeeded; + /* Checks the cache for the data and calls the completion handler with any found data. * @param completionHandler The block that gets called with any fetched data. */ + (void)checkCacheForDataForRequest:(nonnull MVMCoreRequestParameters *)requestParameters completionHandler:(nonnull void (^)(NSDictionary * _Nullable pageFromCache, NSDictionary * _Nullable modulesFromCache))completionHandler; diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m index 7a7d0a9..75feac9 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m @@ -34,6 +34,8 @@ @property (nonatomic, readwrite) BOOL alertToShow; @property (strong, nonatomic, nullable) MVMCoreErrorObject *errorForAlertToShow; +@property (nonatomic, readwrite) BOOL loadingAnimationRunning; + @end @implementation MVMCoreLoadRequestOperation @@ -64,14 +66,13 @@ - (void)cancel { [super cancel]; [self.sessionTask cancel]; + [self stopLoadingAnimationIfNeeded]; } - (void)start { // Adds a loading overlay if necessary. - if (!self.backgroundLoad && !self.requestParameters.noloadingOverlay) { - [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] startLoading]; - } + [self startLoadingAnimationIfNeeded]; [super start]; } @@ -83,9 +84,7 @@ - (void)markAsFinished { // stop any loading animation we may have started - if (!self.backgroundLoad && !self.requestParameters.noloadingOverlay) { - [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:NO]; - } + [self stopLoadingAnimationIfNeeded]; MVMCoreLog(@"Load Operation finished for page type %@, background load %@", self.requestParameters.pageType, @(self.backgroundLoad)); [super markAsFinished]; @@ -205,6 +204,20 @@ #pragma mark - Load Functions +- (void)startLoadingAnimationIfNeeded { + if (self.loadingAnimationRunning) { return; } + if (self.backgroundLoad) { return; } + if (self.requestParameters.noloadingOverlay) { return; } + [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] startLoading]; + self.loadingAnimationRunning = YES; +} + +- (void)stopLoadingAnimationIfNeeded { + if (!self.loadingAnimationRunning) { return; } + [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:YES]; + self.loadingAnimationRunning = NO; +} + + (void)checkCacheForDataForRequest:(nonnull MVMCoreRequestParameters *)requestParameters completionHandler:(nonnull void (^)(NSDictionary * _Nullable pageFromCache, NSDictionary * _Nullable modulesFromCache))completionHandler { if (requestParameters.neverLoadFromCache) {