├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle ├── bintray-android-v1.gradle ├── bintray-java-v1.gradle ├── install-v1.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── medias ├── flavor.png └── splash_push.png ├── navigator ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── navigator │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── navigator │ │ ├── Destination.kt │ │ ├── Flavor.kt │ │ ├── FlavorBinder.kt │ │ ├── IntentKt.kt │ │ ├── Navigator.kt │ │ ├── Param.kt │ │ ├── Route.kt │ │ ├── RouteExtensions.kt │ │ ├── exceptions │ │ └── Exceptions.kt │ │ ├── starter │ │ ├── DesintationWithParams.kt │ │ ├── NavigatorStarter.kt │ │ └── StarterHandler.kt │ │ └── uri │ │ └── PathMatcher.kt │ └── test │ └── java │ └── com │ └── github │ └── florent37 │ └── navigator │ └── ExampleUnitTest.kt ├── publish.sh ├── sample ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── navigator │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── github │ │ │ │ └── florent37 │ │ │ │ └── navigator │ │ │ │ └── sample │ │ │ │ ├── KoinProvider.kt │ │ │ │ └── MainActivity.kt │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── navigator │ │ └── ExampleUnitTest.kt ├── home │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── home │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── github │ │ │ │ └── florent37 │ │ │ │ └── home │ │ │ │ ├── DependenciesProvider.kt │ │ │ │ ├── HomeActivity.kt │ │ │ │ ├── RouteProvider.kt │ │ │ │ ├── tabs │ │ │ │ ├── ViewPagerAdapter.kt │ │ │ │ ├── posts │ │ │ │ │ ├── PostsAdapter.kt │ │ │ │ │ ├── PostsFragment.kt │ │ │ │ │ ├── PostsUseCase.kt │ │ │ │ │ └── PostsViewModel.kt │ │ │ │ └── users │ │ │ │ │ ├── UsersAdapter.kt │ │ │ │ │ ├── UsersFragment.kt │ │ │ │ │ └── UsersViewModel.kt │ │ │ │ └── utils │ │ │ │ └── ViewPagerBottomNav.kt │ │ └── res │ │ │ ├── drawable │ │ │ ├── ic_account_circle_black_24dp.xml │ │ │ └── ic_folder_black_24dp.xml │ │ │ ├── layout │ │ │ ├── activity_home.xml │ │ │ ├── cell_post.xml │ │ │ ├── cell_user.xml │ │ │ ├── fragment_posts.xml │ │ │ └── fragment_users.xml │ │ │ └── menu │ │ │ └── bottom_navigation_menu.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── home │ │ └── ExampleUnitTest.kt ├── post-core │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── routing │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── post │ │ │ └── core │ │ │ ├── DependenciesProvider.kt │ │ │ ├── Post.kt │ │ │ ├── PostRepository.kt │ │ │ └── PostRepositoryImpl.kt │ │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── routing │ │ └── ExampleUnitTest.kt ├── routing │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── routing │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── routing │ │ │ └── Routes.kt │ │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── routing │ │ └── ExampleUnitTest.kt ├── splash │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── splash │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── github │ │ │ │ └── florent37 │ │ │ │ └── splash │ │ │ │ ├── DependenciesProvider.kt │ │ │ │ ├── RouteProvider.kt │ │ │ │ ├── SplashActivity.kt │ │ │ │ └── SplashViewModel.kt │ │ └── res │ │ │ └── layout │ │ │ └── activity_splash.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── splash │ │ └── ExampleUnitTest.kt ├── user-core │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── routing │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── user │ │ │ └── core │ │ │ ├── DependenciesProvider.kt │ │ │ ├── User.kt │ │ │ ├── UserRepository.kt │ │ │ └── UserRepositoryImpl.kt │ │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── routing │ │ └── ExampleUnitTest.kt └── user │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── home │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── user │ │ │ ├── DependenciesProvider.kt │ │ │ ├── RouteProvider.kt │ │ │ ├── UserActivity.kt │ │ │ └── UserViewModel.kt │ └── res │ │ └── layout │ │ └── activity_user.xml │ └── test │ └── java │ └── com │ └── github │ └── florent37 │ └── home │ └── ExampleUnitTest.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches/build_file_checksums.ser 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | 9 | .DS_Store 10 | /build 11 | /captures 12 | .externalNativeBuild 13 | ### Android template 14 | # Built application files 15 | *.apk 16 | *.ap_ 17 | 18 | # Files for the ART/Dalvik VM 19 | *.dex 20 | 21 | # Java class files 22 | *.class 23 | 24 | # Generated files 25 | bin/ 26 | gen/ 27 | out/ 28 | 29 | # Gradle files 30 | .gradle/ 31 | build/ 32 | 33 | # Local configuration file (sdk path, etc) 34 | local.properties 35 | 36 | # Proguard folder generated by Eclipse 37 | proguard/ 38 | 39 | # Log Files 40 | *.log 41 | 42 | # Android Studio Navigation editor temp files 43 | .navigation/ 44 | 45 | # Android Studio captures folder 46 | captures/ 47 | 48 | # Intellij 49 | .idea/ 50 | *.iml 51 | .idea/workspace.xml 52 | .idea/tasks.xml 53 | .idea/gradle.xml 54 | .idea/assetWizardSettings.xml 55 | .idea/dictionaries 56 | .idea/libraries 57 | .idea/caches 58 | 59 | # Keystore files 60 | # Uncomment the following line if you do not want to check your keystore files in. 61 | #*.jks 62 | 63 | # External native build folder generated in Android Studio 2.2 and later 64 | .externalNativeBuild 65 | 66 | # Google Services (e.g. APIs or Firebase) 67 | google-services.json 68 | 69 | # Freeline 70 | freeline.py 71 | freeline/ 72 | freeline_project_description.json 73 | 74 | # fastlane 75 | fastlane/report.xml 76 | fastlane/Preview.html 77 | fastlane/screenshots 78 | fastlane/test_output 79 | fastlane/readme.md 80 | 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Navigator 2 | 3 | Android Multi-module navigator, trying to find a way to navigate into a modularized android multi module project 4 | 5 | # Import 6 | 7 | ```groovy 8 | implementation "com.github.florent37:navigator:1.0.0" 9 | ``` 10 | 11 | # Multi module sample 12 | 13 | ``` 14 | project 15 | | 16 | \--app 17 | | 18 | \--routing 19 | | 20 | \--splash 21 | | 22 | \--home 23 | | 24 | \--user 25 | ``` 26 | 27 | And we want the next screen flow 28 | ``` 29 | app --[directly starts]--> Splash --> Home --[clicks on an user]--> User(id) 30 | ``` 31 | 32 | - `splash` should only know `routing` 33 | - `home` should only know `routing` 34 | - `user` should only know `routing` 35 | - `app` should only know `routing` to present the splash 36 | - `routing` should not know others modules 37 | 38 | # Define routes 39 | 40 | In a dedicated module, created shared routes into a `Route` object 41 | 42 | in module `routing` : 43 | ```kotlin 44 | object Routes { 45 | 46 | object Splash : Route("/") 47 | 48 | object Home : Route("/home") 49 | 50 | object User : RouteWithParams("/user/{userId}") { 51 | class UserParams(val userId: String) : Param 52 | } 53 | } 54 | ``` 55 | 56 | # Bind routes 57 | 58 | You need to associate an intent to each `Route` in your feature's module 59 | 60 | in module `splash` : 61 | ```kotlin 62 | Routes.Splash.register { context -> 63 | Intent(context, SplashActivity::class.java) 64 | } 65 | ``` 66 | 67 | If you want an android module to register automatically its route, 68 | you can bind using [applicationprovider's auto providers](https://github.com/florent37/ApplicationProvider) : 69 | 70 | ```kotlin 71 | //automatically launched after application's onCreate() 72 | class RouteProvider : Provider() { 73 | override fun provide() { 74 | Routes.Splash.register { context -> 75 | Intent(context, SplashActivity::class.java) 76 | } 77 | } 78 | } 79 | ``` 80 | 81 | then declare it in your module's AndroidManifest.xml 82 | 83 | ```xml 84 | 87 | ``` 88 | 89 | # Navigation 90 | 91 | ## Push 92 | 93 | You can push a route from an activity (or fragment) using 94 | 95 | ```kotlin 96 | Navigator.of(this).push(Routes.Home) 97 | ``` 98 | 99 | or by its path 100 | 101 | ```kotlin 102 | Navigator.of(this).push("/home/") 103 | ``` 104 | 105 | 106 | ![Push](./medias/splash_push.png) 107 | 108 | You can also change the route from anywhere (eg: an android ViewModel) using `Navigator.current()` 109 | 110 | ```kotlin 111 | Navigator.current()?.push(Routes.Home) 112 | ``` 113 | 114 | ## A route with parameters 115 | 116 | Navigating to a route with parameters forces to specify values 117 | ```kotlin 118 | Navigator.of(this).push(Routes.User, UserParams(userId= "3")) 119 | ``` 120 | 121 | You can retrieve them using kotlin's delegated properties 122 | 123 | ```kotlin 124 | class UserActivity : Activity { 125 | private val args by parameter() 126 | 127 | override fun onCreate(savedInstanceState: Bundle?) { 128 | super.onCreate(savedInstanceState) 129 | 130 | val userId = args.userId 131 | ``` 132 | 133 | You can also call it by path's params 134 | ```kotlin 135 | Navigator.of(this).push("/user/3") 136 | ``` 137 | 138 | It will use [moshi](https://github.com/square/moshi) to create the parameter from path params 139 | 140 | ## Pop 141 | 142 | ```kotlin 143 | Navigator.of(this).pop() 144 | ``` 145 | 146 | ## PushReplacement 147 | 148 | If you want to replace the current screen 149 | 150 | ```kotlin 151 | Navigator.current()?.pushReplacement(Routes.Splash) 152 | ``` 153 | 154 | # Route Flavors 155 | 156 | A flavor is an endpoint of a route, you can use them to navigate to an Activity's BottomNavigation item 157 | 158 | ```kotlin 159 | object Home : Route("/home/") { 160 | object UserTabs : Flavor(this,"home/tabUsers") 161 | 162 | object PostsTabs : FlavorWithParams(this,"home/tabPosts") { 163 | class Params(val userId: Int?) : Parameter() 164 | } 165 | } 166 | ``` 167 | 168 | You can push this like a route 169 | 170 | ```kotlin 171 | Navigator.current()?.push(Routes.Home.UserTabs) 172 | ``` 173 | 174 | ![Push](./medias/flavor.png) 175 | 176 | And bind this into your Activity 177 | 178 | ```kotlin 179 | class HomeActivity : AppCompatActivity(R.layout.activity_home) { 180 | 181 | override fun onCreate(savedInstanceState: Bundle?) { 182 | super.onCreate(savedInstanceState) 183 | 184 | viewPager.adapter = ViewPagerAdapter( 185 | supportFragmentManager 186 | ) 187 | 188 | bottomNav.setupWithViewPager(viewPager) 189 | 190 | bindFlavor(Routes.Home.UserTabs, intent) 191 | .withBottomNav(bottomNav, R.id.tabUsers) 192 | 193 | bindFlavor(Routes.Home.PostsTabs, intent) 194 | .withBottomNav(bottomNav, R.id.tabPosts) 195 | 196 | } 197 | } 198 | ``` 199 | 200 | And to retrieve a flavor parameter 201 | 202 | ```kotlin 203 | class PostsFragment : Fragment { 204 | private var args by optionalFlavorParameter() 205 | 206 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 207 | super.onViewCreated(view, savedInstanceState) 208 | 209 | val userId = args.userId 210 | ``` 211 | 212 | # Credits 213 | 214 | Author: Florent Champigny [http://www.florentchampigny.com/](http://www.florentchampigny.com/) 215 | 216 | 217 | License 218 | -------- 219 | 220 | Copyright 2019 Florent37, Inc. 221 | 222 | Licensed under the Apache License, Version 2.0 (the "License"); 223 | you may not use this file except in compliance with the License. 224 | You may obtain a copy of the License at 225 | 226 | http://www.apache.org/licenses/LICENSE-2.0 227 | 228 | Unless required by applicable law or agreed to in writing, software 229 | distributed under the License is distributed on an "AS IS" BASIS, 230 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 231 | See the License for the specific language governing permissions and 232 | limitations under the License. 233 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.3.60' 5 | ext.coroutines_version = '1.3.3' 6 | ext.lifecycle_version = '2.2.0-rc03' 7 | ext.koin_version = "2.0.1" 8 | ext.applicationprovider_version = "1.0.4" 9 | 10 | repositories { 11 | google() 12 | jcenter() 13 | } 14 | dependencies { 15 | classpath 'com.android.tools.build:gradle:3.5.2' 16 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 17 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 18 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' 19 | // NOTE: Do not place your application dependencies here; they belong 20 | // in the individual module build.gradle files 21 | } 22 | } 23 | 24 | allprojects { 25 | repositories { 26 | google() 27 | jcenter() 28 | } 29 | } 30 | 31 | task clean(type: Delete) { 32 | delete rootProject.buildDir 33 | } 34 | 35 | 36 | ext { 37 | COMPILE_SDK = 28 38 | TARGET_SDK = 28 39 | MIN_SDK = 14 40 | 41 | libraryVersion = "1.0.0" 42 | } 43 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | -------------------------------------------------------------------------------- /gradle/bintray-android-v1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | version = libraryVersion 4 | 5 | task sourcesJar(type: Jar) { 6 | from android.sourceSets.main.java.srcDirs 7 | classifier = 'sources' 8 | } 9 | 10 | task javadoc(type: Javadoc) { 11 | source = android.sourceSets.main.java.srcDirs 12 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 13 | } 14 | 15 | task javadocJar(type: Jar, dependsOn: javadoc) { 16 | classifier = 'javadoc' 17 | from javadoc.destinationDir 18 | } 19 | artifacts { 20 | //archives javadocJar 21 | archives sourcesJar 22 | } 23 | 24 | def _user = System.getenv("BINTRAY_USER") 25 | def _key = System.getenv("BINTRAY_API_KEY") 26 | def _passphrase = System.getenv("BINTRAY_PASSPHRASE") 27 | 28 | if(project.rootProject.file('local.properties').exists() && (_user == null || _user.isEmpty())){ 29 | Properties properties = new Properties() 30 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 31 | 32 | _user = properties.getProperty("bintray.user") 33 | _key = properties.getProperty("bintray.apikey"); 34 | _passphrase = properties.getProperty("bintray.gpg.password") 35 | } 36 | 37 | // Bintray 38 | 39 | bintray { 40 | user = _user 41 | key = _key 42 | override = true 43 | configurations = ['archives'] 44 | pkg { 45 | repo = bintrayRepo 46 | name = bintrayName 47 | desc = libraryDescription 48 | userOrg = orgName 49 | websiteUrl = siteUrl 50 | vcsUrl = gitUrl 51 | licenses = allLicenses 52 | publish = true 53 | publicDownloadNumbers = true 54 | version { 55 | desc = libraryDescription 56 | gpg { 57 | sign = true //Determines whether to GPG sign the files. The default is false 58 | passphrase = _passphrase 59 | //Optional. The passphrase for GPG signing' 60 | } 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /gradle/bintray-java-v1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | version = libraryVersion 4 | 5 | task sourcesJar(type: Jar) { 6 | from sourceSets.main.allSource 7 | classifier = 'sources' 8 | } 9 | 10 | task javadocJar(type: Jar, dependsOn: javadoc) { 11 | classifier = 'javadoc' 12 | from javadoc.destinationDir 13 | } 14 | artifacts { 15 | archives javadocJar 16 | archives sourcesJar 17 | } 18 | 19 | // Bintray 20 | 21 | def _user = System.getenv("BINTRAY_USER") 22 | def _key = System.getenv("BINTRAY_API_KEY") 23 | def _passphrase = System.getenv("BINTRAY_PASSPHRASE") 24 | 25 | if(project.rootProject.file('local.properties').exists() && (_user == null || _user.isEmpty())){ 26 | Properties properties = new Properties() 27 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 28 | 29 | _user = properties.getProperty("bintray.user") 30 | _key = properties.getProperty("bintray.apikey"); 31 | _passphrase = properties.getProperty("bintray.gpg.password") 32 | } 33 | 34 | bintray { 35 | user = _user 36 | key = _key 37 | 38 | configurations = ['archives'] 39 | pkg { 40 | repo = bintrayRepo 41 | name = bintrayName 42 | desc = libraryDescription 43 | userOrg = orgName 44 | websiteUrl = siteUrl 45 | vcsUrl = gitUrl 46 | licenses = ['Apache-2.0'] 47 | publish = true 48 | publicDownloadNumbers = true 49 | version { 50 | desc = libraryDescription 51 | gpg { 52 | sign = true //Determines whether to GPG sign the files. The default is false 53 | passphrase = _passphrase 54 | //Optional. The passphrase for GPG signing' 55 | } 56 | } 57 | } 58 | } 59 | 60 | //from https://github.com/workarounds/bundler/blob/master/gradle/bintray-java-v1.gradle -------------------------------------------------------------------------------- /gradle/install-v1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | 3 | group = publishedGroupId // Maven Group ID for the artifact 4 | 5 | install { 6 | repositories.mavenInstaller { 7 | // This generates POM.xml with proper parameters 8 | pom { 9 | project { 10 | packaging 'aar' 11 | groupId publishedGroupId 12 | artifactId artifact 13 | 14 | // Add your description here 15 | name libraryName 16 | description libraryDescription 17 | url siteUrl 18 | 19 | // Set your license 20 | licenses { 21 | license { 22 | name licenseName 23 | url licenseUrl 24 | } 25 | } 26 | developers { 27 | developer { 28 | id developerId 29 | name developerName 30 | email developerEmail 31 | } 32 | } 33 | scm { 34 | connection gitUrl 35 | developerConnection gitUrl 36 | url siteUrl 37 | 38 | } 39 | } 40 | } 41 | } 42 | } 43 | 44 | //from https://github.com/workarounds/bundler/blob/master/gradle/install-v1.gradle -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Dec 12 16:25:38 CET 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /medias/flavor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/medias/flavor.png -------------------------------------------------------------------------------- /medias/splash_push.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/medias/splash_push.png -------------------------------------------------------------------------------- /navigator/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /navigator/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdkVersion project.COMPILE_SDK 6 | 7 | defaultConfig { 8 | minSdkVersion project.MIN_SDK 9 | targetSdkVersion project.TARGET_SDK 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | 14 | compileOptions { 15 | targetCompatibility 1.8 16 | sourceCompatibility 1.8 17 | } 18 | } 19 | 20 | dependencies { 21 | api "com.github.florent37:applicationprovider:$applicationprovider_version" 22 | api "androidx.fragment:fragment:1.1.0" 23 | api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 24 | api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" 25 | api "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" 26 | implementation 'com.squareup.okio:okio:2.2.2' 27 | 28 | api "com.squareup.moshi:moshi:1.9.2" 29 | api "com.squareup.moshi:moshi-kotlin:1.9.2" 30 | 31 | api 'com.google.android.material:material:1.0.0' 32 | } 33 | 34 | ext { 35 | bintrayRepo = 'maven' 36 | bintrayName = 'navigator' 37 | orgName = 'florent37' 38 | 39 | publishedGroupId = 'com.github.florent37' 40 | libraryName = 'Navigator' 41 | artifact = 'navigator' 42 | 43 | libraryDescription = 'Navigator' 44 | 45 | siteUrl = 'https://github.com/florent37/Navigator' 46 | gitUrl = 'https://github.com/florent37/Navigator.git' 47 | 48 | libraryVersion = rootProject.ext.libraryVersion 49 | 50 | developerId = 'florent37' 51 | developerName = 'florent37' 52 | developerEmail = 'champigny.florent@gmail.com' 53 | 54 | licenseName = 'The Apache Software License, Version 2.0' 55 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' 56 | allLicenses = ["Apache-2.0"] 57 | } 58 | 59 | 60 | apply from: rootProject.file('gradle/install-v1.gradle') 61 | apply from: rootProject.file('gradle/bintray-android-v1.gradle') -------------------------------------------------------------------------------- /navigator/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/navigator/consumer-rules.pro -------------------------------------------------------------------------------- /navigator/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /navigator/src/androidTest/java/com/github/florent37/navigator/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.navigator.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /navigator/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/Destination.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import com.github.florent37.navigator.uri.PathMatcher 4 | 5 | interface Destination { 6 | val path: String 7 | val paths: List 8 | val pathMatchers : List 9 | } -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/Flavor.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | import com.github.florent37.navigator.AllFlavors._allFlavors 6 | import com.github.florent37.navigator.exceptions.MissingFlavorImplementation 7 | import com.github.florent37.navigator.uri.PathMatcher 8 | 9 | object AllFlavors { 10 | internal val _allFlavors = mutableListOf>() 11 | val allFlavors: List> 12 | get() = _allFlavors 13 | 14 | @Throws(MissingFlavorImplementation::class) 15 | fun assertAllFlavorsImplemented(){ 16 | allFlavors.forEach { 17 | if(!Navigator.hasImplementation(it)){ 18 | throw MissingFlavorImplementation(it.path) 19 | } 20 | } 21 | } 22 | } 23 | 24 | /** 25 | * Base class of flavors 26 | * 27 | * yoy can associate a flavor with an activity 28 | * `TheFlavor.registerActivity()` 29 | */ 30 | @Suppress("LeakingThis") 31 | abstract class AbstractFlavor(val route: R, path: String) : Destination { 32 | 33 | override val path: String = if(path.startsWith(route.path)){ 34 | path 35 | } else { 36 | val routePath = if(route.path.endsWith("/")) route.path.substring(startIndex = 0, endIndex = route.path.length-1) else route.path 37 | val flavorPath = if(path.startsWith("/")) path else "/$path" 38 | routePath + flavorPath 39 | } 40 | 41 | private val _pathMatchers: MutableList = mutableListOf() 42 | 43 | inline fun registerActivity(noinline intentParameter: INTENT_PARAMETER? = null) { 44 | Navigator.registerRoute(this) { context -> 45 | Intent(context, T::class.java).also { intentParameter?.invoke(it) } 46 | } 47 | } 48 | 49 | init { 50 | _allFlavors.add(this) 51 | addPath(this.path) 52 | } 53 | 54 | fun register(creator: INTENT_CREATOR) { 55 | Navigator.registerRoute(this, creator) 56 | } 57 | 58 | fun addPath(path: String) { 59 | _pathMatchers.add(PathMatcher(path)) 60 | } 61 | 62 | fun removePath(path: String) { 63 | _pathMatchers.removeAll { it.path == path } 64 | } 65 | 66 | fun clear() { 67 | Navigator.clearRoute(this) 68 | } 69 | 70 | override val pathMatchers 71 | get() = _pathMatchers.toMutableList() 72 | 73 | override val paths 74 | get() = pathMatchers.map { it.path } 75 | 76 | } 77 | 78 | /** 79 | * A Flavor without parameter 80 | * 81 | * object MyRoute : Route("..."){ 82 | * object MyFlavor : Flavor("theName") 83 | * } 84 | */ 85 | abstract class Flavor( 86 | route: R, 87 | name: String 88 | ) : AbstractFlavor(route, name) 89 | 90 | /** 91 | * A Flavor with parameter 92 | * 93 | * object MyRoute : Route("..."){ 94 | * object MyFlavor : FlavorWithParams("theName") { 95 | * class FlavorParameter(val param1: Int, val param2: String) : Param 96 | * } 97 | * } 98 | */ 99 | abstract class FlavorWithParams( 100 | route: R, 101 | name: String 102 | ) : AbstractFlavor(route, name) -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/FlavorBinder.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | import android.util.Log 6 | import androidx.fragment.app.FragmentManager 7 | import androidx.fragment.app.FragmentTransaction 8 | import androidx.lifecycle.LifecycleOwner 9 | import androidx.lifecycle.lifecycleScope 10 | import com.google.android.material.bottomnavigation.BottomNavigationView 11 | import kotlinx.coroutines.CoroutineScope 12 | import kotlinx.coroutines.flow.launchIn 13 | import kotlinx.coroutines.flow.onEach 14 | 15 | fun > Activity.invokeOnRouteFlavor( 16 | intent: Intent? = null, 17 | configuration: C, 18 | block: C.() -> Unit 19 | ) { 20 | val intenT = intent ?: this.intent 21 | intenT.extras?.apply { 22 | try { 23 | val subRoute = getString(SUB_ROUTE_INTENT_KEY) 24 | if (subRoute == configuration.path) { 25 | block(configuration) 26 | } 27 | } catch (t: Throwable) { 28 | Log.e("Navigator", t.message, t) 29 | } 30 | } 31 | } 32 | 33 | class FlavorBinder>(val activity: Activity, val intent: Intent?, val flavor: F) { 34 | fun withAction(block: F.() -> Unit) = this.also { 35 | activity.invokeOnRouteFlavor(intent, flavor, block) 36 | } 37 | } 38 | 39 | fun > FlavorBinder.withBottomNav(bottomNav: BottomNavigationView?, id: Int) = this.also { 40 | withAction { 41 | bottomNav?.selectedItemId = id 42 | } 43 | } 44 | 45 | fun > Activity.bindFlavor(flavor: F) = FlavorBinder(this, this.intent, flavor) 46 | fun > Activity.bindFlavor(flavor: F, intent: Intent?) = FlavorBinder(this, intent, flavor) 47 | 48 | fun > FlavorBinder.withFragmentTransaction( 49 | fragmentManager: FragmentManager, 50 | block: FragmentTransaction.() -> FragmentTransaction 51 | ) = this.also { 52 | withAction { 53 | block(fragmentManager.beginTransaction()).commitAllowingStateLoss() 54 | } 55 | } 56 | 57 | fun Intent?.updateWith(newIntent: Intent?) : Intent? { 58 | return this?.apply { 59 | newIntent?.let { 60 | putExtras(newIntent) 61 | } 62 | } ?: newIntent 63 | } 64 | 65 | fun LifecycleOwner.onNavigationChange(block: (Destination?) -> Unit) = lifecycleScope.onNavigationChange(block) 66 | 67 | fun CoroutineScope.onNavigationChange(block: (Destination?) -> Unit) { 68 | Navigator.navigation.onEach { 69 | //reload when the navigation changes 70 | block(it) 71 | }.launchIn(this) 72 | } -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/IntentKt.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | 6 | /** 7 | * Updated an intent adding all extras of given arg 8 | * from Activity : 9 | * 10 | * this.updateIntent(newIntent) 11 | */ 12 | fun Activity.updateIntent(newIntent: Intent?){ 13 | this.intent = this.intent?.updateWith(newIntent) 14 | } 15 | -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/Navigator.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import android.app.Activity 4 | import android.app.Application 5 | import android.content.Context 6 | import android.content.Intent 7 | import android.util.Log 8 | import androidx.fragment.app.Fragment 9 | import com.github.florent37.application.provider.ActivityProvider 10 | import com.github.florent37.navigator.exceptions.AlreadyRegisteredException 11 | import com.github.florent37.navigator.exceptions.MissingRouteImplementation 12 | import com.github.florent37.navigator.starter.DesintationWithParams 13 | import com.github.florent37.navigator.starter.NavigatorStarter 14 | import com.github.florent37.navigator.starter.StarterHandler 15 | import kotlinx.coroutines.GlobalScope 16 | import kotlinx.coroutines.channels.ConflatedBroadcastChannel 17 | import kotlinx.coroutines.flow.Flow 18 | import kotlinx.coroutines.flow.asFlow 19 | import kotlinx.coroutines.flow.collect 20 | import kotlinx.coroutines.launch 21 | import java.util.* 22 | 23 | typealias INTENT_CREATOR = (Context) -> Intent 24 | 25 | class Routing(val creator: INTENT_CREATOR) 26 | 27 | class RouteListener { 28 | private val route = Stack() 29 | private val _currentRoute = ConflatedBroadcastChannel() 30 | val currentRoute = _currentRoute.asFlow() 31 | 32 | fun last(): Destination? = try { 33 | route.peek() 34 | } catch (t: Throwable) { 35 | null 36 | } 37 | 38 | fun update() { 39 | _currentRoute.offer(last()) 40 | Log.d("RouteListener", route.toString()) 41 | } 42 | 43 | fun pop() { 44 | try { 45 | route.pop() 46 | } catch (t: Throwable) { 47 | } catch (e: Exception) { 48 | } 49 | update() 50 | } 51 | 52 | fun pushReplacement(destination: Destination) { 53 | try { 54 | route.pop() 55 | } catch (t: Throwable) { 56 | } catch (e: Exception) { 57 | } 58 | route.push(destination) 59 | } 60 | 61 | fun push(destination: Destination) { 62 | route.push(destination) 63 | } 64 | 65 | fun navigationStack(): List { 66 | return route 67 | } 68 | } 69 | 70 | /** 71 | * Singleton naviagor object, use this one to push or pop routes 72 | * @see Navigator.push 73 | * @see Navigator.pushForResult 74 | * @see Navigator.pop 75 | */ 76 | object Navigator { 77 | private val routing = mutableMapOf() 78 | 79 | private val routeListener = RouteListener() 80 | 81 | init { 82 | listenToBackpressed() 83 | listenActivityChanged() 84 | } 85 | 86 | private val TAG = "Navigator_TAG" 87 | 88 | 89 | /** 90 | * Try to listen to backpress, without implementing it into activity.onbackpress 91 | * used to pop last route from RouteListener 92 | * A backpressed => OnPause => OnStop => OnDestroy (without any other state) 93 | */ 94 | private fun listenToBackpressed() { 95 | GlobalScope.launch { 96 | ActivityProvider.listenToOnBackPress.collect { 97 | routeListener.pop() 98 | } 99 | } 100 | } 101 | 102 | 103 | /** 104 | * Try to listen to activities changes 105 | * update listeners 106 | * A backpressed => OnPause => OnStop => OnDestroy (without any other state) 107 | */ 108 | private fun listenActivityChanged() { 109 | GlobalScope.launch { 110 | ActivityProvider.listenActivityChanged() 111 | .collect { 112 | Log.d("activity_changed", it.name) 113 | routeListener.update() 114 | } 115 | } 116 | } 117 | 118 | /** 119 | * Used by routes, register a route to an intent creator 120 | */ 121 | fun registerRoute(route: AbstractRoute, creator: INTENT_CREATOR) { 122 | if (routing.containsKey(route)) { 123 | throw AlreadyRegisteredException(route.path) 124 | } else { 125 | routing[route] = Routing(creator) 126 | } 127 | } 128 | 129 | fun clearRoute(destination: Destination) { 130 | routing.remove(destination) 131 | } 132 | 133 | 134 | /** 135 | * Used by flavors, register a flavor to an intent creator 136 | */ 137 | fun registerRoute(flavor: AbstractFlavor<*>, creator: INTENT_CREATOR) { 138 | if (routing.containsKey(flavor)) { 139 | throw AlreadyRegisteredException(flavor.path) 140 | } else { 141 | routing[flavor] = Routing(creator) 142 | } 143 | } 144 | 145 | /** 146 | * Access the current stack of routes & flavors 147 | */ 148 | val navigationStack: List 149 | get() = routeListener.navigationStack() 150 | 151 | /** 152 | * Listen to the current stack of routes & flavors 153 | */ 154 | val navigation: Flow 155 | get() = routeListener.currentRoute 156 | 157 | /** 158 | * Retrieve the navigation starter from an application context 159 | */ 160 | fun of(application: Application) = 161 | NavigatorStarter( 162 | StarterHandler.ApplicationStarter( 163 | application 164 | ), 165 | routeListener, 166 | routing.toMap() 167 | ) 168 | 169 | /** 170 | * Retrieve the navigation starter from an activity 171 | */ 172 | fun of(activity: Activity) = 173 | NavigatorStarter( 174 | StarterHandler.ActivityStarter( 175 | activity 176 | ), 177 | routeListener, 178 | routing.toMap() 179 | ) 180 | 181 | /** 182 | * Retrieve the navigation starter from a fragment 183 | */ 184 | fun of(fragment: Fragment) = 185 | NavigatorStarter( 186 | StarterHandler.FragmentStarter( 187 | fragment 188 | ), 189 | routeListener, 190 | routing.toMap() 191 | ) 192 | 193 | /** 194 | * Retrieve the navigation without any context 195 | * It uses ActivityProvider from https://github.com/florent37/ApplicationProvider 196 | */ 197 | fun current(): NavigatorStarter? = ActivityProvider.currentActivity?.let { activity -> 198 | NavigatorStarter( 199 | StarterHandler.ActivityStarter( 200 | activity 201 | ), 202 | routeListener, 203 | routing.toMap() 204 | ) 205 | } 206 | 207 | /** 208 | * Search a route or flavor by path 209 | */ 210 | fun findDestination(path: String): Destination? { 211 | routing.forEach { 212 | val route = it.key 213 | if (it.key.path == path) { 214 | return route 215 | } 216 | } 217 | return null 218 | } 219 | 220 | /** 221 | * Search a route by path 222 | */ 223 | fun findRoute(path: String): AbstractRoute? { 224 | return findDestination(path) as? AbstractRoute 225 | } 226 | 227 | /** 228 | * Search a flavor by path 229 | */ 230 | fun findFlavor(path: String): AbstractFlavor<*>? { 231 | return findDestination(path) as? AbstractFlavor<*> 232 | } 233 | 234 | fun hasImplementation(destination: Destination): Boolean { 235 | return routing.containsKey(destination) 236 | } 237 | 238 | fun findDestinationWithParams(path: String) : DesintationWithParams? { 239 | //try with routes 240 | for ((destination, routing) in routing) { 241 | destination.pathMatchers.forEach { pathMatcher -> 242 | if(pathMatcher.matches(url= path)){ 243 | val parametersValues = pathMatcher.parametersValues(url= path) 244 | return DesintationWithParams( 245 | destination = destination, 246 | params = parametersValues, 247 | routing= routing 248 | ) 249 | } 250 | } 251 | } 252 | //try with flavors 253 | for (flavor in AllFlavors.allFlavors) { 254 | flavor.pathMatchers.forEach { pathMatcher -> 255 | if(pathMatcher.matches(url= path)){ 256 | val routing = routing[flavor.route] ?: throw MissingRouteImplementation(flavor.route.path) 257 | val parametersValues = pathMatcher.parametersValues(url= path) 258 | return DesintationWithParams( 259 | destination = flavor, 260 | params = parametersValues, 261 | routing= routing 262 | ) 263 | } 264 | } 265 | } 266 | return null 267 | } 268 | 269 | } -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/Param.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import android.app.Activity 4 | import androidx.fragment.app.Fragment 5 | import com.github.florent37.navigator.exceptions.MissingRequiredParameter 6 | import com.squareup.moshi.Moshi 7 | import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory 8 | import java.io.Serializable 9 | import kotlin.reflect.KProperty 10 | 11 | open class Param : Serializable 12 | 13 | /** 14 | * Retrieve a non nullable route Parameter 15 | * 16 | * val args = routeParamValue 17 | */ 18 | fun Activity.routeParamValue(parameterClazz: Class): T { 19 | return when { 20 | intent.hasExtra(ROUTE_ARGS_KEY) -> { 21 | this.intent.getSerializableExtra(ROUTE_ARGS_KEY) as T 22 | } 23 | intent.hasExtra(ROUTE_KEY_STR_PARAMS) -> { 24 | val jsonString = intent.getStringExtra(ROUTE_KEY_STR_PARAMS) 25 | jsonString.fromJson(parameterClazz) 26 | } 27 | else -> { 28 | throw MissingRequiredParameter(parameterClazz.simpleName) 29 | } 30 | } 31 | } 32 | 33 | /** 34 | * Retrieve a non nullable flavor Parameter 35 | * 36 | * val args = flavorParamValue 37 | */ 38 | fun Activity.flavorParamValue(parameterClazz: Class): T { 39 | return when { 40 | intent.hasExtra(ROUTE_FLAVOR_ARGS_KEY) -> { 41 | this.intent.getSerializableExtra(ROUTE_FLAVOR_ARGS_KEY) as T 42 | } 43 | intent.hasExtra(ROUTE_KEY_STR_PARAMS) -> { 44 | val jsonString = intent.getStringExtra(ROUTE_KEY_STR_PARAMS) 45 | jsonString.fromJson(parameterClazz) 46 | } 47 | else -> { 48 | throw MissingRequiredParameter(parameterClazz.simpleName) 49 | } 50 | } 51 | } 52 | 53 | internal var moshi : Moshi? = null 54 | 55 | internal fun String.fromJson(clazz: Class) : T { 56 | if(moshi == null) { 57 | moshi = Moshi.Builder() 58 | .add(KotlinJsonAdapterFactory()) 59 | .build() 60 | } 61 | val jsonAdapter = moshi!!.adapter(clazz) 62 | return jsonAdapter.fromJson(this) as T 63 | } 64 | 65 | /** 66 | * Retrieve a nullable route Parameter 67 | * 68 | * val args : MyRoute.MyParam? = optionalRouteParamValue 69 | */ 70 | fun Activity.optionalRouteParamValue(parameterClazz: Class): T? { 71 | return when { 72 | intent.hasExtra(ROUTE_ARGS_KEY) -> { 73 | this.intent?.getSerializableExtra(ROUTE_ARGS_KEY) as? T 74 | } 75 | intent.hasExtra(ROUTE_KEY_STR_PARAMS) -> { 76 | val jsonString = intent.getStringExtra(ROUTE_KEY_STR_PARAMS) 77 | jsonString.fromJson(parameterClazz) 78 | } 79 | else -> { 80 | throw MissingRequiredParameter(parameterClazz.simpleName) 81 | } 82 | } 83 | } 84 | 85 | /** 86 | * Retrieve a nullable flavor Parameter 87 | * 88 | * val args : MyRoute.MyParam? = optionalFlavorParamValue 89 | */ 90 | fun Activity.optionalFlavorParamValue(parameterClazz: Class): T? { 91 | return when { 92 | intent.hasExtra(ROUTE_FLAVOR_ARGS_KEY) -> { 93 | this.intent?.getSerializableExtra(ROUTE_FLAVOR_ARGS_KEY) as? T 94 | } 95 | intent.hasExtra(ROUTE_KEY_STR_PARAMS) -> { 96 | val jsonString = intent.getStringExtra(ROUTE_KEY_STR_PARAMS) 97 | jsonString.fromJson(parameterClazz) 98 | } 99 | else -> { 100 | throw MissingRequiredParameter(parameterClazz.simpleName) 101 | } 102 | } 103 | } 104 | 105 | class ParameterDelegate(val parameterClazz: Class, val flavor: Boolean = false) { 106 | operator fun getValue( 107 | thisRef: Any?, 108 | property: KProperty<*> 109 | ): T { 110 | // return value 111 | if (thisRef != null && thisRef is Activity) { 112 | return if (flavor) { 113 | thisRef.flavorParamValue(parameterClazz) 114 | } else { 115 | thisRef.routeParamValue(parameterClazz) 116 | } 117 | } else if (thisRef != null && thisRef is Fragment && thisRef.activity != null) { 118 | return if (flavor) { 119 | thisRef.activity!!.flavorParamValue(parameterClazz) 120 | } else { 121 | thisRef.activity!!.routeParamValue(parameterClazz) 122 | } 123 | } 124 | throw MissingRequiredParameter(parameterClazz.simpleName) 125 | } 126 | 127 | operator fun setValue( 128 | thisRef: Any?, 129 | property: KProperty<*>, value: T 130 | ) { 131 | //not implemented 132 | } 133 | } 134 | 135 | class OptionalParameterDelegate( 136 | val parameterClazz: Class, 137 | val flavor: Boolean = false 138 | ) { 139 | operator fun getValue( 140 | thisRef: Any?, 141 | property: KProperty<*> 142 | ): T? { 143 | try { 144 | // return value 145 | return if (thisRef != null && thisRef is Activity) { 146 | if (flavor) { 147 | thisRef.optionalFlavorParamValue(parameterClazz) 148 | } else { 149 | thisRef.optionalRouteParamValue(parameterClazz) 150 | } 151 | } else if (thisRef != null && thisRef is Fragment && thisRef.activity != null) { 152 | if (flavor) { 153 | thisRef.activity?.optionalFlavorParamValue(parameterClazz) 154 | } else { 155 | thisRef.activity?.optionalRouteParamValue(parameterClazz) 156 | } 157 | } else { 158 | null 159 | } 160 | } catch (t: Throwable) { 161 | return null 162 | } 163 | } 164 | 165 | operator fun setValue( 166 | thisRef: Any?, 167 | property: KProperty<*>, value: T? 168 | ) { 169 | //not implemented 170 | } 171 | } 172 | 173 | /** 174 | * Retrieve a non nullable route Parameter 175 | * 176 | * val args : MyRoute.MyParam by parameter() 177 | */ 178 | inline fun Activity.parameter() = 179 | ParameterDelegate(parameterClazz = T::class.java) 180 | 181 | /** 182 | * Retrieve a nullable route Parameter 183 | * 184 | * val args : MyRoute.MyParam? by parameter() 185 | */ 186 | inline fun Activity.optionalParameter() = 187 | OptionalParameterDelegate(parameterClazz = T::class.java) 188 | 189 | /** 190 | * Retrieve a non nullable route Parameter 191 | * 192 | * val args : MyRoute.MyParam by parameter() 193 | */ 194 | inline fun Fragment.routeParameter() = 195 | ParameterDelegate(parameterClazz = T::class.java, flavor = false) 196 | 197 | /** 198 | * Retrieve a nullable route Parameter 199 | * 200 | * val args : MyRoute.MyParam? by parameter() 201 | */ 202 | inline fun Fragment.optionalRouteParameter() = 203 | OptionalParameterDelegate(parameterClazz = T::class.java, flavor = false) 204 | 205 | 206 | /** 207 | * Retrieve a non nullable flavor Parameter 208 | * 209 | * val args : MyRoute.MyParam by flavorParameter() 210 | */ 211 | inline fun Fragment.flavorParameter() = 212 | ParameterDelegate(parameterClazz = T::class.java, flavor = true) 213 | 214 | /** 215 | * Retrieve a nullable flavor Parameter 216 | * 217 | * val args : MyRoute.MyParam by flavorParameter() 218 | */ 219 | inline fun Fragment.optionalFlavorParameter() = 220 | OptionalParameterDelegate(parameterClazz = T::class.java, flavor = true) 221 | 222 | /** 223 | * Retrieve a non nullable flavor Parameter 224 | * 225 | * val args : MyRoute.MyParam by flavorParameter() 226 | */ 227 | inline fun Activity.flavorParameter() = 228 | ParameterDelegate(parameterClazz = T::class.java, flavor = true) 229 | 230 | /** 231 | * Retrieve a nullable flavor Parameter 232 | * 233 | * val args : MyRoute.MyParam by flavorParameter() 234 | */ 235 | inline fun Activity.optionalFlavorParameter() = 236 | OptionalParameterDelegate(parameterClazz = T::class.java, flavor = true) 237 | 238 | 239 | -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/Route.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | import com.github.florent37.navigator.AllRoutes._allRoutes 6 | import com.github.florent37.navigator.exceptions.MissingFlavorImplementation 7 | import com.github.florent37.navigator.exceptions.MissingRouteImplementation 8 | import com.github.florent37.navigator.uri.PathMatcher 9 | 10 | internal const val ROUTE_ARGS_KEY = "__\$__ROUTE_ARGS_KEY__\$__" 11 | internal const val ROUTE_FLAVOR_ARGS_KEY = "__\$_ROUTE_FLAVOR_ARGS_KEY__\$__" 12 | internal const val ROUTE_INTENT_KEY = "__\$__NAVIGATOR_ROUTE_KEY__\$__" 13 | internal const val SUB_ROUTE_INTENT_KEY = "__\$__NAVIGATOR_SUB_ROUTE_KEY__\$__" 14 | 15 | internal const val ROUTE_KEY_STR_PARAMS = "__\$__ROUTE_STR_PARAMS__\$__" 16 | 17 | typealias INTENT_PARAMETER = (Intent) -> Unit 18 | 19 | object AllRoutes { 20 | internal val _allRoutes = mutableListOf() 21 | val allRoutes: List 22 | get() = _allRoutes 23 | 24 | @Throws(MissingRouteImplementation::class, MissingFlavorImplementation::class) 25 | fun assertAllRoutesAndFlavorsImplemented(){ 26 | assertAllRoutesImplemented() 27 | AllFlavors.assertAllFlavorsImplemented() 28 | } 29 | 30 | @Throws(MissingRouteImplementation::class) 31 | fun assertAllRoutesImplemented(){ 32 | allRoutes.forEach { 33 | if(!Navigator.hasImplementation(it)){ 34 | throw MissingRouteImplementation(it.path) 35 | } 36 | } 37 | } 38 | } 39 | 40 | /** 41 | * Base class of routes & routes with parameters 42 | * can associate a route with an activity 43 | * 44 | * `TheRoute.registerActivity()` 45 | */ 46 | @Suppress("LeakingThis") 47 | abstract class AbstractRoute( 48 | override val path: String 49 | ) : Destination { 50 | 51 | private val _pathMatchers = mutableListOf() 52 | 53 | init { 54 | _allRoutes.add(this) 55 | addPath(path) 56 | } 57 | 58 | fun clear() { 59 | Navigator.clearRoute(this) 60 | } 61 | 62 | inline fun registerActivity(noinline intentParameter: INTENT_PARAMETER? = null) { 63 | Navigator.registerRoute(this) { context -> 64 | Intent(context, T::class.java).also { intentParameter?.invoke(it) } 65 | } 66 | } 67 | 68 | fun register(creator: INTENT_CREATOR) { 69 | Navigator.registerRoute(this, creator) 70 | } 71 | 72 | fun addPath(path: String) { 73 | _pathMatchers.add(PathMatcher(path)) 74 | } 75 | 76 | fun removePath(path: String) { 77 | _pathMatchers.removeAll { it.path == path } 78 | } 79 | 80 | override val pathMatchers 81 | get() = _pathMatchers.toMutableList() 82 | 83 | override val paths 84 | get() = pathMatchers.map { it.path } 85 | } 86 | 87 | /** 88 | * A Route without parameter 89 | * 90 | * object MyRoute : Route("theAdress") 91 | */ 92 | open class Route(path: String) : AbstractRoute(path) 93 | 94 | /** 95 | * A Route without parameter 96 | * 97 | * object MyRoute : RouteWithParams("theAdress") { 98 | * class ParameterClass(val param1: Int, val param2: String) : Param 99 | * } 100 | */ 101 | open class RouteWithParams

(path: String) : AbstractRoute(path) { 102 | /* 103 | fun register(creator: (Context, P) -> Intent) { 104 | Navigator.registerRoute(this, creator) 105 | } 106 | */ 107 | } -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/RouteExtensions.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | 6 | inline fun FlavorWithParams.registerActivity( 7 | noinline intentParameter: INTENT_PARAMETER? = null 8 | ) { 9 | Navigator.registerRoute(this) { context -> 10 | Intent(context, T::class.java).also { intentParameter?.invoke(it) } 11 | } 12 | } -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/exceptions/Exceptions.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator.exceptions 2 | 3 | class PathNotFound(path: String): Throwable("no route found for path $path") 4 | class MissingRouteImplementation(argName: String): Throwable("required route implementation $argName") 5 | class MissingFlavorImplementation(argName: String): Throwable("required flavor implementation $argName") 6 | class MissingRequiredParameter(argName: String): Throwable("required parameter $argName not specified") 7 | class MissingIntentThrowable(routeName: String): Throwable("no intent found for $routeName") 8 | class AlreadyRegisteredException(routeName: String): Throwable("route already registered $routeName") -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/starter/DesintationWithParams.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator.starter 2 | 3 | import com.github.florent37.navigator.Destination 4 | import com.github.florent37.navigator.Routing 5 | 6 | data class DesintationWithParams( 7 | val destination: Destination, 8 | val params: Map, 9 | val routing: Routing 10 | ) -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/starter/NavigatorStarter.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator.starter 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import com.github.florent37.navigator.* 6 | import com.github.florent37.navigator.exceptions.MissingIntentThrowable 7 | import com.github.florent37.navigator.exceptions.PathNotFound 8 | import org.json.JSONObject 9 | 10 | typealias IntentConfig = (Intent) -> Unit 11 | 12 | class NavigatorStarter( 13 | private val starterHandler: StarterHandler, 14 | private val routeListener: RouteListener, 15 | private val routing: Map 16 | ) { 17 | fun push(route: T, intentConfig: IntentConfig? = null): Boolean { 18 | val success = 19 | this.startInternal(destination = route, resultCode = null, intentConfig = intentConfig) 20 | if (success) { 21 | routeListener.push(route) 22 | } 23 | return success 24 | } 25 | 26 | fun pushReplacement(route: T, intentConfig: IntentConfig? = null): Boolean { 27 | val success = this.startInternal( 28 | destination = route, 29 | resultCode = null, 30 | intentConfig = intentConfig, 31 | finishActivity = true 32 | ) 33 | 34 | if (success) { 35 | routeListener.pushReplacement(route) 36 | } 37 | return success 38 | } 39 | 40 | fun pop(resultCode: Int, data: Intent?) { 41 | starterHandler.activity?.let { 42 | it.setResult(resultCode, data) 43 | it.finish() 44 | } 45 | } 46 | 47 | fun

> push( 48 | route: T, 49 | arguments: P, 50 | intentConfig: IntentConfig? = null 51 | ): Boolean { 52 | val success = this.startInternal( 53 | destination = route, 54 | resultCode = null, 55 | intentConfig = intentConfig, 56 | routeParameter = arguments 57 | ) 58 | if (success) { 59 | routeListener.push(route) 60 | } 61 | return success 62 | } 63 | 64 | fun

> pushReplacement( 65 | route: T, 66 | arguments: P, 67 | intentConfig: IntentConfig? = null 68 | ): Boolean { 69 | val success = this.startInternal( 70 | destination = route, 71 | resultCode = null, 72 | intentConfig = intentConfig, 73 | routeParameter = arguments, 74 | finishActivity = true 75 | ) 76 | if (success) { 77 | routeListener.pushReplacement(route) 78 | } 79 | return success 80 | } 81 | 82 | fun pushForResult( 83 | route: T, 84 | resultCode: Int, 85 | intentConfig: IntentConfig? = null 86 | ): Boolean { 87 | val success = this.startInternal( 88 | destination = route, 89 | resultCode = resultCode, 90 | intentConfig = intentConfig 91 | ) 92 | if (success) { 93 | routeListener.push(route) 94 | } 95 | return success 96 | } 97 | 98 | fun

> pushForResult( 99 | route: T, 100 | resultCode: Int, 101 | arguments: P, 102 | intentConfig: IntentConfig? = null 103 | ): Boolean { 104 | val success = this.startInternal( 105 | destination = route, 106 | resultCode = resultCode, 107 | intentConfig = intentConfig, 108 | routeParameter = arguments 109 | ) 110 | if (success) { 111 | routeListener.push(route) 112 | } 113 | return success 114 | } 115 | 116 | /** 117 | * For route 118 | * if route has arguments => routeArguments != null 119 | */ 120 | private fun startInternal( 121 | destination: T, 122 | resultCode: Int? = null, 123 | intentConfig: IntentConfig? = null, 124 | routeParameter: Param? = null, 125 | finishActivity: Boolean = false 126 | ): Boolean { 127 | val containRoute = routing.containsKey(destination) 128 | val context = starterHandler.context ?: return false 129 | if (containRoute) { 130 | val intentCreator = routing[destination] 131 | intentCreator?.let { 132 | 133 | val intent: Intent = it.creator(context) 134 | 135 | val extras = Bundle() 136 | routeParameter?.let { 137 | extras.putSerializable(ROUTE_ARGS_KEY, routeParameter) 138 | } 139 | 140 | extras.putString(ROUTE_INTENT_KEY, destination.path) 141 | 142 | intent.putExtras(extras) 143 | 144 | intentConfig?.invoke(intent) 145 | 146 | if (resultCode == null) { 147 | starterHandler.start(intent) 148 | } else { 149 | starterHandler.startForResult(intent, resultCode) 150 | } 151 | 152 | if (finishActivity) { 153 | starterHandler.activity?.finish() 154 | } 155 | } ?: run { 156 | throw MissingIntentThrowable(routeName = destination.path) 157 | } 158 | } else { 159 | throw MissingIntentThrowable(routeName = destination.path) 160 | } 161 | return containRoute 162 | } 163 | 164 | fun > push( 165 | routeConfiguration: T, 166 | intentConfig: IntentConfig? = null 167 | ): Boolean { 168 | val success = this.startInternal( 169 | routeConfiguration = routeConfiguration, 170 | resultCode = null, 171 | intentConfig = intentConfig 172 | ) 173 | if (success) { 174 | routeListener.push(routeConfiguration) 175 | } 176 | return success 177 | } 178 | 179 | fun > push( 180 | routeConfiguration: F, 181 | arguments: FR, 182 | intentConfig: IntentConfig? = null 183 | ): Boolean { 184 | val success = this.startInternal( 185 | routeConfiguration = routeConfiguration, 186 | resultCode = null, 187 | intentConfig = intentConfig, 188 | flavorParameters = arguments 189 | ) 190 | if (success) { 191 | routeListener.push(routeConfiguration) 192 | } 193 | return success 194 | } 195 | 196 | fun , F : FlavorWithParams> push( 197 | routeConfiguration: F, 198 | routeArguments: RP, 199 | arguments: FR, 200 | intentConfig: IntentConfig? = null 201 | ): Boolean { 202 | val success = this.startInternal( 203 | routeConfiguration = routeConfiguration, 204 | resultCode = null, 205 | intentConfig = intentConfig, 206 | routeParameters = routeArguments, 207 | flavorParameters = arguments 208 | ) 209 | if (success) { 210 | routeListener.push(routeConfiguration) 211 | } 212 | return success 213 | } 214 | 215 | fun > pushForResult( 216 | route: T, 217 | resultCode: Int, 218 | intentConfig: IntentConfig? = null 219 | ): Boolean { 220 | val success = this.startInternal( 221 | routeConfiguration = route, 222 | resultCode = resultCode, 223 | intentConfig = intentConfig 224 | ) 225 | if (success) { 226 | routeListener.push(route) 227 | } 228 | return success 229 | } 230 | 231 | fun > pushForResult( 232 | route: T, 233 | resultCode: Int, 234 | arguments: R, 235 | intentConfig: IntentConfig? = null 236 | ): Boolean { 237 | val success = this.startInternal( 238 | routeConfiguration = route, 239 | resultCode = resultCode, 240 | intentConfig = intentConfig, 241 | flavorParameters = arguments 242 | ) 243 | if (success) { 244 | routeListener.push(route) 245 | } 246 | return success 247 | } 248 | 249 | /** 250 | * For flavors 251 | * if flavor has params => flavorParameters != null 252 | * if route has params => routeParameters != null 253 | */ 254 | private fun > startInternal( 255 | routeConfiguration: T, 256 | resultCode: Int? = null, 257 | intentConfig: IntentConfig? = null, 258 | routeParameters: Param? = null, 259 | flavorParameters: Param? = null 260 | ): Boolean { 261 | 262 | val destination = routeConfiguration.route 263 | 264 | val containRoute = routing.containsKey(destination) 265 | val context = starterHandler.context ?: return false 266 | if (containRoute) { 267 | val intentCreator = routing[destination] 268 | intentCreator?.let { 269 | 270 | val extras = Bundle() 271 | 272 | extras.putString(ROUTE_INTENT_KEY, destination.path) 273 | extras.putString(SUB_ROUTE_INTENT_KEY, routeConfiguration.path) 274 | 275 | if (flavorParameters != null) { 276 | extras.putSerializable(ROUTE_FLAVOR_ARGS_KEY, flavorParameters) 277 | } 278 | if (routeParameters != null) { 279 | extras.putSerializable(ROUTE_ARGS_KEY, routeParameters) 280 | } 281 | 282 | val intent: Intent = it.creator(context) 283 | 284 | intent.putExtras(extras) 285 | 286 | intentConfig?.invoke(intent) 287 | 288 | if (resultCode == null) { 289 | starterHandler.start(intent) 290 | } else { 291 | starterHandler.startForResult(intent, resultCode) 292 | } 293 | } ?: run { 294 | throw MissingIntentThrowable(routeName = destination.path) 295 | } 296 | } else { 297 | throw MissingIntentThrowable(routeName = destination.path) 298 | } 299 | return containRoute 300 | } 301 | 302 | fun push(path: String) : Boolean { 303 | return pushInternal(path = path) 304 | } 305 | 306 | fun pushReplacement(path: String) : Boolean { 307 | return pushInternal(path = path) 308 | } 309 | 310 | fun pushForResult(path: String, resultCode: Int) : Boolean { 311 | return pushInternal(path = path, resultCode = resultCode) 312 | } 313 | 314 | private fun pushInternal(path: String, resultCode: Int? = null, finishActivity: Boolean = false) : Boolean { 315 | //try to find by name 316 | Navigator.findDestination(path = path)?.let { destination -> 317 | when (destination) { 318 | is Route -> { 319 | if(finishActivity) pushReplacement(destination) 320 | push(destination) 321 | return true 322 | } 323 | is Flavor<*> -> { 324 | push(destination) 325 | //if(finishActivity) pushReplacement(destination) 326 | //else push(destination) 327 | return true 328 | } 329 | else -> { /* nothing to do */ 330 | } 331 | } 332 | } 333 | //try to find by regex 334 | Navigator.findDestinationWithParams(path = path)?.let { 335 | pushWithParamsInternal( 336 | destinationWithParams = it, 337 | resultCode = resultCode, 338 | finishActivity = finishActivity 339 | ) 340 | return true 341 | } 342 | return false 343 | } 344 | 345 | private fun pushWithParamsInternal( 346 | destinationWithParams: DesintationWithParams, 347 | resultCode: Int? = null, 348 | finishActivity: Boolean = false 349 | ): Boolean { 350 | val context = starterHandler.context ?: return false 351 | val destination = destinationWithParams.destination 352 | 353 | val extras = Bundle() 354 | 355 | if (destination is AbstractFlavor<*>) { 356 | extras.putString(SUB_ROUTE_INTENT_KEY, destination.path) 357 | extras.putString(ROUTE_INTENT_KEY, destination.route.path) 358 | } else { 359 | extras.putString(ROUTE_INTENT_KEY, destination.path) 360 | } 361 | 362 | val json = JSONObject() 363 | destinationWithParams.params.forEach { 364 | json.put(it.key, it.value) 365 | } 366 | val jsonString = json.toString() 367 | 368 | extras.putString(ROUTE_KEY_STR_PARAMS, jsonString) 369 | 370 | destinationWithParams.routing.let { 371 | val intent: Intent = it.creator(context) 372 | 373 | intent.putExtras(extras) 374 | 375 | if (resultCode == null) { 376 | starterHandler.start(intent) 377 | } else { 378 | starterHandler.startForResult(intent, resultCode) 379 | } 380 | 381 | if(finishActivity){ 382 | starterHandler.activity?.finish() 383 | } 384 | } 385 | 386 | return true 387 | } 388 | } -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/starter/StarterHandler.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator.starter 2 | 3 | import android.app.Activity 4 | import android.app.Application 5 | import android.content.Context 6 | import android.content.Intent 7 | import androidx.fragment.app.Fragment 8 | import com.github.florent37.application.provider.ActivityProvider 9 | 10 | sealed class StarterHandler { 11 | abstract val context: Context? 12 | abstract val activity: Activity? 13 | abstract fun start(intent: Intent) 14 | abstract fun startForResult(intent: Intent, code: Int) 15 | 16 | class ActivityStarter(override val activity: Activity) : StarterHandler() { 17 | override val context = activity 18 | 19 | override fun start(intent: Intent) { 20 | activity.startActivity(intent) 21 | } 22 | 23 | override fun startForResult(intent: Intent, code: Int) { 24 | activity.startActivityForResult(intent, code) 25 | } 26 | 27 | } 28 | 29 | class FragmentStarter(private val fragment: Fragment) : StarterHandler() { 30 | override val context = fragment.context 31 | override val activity: Activity? = fragment.activity 32 | 33 | override fun start(intent: Intent) { 34 | fragment.startActivity(intent) 35 | } 36 | 37 | override fun startForResult(intent: Intent, code: Int) { 38 | fragment.startActivityForResult(intent, code) 39 | } 40 | 41 | } 42 | 43 | class ApplicationStarter(val application: Application) : StarterHandler() { 44 | 45 | override val context = application 46 | override val activity: Activity? = ActivityProvider.currentActivity 47 | 48 | override fun start(intent: Intent) { 49 | activity?.let { 50 | it.startActivity(intent) 51 | } ?: run { 52 | application.startActivity(intent) 53 | } 54 | } 55 | 56 | override fun startForResult(intent: Intent, code: Int) { 57 | activity?.let { 58 | it.startActivityForResult(intent, code) 59 | } ?: run { 60 | //not possible to startForResult without an activity context 61 | application.startActivity(intent) 62 | } 63 | } 64 | } 65 | 66 | } -------------------------------------------------------------------------------- /navigator/src/main/java/com/github/florent37/navigator/uri/PathMatcher.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator.uri; 2 | 3 | import java.util.regex.Pattern 4 | 5 | class PathMatcher(val path: String) { 6 | 7 | companion object { 8 | private const val PARAM = "([a-zA-Z][a-zA-Z0-9_-]*)" 9 | private const val PARAM_REGEX = "\\{$PARAM\\}" 10 | private const val PARAM_VALUE = "([a-zA-Z0-9_#'!+%~,\\-\\.\\@\\$\\:]+)" 11 | 12 | private val PARAM_PATTERN = Pattern.compile(PARAM_REGEX) 13 | 14 | } 15 | 16 | private val regex: Pattern 17 | private val pathParams: List 18 | 19 | init { 20 | val pathReplaced = path.replace( 21 | PARAM_REGEX.toRegex(), 22 | PARAM_VALUE 23 | ) 24 | this.pathParams = path.valuesOf(PARAM_PATTERN) 25 | regex = Pattern.compile("^$pathReplaced\$") 26 | } 27 | 28 | fun matches(url: String): Boolean { 29 | try { 30 | return regex.matcher(url).find() 31 | } catch (t: Throwable) { 32 | t.printStackTrace() 33 | return false 34 | } 35 | } 36 | 37 | fun parametersValues(url: String): Map { 38 | val paramsMap = mutableMapOf() 39 | 40 | val matcher = regex.matcher(url) 41 | 42 | if (matcher.matches()) { 43 | for(i in 0 until matcher.groupCount()) { 44 | val value = matcher.group(i + 1) 45 | val name = pathParams[i] 46 | if (value != null && "" != value.trim { it <= ' ' }) { 47 | paramsMap[name] = value 48 | } 49 | } 50 | } 51 | return paramsMap 52 | } 53 | 54 | fun String.valuesOf(pattern: Pattern): List { 55 | val paramsNames = mutableListOf() 56 | val matcher = pattern.matcher(this) 57 | 58 | while (matcher.find()) { 59 | val value = matcher.group() 60 | if (!value.isNullOrBlank()) { 61 | val name = value.substring(1, value.length - 1) //removes the { } 62 | paramsNames.add(name) 63 | } 64 | } 65 | return paramsNames 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /navigator/src/test/java/com/github/florent37/navigator/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /publish.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | ./gradlew clean assembleDebug install 3 | ./gradlew bintrayUpload -------------------------------------------------------------------------------- /sample/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 28 9 | defaultConfig { 10 | applicationId "com.github.florent37.navigator" 11 | minSdkVersion 21 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation project(":sample:routing") 27 | implementation project(':sample:splash') 28 | implementation project(':sample:user') 29 | implementation project(':sample:home') 30 | implementation project(":navigator") 31 | implementation "com.github.florent37:applicationprovider:$applicationprovider_version" 32 | 33 | implementation "org.koin:koin-android:$koin_version" 34 | implementation "org.koin:koin-android-viewmodel:$koin_version" 35 | 36 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 37 | implementation 'androidx.appcompat:appcompat:1.0.2' 38 | implementation 'androidx.core:core-ktx:1.0.2' 39 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 40 | testImplementation 'junit:junit:4.12' 41 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 42 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 43 | } 44 | -------------------------------------------------------------------------------- /sample/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/app/src/androidTest/java/com/github/florent37/navigator/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.navigator", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /sample/app/src/main/java/com/github/florent37/navigator/sample/KoinProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator.sample 2 | 3 | import com.github.florent37.application.provider.Provider 4 | import org.koin.android.ext.koin.androidContext 5 | import org.koin.core.context.startKoin 6 | 7 | /* Called at startup */ 8 | class KoinProvider : Provider() { 9 | override fun provide() { 10 | startKoin { 11 | androidContext(context) 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /sample/app/src/main/java/com/github/florent37/navigator/sample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator.sample 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import com.github.florent37.navigator.Navigator 6 | import com.github.florent37.routing.Routes 7 | 8 | class MainActivity : AppCompatActivity() { 9 | 10 | override fun onCreate(savedInstanceState: Bundle?) { 11 | super.onCreate(savedInstanceState) 12 | setContentView(R.layout.activity_main) 13 | 14 | Navigator.of(this).pushReplacement(Routes.Splash) 15 | } 16 | } -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /sample/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Navigator 3 | 4 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample/app/src/test/java/com/github/florent37/navigator/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.navigator 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/home/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/home/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 28 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation "com.github.florent37:applicationprovider:$applicationprovider_version" 29 | 30 | implementation project(":navigator") 31 | implementation project(":sample:routing") 32 | implementation project(":sample:user-core") 33 | implementation project(":sample:post-core") 34 | 35 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 36 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" 37 | 38 | implementation 'androidx.appcompat:appcompat:1.0.2' 39 | implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" 40 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" 41 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" 42 | 43 | implementation "org.koin:koin-android:$koin_version" 44 | implementation "org.koin:koin-android-viewmodel:$koin_version" 45 | 46 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 47 | implementation 'com.github.bumptech.glide:glide:4.10.0' 48 | 49 | testImplementation 'junit:junit:4.12' 50 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 51 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 52 | } 53 | -------------------------------------------------------------------------------- /sample/home/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/home/consumer-rules.pro -------------------------------------------------------------------------------- /sample/home/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/home/src/androidTest/java/com/github/florent37/home/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.feature2.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/home/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 8 | 9 | 12 | 13 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/DependenciesProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home 2 | 3 | import com.github.florent37.application.provider.Provider 4 | import com.github.florent37.home.tabs.posts.PostsUseCase 5 | import com.github.florent37.home.tabs.posts.PostsViewModel 6 | import com.github.florent37.home.tabs.users.UsersViewModel 7 | import org.koin.android.viewmodel.dsl.viewModel 8 | import org.koin.core.context.loadKoinModules 9 | import org.koin.dsl.module 10 | 11 | /* Called at startup */ 12 | class DependenciesProvider : Provider() { 13 | override fun provide() { 14 | loadKoinModules(module { 15 | viewModel { UsersViewModel(get()) } 16 | viewModel { PostsViewModel(get()) } 17 | factory { PostsUseCase(get(), get()) } 18 | }) 19 | } 20 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/HomeActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import androidx.appcompat.app.AppCompatActivity 6 | import androidx.fragment.app.FragmentManager 7 | import androidx.fragment.app.FragmentTransaction 8 | import com.github.florent37.home.tabs.ViewPagerAdapter 9 | import com.github.florent37.home.tabs.users.UsersFragment 10 | import com.github.florent37.home.utils.setupWithViewPager 11 | import com.github.florent37.navigator.* 12 | import com.github.florent37.routing.Routes 13 | import kotlinx.android.synthetic.main.activity_home.* 14 | 15 | class HomeActivity : AppCompatActivity(R.layout.activity_home) { 16 | 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | 20 | viewPager.adapter = ViewPagerAdapter( 21 | supportFragmentManager 22 | ) 23 | 24 | bottomNav.setupWithViewPager(viewPager) 25 | 26 | //when working with singleTop, activity.onNewIntent do not call this.setIntent(newIntent) 27 | setupFlavor() 28 | } 29 | 30 | override fun onNewIntent(newIntent: Intent?) { 31 | super.onNewIntent(newIntent) 32 | //when working with singleTop, activity.onNewIntent do not call this.setIntent(newIntent) 33 | updateIntent(newIntent) 34 | setupFlavor() 35 | } 36 | 37 | private fun setupFlavor() { 38 | bindFlavor(Routes.Home.UserTabs, intent) 39 | .withFragmentTransaction(supportFragmentManager) { 40 | replace(R.id.content, UsersFragment()) 41 | } 42 | // .withBottomNav(bottomNav, R.id.tabUsers) 43 | 44 | bindFlavor(Routes.Home.PostsTabs, intent) 45 | .withBottomNav(bottomNav, R.id.tabPosts) 46 | } 47 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/RouteProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home 2 | 3 | import com.github.florent37.application.provider.Provider 4 | import com.github.florent37.routing.Routes 5 | 6 | /* Called at startup */ 7 | class RouteProvider : Provider() { 8 | override fun provide() { 9 | Routes.Home.registerActivity() 10 | } 11 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/ViewPagerAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs 2 | 3 | import androidx.fragment.app.Fragment 4 | import androidx.fragment.app.FragmentManager 5 | import androidx.fragment.app.FragmentStatePagerAdapter 6 | import com.github.florent37.home.tabs.posts.PostsFragment 7 | import com.github.florent37.home.tabs.users.UsersFragment 8 | 9 | class ViewPagerAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) { 10 | 11 | override fun getItem(position: Int) = when(position) { 12 | 0 -> UsersFragment() 13 | 1 -> PostsFragment() 14 | else -> Fragment() //never happen 15 | } 16 | 17 | override fun getCount() = 2 18 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/posts/PostsAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs.posts 2 | 3 | import android.view.LayoutInflater 4 | import android.view.View 5 | import android.view.ViewGroup 6 | import androidx.recyclerview.widget.RecyclerView 7 | import com.github.florent37.home.R 8 | import kotlinx.android.synthetic.main.cell_post.view.* 9 | 10 | typealias RecyclerItem = PostWithUser 11 | typealias PostClickListener = (RecyclerItem) -> Unit 12 | 13 | class PostsAdapter(val listener: PostClickListener) : RecyclerView.Adapter() { 14 | 15 | var items = listOf() 16 | set(value) { 17 | field = value 18 | notifyDataSetChanged() 19 | } 20 | 21 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = UserViewHolder.newInstance(parent, listener) 22 | 23 | override fun getItemCount() = items.size 24 | 25 | override fun onBindViewHolder(holder: UserViewHolder, position: Int) { 26 | holder.bind(items[position]) 27 | } 28 | } 29 | 30 | class UserViewHolder( 31 | view: View, 32 | val listener: PostClickListener 33 | ) : RecyclerView.ViewHolder(view) { 34 | 35 | companion object { 36 | fun newInstance( 37 | parent: ViewGroup, 38 | listener: PostClickListener 39 | ) = LayoutInflater.from(parent.context).inflate(R.layout.cell_post, parent, false).let { 40 | UserViewHolder(it, listener) 41 | } 42 | } 43 | 44 | private var item: RecyclerItem? = null 45 | 46 | init { 47 | itemView.setOnClickListener { 48 | item?.let(listener) 49 | } 50 | } 51 | 52 | fun bind(item: RecyclerItem) { 53 | this.item = item 54 | itemView.author.text = item.author?.name ?: "" 55 | itemView.text.text = item.post.text 56 | } 57 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/posts/PostsFragment.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs.posts 2 | 3 | import android.os.Bundle 4 | import android.view.View 5 | import androidx.fragment.app.Fragment 6 | import androidx.lifecycle.LifecycleOwner 7 | import androidx.lifecycle.Observer 8 | import androidx.lifecycle.lifecycleScope 9 | import androidx.recyclerview.widget.LinearLayoutManager 10 | import com.github.florent37.home.R 11 | import com.github.florent37.navigator.Destination 12 | import com.github.florent37.navigator.Navigator 13 | import com.github.florent37.navigator.onNavigationChange 14 | import com.github.florent37.navigator.optionalFlavorParameter 15 | import com.github.florent37.routing.Routes 16 | import kotlinx.android.synthetic.main.fragment_posts.* 17 | import kotlinx.coroutines.flow.launchIn 18 | import kotlinx.coroutines.flow.onEach 19 | import org.koin.android.viewmodel.ext.android.viewModel 20 | 21 | class PostsFragment : Fragment(R.layout.fragment_posts) { 22 | 23 | private var args by optionalFlavorParameter() 24 | 25 | private val viewHolder: PostsViewModel by viewModel() 26 | 27 | private val adapter = PostsAdapter(listener = { 28 | 29 | }) 30 | 31 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 32 | super.onViewCreated(view, savedInstanceState) 33 | 34 | postsRecycler.adapter = adapter 35 | postsRecycler.layoutManager = LinearLayoutManager(context) 36 | 37 | viewHolder.viewState.observe(viewLifecycleOwner, Observer { 38 | adapter.items = it.posts 39 | headerText.text = it.headerText 40 | }) 41 | 42 | onNavigationChange { 43 | //reload when the navigation changes 44 | viewHolder.loadPosts(args?.userId) 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/posts/PostsUseCase.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs.posts 2 | 3 | import com.github.florent37.post.core.Post 4 | import com.github.florent37.post.core.PostRepository 5 | import com.github.florent37.user.core.User 6 | import com.github.florent37.user.core.UserRepository 7 | 8 | class PostWithUser(val post: Post, val author: User? = null) 9 | 10 | class PostsUseCase(val postRepository: PostRepository, val userRepository: UserRepository) { 11 | 12 | suspend fun getPosts(ofUserId: Int? = null): List { 13 | return if (ofUserId != null) { 14 | val user: User? = userRepository.getUser(ofUserId) 15 | postRepository.getPostsOf(userId = ofUserId) 16 | .map { post -> 17 | PostWithUser( 18 | post = post, 19 | author = user 20 | ) 21 | } 22 | } else { 23 | val users = userRepository.getUsers() 24 | postRepository.getPosts().map { 25 | PostWithUser( 26 | post = it, 27 | author = users[it.authorId] 28 | ) 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/posts/PostsViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs.posts 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.MutableLiveData 5 | import androidx.lifecycle.ViewModel 6 | import androidx.lifecycle.viewModelScope 7 | import kotlinx.coroutines.launch 8 | 9 | class ViewState(val headerText: String?, val posts: List) 10 | 11 | class PostsViewModel(private val useCase: PostsUseCase) : ViewModel() { 12 | 13 | private val _viewState = MutableLiveData() 14 | val viewState: LiveData = _viewState 15 | 16 | private val loadedUser : Int? = null 17 | 18 | fun loadPosts(ofUser: Int? = null) { 19 | if(loadedUser != ofUser || ofUser == null) { 20 | viewModelScope.launch { 21 | val posts = useCase.getPosts(ofUser) 22 | val author = if (ofUser != null) posts.getOrNull(0)?.author else null 23 | val headerText = author?.let { "Posts of ${it.name}" } ?: "All posts" 24 | _viewState.postValue( 25 | ViewState( 26 | headerText = headerText, 27 | posts = posts 28 | ) 29 | ) 30 | } 31 | } 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/users/UsersAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs.users 2 | 3 | import android.view.LayoutInflater 4 | import android.view.View 5 | import android.view.ViewGroup 6 | import androidx.recyclerview.widget.RecyclerView 7 | import com.bumptech.glide.Glide 8 | import com.github.florent37.home.R 9 | import com.github.florent37.user.core.User 10 | import kotlinx.android.synthetic.main.cell_user.view.* 11 | 12 | typealias UserClickListener = (User) -> Unit 13 | 14 | class UsersAdapter(val listener: UserClickListener) : RecyclerView.Adapter() { 15 | 16 | var items = listOf() 17 | set(value) { 18 | field = value 19 | notifyDataSetChanged() 20 | } 21 | 22 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = UserViewHolder.newInstance(parent, listener) 23 | 24 | override fun getItemCount() = items.size 25 | 26 | override fun onBindViewHolder(holder: UserViewHolder, position: Int) { 27 | holder.bind(items[position]) 28 | } 29 | } 30 | 31 | class UserViewHolder( 32 | view: View, 33 | private val listener: UserClickListener 34 | ) : RecyclerView.ViewHolder(view) { 35 | 36 | companion object { 37 | fun newInstance( 38 | parent: ViewGroup, 39 | listener: UserClickListener 40 | ) = LayoutInflater.from(parent.context).inflate(R.layout.cell_user, parent, false).let { 41 | UserViewHolder(it, listener) 42 | } 43 | } 44 | 45 | private var user: User? = null 46 | 47 | init { 48 | itemView.setOnClickListener { 49 | user?.let(listener) 50 | } 51 | } 52 | 53 | fun bind(user: User) { 54 | this.user = user 55 | itemView.userName.text = user.name 56 | Glide.with(itemView.context).load(user.avatarUrl).into(itemView.userImage) 57 | } 58 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/users/UsersFragment.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs.users 2 | 3 | import android.os.Bundle 4 | import android.view.View 5 | import androidx.fragment.app.Fragment 6 | import androidx.lifecycle.Observer 7 | import com.github.florent37.home.R 8 | import com.github.florent37.navigator.Navigator 9 | import com.github.florent37.navigator.onNavigationChange 10 | import com.github.florent37.routing.Routes 11 | import kotlinx.android.synthetic.main.fragment_users.* 12 | import org.koin.android.viewmodel.ext.android.viewModel 13 | 14 | class UsersFragment : Fragment(R.layout.fragment_users) { 15 | 16 | private lateinit var adapter : UsersAdapter 17 | 18 | private val userViewModel : UsersViewModel by viewModel() 19 | 20 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 21 | super.onViewCreated(view, savedInstanceState) 22 | 23 | adapter = UsersAdapter(listener = { user -> 24 | userViewModel.onUserClicked(user) 25 | }) 26 | 27 | usersRecycler.adapter = adapter 28 | 29 | userViewModel.users.observe(viewLifecycleOwner, Observer { 30 | adapter.items = it.users 31 | }) 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/tabs/users/UsersViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.tabs.users 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.MutableLiveData 5 | import androidx.lifecycle.ViewModel 6 | import androidx.lifecycle.viewModelScope 7 | import com.github.florent37.navigator.Navigator 8 | import com.github.florent37.routing.Routes 9 | import com.github.florent37.user.core.User 10 | import com.github.florent37.user.core.UserRepository 11 | import kotlinx.coroutines.launch 12 | 13 | class UserViewState( 14 | val users: List 15 | ) 16 | 17 | class UsersViewModel(private val userRepository : UserRepository) : ViewModel() { 18 | 19 | private val _users = MutableLiveData() 20 | val users : LiveData = _users 21 | 22 | init { 23 | viewModelScope.launch { 24 | val users = userRepository.getUsers() 25 | _users.postValue(UserViewState(users)) 26 | } 27 | } 28 | 29 | fun onUserClicked(user: User) { 30 | //Navigator.current()?.push(Routes.Home.PostsTabs, Routes.Home.PostsTabs.Params(userId = user.id)) 31 | //Navigator.current()?.push(Routes.User, Routes.User.Params(userId = user.id)) 32 | Navigator.current()?.push("/user/${user.id}") 33 | } 34 | } -------------------------------------------------------------------------------- /sample/home/src/main/java/com/github/florent37/home/utils/ViewPagerBottomNav.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home.utils 2 | 3 | import androidx.viewpager.widget.ViewPager 4 | import com.google.android.material.bottomnavigation.BottomNavigationView 5 | 6 | fun BottomNavigationView.setupWithViewPager(viewPager: ViewPager) { 7 | this.setOnNavigationItemSelectedListener { 8 | this.findItemIndex(it.itemId).also { index -> 9 | if(viewPager.currentItem != index){ 10 | viewPager.currentItem = index 11 | } 12 | } 13 | true 14 | } 15 | 16 | viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() { 17 | override fun onPageSelected(position: Int) { 18 | this.apply { 19 | menu.getItem(position).itemId.also { id -> 20 | if(selectedItemId != id) 21 | selectedItemId = id 22 | } 23 | } 24 | } 25 | }) 26 | } 27 | 28 | fun BottomNavigationView.findItemIndex(id: Int): Int { 29 | for (i in 0 until menu.size()) { 30 | if (menu.getItem(i).itemId == id) { 31 | return i 32 | } 33 | } 34 | return 0 35 | } 36 | 37 | -------------------------------------------------------------------------------- /sample/home/src/main/res/drawable/ic_account_circle_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/home/src/main/res/drawable/ic_folder_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/home/src/main/res/layout/activity_home.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 29 | -------------------------------------------------------------------------------- /sample/home/src/main/res/layout/cell_post.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 17 | 18 | 22 | 23 | 33 | 34 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /sample/home/src/main/res/layout/cell_user.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 18 | 19 | 26 | 27 | -------------------------------------------------------------------------------- /sample/home/src/main/res/layout/fragment_posts.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 | 21 | 22 | 27 | 28 | -------------------------------------------------------------------------------- /sample/home/src/main/res/layout/fragment_users.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | -------------------------------------------------------------------------------- /sample/home/src/main/res/menu/bottom_navigation_menu.xml: -------------------------------------------------------------------------------- 1 | 2 |

3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/home/src/test/java/com/github/florent37/home/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/post-core/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/post-core/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 28 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation "com.github.florent37:applicationprovider:$applicationprovider_version" 29 | implementation "org.koin:koin-android:$koin_version" 30 | 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 36 | } 37 | -------------------------------------------------------------------------------- /sample/post-core/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/post-core/consumer-rules.pro -------------------------------------------------------------------------------- /sample/post-core/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/post-core/src/androidTest/java/com/github/florent37/routing/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.routing 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.routing.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/post-core/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /sample/post-core/src/main/java/com/github/florent37/post/core/DependenciesProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.post.core 2 | 3 | import com.github.florent37.application.provider.Provider 4 | import org.koin.core.context.loadKoinModules 5 | import org.koin.dsl.module 6 | 7 | /* Called at startup */ 8 | class DependenciesProvider : Provider() { 9 | override fun provide() { 10 | loadKoinModules(module { 11 | single { PostRepositoryImpl() } 12 | }) 13 | } 14 | } -------------------------------------------------------------------------------- /sample/post-core/src/main/java/com/github/florent37/post/core/Post.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.post.core 2 | 3 | class Post(val id:Int, val text: String, val authorId: Int) -------------------------------------------------------------------------------- /sample/post-core/src/main/java/com/github/florent37/post/core/PostRepository.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.post.core 2 | 3 | interface PostRepository { 4 | suspend fun getPosts() : List 5 | suspend fun getPostsOf(userId: Int) : List 6 | } -------------------------------------------------------------------------------- /sample/post-core/src/main/java/com/github/florent37/post/core/PostRepositoryImpl.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.post.core 2 | 3 | class PostRepositoryImpl : PostRepository { 4 | 5 | private val posts = listOf( 6 | Post(id = 1, authorId = 1, text = "Ya le mec à Milla"), 7 | Post(id = 2, authorId = 1, text = "T'as pas de shampoing ?"), 8 | Post(id = 3, authorId = 2, text = "Mais t'es pas nette !"), 9 | Post( 10 | id = 4, 11 | authorId = 3, 12 | text = "Ya baptiste qui est en train de faire bruler un truc dans sa chambre" 13 | ), 14 | Post(id = 5, authorId = 2, text = "Mais si jsuis très net") 15 | ) 16 | 17 | override suspend fun getPosts(): List = posts 18 | 19 | override suspend fun getPostsOf(userId: Int): List = posts.filter { 20 | it.authorId == userId 21 | } 22 | } -------------------------------------------------------------------------------- /sample/post-core/src/test/java/com/github/florent37/routing/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.routing 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/routing/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/routing/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 28 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation project(":navigator") 29 | 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 32 | testImplementation 'junit:junit:4.12' 33 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 35 | } 36 | -------------------------------------------------------------------------------- /sample/routing/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/routing/consumer-rules.pro -------------------------------------------------------------------------------- /sample/routing/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/routing/src/androidTest/java/com/github/florent37/routing/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.routing 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.routing.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/routing/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /sample/routing/src/main/java/com/github/florent37/routing/Routes.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.routing 2 | 3 | import com.github.florent37.navigator.* 4 | import com.squareup.moshi.Json 5 | 6 | object Routes { 7 | 8 | object Splash : Route("/") 9 | 10 | object Home : Route("/home/") { 11 | object UserTabs : Flavor(this,"/tabUsers") 12 | 13 | object PostsTabs : FlavorWithParams(this,"/tabPosts/{userId}") { 14 | data class Params(@Json(name= "userId") val userId: Int?) : Param() 15 | } 16 | } 17 | 18 | object User : RouteWithParams("/user/{id}") { 19 | data class Params(@Json(name= "id") val userId: Int) : Param() 20 | } 21 | } -------------------------------------------------------------------------------- /sample/routing/src/test/java/com/github/florent37/routing/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.routing 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/splash/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/splash/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 28 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation "com.github.florent37:applicationprovider:$applicationprovider_version" 29 | implementation project(":navigator") 30 | implementation project(":sample:routing") 31 | 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" 34 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" 35 | 36 | implementation "org.koin:koin-android:$koin_version" 37 | implementation "org.koin:koin-android-viewmodel:$koin_version" 38 | 39 | implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" 40 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" 41 | 42 | implementation 'androidx.appcompat:appcompat:1.0.2' 43 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 44 | implementation 'com.google.android.material:material:1.0.0' 45 | 46 | testImplementation 'junit:junit:4.12' 47 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 48 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 49 | } 50 | -------------------------------------------------------------------------------- /sample/splash/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/splash/consumer-rules.pro -------------------------------------------------------------------------------- /sample/splash/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/splash/src/androidTest/java/com/github/florent37/splash/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.splash 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.feature1.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/splash/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | 10 | 13 | 14 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /sample/splash/src/main/java/com/github/florent37/splash/DependenciesProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.splash 2 | 3 | import com.github.florent37.application.provider.Provider 4 | import org.koin.android.viewmodel.dsl.viewModel 5 | import org.koin.core.context.loadKoinModules 6 | import org.koin.dsl.module 7 | 8 | /* Called at startup */ 9 | class DependenciesProvider : Provider() { 10 | override fun provide() { 11 | loadKoinModules(module { 12 | viewModel { SplashViewModel() } 13 | }) 14 | } 15 | } -------------------------------------------------------------------------------- /sample/splash/src/main/java/com/github/florent37/splash/RouteProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.splash 2 | 3 | import android.content.Intent 4 | import com.github.florent37.application.provider.Provider 5 | import com.github.florent37.routing.Routes 6 | 7 | /* Called at startup */ 8 | class RouteProvider : Provider() { 9 | override fun provide() { 10 | Routes.Splash.register { context -> 11 | Intent(context, SplashActivity::class.java) 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /sample/splash/src/main/java/com/github/florent37/splash/SplashActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.splash 2 | 3 | import android.os.Bundle 4 | import androidx.appcompat.app.AppCompatActivity 5 | import com.github.florent37.navigator.* 6 | import com.github.florent37.routing.Routes 7 | import kotlinx.coroutines.* 8 | import org.koin.android.viewmodel.ext.android.viewModel 9 | 10 | class SplashActivity : AppCompatActivity(), CoroutineScope by MainScope() { 11 | 12 | private val splashViewModel : SplashViewModel by viewModel() 13 | 14 | override fun onCreate(savedInstanceState: Bundle?) { 15 | super.onCreate(savedInstanceState) 16 | setContentView(R.layout.activity_splash) 17 | 18 | splashViewModel.load() 19 | } 20 | 21 | override fun onDestroy() { 22 | cancel() 23 | super.onDestroy() 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /sample/splash/src/main/java/com/github/florent37/splash/SplashViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.splash 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.viewModelScope 5 | import com.github.florent37.navigator.Navigator 6 | import com.github.florent37.routing.Routes 7 | import kotlinx.coroutines.delay 8 | import kotlinx.coroutines.launch 9 | 10 | class SplashViewModel : ViewModel() { 11 | 12 | fun load() { 13 | viewModelScope.launch { 14 | delay(4 * 1000) 15 | 16 | Navigator.current()?.pushReplacement(Routes.Home) 17 | //Navigator.current()?.push("/home/") 18 | } 19 | } 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /sample/splash/src/main/res/layout/activity_splash.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 18 | 19 | -------------------------------------------------------------------------------- /sample/splash/src/test/java/com/github/florent37/splash/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.splash 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/user-core/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/user-core/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 28 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation "com.github.florent37:applicationprovider:$applicationprovider_version" 29 | implementation "org.koin:koin-android:$koin_version" 30 | 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 36 | } 37 | -------------------------------------------------------------------------------- /sample/user-core/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/user-core/consumer-rules.pro -------------------------------------------------------------------------------- /sample/user-core/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/user-core/src/androidTest/java/com/github/florent37/routing/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.routing 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.routing.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/user-core/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /sample/user-core/src/main/java/com/github/florent37/user/core/DependenciesProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user.core 2 | 3 | import com.github.florent37.application.provider.Provider 4 | import org.koin.core.context.loadKoinModules 5 | import org.koin.dsl.module 6 | 7 | /* Called at startup */ 8 | class DependenciesProvider : Provider() { 9 | override fun provide() { 10 | loadKoinModules(module { 11 | single { UserRepositoryImpl() } 12 | }) 13 | } 14 | } -------------------------------------------------------------------------------- /sample/user-core/src/main/java/com/github/florent37/user/core/User.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user.core 2 | 3 | class User(val id:Int, val name: String, val avatarUrl: String) -------------------------------------------------------------------------------- /sample/user-core/src/main/java/com/github/florent37/user/core/UserRepository.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user.core 2 | 3 | interface UserRepository { 4 | suspend fun getUsers() : List 5 | suspend fun getUser(id: Int) : User? 6 | } -------------------------------------------------------------------------------- /sample/user-core/src/main/java/com/github/florent37/user/core/UserRepositoryImpl.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user.core 2 | 3 | class UserRepositoryImpl : UserRepository { 4 | 5 | private val users = listOf( 6 | User(id = 1, name = "florent", avatarUrl = "https://picsum.photos/200/300"), 7 | User(id = 2, name = "florian", avatarUrl = "https://picsum.photos/200/301"), 8 | User(id = 3, name = "florence", avatarUrl = "https://picsum.photos/200/302"), 9 | User(id = 4, name = "olivier", avatarUrl = "https://picsum.photos/200/303"), 10 | User(id = 5, name = "jonathan", avatarUrl = "https://picsum.photos/200/304") 11 | ) 12 | 13 | override suspend fun getUsers() = users 14 | 15 | override suspend fun getUser(id: Int): User? { 16 | return users.find { it.id == id } 17 | } 18 | } -------------------------------------------------------------------------------- /sample/user-core/src/test/java/com/github/florent37/routing/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.routing 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/user/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/user/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 28 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation project(":navigator") 29 | implementation project(":sample:routing") 30 | implementation project(":sample:user-core") 31 | 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" 34 | implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" 35 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" 36 | 37 | implementation "org.koin:koin-android:$koin_version" 38 | implementation "org.koin:koin-android-viewmodel:$koin_version" 39 | 40 | implementation 'com.github.bumptech.glide:glide:4.10.0' 41 | 42 | implementation 'androidx.appcompat:appcompat:1.0.2' 43 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 44 | 45 | testImplementation 'junit:junit:4.12' 46 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 47 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 48 | } 49 | -------------------------------------------------------------------------------- /sample/user/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/Navigator/2357c4ffe8ef6a9ea0839edad621b91486af10d6/sample/user/consumer-rules.pro -------------------------------------------------------------------------------- /sample/user/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/user/src/androidTest/java/com/github/florent37/home/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.home 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.florent37.feature2.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/user/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 11 | 12 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /sample/user/src/main/java/com/github/florent37/user/DependenciesProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user 2 | 3 | import com.github.florent37.application.provider.Provider 4 | import org.koin.android.viewmodel.dsl.viewModel 5 | import org.koin.core.context.loadKoinModules 6 | import org.koin.dsl.module 7 | 8 | /* Called at startup */ 9 | class DependenciesProvider : Provider() { 10 | override fun provide() { 11 | loadKoinModules(module { 12 | viewModel { UserViewModel(get()) } 13 | }) 14 | } 15 | } -------------------------------------------------------------------------------- /sample/user/src/main/java/com/github/florent37/user/RouteProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user 2 | 3 | import android.content.Intent 4 | import com.github.florent37.application.provider.Provider 5 | import com.github.florent37.routing.Routes 6 | 7 | /* Called at startup */ 8 | class RouteProvider : Provider() { 9 | override fun provide() { 10 | Routes.User.register { context -> 11 | Intent(context, UserActivity::class.java) 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /sample/user/src/main/java/com/github/florent37/user/UserActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.lifecycle.Observer 6 | import com.bumptech.glide.Glide 7 | import com.github.florent37.navigator.Navigator 8 | import com.github.florent37.navigator.parameter 9 | import com.github.florent37.routing.Routes 10 | import com.squareup.moshi.Json 11 | import kotlinx.android.synthetic.main.activity_user.* 12 | import org.koin.android.viewmodel.ext.android.viewModel 13 | 14 | class UserActivity : AppCompatActivity() { 15 | 16 | private val args by parameter() 17 | 18 | private val userViewModel by viewModel() 19 | 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_user) 23 | 24 | userViewModel.loadUser(args.userId) 25 | 26 | userViewModel.user.observe(this, Observer { 27 | textView.text = it.user.name 28 | Glide.with(this).load(it.user.avatarUrl).into(imageView) 29 | }) 30 | 31 | goToPosts.setOnClickListener { 32 | Navigator.of(this).push("/home/tabPosts/${args.userId}") 33 | //Navigator.of(this).push(Routes.Home.PostsTabs, Routes.Home.PostsTabs.Params(userId = args.userId)) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /sample/user/src/main/java/com/github/florent37/user/UserViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.github.florent37.user 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.MutableLiveData 5 | import androidx.lifecycle.ViewModel 6 | import androidx.lifecycle.viewModelScope 7 | import com.github.florent37.user.core.User 8 | import com.github.florent37.user.core.UserRepository 9 | import kotlinx.coroutines.launch 10 | 11 | class UserViewState(val user: User) 12 | 13 | class UserViewModel(val userRepository: UserRepository) : ViewModel() { 14 | 15 | private val _user = MutableLiveData() 16 | val user: LiveData = _user 17 | 18 | fun loadUser(id: Int) { 19 | if (user.value == null || user.value!!.user.id != id) { 20 | viewModelScope.launch { 21 | val newValue = userRepository.getUser(id) 22 | if (newValue != null) { 23 | _user.postValue(UserViewState( 24 | newValue 25 | )) 26 | } 27 | } 28 | } 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /sample/user/src/main/res/layout/activity_user.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 20 | 21 | 30 | 31 |