Giter Site home page Giter Site logo

mvvm_rxswift's Introduction

MVVM_RxSwift

MVVM Architecture pattern with RxSwift is fun when we fully utilize the power of RX

ViewControllerType

ViewControllerType, if we think about it we would want two mandatory functions, One is viewmodel variable and other is a func for binding the viewmodel

protocol ViewControllerType {
    associatedtype ViewModel: ViewModelType
    var viewModel: ViewModel! { get set}
    func bindViewModel()
}

ViewModelType

Basically, ViewModel's function is to receive input from Viewcontroller process the input and convert it into Output. We have taken fetching posts from network as an example

protocol ViewModelType {
    associatedtype Input
    associatedtype Output
    func transform(input: Input) -> Output
}

Segregating the problem

Now lets say we have a array of posts object to fetch from network or DB in future(We will implement Repository layer in Same Repo later). So There is a VC with a tableview that displays a list of posts that is fetched from network(for now).

Input and Output

Input and Output can be struct that contains Driver(Rx).

class PostsViewModel: ViewModelType {
    struct Input {
        let trigger: Driver<Void>
    }
    
    struct Output {
        let posts: Driver<[Post]>
    }
    
    func transform(input: Input) -> Output {
        let post = PostsDataManager.shared.getPosts()
        let postDriver = input.trigger.flatMapLatest{ _ in
            return post.asDriver(onErrorJustReturn: [])
        }
        return Output(posts: postDriver)
}

Feel free to dive into this simple code structure to understand the Usage

mvvm_rxswift's People

Contributors

kishorekumarek avatar

Stargazers

Karthi avatar  avatar

Watchers

James Cloos 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.