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

Container tweaks #87

Merged
merged 3 commits into from
Dec 18, 2019
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

##### Enhancements

* None
* 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)

##### Bug Fixes

Expand Down
24 changes: 12 additions & 12 deletions Sources/UtiliKit/Container/ContainerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ open class ContainerViewController: UIViewController {
guard shouldAutomaticallyTransitionOnLoad, let initial = managedChildren.first else { return }
transition(to: initial.viewController)
}
}

//MARK: Public Interface
extension ContainerViewController {

open func transitionToControllerForChild(withIdentifier identifier: AnyHashable, completion: ((Bool) -> Void)? = nil) {
managedChildren.first { identifier == $0.identifier }.map { transitionToController(for: $0, completion: completion) }
// MARK: Public Interface
open func child(at index: Int) -> ManagedChild? {
guard index >= managedChildren.startIndex && index < managedChildren.endIndex else { return nil }
return managedChildren[index]
}

open func transitionToController(for child: ManagedChild, completion: ((Bool) -> Void)? = nil) {
open func addManagedChildIfNeeded(_ child: ManagedChild) {
if !managedChildren.contains { $0.viewController === child.viewController } {
managedChildren.insert(child, at: managedChildren.startIndex)
}
Expand All @@ -67,13 +65,15 @@ extension ContainerViewController {
managedChildren.removeAll { $0.identifier == child.identifier }
managedChildren.insert(child, at: managedChildren.startIndex)
}

transition(to: child.viewController, completion: completion)
}

open func child(at index: Int) -> ManagedChild? {
guard index >= managedChildren.startIndex && index < managedChildren.endIndex else { return nil }
return managedChildren[index]
open func transitionToControllerForChild(withIdentifier identifier: AnyHashable, completion: ((Bool) -> Void)? = nil) {
managedChildren.first { identifier == $0.identifier }.map { transitionToController(for: $0, completion: completion) }
}

open func transitionToController(for child: ManagedChild, completion: ((Bool) -> Void)? = nil) {
addManagedChildIfNeeded(child)
transition(to: child.viewController, completion: completion)
}
}

Expand Down