diff --git a/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift b/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift index 9cbf85ed..8b624a7b 100644 --- a/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift +++ b/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift @@ -20,7 +20,7 @@ public class ReplaceableMoleculeBehaviorModel: PageBehaviorModelProtocol { } } -public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, CoreLogging { +public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, PageVisibilityBehavior, CoreLogging { public var loggingPrefix: String { "\(self) \(ObjectIdentifier(self).hashValue) \(moleculeIds.prefix(3)) \(moleculeIds.count > 3 ? "+ \(moleculeIds.count - 3) more" : ""):\n" @@ -30,6 +30,8 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, Co String(describing: Self.self) } + var hasPreviouslyBeenShown = false + var previouslyReplacedIds = Set() var moleculeIds: [String] public var modulesToListenFor: [String] private var observingForResponses: NSObjectProtocol? @@ -75,7 +77,7 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, Co return rootMolecule } debugLog("top replacing \(rootMolecule) with \(updatedMolecule)") - logUpdated(molecule: updatedMolecule) + logUpdated(moleculeId: updatedMolecule.id) changeList.append(updatedMolecule) return updatedMolecule } @@ -92,7 +94,7 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, Co return } debugLog("deep replacing \(replacedMolecule) with \(newMolecule)") - logUpdated(molecule: newMolecule) + logUpdated(moleculeId: newMolecule.id) changeList.append(newMolecule) } } catch { @@ -111,15 +113,28 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, Co return hasReplacement ? updatedRootMolecules : nil } - private func logUpdated(molecule: MoleculeModelProtocol) { - guard let module: [AnyHashable: Any] = delegateObject?.moleculeDelegate?.getModuleWithName(molecule.id), + private func logUpdated(moleculeId: String) { + guard let module: [AnyHashable: Any] = delegateObject?.moleculeDelegate?.getModuleWithName(moleculeId), let viewController = delegateObject?.moleculeDelegate as? MVMCoreViewControllerProtocol else { - debugLog("Missing the originating module \(molecule.id) creating this molecule!") + debugLog("Missing the originating module \(moleculeId) creating this molecule!") return } + previouslyReplacedIds.insert(moleculeId) MVMCoreUILoggingHandler.shared()?.defaultLogPageUpdate(forController: viewController, from: module) } + public func onPageShown(_ delegateObject: MVMCoreUIDelegateObject?) { + guard hasPreviouslyBeenShown else { + // Skip first replacement build handled in onPageNew. (Pulled and reported from cache.) + hasPreviouslyBeenShown = true + return + } + debugLog("Page reshown. Resend replaced molecule analytics for: \(previouslyReplacedIds)") + previouslyReplacedIds.forEach { id in + logUpdated(moleculeId: id) + } + } + deinit { debugLog("deinit") }