From 90f9a0bcf57cd7508d5971dbbbecaf761ee29d71 Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Wed, 3 Jul 2024 18:39:28 -0400 Subject: [PATCH] Digital PCT265 defect CXTDT-579050: Code simplification and fixes of combine pipeline for page updates. --- .../Atomic/Organisms/Carousel/Carousel.swift | 11 ++++++++ .../BaseControllers/ViewController.swift | 26 +++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift b/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift index fc79b5bd..84124880 100644 --- a/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift +++ b/MVMCoreUI/Atomic/Organisms/Carousel/Carousel.swift @@ -285,6 +285,17 @@ open class Carousel: View { /// Registers the cells with the collection view func registerCells(with carouselModel: CarouselModel, delegateObject: MVMCoreUIDelegateObject?) { + for molecule in carouselModel.molecules { + if let info = getMoleculeInfo(with: molecule, delegateObject: delegateObject) { + collectionView.register(info.class, forCellWithReuseIdentifier: info.identifier) + } + } + let moleculeInfo = carouselModel.molecules.map { + getMoleculeInfo(with: $0, delegateObject: delegateObject) + } + // For each molecule with info, register it. + moleculeInfo.compactMap({ $0 }).forEach { collectionView.register($0.class, forCellWithReuseIdentifier: $0.identifier) } + for molecule in carouselModel.molecules { if let info = getMoleculeInfo(with: molecule, delegateObject: delegateObject) { collectionView.register(info.class, forCellWithReuseIdentifier: info.identifier) diff --git a/MVMCoreUI/BaseControllers/ViewController.swift b/MVMCoreUI/BaseControllers/ViewController.swift index 950b330e..a6c43b2d 100644 --- a/MVMCoreUI/BaseControllers/ViewController.swift +++ b/MVMCoreUI/BaseControllers/ViewController.swift @@ -69,27 +69,16 @@ import Combine observingForResponses = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: NotificationResponseLoaded)) .receive(on: self.pageUpdateQueue) // Background serial queue. - .map { [weak self] notification in - self?.pullUpdates(from: notification) ?? (nil, nil, nil) - } - .filter { (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) in - // Skip any non-updates. - (pageUpdates != nil && pageModel != nil) || (moduleUpdates != nil && moduleUpdates!.count > 0) + .compactMap { [weak self] notification in + self?.pullUpdates(from: notification) ?? nil } // Merge all page and module updates into one update event. - //.print("[Update pipe] merging") .scan((nil, nil, nil)) { accumulator, next in // Always take the latest page and the latest modules with same key. - return (next.0, next.1, next.2?.mergingRight(accumulator.2 ?? [:])) + return (next.0 ?? accumulator.0, next.1 ?? accumulator.1, next.2?.mergingRight(accumulator.2 ?? [:])) } - //.print("[Update pipe] into buffer") - // Hold onto the latest merged state until UI is ready for an update. Keep only the latest from scan. - .buffer(size: 1, prefetch: .byRequest, whenFull: .dropOldest) - //.print("[Update pipe] out of buffer") // Delay allowing the previous model update to settle before triggering a re-render. - .flatMap(maxPublishers: .max(1)) { buffer in - Just(buffer).delay(for: .seconds(0.1), scheduler: RunLoop.main) - } + .throttle(for: .seconds(0.05), scheduler: RunLoop.main, latest: true) .sink { [weak self] (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) in guard let self = self else { return } if let pageUpdates, pageModel != nil { @@ -108,7 +97,7 @@ import Combine self.observingForResponses = nil } - func pullUpdates(from notification: Notification) -> (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) { + func pullUpdates(from notification: Notification) -> (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?)? { // Get the page data. let pageUpdates = extractInterestedPageType(from: notification.userInfo?.optionalDictionaryForKey(KeyPageMap) ?? [:]) // Convert the page data into a new model. @@ -125,7 +114,10 @@ import Combine } // Get the module data. let moduleUpdates = extractInterestedModules(from: notification.userInfo?.optionalDictionaryForKey(KeyModuleMap) ?? [:]) - debugLog("Receiving page \(pageModel?.pageType ?? "none") & \(moduleUpdates?.keys.description ?? "none") modules from \((notification.userInfo?["MVMCoreLoadObject"] as? MVMCoreLoadObject)?.requestParameters?.url?.absoluteString ?? "")") + debugLog("Receiving page \(pageModel?.pageType ?? "none") & \(moduleUpdates?.keys.description ?? "none") modules from \((notification.userInfo?["MVMCoreLoadObject"] as? MVMCoreLoadObject)?.requestParameters?.url?.absoluteString ?? "")") + + guard (pageUpdates != nil && pageModel != nil) || (moduleUpdates != nil && moduleUpdates!.count > 0) else { return nil } + // Bundle the transformations. return (pageUpdates, pageModel, moduleUpdates) }