Git Product home page Git Product logo

connectable's Introduction

Connectable

Connectable protocol for readable ViewModel

Installation

Install via Carthage

github "atsushi130/Connectable"

Usage

Please your ViewModel conforms to Connectable and implement computed properties on each name space.

final class GithubViewModel: Connectable {

    fileprivate let searchText = Variable<String>("")
    fileprivate let searched   = PublishSubject<[GithubRepository]>()
    private let disposeBag = DisposeBag()

    init() {
        self.searchText.asObservable()
            .flatMapLatest { GithubApi.shared.searched(by: $0) }
            .bind(to: self.searched)
            .disposed(by: self.disposeBag)
    }
}

// MARK: - Input
extension InputSpace where Definer == GithubViewModel {
    var searchText: Binder<String?> {
        return Binder(self.difiner) { element, value in
            guard let searchText = value else { return }
            element.searchedText.value = searchText
        }
    }
}

// MARK: - Output
extension OutputSpace where Definer == GithubViewModel {
    var searched: Observable<[GithubRepository]> {
        return self.difiner.searched.asObservable()
    }
}

Bind and subscribe.

final class GithubViewController: UIViewController {

    @IBOutpet private weak var textField: UITextField!

    private let viewModel  = GithubViewModel()
    private let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        // self.viewModel.in.*
        self.textField.rx.text.asObservable()
            .bind(to: self.viewModel.in.searchText)
            .disposed(by: self.disposeBag)

        // self.viewModel.out.*
        self.viewModel.out.searched.subscribe(onNext: { repositories in
            print(repositories)
        }).disposed(by: self.disposeBag)
    }
}

Requirements

  • Swift 4 or later

License

Connectable is available under the MIT license. See the LICENSE file.

connectable's People

Contributors

atsushi130 avatar

Watchers

 avatar  avatar

connectable's Issues

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.