Skip to content

Feature/swiftui view #8

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 3 commits 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
2 changes: 2 additions & 0 deletions ReMVVMExt/Sources/Reducers/DismissModalReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public struct DismissModalReducer: Reducer {
public static func reduce(state: Navigation, with action: DismissModal) -> Navigation {

var modals = state.modals

guard !modals.isEmpty else { return Navigation(root: state.root, modals: modals) }
if action.dismissAllViews {
modals.removeAll()
} else {
Expand Down
4 changes: 2 additions & 2 deletions ReMVVMExt/Sources/Reducers/PopReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct PopReducer: Reducer {

private static func updateStateTree(_ stateTree: Navigation, for mode: PopMode) -> Navigation {
switch mode {
case .popToRoot:
case .popToRoot, .resetStack:
return popStateTree(stateTree, dropCount: stateTree.topStack.count - 1)
case .pop(let count):
return popStateTree(stateTree, dropCount: count)
Expand Down Expand Up @@ -69,7 +69,7 @@ public struct PopMiddleware<State: NavigationState>: Middleware {
// side effect

switch action.mode {
case .popToRoot:
case .popToRoot, .resetStack:
self.uiState.navigationController?.popToRootViewController(animated: action.animated)
case .pop(let count):
if count > 1 {
Expand Down
6 changes: 5 additions & 1 deletion ReMVVMExt/Sources/Reducers/PushReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public struct PushReducer: Reducer {
}

private static func updateStack(_ stack: [ViewModelFactory], for pop: PopMode?) -> [ViewModelFactory] {
guard let popMode = pop, stack.count > 1 else { return stack }
guard let popMode = pop, stack.count > 0 else { return stack }

switch popMode {
case .resetStack:
return []
case .pop(let count):
let dropCount = min(count, stack.count)
return Array(stack.dropLast(dropCount))
Expand Down Expand Up @@ -84,6 +86,8 @@ public struct PushMiddleware<State: NavigationState>: Middleware {
if let pop = action.pop {
var viewControllers = navigationController.viewControllers
switch pop {
case .resetStack:
viewControllers = []
case .popToRoot:
viewControllers = viewControllers.dropLast(viewControllers.count - 1)
case .pop(let count):
Expand Down
1 change: 0 additions & 1 deletion ReMVVMExt/Sources/Reducers/ShowOnRootReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public struct ShowOnRootMiddleware<State: NavigationState>: Middleware {

interceptor.next { _ in // newState - state variable is used below
// side effect

uiState.setRoot(controller: action.controllerInfo.loader.load(),
animated: action.controllerInfo.animated,
navigationBarHidden: action.navigationBarHidden)
Expand Down
122 changes: 102 additions & 20 deletions ReMVVMExt/Sources/StoreActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,92 @@
import Loaders
import ReMVVMCore
import RxSwift
import SwiftUI
import UIKit

public struct SynchronizeState: StoreAction {

public let type: SynchronizeType
public init(_ type: SynchronizeType) {
self.type = type
}

public enum SynchronizeType {
case navigation, modal
}
}

public struct ShowOnRoot: StoreAction {

public let controllerInfo: LoaderWithFactory
public let navigationBarHidden: Bool

public init(loader: Loader<UIViewController>,
factory: ViewModelFactory? = nil,
animated: Bool = true,
navigationBarHidden: Bool = true) {

self.controllerInfo = LoaderWithFactory(loader: loader,
factory: factory,
animated: animated)
self.navigationBarHidden = navigationBarHidden
}

@available(iOS 13.0, *)
public init<V>(view: V,
factory: ViewModelFactory? = nil,
animated: Bool = true,
navigationBarHidden: Bool = true) where V: View {

self.controllerInfo = LoaderWithFactory(view: view,
factory: factory,
animated: animated)
self.navigationBarHidden = navigationBarHidden

}
}

public struct Show: StoreAction {
public let controllerInfo: LoaderWithFactory
public let navigationBarHidden: Bool
public let item: AnyNavigationItem
let navigationType: NavigationType

public init<Item: CaseIterableNavigationItem>(on item: Item,
loader: Loader<UIViewController>,
factory: ViewModelFactory? = nil,
animated: Bool = true,
navigationBarHidden: Bool = true) {

loader: Loader<UIViewController>,
factory: ViewModelFactory? = nil,
animated: Bool = true,
navigationBarHidden: Bool = true) {
self.controllerInfo = LoaderWithFactory(loader: loader,
factory: factory,
animated: animated)
self.navigationBarHidden = navigationBarHidden
self.item = AnyNavigationItem(item)
self.navigationType = Item.navigationType
}

@available(iOS 13.0, *)
public init<V, Item: CaseIterableNavigationItem>(on item: Item,
view: V,
factory: ViewModelFactory? = nil,
animated: Bool = true,
navigationBarHidden: Bool = true) where V: View {

self.controllerInfo = LoaderWithFactory(view: view,
factory: factory,
animated: animated)
self.navigationBarHidden = navigationBarHidden
self.item = AnyNavigationItem(item)
self.navigationType = Item.navigationType
}
}

public struct Push: StoreAction {

public let controllerInfo: LoaderWithFactory
public let pop: PopMode?

public init(loader: Loader<UIViewController>,
factory: ViewModelFactory? = nil,
pop: PopMode? = nil,
Expand All @@ -74,10 +104,23 @@ public struct Push: StoreAction {
factory: factory,
animated: animated)
}

@available(iOS 13.0, *)
public init<V>(view: V,
factory: ViewModelFactory? = nil,
pop: PopMode? = nil,
animated: Bool = true) where V: View {
self.pop = pop
self.controllerInfo = LoaderWithFactory(view: view,
factory: factory,
animated: animated)
}

}

public enum PopMode {
case popToRoot, pop(Int)
case resetStack
}

public struct Pop: StoreAction {
Expand All @@ -90,21 +133,21 @@ public struct Pop: StoreAction {
}

public struct ShowModal: StoreAction {

public let controllerInfo: LoaderWithFactory
public let withNavigationController: Bool
public let showOverSplash: Bool
public let showOverSelfType: Bool
public let presentationStyle: UIModalPresentationStyle

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

self.controllerInfo = LoaderWithFactory(loader: loader,
factory: factory,
animated: animated)
Expand All @@ -113,28 +156,67 @@ public struct ShowModal: StoreAction {
self.showOverSelfType = showOverSelfType
self.presentationStyle = presentationStyle
}

@available(iOS 13.0, *)
public init<V>(view: V,
factory: ViewModelFactory? = nil,
animated: Bool = true,
withNavigationController: Bool = true,
showOverSplash: Bool = true,
showOverSelfType: Bool = true,
presentationStyle: UIModalPresentationStyle = .fullScreen,
clearBackground: Bool = false) where V: View {
self.controllerInfo = LoaderWithFactory(view: view,
factory: factory,
animated: animated,
clearBackground: clearBackground)
self.withNavigationController = withNavigationController
self.showOverSplash = showOverSplash
self.showOverSelfType = showOverSelfType
self.presentationStyle = presentationStyle
}
}

public struct DismissModal: StoreAction {

public let dismissAllViews: Bool
public let animated: Bool

public init(dismissAllViews: Bool = false, animated: Bool = true) {
self.dismissAllViews = dismissAllViews
self.animated = animated
}
}

public struct LoaderWithFactory {

public let loader: Loader<UIViewController>
public let factory: ViewModelFactory?
public let animated: Bool

public init(loader: Loader<UIViewController>, factory: ViewModelFactory?, animated: Bool = true) {

public init(loader: Loader<UIViewController>,
factory: ViewModelFactory?,
animated: Bool = true) {
self.loader = loader
self.factory = factory
self.animated = animated
}

@available(iOS 13.0, *)
public init<V>(view: V,
factory: ViewModelFactory?,
animated: Bool = true,
clearBackground: Bool = false) where V: View {

let hostLoader: Loader<UIViewController> = Loader {
let loader = Loader(view).load()
if clearBackground {
loader.view.backgroundColor = .clear
}
return loader

}

self.init(loader: hostLoader, factory: factory, animated: animated)
}
}