Created
April 12, 2016 02:10
-
-
Save agentk/bd4527db455f00712b2423ffdfa9a136 to your computer and use it in GitHub Desktop.
ReSwift Store and ViewModel Scaffolding
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
import ReSwift | |
import ReactiveKit | |
class ObservableStore<State: StateType>: Store<State> { | |
let observable: Observable<State> | |
var reducer: AnyReducer! | |
override var state: State! { | |
didSet { | |
observable.value = state | |
} | |
} | |
required init(reducer: AnyReducer, state: State, middleware: [Middleware]) { | |
observable = Observable(state) | |
super.init(reducer: reducer, state: state, middleware: middleware) | |
self.reducer = reducer | |
} | |
func map<U: Equatable>(selector: State -> U) -> Stream<U> { | |
return observable.map(selector) | |
} | |
func dispatchAction(action: Action) { | |
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
import UIKit | |
import FeatherweightRouter | |
func signInPresenter(store: AppStore) -> UIRouterDelegate { | |
return lazyCachedViewPresenter { | |
return SignInViewController( | |
SignInViewModel( | |
state: store.observable, | |
dispatch: store.dispatchAction)) | |
} | |
} |
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
import UIKit | |
import ReactiveKit | |
import ReactiveUIKit | |
protocol SignInViewInput { | |
var email: Observable<String> { get } | |
var password: Observable<String> { get } | |
} | |
protocol SignInViewOutput { | |
var updateEmail: (String) -> Void { get } | |
var updatePassword: (String) -> Void { get } | |
var didTapSignIn: () -> Void { get } | |
var didTapForgot: () -> Void { get } | |
} | |
class SignInViewController: UIViewController { | |
typealias InputOutput = protocol<SignInViewInput, SignInViewOutput> | |
let viewModel: InputOutput | |
} |
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
import ReSwift | |
import ReactiveKit | |
struct SignInViewModel: SignInViewController.InputOutput { | |
let email = Observable("") | |
var password = Observable("") | |
let updateEmail: (String) -> Void | |
let updatePassword: (String) -> Void | |
var didTapSignIn: () -> Void = {} | |
var didTapForgot: () -> Void = {} | |
init(state: ObservableState, dispatch: Action -> ()) { | |
state.map { $0.signin.email }.bindTo(email) | |
state.map { $0.signin.password }.bindTo(password) | |
updateEmail = { dispatch(SignInAction.SetEmail($0)) } | |
updatePassword = { dispatch(SignInAction.SetPassword($0)) } | |
didTapSignIn = { dispatch(SigninAction.AttemptSignin) } | |
didTapForgot = { dispatch(RouteAction.Set("forgot-password")) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment