Giter Site home page Giter Site logo

Comments (4)

devxoul avatar devxoul commented on May 20, 2024

Why not just:

mailLoginButtonTaps.asObservable()
  .flatMap {
    authenticationService.login(withEmail: email.value, password: password.value)
  }
  .asDriver(onErrorJustReturn: false)

Also, this is not just for the readability. It's more about responsibility. If using second code, View calls emailLogin(), then the View will have a responsibility to handle the LoginResult data to map to UI components. This means, View has control flow. This is against the first rule: View doesn't have control flow. View cannot modify the data. View only knows how to map the data.

With the first code, the whole process of login and the result are handled by ViewModel. Then you can expose the specific data what view need.

ViewModel

let loggingIn = mailLoginButtonTaps.asObservable()
  .flatMap { in
    authenticationService.login(withEmail: email.value, password: password.value)
  }

self.errorMessageLabelText = loggingIn
  .catchError { error -> Driver<String> in
    Driver.just("\(error)")
  }

self.presentNextViewController = loggingIn
  .flatMap { result in
    return // ...
  }

from rxtodo.

edward-s avatar edward-s commented on May 20, 2024

I appreciate your input. I am learning the optimal way to implement MVVM + RxSwift architecture and your sample project has certainly helped.

Firstly,
The reason why I did this

.withLatestFrom(Observable.combineLatest(email.asObservable(), password.asObservable()) { ($0, $1) })

is that email and password is declared as member variables,

hence if I were to copy+paste your codes, adding the missing required self.:

authenticationService.login(withEmail:*self*.email.value, password: *self*.password.value)

this will in turn produces a compilation error: self captured by a closure before all members were initialized.

If I were to move this binding out of the init()

This won't work anymore,

self.errorMessageLabelText = loggingIn

since errorMessageLabelText has to be initialized in init(), and it requires loggingIn

I'm trying to following your advice for separating responsibilities, however, with all these code jugglings, I ended up with twice the amount of codes and am worried about the long-term readability and maintananibility of the codes.

from rxtodo.

edward-s avatar edward-s commented on May 20, 2024

I'm looking at 2 other MVVM+RxSwift projects which does not seem to enforce this rule strictly.

https://github.com/artsy/eidolon
https://github.com/ivanbruel/SwipeIt

Your thoughts? Or do you know of other open-source projects that managed to implement your coding guidelines successfully?

from rxtodo.

devxoul avatar devxoul commented on May 20, 2024
  1. Variables are class. You can declare those first than assign to properties:

    ViewModel

    init() {
      // declare local variables
      let email: Variable<String> = ...
      let password: Variable<String> = ...
    
      // do with local variables
      authenticationService.login(withEmail: email.value, password: password.value)
    
      // assign to properties
      self.email = email
      self.password = password
    }
  2. I understand what you're worrying about. I have same worries too. I'm always thinking and thinking to write a good code. What I think the good code should be is: responsibility-separated, high-readable, long-term-maintainable and easy-testable.

    What I wrote in Philosophy section is just a result of my philosophical speculation, and those are closer to 'Guidelines' than the 'Rules'. So please don't be restricted too much in guideline because there are always exceptions in the real world. (The only way to not make exceptions is not to write a code).

    PS: I think there're no open sourced apps using my guideline, but I'm developing social media app with these guidelines.

from rxtodo.

Related Issues (13)

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.