iosdev

Coordinator updated, now with less Any

I have updated my Coordinator library, cleaning up the use of Any in both parent and childCoordinators properties. This is a breaking change from previous version, thus the version is now 4.0.

I have introduced Coordinating protocol which is very simple and both properties mentioned above are now of that type. The only purpose of those properties is to maintain coordinator hierarchy; for which I only need Coordinator’s identifier and its coordinatingResponder.

public protocol Coordinating: class {
	var identifier: String { get }
	var coordinatingResponder: UIResponder? { get }
}

Coordinator class naturally adopts this protocol:

open class Coordinator<T>: UIResponder, Coordinating {
	...
	open lazy var identifier: String = {
		return String(describing: type(of: self))
	}()

	/// Parent Coordinator can be any other Coordinator
	open weak var parent: Coordinating?

	///	A dictionary of child Coordinators, where key is Coordinator's identifier property
	fileprivate(set) public var childCoordinators: [String: Coordinating] = [:]

	open override var coordinatingResponder: UIResponder? {
		return parent as? UIResponder
	}
	...
}

I wasted way too much time trying to type-erase the Coordinator<T> before giving up and realizing I don’t need to do that at all. Goes to show that sometimes you need to stop and think back about the problem you actually need to solve.

start and stop methods also got simple default implementation that calls their completion blocks. Thus when you override them, you can now freely call super.start() and super.stop() at the end of your overrides.

The accompanying example app is updated to use the new library and everything seems to work just fine.