/Combine-MVVM

Sample project with Combine + SwiftUI/UIKit and MVVM architecture

Primary LanguageSwift

Sample project with Combine + SwiftUI/UIKit and MVVM architecture

UIKit SwiftUI
Simulator Screen Shot - iPhone 12 - 2021-07-11 at 21 13 44 Simulator Screen Shot - iPhone 12 - 2021-07-11 at 21 14 01

This sample app consist includes basic concepts that are common use cases for using reactive programming, that is implemented with the MVVM pattern, heavy use of Combine with both UIKit and SwiftUI, which makes binding very easy.

MVVMPattern

ViewModel

  • ViewModel is the main point of MVVM application. The primary responsibility of the ViewModel is to provide data to the view, so that view can put that data on the screen.
  • It also allows the user to interact with data and change the data.
  • The other key responsibility of a ViewModel is to encapsulate the interaction logic for a view, but it does not mean that all of the logic of the application should go into ViewModel.
  • It should be able to handle the appropriate sequencing of calls to make the right thing happen based on user or any changes on the view.
  • ViewModel should also manage any navigation logic like deciding when it is time to navigate to a different view. Source

ViewModelType is a protocol performs pure transformation of a user Input to the Output:

protocol ViewModelType {
    associatedtype Input
    associatedtype Output
    
    func transform(_ input: Input, _ cancelBag: CancelBag) -> Output
}

Folder Structure

.
├── Sources
│   ├── Utility
│   ├── Entities
│   ├── Network
│   ├── Sences
│   ├── ├── Storyboards
│   │   │    ├── Main.storyboard
│   │   │    └── Login.storyboard
│   │   │
│   │   ├── Sence_1
│   │   │    ├── Sence_1ViewModel
│   │   │    ├── Sence_1ViewController
│   │   │    ├── Sence_1UseCase
│   │   │    └── Sence_1Navigator
│   │   │
│   │   ├── Sence_2
│   │   │    ├── Sence_2ViewModel
│   │   │    ├── Sence_2ViewController
│   │   │    ├── Sence_2UseCase
│   │   │    └── Sence_2Navigator
│   │   └── ........
│   │
│   ├── AppDelegate  
│   ├── SceneDelegate
│   ├── Assets
│   ├── Info.plist
│   └── LaunchScreen
│       
└── Tests
    └── Testcases

Setup

  • Xcode 12
  • Swift 5

TODO

  • Add test
  • Add Xcode Template for this Architecture

Links