Skip to content

Commit

Permalink
👌 Extract completed state check
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed Jul 21, 2022
1 parent 2a3842b commit bb72a7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ extension ExperienceStateMachine {
)
case let (.endingStep(experience, currentIndex, _), .startStep(stepRef)):
return Transition.fromEndingStepToBeginningStep(experience, currentIndex, stepRef, traitComposer)
case let (.endingExperience(experience, stepIndex, markComplete), .reset):
case let (.endingExperience(experience, _, _), .reset):
var sideEffect: SideEffect?
if markComplete || stepIndex == experience.stepIndices.last {
if self.isExperienceCompleted {
sideEffect = .processActions(experience.postExperienceActions)
}
return Transition(toState: .idling, sideEffect: sideEffect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,29 @@ internal protocol ExperienceStateObserver: AnyObject {
func evaluateIfSatisfied(result: StateResult) -> Bool
}

@available(iOS 13.0, *)
extension ExperienceStateMachine.State {
var isExperienceCompleted: Bool {
switch self {
case let .endingExperience(experience, stepIndex, markComplete):
return markComplete || stepIndex == experience.stepIndices.last
default:
return false
}
}
}

@available(iOS 13.0, *)
extension Result where Success == ExperienceStateMachine.State, Failure == ExperienceStateMachine.ExperienceError {
var isExperienceCompleted: Bool {
switch self {
case let .success(state):
return state.isExperienceCompleted
default:
return false
}
}

/// Check if the result pertains to a specific experience ID.
func matches(instanceID: UUID?) -> Bool {
guard let instanceID = instanceID else { return true }
Expand Down Expand Up @@ -81,8 +102,8 @@ extension ExperienceStateMachine {
trackLifecycleEvent(.stepSeen, LifecycleEvent.properties(experience, stepIndex))
case let .success(.endingStep(experience, stepIndex, _)):
trackLifecycleEvent(.stepCompleted, LifecycleEvent.properties(experience, stepIndex))
case let .success(.endingExperience(experience, stepIndex, markComplete)):
if markComplete || stepIndex == experience.stepIndices.last {
case let .success(.endingExperience(experience, stepIndex, _)):
if result.isExperienceCompleted {
trackLifecycleEvent(.experienceCompleted, LifecycleEvent.properties(experience))
} else {
trackLifecycleEvent(.experienceDismissed, LifecycleEvent.properties(experience, stepIndex))
Expand Down

0 comments on commit bb72a7e

Please sign in to comment.