Skip to content

Commit

Permalink
👌 Simplify usage of scroll view delegate during recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
iujames committed Dec 6, 2023
1 parent dfcb17c commit c9b32d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal class StepRecoveryObserver: ExperienceStateObserver {
}

func stopRetryHandler() {
AppcuesScrollViewDelegate.shared.observer = nil
AppcuesScrollViewDelegate.shared.detach()
}

func evaluateIfSatisfied(result: ExperienceStateObserver.StateResult) -> Bool {
Expand All @@ -74,7 +74,7 @@ internal class StepRecoveryObserver: ExperienceStateObserver {
}

private func startRetryHandler() {
AppcuesScrollViewDelegate.shared.observer = self
AppcuesScrollViewDelegate.shared.attach(using: self)
}
}

Expand Down Expand Up @@ -106,9 +106,6 @@ internal class ExperienceRenderer: ExperienceRendering, StateMachineOwning {
self.stepRecoveryObserver = StepRecoveryObserver(stateMachine: stateMachine)

stateMachines[ownerFor: .modal] = self

// TODO: guard against multiple
UIScrollView.swizzleScrollViewGetDelegate()
}

func start(owner: StateMachineOwning, forContext context: RenderContext) {
Expand Down Expand Up @@ -170,9 +167,7 @@ internal class ExperienceRenderer: ExperienceRendering, StateMachineOwning {

private func show(experience: ExperienceData, completion: ((Result<Void, Error>) -> Void)?) {
guard Thread.isMainThread else {
DispatchQueue.main.async {
self.show(experience: experience, completion: completion)
}
DispatchQueue.main.async { self.show(experience: experience, completion: completion) }
return
}

Expand Down Expand Up @@ -376,9 +371,7 @@ internal class ExperienceRenderer: ExperienceRendering, StateMachineOwning {

func resetAll() {
guard Thread.isMainThread else {
DispatchQueue.main.async {
self.resetAll()
}
DispatchQueue.main.async { self.resetAll() }
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@ internal class AppcuesScrollViewDelegate: NSObject, UIScrollViewDelegate {
// This could have been a more sophisticated list of weak references to some Protocol implementation,
// but that would seem to add unnecessary complexity and list management, when really only a single
// RenderContext in the application (the modal context) can be in recovery mode at any given time.
weak var observer: StepRecoveryObserver?
private weak var observer: StepRecoveryObserver?

override private init() {
super.init()
UIScrollView.swizzleScrollViewGetDelegate()
}

func attach(using observer: StepRecoveryObserver) {
self.observer = observer
}

func detach() {
self.observer = nil
}

// if any scroll activity is currently active, cancel any pending scrollEnded notifications
// and wait for the next scroll completion to attempt any retry
Expand Down

0 comments on commit c9b32d0

Please sign in to comment.