Commit bb76bf35 authored by Romain Bertozzi's avatar Romain Bertozzi

wizard: bind V to VM for create Ring account

This patch creates a basic binding between the view and the modelview
of the add account screen.

It is done by using RxSwift.

For the moment, the unique button of the screen is bound. The action
between the VM and the AccountService is still strongly link. This
will be changed in a next patch to use Rx instead.

Change-Id: Iebc4f49edd0de4a0ec1db70af854d0d6f98f336e
Tuleap: #1400
parent 06adf62e
......@@ -18,12 +18,48 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import UIKit
import RxSwift
/**
A structure representing the ViewModel (MVVM) of the accounts managed by Ring.
Its responsabilities:
- expose to the Views a public API for its interactions concerning the Accounts,
- react to the Views user events concerning the Accounts (add an account...)
*/
struct AccountViewModel {
fileprivate var account: AccountModel
/**
Dispose bag that contains the Disposable objects of the ViewModel, and managing their disposes.
*/
fileprivate let disposeBag = DisposeBag()
/**
Create the observers to the streams passed in parameters.
It will allow this ViewModel to react to other entities' events.
init (withAccount account: AccountModel) {
self.account = account
- Parameter observable: An observable stream to subscribe on.
Any observed event on this stream will trigger the action of creating an account.
- Parameter onStart: Closure that will be triggered when the action will begin.
- Parameter onError: Closure that will be triggered in case of error.
*/
func configureAddAccountObservers(observable: Observable<Void>,
onStart: ((() -> Void)?),
onSuccess: ((() -> Void)?),
onError: (((Error?) -> Void)?)) {
_ = observable
.subscribe(onNext: {
if onStart != nil {
onStart!()
}
AccountsService.sharedInstance.addAccount()
}, onError: { (error) in
if onError != nil {
onError!(error)
}
}, onCompleted: {
//~ Nothing to do.
}, onDisposed: {
//~ Nothing to do.
})
.addDisposableTo(disposeBag)
}
}
......@@ -19,6 +19,9 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import RxCocoa
import RxSwift
class AccountsService: AccountAdapterDelegate {
// MARK: Private members
/**
......
......@@ -20,8 +20,58 @@
import UIKit
import RxCocoa
import RxSwift
class CreateRingAccountViewController: UIViewController {
@IBAction func createRingAccount(_ sender: Any) {
print("Create Ring account.")
var mAccountViewModel: AccountViewModel = AccountViewModel()
@IBOutlet weak var mCreateAccountButton: RoundedButton!
override func viewDidLoad() {
super.viewDidLoad()
self.bindViews()
}
/**
Bind all the necessary of this View to its ViewModel.
That allows to build the binding part of the MVVM pattern.
*/
fileprivate func bindViews() {
//~ Create the stream. Won't start until an observer subscribes to it.
let createAccountObservable:Observable<Void> = self.mCreateAccountButton
.rx
.tap
.takeUntil(self.rx.deallocated)
mAccountViewModel.configureAddAccountObservers(
observable: createAccountObservable,
onStart: { [weak self] in
self?.setCreateAccountAsLoading()
},
onSuccess: { [weak self] in
print("Account created.")
self?.setCreateAccountAsIdle()
},
onError: { [weak self] (error) in
print("Error creating account...")
if error != nil {
print(error!)
}
self?.setCreateAccountAsIdle()
})
}
fileprivate func setCreateAccountAsLoading() {
print("Creating account...")
self.mCreateAccountButton.setTitle("Loading...", for: .normal)
self.mCreateAccountButton.isUserInteractionEnabled = false
}
fileprivate func setCreateAccountAsIdle() {
self.mCreateAccountButton.setTitle("Create a Ring account", for: .normal)
self.mCreateAccountButton.isUserInteractionEnabled = true
}
}
......@@ -139,9 +139,6 @@
<state key="normal" title="Create a Ring account">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="createRingAccount:" destination="ja8-pO-qtF" eventType="touchUpInside" id="dqx-z5-xdu"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="0.0" green="0.29803921570000003" blue="0.37647058820000001" alpha="1" colorSpace="calibratedRGB"/>
......@@ -151,6 +148,7 @@
</constraints>
</view>
<connections>
<outlet property="mCreateAccountButton" destination="exC-LF-5rp" id="z71-gN-58V"/>
<segue destination="E3W-r7-J4y" kind="show" identifier="AccountToPermissionsSegue" id="KYM-Jg-1oB"/>
</connections>
</viewController>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment