Git Product home page Git Product logo

clean-architecture-rx-swift's Introduction

Clean Architecture with RxSwift

Requirements

  1. Cocoapods
  2. Xcode 13.3.1

Installation

  1. Clone project
  2. cd path_to_project_folder
  3. pod install
  4. open AppetiserInterview.xcworkspace
  5. Build & Run

Functionalities

  1. Movies list: Display movies that is fetched from api and saved to local storage.
  2. Movie detail: Display detailed about a movie.
  3. Favorites: Add movies to Favorites, display favorited movie in Favorites screen.
  4. Offline mode: Datas are stored in local storage. User can use all app's features without internet connection.

Tech stack

  1. Clean Architecture
  2. Design pattern: MVVM-C (Model-View-ViewModel-Coordinator)
  3. Dependency manager: Cocoapods
  4. Dependency Injection: Resolver
  5. Coding languages: Swift, RxSwift
  6. Network calls using URLSession
  7. Libraries: UIKit, RxSwift, RxCocoa, RxDatasources, RxGesture, XCoordinator, RealmSwift, Kingfisher, SnapKit, Resolver, ReachabilitySwift

Clean Architecture

enter image description here

  • Divide the app into different layers. Each has at least one layer for business rules, and another for interfaces.

    • Independent of frameworks: The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints.
    • Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.
    • Independent of UI. The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules.
    • Independent of Database. You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database.
    • Independent of any external agency. In fact your business rules simply don’t know anything at all about the outside world.
  • Disadvantages: Lots of files. The number of layers will increase for each function.

MVVM-C (Model-View-ViewModel-Coordinator)

enter image description here

Coordinator

Overview

Coordinator is a object that handle app's navigation. Coordinator's responsibilities:

  • Initialize ViewControllers & ViewModels
  • Inject dependencies into ViewControllers & ViewModels
  • Present/push ViewControllers
  • Handle Events between screens (passing data,...)

Why Coordinator?

  • If we don't use Coordinator, ViewController will have to handle navigation itself. A ViewController should only display UI elements and handle user's controls. All navigation should be handled by another Object.

Disadvantages

  • Hard to understand
  • Have to create a lot of files

MVVM

Advantages

  • ViewController doesn't contains logic, just display UI based on ViewModel
  • ViewModel is UIKit independent and fully testable
  • Data binding: Datas are kept in sync.

Disadvantages

  • Needs to create a lot of files: ViewControllers, ViewModels and binding them all together
  • Communication between various components and data binding can be painful

Dependency Injection: Resolver

Dependency Injection frameworks support the Inversion of Control design pattern. Technical definitions aside, dependency injection pretty much boils down to:

| Giving an object the things it needs to do its job.

That's it. Dependency injection allows us to write code that's loosely coupled, and as such, easier to reuse, to mock, and to test.

Types

  • Constructor Injection: Injecting dependencies through constructors or initializers.
  • Method Injection: Injecting dependencies through methods.
  • Property Injection: Injecting dependencies through properties.
  • Annotation Injection: Resolver uses @Injected as a property wrapper to inject dependencies.

Advantages

  • By injecting the dependencies of an object, the responsibilities and requirements of a class or structure become more clear and more transparent. By injecting a request manager into a view controller, we understand that the view controller depends on the request manager and we can assume that the view controller is responsible for request managing and/or handling.
  • Dependency injection makes it very easy to replace an object's dependencies with mock objects, making unit tests easier to set up and isolate behavior.

Disadvantages

  • I don't see any disadvantages of Dependency Injection

Networking: URLSession

URLSession is the key object responsible for sending and receiving requests. Alamofire and URLSession both help us to make network requests in Swift. The URLSession API is part of the foundation framework, whereas Alamofire needs to be added as an external dependency. Personally, I always try to use as few third party libraries as possible, so I choose to use URLSession over Alamofire for api requests.

Advantages:

  • Asynchrony is simplified with Declarative Code
  • Multithreading is simplified
  • Cleaner code
  • Multi-platform

Disadvantages:

  • Hard to learn
  • Hard to debug
  • May cause memory leaks if not used carefully

Local database: Realm

I have experience working with Realm, so I choose to use it in this coding challenge.

Advantages

  • Fast and lightweight
  • Simple to start and scale
  • Built-in mobile to cloud sync
  • Cross-platform support

Disadvantages

  • Don't support multi-threading

Persistence flow:

  • Datas are fetched from both local storage and API:
    • Save default data from json file if needed (only save once)
    • Fetch from local first, then fetch from API
    • If API failed => display local datas
    • If API success => save to local database => Display to UI
  • If user launches app with or without internet connection, they can still use the app offline (Go through Movies List, Movie Detail, Add/remove movies to/from Favorites)
  • User's last visited screen's index and time are stored in UserDefaults

clean-architecture-rx-swift's People

Contributors

nannymcphee avatar

Watchers

 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.