Skip to content

Commit

Permalink
♻️ Reset AppcuesFrameView on overridden render context
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed Aug 15, 2023
1 parent b074f69 commit 716e810
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ internal class ExperienceRenderer: ExperienceRendering, StateMachineOwning {
func start(owner: StateMachineOwning, forContext context: RenderContext) {
guard let container = appcues?.container else { return }

// If there's already a frame for the context, reset it back to its unregistered state.
if let existingFrameView = stateMachines[ownerFor: context] as? AppcuesFrameView {
existingFrameView.reset()
}

owner.stateMachine = ExperienceStateMachine(container: container)
stateMachines[ownerFor: context] = owner
if let pendingExperiences = potentiallyRenderableExperiences[context] {
Expand Down
10 changes: 10 additions & 0 deletions Sources/AppcuesKit/Presentation/UI/AppcuesFrameView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class AppcuesFrameView: UIView, StateMachineOwning {
}

private weak var parentViewController: UIViewController?
private weak var experienceViewController: UIViewController?

// when the view content is empty, we use this zero height constraint
private lazy var emptyHeightConstraint: NSLayoutConstraint = {
Expand Down Expand Up @@ -94,6 +95,7 @@ public class AppcuesFrameView: UIView, StateMachineOwning {
configureConstraints(isEmpty: false)

viewController.embedChildViewController(experienceController, inSuperview: self, margins: margins)
experienceViewController = experienceController

switch transition {
case .none:
Expand Down Expand Up @@ -134,4 +136,12 @@ public class AppcuesFrameView: UIView, StateMachineOwning {
)
}
}

/// Set the Frame back to an unregistered state.
internal func reset() {
if let experienceViewController = experienceViewController {
unembed(experienceViewController, transition: .none, completion: nil)
}
_stateMachine = nil
}
}

0 comments on commit 716e810

Please sign in to comment.