Created
April 23, 2019 13:25
-
-
Save ElonPark/5c6cd04efdf95b484478af73183b1e0d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// ViewController.swift | |
// Rainist | |
// | |
// Created by Elon on 23/04/2019. | |
// Copyright © 2019 Elon. All rights reserved. | |
// | |
import UIKit | |
import RxSwift | |
struct Total { | |
let soda: Int | |
let fish: Int | |
} | |
enum ValueType { | |
case soda(Int) | |
case fish(Int) | |
} | |
class ViewController: UIViewController { | |
let disposeBag = DisposeBag() | |
let soda = Observable<Int>.interval(1.0, scheduler: MainScheduler.instance) | |
.map { ValueType.soda($0) } | |
let fish = Observable<Int>.interval(3.0, scheduler: MainScheduler.instance) | |
.map { ValueType.fish($0) } | |
let total: Observable<Total> = Observable<Int>.interval(7.0, scheduler: MainScheduler.instance) | |
.map { _ in Total(soda: 0, fish: 0) } | |
lazy var createTotal = Observable<ValueType>.merge(soda, fish) | |
.scan(Total(soda: 0, fish: 0)) { (total, valueType) -> Total in | |
switch valueType { | |
case .soda(let number): | |
return Total(soda: number, fish: total.fish) | |
case .fish(let number): | |
return Total(soda: total.soda, fish: number) | |
} | |
} | |
lazy var currentTotal: Observable<Total> = Observable.merge(total, createTotal) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
currentTotal | |
.debug() | |
.subscribe() | |
.disposed(by: disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment