简体   繁体   中英

Counting beans with ReactiveCocoa 4 and an NSButton

I have the following:

  • Two interesting classes: a ViewController and a ViewModel
  • A button nsButtonMorePlease:NSButton in the view of ViewController
  • A text box nsTextView:NSTextView in the view as well

I want the following behavior:

  • When you launch the program, the "count" starts at 0 and is displayed in the text box nsTextView
  • When you press the button nsButtonMorePlease , the count is incremented by 1 and the updated count is reflected in the nsTextView

I would like to ensure:

  • I use ReactiveCocoa 4 (that's the point kind of)
  • The model class contains numberOfBeans: MutableProperty<Int> starting at 0
  • The design is purely functional or close to it - that is (if I understand the term), every link in the chain mapping the event of mouse click to the MutableProperty of numberOfBeans to responding to it in the text view, is all without side effects.

Here's what I have. Fair warning: this doesn't come close to working or compiling I believe. But I do feel like maybe I want to use one of combineLatest , collect , reduce , etc. Just lost on what to do specifically. I do feel like this makes something easy quite hard.

class CandyViewModel {

    private let racPropertyBeansCount: MutableProperty<Int> = MutableProperty<Int>(0)

    lazy var racActionIncrementBeansCount: Action<AnyObject?, Int, NoError> = {
        return Action { _ in SignalProducer<Int, NoError>(value: 1)
        }
    }()

    var racCocoaIncrementBeansAction: CocoaAction

    init() {
        racCocoaIncrementBeansAction = CocoaAction.init(racActionIncrementBeansCount, input: "")
        // ???
        var sig = racPropertyBeansCount.producer.combineLatestWith(racActionIncrementBeansCount.)
    }

}

class CandyView: NSViewController {

    @IBOutlet private var guiButtonMoreCandy: NSButton!
    @IBOutlet private var guiTextViewCandyCt: NSTextView!



}
class CandyViewModel {

    let racPropertyBeansCount = MutableProperty<Int>(0)

    let racActionIncrementBeansCount = Action<(), Int, NoError>{ _ in SignalProducer(value: 1) }

    init() {

        // reduce the value from the action to the mutableproperty
        racPropertyBeansCount <~ racActionIncrementBeansCount.values.reduce(racPropertyBeansCount.value) { $0 + $1 }

    }

}

class CandyView: NSViewController {

    // define outlets

    let viewModel = CandyViewModel()


    func bindObservers() {

        // bind the Action to the button
        guiButtonMoreCandy.addTarget(viewModel.racActionIncrementBeansCount.unsafeCocoaAction, action: CocoaAction.selector, forControlEvents: .TouchUpInside)

        // observe the producer of the mutableproperty
        viewModel.racPropertyBeansCount.producer.startWithNext {
            self.guiTextViewCandyCt.text = "\($0)"
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM