|
1 | 1 | # ModuleRoute
|
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +ModuleRoute is a modular routing framework designed to simplify navigation within your Swift applications. Leveraging the Service Locator pattern, ModuleRoute enables clean, scalable, and maintainable routing architecture for iOS projects. |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +- [Features](#features) |
| 12 | +- [Installation](#installation) |
| 13 | +- [Usage](#usage) |
| 14 | + - [Setup](#setup) |
| 15 | + - [Registering Modules and Routes](#registering-modules-and-routes) |
| 16 | + - [Navigating Between Modules](#navigating-between-modules) |
| 17 | + - [Deep Linking](#deep-linking) |
| 18 | + - [Middleware and Interceptors](#middleware-and-interceptors) |
| 19 | +- [Contribution](#contribution) |
| 20 | +- [License](#license) |
| 21 | + |
| 22 | +## Features |
| 23 | + |
| 24 | +- **Modular Architecture**: Organize your app into distinct modules with clear separation of concerns. |
| 25 | +- **Service Locator Integration**: Efficient dependency management using Service Locator. |
| 26 | +- **Middleware Support**: Process routes through customizable middleware. |
| 27 | +- **Interceptors**: Intercept and handle routes based on custom logic. |
| 28 | +- **Deep Linking**: Handle URL schemes and universal links seamlessly. |
| 29 | +- **Flexible Navigation**: Support for various navigation types including push, present, modal, replace, and custom transitions. |
| 30 | +- **Logging**: Built-in logging for route processing. |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +ModuleRoute is distributed via Swift Package Manager (SPM). |
| 35 | + |
| 36 | +### Swift Package Manager |
| 37 | + |
| 38 | +1. Open your project in Xcode. |
| 39 | +2. Go to `File` > `Add Packages...`. |
| 40 | +3. Enter the ModuleRoute repository URL: |
| 41 | + ``` |
| 42 | + https://github.com/GIKICoder/ModuleRoute |
| 43 | + ``` |
| 44 | +4. Choose the version you want to install and add the package to your project. |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +### Setup |
| 49 | + |
| 50 | +First, initialize the `ServiceLocator` and `MRNavigator` in your AppDelegate or SceneDelegate. |
| 51 | + |
| 52 | +```swift |
| 53 | +import ModuleRoute |
| 54 | + |
| 55 | +@UIApplicationMain |
| 56 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 57 | + |
| 58 | + let serviceLocator = ServiceLocator() |
| 59 | + var navigator: MRNavigator! |
| 60 | + |
| 61 | + func application(_ application: UIApplication, |
| 62 | + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 63 | + navigator = MRNavigator(serviceLocator: serviceLocator) |
| 64 | + |
| 65 | + // Register middleware |
| 66 | + navigator.addMiddleware(LoggingMiddleware()) |
| 67 | + |
| 68 | + // Register modules and routes |
| 69 | + registerModules() |
| 70 | + |
| 71 | + return true |
| 72 | + } |
| 73 | + |
| 74 | + private func registerModules() { |
| 75 | + serviceLocator.register(MyModule.self, routes: [MyRoute.self]) { |
| 76 | + MyModule() |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Registering Modules and Routes |
| 83 | + |
| 84 | +Create modules conforming to `MRModule` and define their supported routes. |
| 85 | + |
| 86 | +```swift |
| 87 | +import ModuleRoute |
| 88 | + |
| 89 | +struct MyRoute: MRRoute { |
| 90 | + static var name: String { "myRoute" } |
| 91 | + var params: [String : Any] = [:] |
| 92 | + var callback: ((Any?) -> Void)? = nil |
| 93 | +} |
| 94 | + |
| 95 | +class MyModule: MRModule { |
| 96 | + static var supportedRoutes: [MRRoute.Type] = [MyRoute.self] |
| 97 | + |
| 98 | + func handle(route: MRRoute) -> RouteResult { |
| 99 | + switch route { |
| 100 | + case is MyRoute: |
| 101 | + let viewController = MyViewController() |
| 102 | + return .navigator(viewController) |
| 103 | + default: |
| 104 | + return .none |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Navigating Between Modules |
| 111 | + |
| 112 | +Use `MRNavigator` to navigate to a specific route. |
| 113 | + |
| 114 | +```swift |
| 115 | +let route = MyRoute(params: ["key": "value"]) |
| 116 | +navigator.navigate(to: route, from: currentViewController, navigationType: .push, animated: true) |
| 117 | +``` |
| 118 | + |
| 119 | +### Deep Linking |
| 120 | + |
| 121 | +Register deep link handlers to handle incoming URLs. |
| 122 | + |
| 123 | +```swift |
| 124 | +navigator.registerDeepLinkHandler(scheme: "myapp") { url in |
| 125 | + guard let route = MRRoute.from(url: url) else { return nil } |
| 126 | + return route |
| 127 | +} |
| 128 | + |
| 129 | +// Handle incoming URL |
| 130 | +func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { |
| 131 | + return navigator.handleDeepLink(url) |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Middleware and Interceptors |
| 136 | + |
| 137 | +Customize route processing with middleware and interceptors. |
| 138 | + |
| 139 | +```swift |
| 140 | +// Middleware example |
| 141 | +class AuthenticationMiddleware: MRMiddleware { |
| 142 | + func process(route: MRRoute, navigator: MRNavigator, next: @escaping (MRRoute) -> RouteResult) -> RouteResult { |
| 143 | + if !isAuthenticated { |
| 144 | + // Handle unauthorized access |
| 145 | + return .handler { |
| 146 | + // Show login screen or alert |
| 147 | + } |
| 148 | + } |
| 149 | + return next(route) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// Interceptor example |
| 154 | +class LoggingInterceptor: MRInterceptor { |
| 155 | + func shouldIntercept(route: MRRoute) -> Bool { |
| 156 | + // Define when to intercept |
| 157 | + return true |
| 158 | + } |
| 159 | + |
| 160 | + func handleInterception(route: MRRoute) -> RouteResult { |
| 161 | + // Handle the interception |
| 162 | + print("Route \(type(of: route)) intercepted") |
| 163 | + return .none |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// Adding Middleware and Interceptors |
| 168 | +navigator.addMiddleware(AuthenticationMiddleware()) |
| 169 | +navigator.addInterceptor(LoggingInterceptor()) |
| 170 | +``` |
| 171 | + |
| 172 | +## Contribution |
| 173 | + |
| 174 | +Contributions are welcome! Please follow these steps: |
| 175 | + |
| 176 | +1. Fork the repository. |
| 177 | +2. Create your feature branch: `git checkout -b feature/YourFeature`. |
| 178 | +3. Commit your changes: `git commit -m 'Add some feature'`. |
| 179 | +4. Push to the branch: `git push origin feature/YourFeature`. |
| 180 | +5. Open a pull request. |
| 181 | + |
| 182 | +Please ensure your code follows the project's coding standards and includes appropriate tests. |
| 183 | + |
| 184 | +## License |
| 185 | + |
| 186 | +ModuleRoute is released under the [MIT License](LICENSE). |
| 187 | + |
| 188 | +--- |
| 189 | + |
| 190 | +Feel free to reach out or open an issue if you have any questions or need assistance. |
| 191 | + |
0 commit comments