Skip to content

An iOS library to create a project with the Clean Architecture using Router and MVVM pattern.

License

Notifications You must be signed in to change notification settings

k-angama/iOS-CleanArchKit

Repository files navigation

iOS-CleanArchKit

CleanArchKit is an iOS framework developed in Swift that implements Clean Architecture in a project using the MVVM (Model-View-ViewModel) pattern, as well as Router for managing navigation between screens. It provides base classes for view controllers, view models, and routers, as well as protocols for input and output of view models.

Requirements

CleanArchKit requires the following:

  • Xcode 11.0 or later
  • Swift 5.4 or later
  • iOS 12.0 or later

Installation

CocoaPods

To install this framework using CocoaPods, add the following line to your Podfile:

pod 'CleanArchKit'

Then run pod install in your terminal.

Swift Package Manager

  1. In Xcode, navigate in menu: File > Add Package Dependencies
  2. Add https://github.com/k-angama/iOS-CleanArchKit.git
  3. Click Add Package.

Usage

To use this framework in your project, you can follow the steps below:

  1. Import the framework in the Swift file where you want to use it:
import CleanArchKit
  1. Declare a class inheriting from BaseViewController, specifying the type of ViewModel you want to use, conforming to the ViewModelProtocol protocol. For example:
class MyViewController: BaseViewController<MyViewModel> {
    // ...
}

Make sure your ViewModel (MyViewModel in this example) implements the required InputProtocol, OutputProtocol, and Router protocols.

  1. Implement the required methods in your view class (MyViewController in this example) to configure bindings, observers, and user interface, as needed:
class MyViewController: BaseViewController<MyViewModel> {
    
    // Setup bindings
    override func setupBindings() {
        // Configure bindings here
    }
    
    // Setup des observateurs
    override func setupObservers() {
        // Configure observers here
    }
    
    // Setup de l'interface utilisateur
    override func setupUI() {
        // Configure user interface here
    }
    
    // ...
}
  1. Implement the required methods in your ViewModel (MyViewModel in this example) for configuring your business logic:
class MyViewModel: BaseViewModel<MyRouter, MyInput, MyOutput> {
    
    // Setup business logic
    override func setup() {
        // Configure business logic here
    }
    
    // Exécution des cas d'utilisation
    override func exeUseCase() {
        // Execute use cases here
    }
    
    // Configuration des observateurs
    override func observers() {
        // Configure observers here
    }
    
    // ...
}
  1. Implement the required methods in your Router (MyRouter in this example) for managing navigation between screens:
class MyRouter: BaseRouter<MyRoute> {
    
    // Transition between screens
    override func transition(route: MyRoute) {
         // Handle navigation between screens here
    }
    
    // ...
}
  1. Create classes conforming to the InputProtocol and OutputProtocol protocols:
import CleanArchKit

struct MyInput: InputProtocol {
    // Implement input properties here
}

struct MyOutput: OutputProtocol {
    // Implement output properties here
}

class MyViewModel: BaseViewModel<MyRouter, MyInput, MyOutput> {
    // Implement setup, exeUseCase, and observers methods here
}

You can create your own struct conforming to the InputProtocol and OutputProtocol protocols to define input and output data for your view models.

  1. Define your use cases by creating classes conforming to the UseCase protocol:
struct MyUseCase: UseCase {
    func execute(params: String) -> Bool {
    // Implement your logic here
    }
}
  1. Implement dependency injection using the DIProtocol:
import Foundation
extension MainViewController {
    @objc func di() {
        self.viewModel.input.checkCredentialUseCase = CheckCredentialUseCase(loginAPI: RegistrationAPI())
        self.viewModel.input.checkValidEmailUseCase = CheckValidEmailUseCase()
    }
}

You can use the DIProtocol to define a method di() in your view controllers. In the example above, MainViewController conforms to DIProtocol and implements the di() method to inject dependencies for the CheckCredentialUseCase and CheckValidEmailUseCase into the view model's input properties.

Note: Dependency injection is an important concept in Clean Architecture that helps to decouple components and promote testability.

Example

An example project using CleanArchKit can be found in the Example directory of this repository. This example illustrates how to implement Clean Architecture with MVVM and Router using CleanArchKit in an iOS project.

Contributing

Contributions to CleanArchKit are welcome! If you find any issues or have suggestions for improvement, please feel free to open an issue or submit a pull request.

Author

k.angama, karim.angama@gmail.com

License

CleanArchKit is available under the MIT license. See the LICENSE file for more info.