-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ConnectedView, StoreConnector and StoreProvider
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Thomas Ricouard on 23/07/2019. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public protocol ConnectedView: View { | ||
associatedtype State: FluxState | ||
associatedtype Props | ||
associatedtype V: View | ||
|
||
func map(state: State, dispatch: @escaping (Action) -> Void) -> Props | ||
func body(props: Props) -> V | ||
} | ||
|
||
public extension ConnectedView { | ||
func render(state: State, dispatch: @escaping (Action) -> Void) -> V { | ||
let props = map(state: state, dispatch: dispatch) | ||
return body(props: props) | ||
} | ||
|
||
var body: StoreConnector<State, V> { | ||
return StoreConnector(content: render) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Thomas Ricouard on 23/07/2019. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct StoreConnector<State: FluxState, V: View>: View { | ||
@EnvironmentObject var store: Store<State> | ||
let content: (State, @escaping (Action) -> Void) -> V | ||
|
||
public var body: V { | ||
content(store.state, store.dispatch(action:)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Thomas Ricouard on 23/07/2019. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct StoreProvider<S: FluxState, V: View>: View { | ||
public let store: Store<S> | ||
public let content: () -> V | ||
|
||
public init(store: Store<S>, content: @escaping () -> V) { | ||
self.store = store | ||
self.content = content | ||
} | ||
|
||
public var body: some View { | ||
content().environmentObject(store) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters