Code Monkey home page Code Monkey logo

rxswiftcalculatorexample's Introduction

RxSwiftCalculatorExample

Calculator

Image

RxSwift SwiftPackage

RxSwift github

Usage RxSwift

CalculatorViewModel

class CalculatorViewModel {
    
    var calculatorButtonVariable = BehaviorRelay<CalculatorButton?>(value: .zero)
    var calculatorObservable: Observable<String>!
    
    ...
    
    // Setup calculatorObservable
    private func setupObservable() {
        
        calculatorObservable = calculatorButtonVariable.asObservable().map{ calculatorButton in
            
            switch calculatorButton {
            case .clear:
                self.clear()
                break
            case .plus:
                self.plus()
                break
            case .subtract:
                self.subtract()
                break
            case .multiply:
                self.multiply()
                break
            case .divide:
                self.divide()
                break
            case .equals:
                self.equal()
                break
            case .empty:
                break
            default:
                let number = calculatorButton?.rawValue ?? ""
                self.number(number)
            }
            
            return self.labelString
        }
    }
}

ViewController

class ViewController: UIViewController {

    fileprivate let disposeBag = DisposeBag()
    
    let calculatorResultLabel: UILabel           = CalculatorResultLabel()
    let collectionView: UICollectionView         = CalculatorCollectionView()
    let collectionViewData: [CalculatorButton]   = [.clear, .plus, .subtract, .empty,
                                                    .one, .two, .three, .multiply,
                                                    .four, .five, .six, .divide,
                                                    .seven, .eight, .nine, .equals,
                                                    .zero]
    let calculatorViewModel: CalculatorViewModel = CalculatorViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
                // Subscribe to calculatorObservable on calculatorViewModel
        calculatorViewModel
            .calculatorObservable
            .subscribe(onNext: { reslutLabelString in
                self.calculatorResultLabel.text = reslutLabelString
                
            }).disposed(by: disposeBag)
        
        // Rx collectionview - observe items of the collection view
        let items = Observable.just(collectionViewData)
        items
            .asObservable()
            .bind(to: collectionView.rx.items(cellIdentifier: String(describing: CalculatorButtonCell.self), cellType: CalculatorButtonCell.self)) { row, data, cell in
                cell.buttonLabel.text = data.rawValue
        }.disposed(by: disposeBag)
        
        // Rx collectionview - change calculatorButtonVariable on calculatorViewModel when select a button cell of the collection view
        collectionView
            .rx
            .modelSelected(CalculatorButton.self)
            .bind(to: calculatorViewModel.calculatorButtonVariable)
            .disposed(by: disposeBag)
    }
}

rxswiftcalculatorexample's People

Contributors

tigi44 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.