Created
November 1, 2017 05:37
-
-
Save KatagiriSo/4751eb9ba0782bffaa2247f52498def2 to your computer and use it in GitHub Desktop.
RXSwiftExample
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 Foundation | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
class AmountViewControlelr : UIViewController { | |
@IBOutlet var amount:UILabel! | |
@IBOutlet var optionAmount:UILabel! | |
@IBOutlet var optionAmount2:UILabel! | |
@IBOutlet var calc:UIButton! | |
@IBOutlet weak var amountStepper: UIStepper! | |
@IBOutlet weak var optionStepper: UIStepper! | |
@IBOutlet weak var option2Stepper: UIStepper! | |
var am : AmViewModel = AmViewModel(amount: 0, option: 0, option2: 0) | |
var disposeBag:DisposeBag = DisposeBag() | |
deinit { | |
print("AmountViewCotroller deinit") | |
} | |
override func viewDidLoad() { | |
let amvm = self.am | |
am.amountObs.map { $0.description } | |
.bind(to: amount.rx.text) | |
.addDisposableTo(disposeBag) | |
let stepperV = amountStepper.rx.value | |
stepperV.bind(onNext: amvm.stepperChange(value:)).addDisposableTo(disposeBag) | |
am.enableObs.bind(to: calc.rx.isEnabled).addDisposableTo(disposeBag) | |
let calctap = calc.rx.tap | |
calctap.subscribe {(e:Event) in | |
amvm.calcButtonTapped() | |
}.addDisposableTo(disposeBag) | |
} | |
} | |
class AmViewModel { | |
var presenter:Presenter? | |
private var amount:Double = 0{ | |
didSet { | |
amountObs.onNext(amount) | |
} | |
} | |
private var enable:Bool = true { | |
didSet { | |
enableObs.onNext(enable) | |
} | |
} | |
private var option:Double = 0 | |
private var option2:Double = 0 | |
var amountObs:BehaviorSubject<Double>! | |
var optionObs:BehaviorSubject<Double>! | |
var option2Obs:BehaviorSubject<Double>! | |
var enableObs:BehaviorSubject<Bool>! | |
init(amount:Double, option:Double, option2:Double) { | |
amountObs = BehaviorSubject<Double>(value:amount) | |
optionObs = BehaviorSubject<Double>(value:option) | |
option2Obs = BehaviorSubject<Double>(value:option2) | |
enableObs = BehaviorSubject<Bool>(value:enable) | |
presenter = Presenter(self) | |
} | |
private func calc() -> Double { | |
return amount + option + option2 | |
} | |
func calcButtonTapped() { | |
enable = false | |
getAmountObs?.onNext(calc()) | |
getAmountObs?.onCompleted() | |
getAmountObs?.dispose() | |
getAmountObs = nil | |
} | |
func stepperChange(value:Double) { | |
amount = value | |
} | |
private var getAmountObs:PublishSubject<Double>? = nil | |
public func getAmount() -> Observable<Double> { | |
let obs = PublishSubject<Double>() | |
getAmountObs = obs | |
return obs | |
} | |
} | |
class Presenter { | |
var am:AmViewModel | |
var waitAmount:DisposeBag = DisposeBag() | |
init(_ am:AmViewModel) { | |
self.am = am | |
am.getAmount().subscribe { | |
print($0.debugDescription) | |
}.disposed(by: waitAmount) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment