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.
CleanArchKit requires the following:
- Xcode 11.0 or later
- Swift 5.4 or later
- iOS 12.0 or later
To install this framework using CocoaPods, add the following line to your Podfile:
pod 'CleanArchKit'
Then run pod install
in your terminal.
- In Xcode, navigate in menu: File > Add Package Dependencies
- Add
https://github.com/k-angama/iOS-CleanArchKit.git
- Click Add Package.
To use this framework in your project, you can follow the steps below:
- Import the framework in the Swift file where you want to use it:
import CleanArchKit
- Declare a class inheriting from
BaseViewController
, specifying the type of ViewModel you want to use, conforming to theViewModelProtocol
protocol. For example:
class MyViewController: BaseViewController<MyViewModel> {
// ...
}
Make sure your ViewModel (MyViewModel
in this example) implements the required InputProtocol, OutputProtocol, and Router protocols.
- 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
}
// ...
}
- 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
}
// ...
}
- 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
}
// ...
}
- Create classes conforming to the
InputProtocol
andOutputProtocol
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.
- Define your use cases by creating classes conforming to the UseCase protocol:
struct MyUseCase: UseCase {
func execute(params: String) -> Bool {
// Implement your logic here
}
}
- 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.
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.
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.
k.angama, karim.angama@gmail.com
CleanArchKit is available under the MIT license. See the LICENSE file for more info.