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

Add postTransitionBehavior to ContainerViewController #88

Merged
merged 4 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

* Make it possible to override functions in container, which aid in `ManagedChild` managemet.
[Will McGinty](https://github.com/willmcginty)
[#87](https://github.com/BottleRocketStudios/iOS-UtiliKit/pull/82)
[#87](https://github.com/BottleRocketStudios/iOS-UtiliKit/pull/87)

* Add `postTransitionBehavior` to `ContainerViewController`, automating some common child management scenarios
[Dimitar Milinski](https://github.com/dmilinski)
[#88](https://github.com/BottleRocketStudios/iOS-UtiliKit/pull/88)

##### Bug Fixes

Expand Down
24 changes: 24 additions & 0 deletions Sources/UtiliKit/Container/ContainerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import UIKit

open class ContainerViewController: UIViewController {

// MARK: Subtypes
public enum PostTransitionBehavior {
case removeAllNonVisibleChildrenExcept([AnyHashable])
Copy link
Contributor

@wmcginty wmcginty Jan 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also have a default or none case here and then make the property non-optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I was on the fence about this when I originally worked on it. It actually ends up identical when we handle it in a switch, since optionals have a .none case anyway lol

case none

static var removeAllNonVisibleChildren: PostTransitionBehavior { return .removeAllNonVisibleChildrenExcept([]) }
}

//MARK: Properties
open var managedChildren: [ManagedChild] = []
open var visibleIndex: Int? { return visibleController.flatMap(index(ofChild:)) }
Expand All @@ -26,6 +34,7 @@ open class ContainerViewController: UIViewController {
// MARK: Transitioning
private var containerTransitionContext: UIViewControllerContextTransitioning?
open var containerTransitionCoordinator: ContainerViewControllerTransitionCoordinator?
open var postTransitionBehavior: PostTransitionBehavior = .none

//MARK: Initializers
public convenience init(managedChildren: [ManagedChild], delegate: ContainerViewControllerDelegate? = nil) {
Expand Down Expand Up @@ -162,7 +171,22 @@ private extension ContainerViewController {
visibleController = source
}

switch postTransitionBehavior {
case .removeAllNonVisibleChildrenExcept(let identifiers):
removeAllNonVisibleChildren(except: identifiers)
case .none:
break
}

containerTransitionContext = nil
isTransitioning = false
}

func removeAllNonVisibleChildren(except identifiers: [AnyHashable]) {
managedChildren.forEach {
if $0.viewController != visibleChild?.viewController && !identifiers.contains($0.identifier) {
removeChild($0)
}
}
}
}
50 changes: 50 additions & 0 deletions Tests/ContainerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,54 @@ class ContainerTests: XCTestCase {

XCTAssertNil(container.child(at: children.count))
}

func test_Container_removeNonVisbleChildren() {
let children = [Child(identifier: "title", viewController: UIViewController()),
Child(identifier: "title2", viewController: UIViewController()),
Child(identifier: "title3", viewController: UIViewController()),
Child(identifier: "title4", viewController: UIViewController())]

let container = ContainerViewController(managedChildren: children)
container.loadViewIfNeeded()
container.postTransitionBehavior = .removeAllNonVisibleChildren

let transition = expectation(description: "waitForTransition")

container.transitionToControllerForChild(withIdentifier: "title3") { (_) in
transition.fulfill()
}

waitForExpectations(timeout: 1.0) { (_) in
XCTAssert(container.managedChildren.count == 1)
XCTAssert((container.managedChildren.first?.identifier as? String) == "title3")
}
}

func test_Container_removeNonVisbleChildrenExcept() {
let children = [Child(identifier: "title", viewController: UIViewController()),
Child(identifier: "title2", viewController: UIViewController()),
Child(identifier: "title3", viewController: UIViewController()),
Child(identifier: "title4", viewController: UIViewController())]

let container = ContainerViewController(managedChildren: children)
container.loadViewIfNeeded()
container.postTransitionBehavior = .removeAllNonVisibleChildrenExcept(["title2", "title4"])

let transition = expectation(description: "waitForTransition")

container.transitionToControllerForChild(withIdentifier: "title3") { (_) in
transition.fulfill()
}

waitForExpectations(timeout: 1.0) { (_) in
XCTAssert(container.managedChildren.count == 3)
let identifiers: [String] = container.managedChildren.compactMap {
return $0.identifier as? String
}

XCTAssert(identifiers.contains("title2"))
XCTAssert(identifiers.contains("title4"))
XCTAssert(identifiers.contains("title3"))
}
}
}