Skip to content

Commit

Permalink
1.0.2: added non-autoclosure lazy initializer for Presentables
Browse files Browse the repository at this point in the history
  • Loading branch information
nikans committed Jan 21, 2020
1 parent 861be4d commit 214a1b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Example/MonarchRouterExample/Presenters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,5 @@ func lazyNavigationRoutePresenter() -> RoutePresenterStack

func lazyPresenter(for endpoint: EndpointViewControllerId, router: ProvidesRouteDispatch) -> RoutePresenter
{
return RoutePresenter.lazyPresenter(buildEndpoint(endpoint, router: router))
return RoutePresenter.lazyPresenter(wrap: buildEndpoint(endpoint, router: router))
}
2 changes: 1 addition & 1 deletion MonarchRouter.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'MonarchRouter'
s.version = '1.0.1'
s.version = '1.0.2'
s.summary = 'A lightweight yet powerful state-based router written in Swift.'

s.description = <<-DESC
Expand Down
33 changes: 32 additions & 1 deletion Source/Presenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,38 @@ public struct RoutePresenter: RoutePresenterType, RoutePresenterCapableOfModalsP
/// - parameter unwind: Optional callback executed when the Presentable is no longer presented.
/// - returns: RoutePresenter
public static func lazyPresenter(
_ getPresentable: @escaping @autoclosure () -> (UIViewController),
_ getPresentable: @escaping () -> (UIViewController),
setParameters: ((_ parameters: RouteParameters, _ presentable: UIViewController) -> ())? = nil,
presentModal: ((_ modal: UIViewController, _ over: UIViewController) -> ())? = nil,
dismissModal: ((_ modal: UIViewController)->())? = nil,
unwind: ((_ presentable: UIViewController) -> ())? = nil
) -> RoutePresenter
{
weak var presentable: UIViewController? = nil

let maybeCachedPresentable: () -> (UIViewController) = {
if let cachedPresentable = presentable {
return cachedPresentable
}

let newPresentable = getPresentable()
presentable = newPresentable
return newPresentable
}
let presenter = RoutePresenter(getPresentable: maybeCachedPresentable, setParameters: setParameters, presentModal: presentModal, dismissModal: dismissModal, unwind: unwind)

return presenter
}

/// An autoclosure lazy wrapper around a Presenter creation function that wraps presenter scope, but the Presentable does not get created until invoked.
/// - parameter getPresentable: Autoclosure returning a Presentable object.
/// - parameter setParameters: Optional callback to configure a Presentable with given `RouteParameters`. Don't set if the Presentable conforms to `RouteParametrizedPresentable`.
/// - parameter presentModal: Optional callback to define modals presentation. Default behaviour if undefined.
/// - parameter dismissModal: Optional callback to define modals dismissal. Default behaviour if undefined.
/// - parameter unwind: Optional callback executed when the Presentable is no longer presented.
/// - returns: RoutePresenter
public static func lazyPresenter(
wrap getPresentable: @escaping @autoclosure () -> (UIViewController),
setParameters: ((_ parameters: RouteParameters, _ presentable: UIViewController) -> ())? = nil,
presentModal: ((_ modal: UIViewController, _ over: UIViewController) -> ())? = nil,
dismissModal: ((_ modal: UIViewController)->())? = nil,
Expand Down

0 comments on commit 214a1b1

Please sign in to comment.