ScreenAction

public protocol ScreenAction

A protocol representing the navigation action that is performed in the screen container.

  • A type of container that the action uses for navigation.

    See also

    ScreenContainer

    Declaration

    Swift

    associatedtype Container : ScreenContainer
  • The type of value returned by the action.

    Declaration

    Swift

    associatedtype Output
  • Alias for the closure that is called after the action is completed.

    Declaration

    Swift

    typealias Completion = (Result<Output, Error>) -> Void
  • cast(to:) Default implementation

    Casts the instance to the specified type if it’s possible to do so.

    This method is useful for casting instances of the AnyScreenAction type, because the standard as and is operators are unusable for them. For example, you can’t do something like this:

    let anyAction = ScreenDismissAction<UIViewController>().eraseToAnyScreen()
    
    if let dismissAction = anyAction as? ScreenDismissAction<UIViewController> {
        print(dismissAction.animated)
    }
    

    The compiler will issue the following warning:

    Cast from 'AnyScreenAction<UIViewController, Void>'
    to unrelated type 'ScreenDismissAction<UIViewController>()' always fails
    

    In this case, you can use the cast(to:) method instead:

    if let dismissAction = anyAction.cast(to: ScreenDismissAction<UIViewController>.self) {
        print(dismissAction.animated)
    }
    

    Default implementation uses standard type casting. You don’t need to implement this method yourself.

    Default Implementation

    Declaration

    Swift

    func cast<Action>(to type: Action.Type) -> Action? where Action : ScreenAction

    Parameters

    type

    The type to which the instance will be cast.

    Return Value

    An optional value of the type you are trying to cast to.

  • combine(with:) Default implementation

    Сombines this action with another.

    Some navigation actions can be combined with others to form a single new action that does the same thing. For example, you need to push two view controllers into the navigation stack, this can be done by two calls to the pushViewController(_:animated:) method:

    navigationController.pushViewController(firstViewController, animated: true)
    navigationController.pushViewController(secondViewController, animated: true)
    

    But such a transition will be ugly. It is better to combine two push actions into single call to the setViewControllers(_:animated:) method:

    navigationController.setViewControllers([firstViewController, secondViewController], animated: true)
    

    You can implement combining such navigation actions in this method. Use the cast(to:) method to check the type of navigation action from the other parameter and return nil if the actions cannot be combined:

    guard let other = other.cast(to: SomeAction.self) else {
        return nil
    }
    

    Default implementation returns nil.

    See also

    cast(to:)

    Default Implementation

    Declaration

    Swift

    func combine<Action: ScreenAction>(
        with other: Action
    ) -> AnyScreenAction<Container, Void>?

    Parameters

    other

    Navigation action for combining.

    Return Value

    A new navigation action that is a combination of the source actions or nil if the combination is not possible.

  • Performs action in the screen container.

    See also

    ScreenNavigator

    Declaration

    Swift

    func perform(
        container: Container,
        navigator: ScreenNavigator,
        completion: @escaping Completion
    )

    Parameters

    container

    The screen container in which the navigation action is performed.

    navigator

    The navigator that can be used to perform an action.

    completion

    The closure that is called after the action is completed. This closure has no return value and takes the result of the navigation action.

  • eraseToAnyAction() Extension method

    Wraps this action with a type eraser.

    Use eraseToAnyAction() to expose an instance of AnyScreenAction, rather than this action’s actual type. This form of type erasure preserves abstraction across API boundaries, such as different modules. When you expose your actions as the AnyScreenAction type, you can change the underlying implementation over time without affecting existing clients.

    See also

    AnyScreenAction

    Declaration

    Swift

    public func eraseToAnyAction() -> AnyScreenAction<Container, Output>

    Return Value

    An AnyScreenAction wrapping this action.

  • eraseToAnyVoidAction() Extension method

    Wraps this action with a type eraser, ignoring the output type of the action.

    Use eraseToAnyVoidAction() to expose an instance of AnyScreenAction, rather than this action’s actual type. This form of type erasure preserves abstraction across API boundaries, such as different modules. When you expose your actions as the AnyScreenAction type, you can change the underlying implementation over time without affecting existing clients.

    See also

    AnyScreenAction

    Declaration

    Swift

    public func eraseToAnyVoidAction() -> AnyScreenAction<Container, Void>

    Return Value

    An AnyScreenAction wrapping this action.