Skip to content

Added completion to DismissModal #11

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion ReMVVMExt/Sources/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import ReMVVMCore

public protocol NavigationState: StoreState {

var navigation: Navigation { get }
}

Expand Down
4 changes: 2 additions & 2 deletions ReMVVMExt/Sources/Reducers/DismissModalReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public struct DismissModalMiddleware<State: NavigationState>: Middleware {

//dismiss not needed modals
if action.dismissAllViews {
uiState.dismissAll(animated: action.animated)
uiState.dismissAll(animated: action.animated, completion: action.completion)
} else {
uiState.dismiss(animated: action.animated)
uiState.dismiss(animated: action.animated, completion: action.completion)
}
}

Expand Down
3 changes: 2 additions & 1 deletion ReMVVMExt/Sources/Reducers/PushReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public struct PushMiddleware<State: NavigationState>: Middleware {

//dismiss not needed modals
uiState.dismiss(animated: action.controllerInfo.animated,
number: uiState.modalControllers.count - state.navigation.modals.count)
number: uiState.modalControllers.count - state.navigation.modals.count,
completion: nil)

guard let navigationController = uiState.navigationController else {
assertionFailure("PushMiddleware: No navigation Controller")
Expand Down
5 changes: 3 additions & 2 deletions ReMVVMExt/Sources/Reducers/ShowModalReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public struct ShowModalMiddleware<State: NavigationState>: Middleware {

//dismiss not needed modals
uiState.dismiss(animated: action.controllerInfo.animated,
number: uiState.modalControllers.count - state.navigation.modals.count + 1)
number: uiState.modalControllers.count - state.navigation.modals.count + 1,
completion: nil)

let newModal: UIViewController
if action.withNavigationController {
Expand All @@ -70,7 +71,7 @@ public struct ShowModalMiddleware<State: NavigationState>: Middleware {
}

newModal.modalPresentationStyle = action.presentationStyle
uiState.present(newModal, animated: action.controllerInfo.animated)
uiState.present(newModal, animated: action.controllerInfo.animated, completion: action.completion)
}
}
}
9 changes: 7 additions & 2 deletions ReMVVMExt/Sources/StoreActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ public struct ShowModal: StoreAction {
public let showOverSplash: Bool
public let showOverSelfType: Bool
public let presentationStyle: UIModalPresentationStyle
public let completion: (() -> Void)?

public init(loader: Loader<UIViewController>,
factory: ViewModelFactory? = nil,
animated: Bool = true,
withNavigationController: Bool = true,
showOverSplash: Bool = true,
showOverSelfType: Bool = true,
presentationStyle: UIModalPresentationStyle = .fullScreen) {
presentationStyle: UIModalPresentationStyle = .fullScreen,
completion: (() -> Void)? = nil) {

self.controllerInfo = LoaderWithFactory(loader: loader,
factory: factory,
Expand All @@ -112,17 +114,20 @@ public struct ShowModal: StoreAction {
self.showOverSplash = showOverSplash
self.showOverSelfType = showOverSelfType
self.presentationStyle = presentationStyle
self.completion = completion
}
}

public struct DismissModal: StoreAction {

public let dismissAllViews: Bool
public let animated: Bool
public let completion: (() -> Void)?

public init(dismissAllViews: Bool = false, animated: Bool = true) {
public init(dismissAllViews: Bool = false, animated: Bool = true, completion: (() -> Void)? = nil) {
self.dismissAllViews = dismissAllViews
self.animated = animated
self.completion = completion
}
}

Expand Down
15 changes: 10 additions & 5 deletions ReMVVMExt/Sources/UIState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,28 @@ public final class UIState {
return modalControllers.last ?? rootViewController
}

public func present(_ viewController: UIViewController, animated: Bool) {
public func present(_ viewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
topPresenter.present(viewController, animated: animated, completion: { [topPresenter] in
topPresenter.setNeedsStatusBarAppearanceUpdate()
completion?()
})
modalControllers.append(viewController)
}

public func dismissAll(animated: Bool) {
dismiss(animated: animated, number: Int.max)
public func dismissAll(animated: Bool, completion: (() -> Void)?) {
dismiss(animated: animated, number: Int.max, completion: completion)
}

public func dismiss(animated: Bool, number: Int = 1) {
public func dismiss(animated: Bool, number: Int = 1, completion: (() -> Void)?) {
let number = modalControllers.count >= number ? number : modalControllers.count
guard number > 0 else { return }
guard number > 0 else {
completion?()
return
}
modalControllers.removeLast(number)
topPresenter.dismiss(animated: animated, completion: { [topPresenter] in
topPresenter.setNeedsStatusBarAppearanceUpdate()
completion?()
})
}
}