Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breaking change] Update propagation of stack action #168

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Sources/FluidStack/Helper/UIViewController+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,23 @@ extension UIViewController {
func propagateStackAction(_ action: FluidStackAction) {

func _propagateRecursively(viewController: UIViewController) {

guard (viewController is FluidStackController) == false else {

if let stackController = viewController as? FluidStackController {

for handler in stackController.fluidStackActionHandlers {
handler(action)
}

stackController.topViewController?.propagateStackAction(action)

return
}

// call all of handlers
viewController.fluidStackActionHandlers.forEach {
$0(action)
for handler in viewController.fluidStackActionHandlers {
handler(action)
}

// propagates to children
for viewController in viewController.children {
// recursive
Expand Down
13 changes: 4 additions & 9 deletions Sources/FluidStack/ViewController/FluidStackController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import FluidPortal
/// Actions that comes from ``FluidStackController``
public enum FluidStackAction {

/// on started push operation in the stack
/// dispatches after viewDidLoad, added in hierarchy, before transitioning
/// It will appear on a stack.
/// Emits before the transition starts but after viewDidLoad and inserted into the stack.
case willPush

/// Potentially it won't be emmited after ``FluidStackAction/willPush``
Expand All @@ -18,10 +18,6 @@ public enum FluidStackAction {

/// Potentially it won't be emmited after ``FluidStackAction/willPop``
case didPop

/// will become currenty top view controller on the stack.
/// that happens on push and pop higher view controller.
case willBecomeTop
}

/// A struct that configures how to display in ``FluidStackController``
Expand Down Expand Up @@ -304,8 +300,7 @@ open class FluidStackController: UIViewController {

// propagate after `viewDidLoad`
viewControllerToAdd.propagateStackAction(.willPush)
viewControllerToAdd.propagateStackAction(.willBecomeTop)


// take before modifying.
let currentTop = stackingItems.last

Expand Down Expand Up @@ -462,7 +457,7 @@ open class FluidStackController: UIViewController {
}()

platterView.viewController.propagateStackAction(.willPop)
backView?.viewController.propagateStackAction(.willBecomeTop)
backView?.viewController.propagateStackAction(.willPush)

let newTransitionContext = RemovingTransitionContext(
contentView: platterView,
Expand Down
17 changes: 10 additions & 7 deletions Tests/FluidStackTests/FluidStackControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,15 @@ final class FluidStackControllerTests: XCTestCase {

let childStack = FluidStackController(configuration: .init(retainsRootViewController: false))

let childExp = expectation(description: "child")

childStack.addFluidStackActionHandler { action in
XCTFail("Never gets any actions on child")

if case .willPush = action {
// child will get an event that happens in the higher layer.
childExp.fulfill()
}

}

let expWillPop = expectation(description: "called")
Expand All @@ -466,8 +473,6 @@ final class FluidStackControllerTests: XCTestCase {
break
case .didPop:
expDidPop.fulfill()
case .willBecomeTop:
break
}
}

Expand All @@ -481,7 +486,7 @@ final class FluidStackControllerTests: XCTestCase {

stack.topViewController?.fluidPop()

wait(for: [expWillPop, expDidPop], timeout: 1)
wait(for: [expWillPop, expDidPop, childExp], timeout: 1)
}

func testPropagationActions_willBecomeTop() {
Expand All @@ -495,15 +500,13 @@ final class FluidStackControllerTests: XCTestCase {
controller_1.addFluidStackActionHandler { action in
switch action {
case .willPush:
break
expWillBecomeTop.fulfill()
case .willPop:
break
case .didPush:
break
case .didPop:
break
case .willBecomeTop:
expWillBecomeTop.fulfill()
}
}

Expand Down
Loading