-
Notifications
You must be signed in to change notification settings - Fork 151
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
Connect enabledIf after init #170
Comments
Hmm, I haven't run into that issue. Instead of passing actions into my view models, I usually have the view models generate them from observables that are passed in as initializer parameters. Maybe I'm missing something from your question – anyone else have suggestions? |
Hello guys. Usually I use Action in this manner, maybe this will be helpful var login = BehaviorRelay(value: Optional<String>(""))
var password = BehaviorRelay(value: Optional<String>(""))
override init() {
super.init()
let isEnabled = Observable<Bool>.combineLatest(self.login.asObservable(), self.password.asObservable()) { (login, password) in
if String.stringIsBlank(login)
|| String.stringIsBlank(password) {
return false
} else {
return true
}
}
self.loginAction = Action(enabledIf: isEnabled,
workFactory: { [weak self] in
if let wCredManager = self?.credManager {
return wCredManager.loginObservable(login: self?.login.value ?? "",
password: self?.password.value ?? "",
server: self?.settingsManager?.host ?? "",
port: self?.settingsManager?.port ?? "")
.flatMap({ _ in
Observable<Void>.empty()
})
} else {
return Observable.error(ActionError.notEnabled)
}
})
} |
If you init the action in the viewModel you are forced to inject all the action dependencies into the viewModel. Being able to init the action and injecting it into the ViewModels allow you to move out all the dependencies out of the viewModel. It is then only responsible to connect ui to actions. In my app I've build a generic form which gather inputs and then call the save action. Depending of the flow I'd like to replace the action by another one. I'd rather inject an action then a work factory and its dependencies. |
Gotcha. I hear you – but that’s not how I’ve done mvvm. My view models have all the business logic, and the view controller hooks up the view model’s actions to its UI. Hope that context helps find a solution – let us know how it goes! |
Hello,
I've been looking into this lib and it's working nice. The only thing I can't manage is to bind the enabledIf observable after the action init. This would be useful to inject actions into viewModels. Right now providing the work factory and the enabledIf at the same time is very inconvenient.
Here is an example of my viewModel :
`final class ExampleViewModel {
}
extension ExampleViewModel {
struct Input {
let isValid: AnyObserver
let formValue: AnyObserver
let tap: AnyObserver
}
}`
Being able to bind enabledIf after the init would allow me to drop most of the subjects.
The text was updated successfully, but these errors were encountered: