├── .gitignore ├── LICENSE ├── README.md ├── app ├── build.gradle └── src │ ├── androidTest │ ├── assets │ │ └── success_resp_list.json │ └── java │ │ └── com │ │ └── dev │ │ └── ccodetest │ │ ├── app │ │ ├── CustomInstrumentationRunner.kt │ │ └── TestMainApp.kt │ │ ├── base │ │ └── BaseUITest.kt │ │ ├── di │ │ ├── MainAppInstrumentationDI.kt │ │ ├── MockServerInstrumentationDI.kt │ │ └── NetworkInstrumentationDI.kt │ │ ├── helpers │ │ └── RecyclerMatcher.kt │ │ └── screens │ │ └── login │ │ └── LoginActivityTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dev │ │ │ └── ccodetest │ │ │ ├── app │ │ │ └── MainApp.kt │ │ │ ├── constants │ │ │ └── AppConstants.kt │ │ │ ├── contracts │ │ │ ├── AlertCallBack.kt │ │ │ └── AppLogger.kt │ │ │ ├── di │ │ │ ├── AppUtilDependency.kt │ │ │ ├── MainDIComponent.kt │ │ │ ├── NetworkDependency.kt │ │ │ ├── RepoDIComponent.kt │ │ │ ├── SharedPrefDependency.kt │ │ │ └── UseCasesDIComponent.kt │ │ │ ├── exceptions │ │ │ └── NetworkError.kt │ │ │ ├── models │ │ │ └── login │ │ │ │ ├── AllPeople.kt │ │ │ │ └── AllPeopleResult.kt │ │ │ ├── network │ │ │ └── login │ │ │ │ └── LoginAPIService.kt │ │ │ ├── platform │ │ │ ├── AppGlideModule.kt │ │ │ ├── BaseActivity.kt │ │ │ ├── BaseFragment.kt │ │ │ ├── BaseViewModelFactory.kt │ │ │ ├── LiveDataWrapper.kt │ │ │ └── SharedPreferenceHelper.kt │ │ │ ├── repository │ │ │ └── LoginRepository.kt │ │ │ ├── screens │ │ │ └── login │ │ │ │ ├── LoginActivity.kt │ │ │ │ ├── LoginActivityFragment.kt │ │ │ │ ├── LoginActivityViewModel.kt │ │ │ │ ├── LoginRecyclerViewAdapter.kt │ │ │ │ └── LoginUseCase.kt │ │ │ ├── uigeneric │ │ │ ├── GenericAdapter.kt │ │ │ └── RecyclerOnScrollListener.kt │ │ │ └── utils │ │ │ └── AppUtils.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_baseline_warning_24.xml │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── fragment_main_activity.xml │ │ └── landing_list_view_item.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 │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ ├── java │ └── com │ │ └── dev │ │ └── ccodetest │ │ ├── base │ │ └── BaseUTTest.kt │ │ ├── di │ │ ├── MainDIComponentTest.kt │ │ ├── MockWebServerDITest.kt │ │ └── NetworkDITest.kt │ │ ├── repository │ │ └── LoginRepositoryTest.kt │ │ └── screens │ │ └── login │ │ ├── LoginActivityViewModelTest.kt │ │ └── LoginUseCaseTest.kt │ └── resources │ └── success_resp_list.json ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Project exclude paths 2 | /.gradle/ 3 | /app/build/ 4 | /app/build/intermediates/javac/debug/classes/ -------------------------------------------------------------------------------- /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 | # Clean Code Architecture + MVVM + UI / UT Testing 2 | 3 | Source code of Medium Articles which describes Android Unit and Instrumentation Testing in Clean Code Architecture with MVVM. 4 | 5 | - [Medium Article Part One - Deep dive in Unit Testing](https://medium.com/@sreeharikv112/android-unit-testing-clean-code-architecture-with-mvvm-73eb2992cab7) 6 | - [Medium Article Part Two - Exciting Instrumentation Testing](https://medium.com/@sreeharikv112/android-instrumentation-testing-with-clean-code-architecture-and-mvvm-5df4615f0e4a) 7 | 8 | ## Architecture followed 9 | 10 | ![ArchDiagram1](https://user-images.githubusercontent.com/39777674/71248284-78d00800-2340-11ea-9077-080e29a8c918.png) 11 | 12 |
13 | 14 | ## Main Libraries Used 15 | 16 | - Coroutines 🚀 https://developer.android.com/kotlin/coroutines 17 | 18 | - Retrofit 📲 https://square.github.io/retrofit/ 19 | 20 | - MockWebServer 🎭 https://github.com/square/okhttp/tree/master/mockwebserver 21 | 22 | - Koin (💉Dependency Injection) https://insert-koin.io/ 23 | 24 | - Kotlin 🥇 (https://developer.android.com/kotlin) 25 | 26 | 27 | 28 | ## The article explains 29 | 30 | 31 | - Ways to include dependencies using Koin 32 | - Making usefull data flow using above architecture 33 | - Creating unit test case with dependencies 34 | - Override the default dependencies 35 | - Mock required dependency and create required test cases 36 | - Execute both Unit and Instrumentation test cases 37 | 38 |
39 | 40 | For detailed explanation of points mentioned above, checkout [Article published in Medium](https://medium.com/@sreeharikv112/android-unit-testing-clean-code-architecture-with-mvvm-73eb2992cab7) 41 | 42 |
43 | 44 | ## Landing Screen with data from SWAPI API 45 |

46 | screen1 47 |

48 |
49 | 50 | 51 | ## Uses Swapi API for explaining required data flow 52 | 53 | Uses https://swapi.co/ API as network end points. 54 | 55 | The complete API documentation detail can be found over https://swapi.co/documentation 56 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | apply plugin: 'kotlin-kapt' 8 | 9 | apply plugin: 'kotlinx-serialization' 10 | 11 | 12 | android { 13 | compileSdkVersion 29 14 | buildToolsVersion "29.0.3" 15 | defaultConfig { 16 | applicationId "com.dev.ccodetest" 17 | minSdkVersion 23 18 | targetSdkVersion 29 19 | versionCode 1 20 | versionName "1.0" 21 | testInstrumentationRunner "com.dev.ccodetest.app.CustomInstrumentationRunner" 22 | multiDexEnabled true 23 | buildConfigField "String", "BASE_URL", "\"https://swapi.dev/api/\"" 24 | } 25 | buildTypes { 26 | release { 27 | minifyEnabled false 28 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 29 | } 30 | } 31 | testOptions { 32 | unitTests.returnDefaultValues = true 33 | 34 | unitTests.includeAndroidResources = true 35 | } 36 | } 37 | 38 | dependencies { 39 | 40 | implementation fileTree(dir: 'libs', include: ['*.jar']) 41 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 42 | implementation "androidx.appcompat:appcompat:$app_compat" 43 | implementation "androidx.core:core-ktx:$core_ktx" 44 | implementation "androidx.constraintlayout:constraintlayout:$constraint_lyt" 45 | implementation "com.google.android.material:material:$material_design" 46 | implementation "androidx.recyclerview:recyclerview:$recycler_view_version" 47 | 48 | // Koin for Android 49 | implementation "org.koin:koin-android:$koin_version" 50 | 51 | //Network 52 | implementation "com.squareup.retrofit2:retrofit:$retrofit_version" 53 | implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" 54 | implementation "com.squareup.okhttp3:logging-interceptor:$http_logging" 55 | implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson_fasterxml" 56 | 57 | //Coroutines 58 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$couroutines_version" 59 | implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$coroutines_adapter" 60 | 61 | //LeakCanary 62 | debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanary" 63 | 64 | //Lifecycle 65 | implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_extension" 66 | kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_extension" 67 | 68 | //Multidex 69 | implementation "androidx.multidex:multidex:$multidex" 70 | 71 | //Glide 72 | implementation "com.github.bumptech.glide:glide:$glide_version" 73 | kapt "com.github.bumptech.glide:compiler:$glide_version" 74 | 75 | //---------------------------- TESTING ----------------------------// 76 | 77 | androidTestImplementation "androidx.test.ext:junit:$junit_x_ext" 78 | androidTestImplementation "androidx.test:rules:$testx_rules" 79 | androidTestImplementation "android.arch.core:core-testing:$android_arch_core_testing" 80 | androidTestImplementation "com.squareup.okhttp3:mockwebserver:$mockwebserver_version" 81 | androidTestImplementation "org.koin:koin-test:$koin_version" 82 | androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso_contrib" 83 | 84 | testImplementation "androidx.test.ext:junit:$junit_x_ext" 85 | testImplementation "android.arch.core:core-testing:$android_arch_core_testing" 86 | testImplementation "com.squareup.okhttp3:mockwebserver:$mockwebserver_version" 87 | testImplementation "org.koin:koin-test:$koin_version" 88 | testImplementation "io.mockk:mockk:$mockk_version" 89 | } 90 | -------------------------------------------------------------------------------- /app/src/androidTest/assets/success_resp_list.json: -------------------------------------------------------------------------------- 1 | { 2 | "count": 87, 3 | "next": "https://swapi.co/api/people/?page=2", 4 | "previous": null, 5 | "results": [ 6 | { 7 | "name": "Luke Skywalker", 8 | "height": "172", 9 | "mass": "77", 10 | "hair_color": "blond", 11 | "skin_color": "fair", 12 | "eye_color": "blue", 13 | "birth_year": "19BBY", 14 | "gender": "male", 15 | "homeworld": "https://swapi.co/api/planets/1/", 16 | "films": [ 17 | "https://swapi.co/api/films/2/", 18 | "https://swapi.co/api/films/6/", 19 | "https://swapi.co/api/films/3/", 20 | "https://swapi.co/api/films/1/", 21 | "https://swapi.co/api/films/7/" 22 | ], 23 | "species": [ 24 | "https://swapi.co/api/species/1/" 25 | ], 26 | "vehicles": [ 27 | "https://swapi.co/api/vehicles/14/", 28 | "https://swapi.co/api/vehicles/30/" 29 | ], 30 | "starships": [ 31 | "https://swapi.co/api/starships/12/", 32 | "https://swapi.co/api/starships/22/" 33 | ], 34 | "created": "2014-12-09T13:50:51.644000Z", 35 | "edited": "2014-12-20T21:17:56.891000Z", 36 | "url": "https://swapi.co/api/people/1/" 37 | }, 38 | { 39 | "name": "C-3PO", 40 | "height": "167", 41 | "mass": "75", 42 | "hair_color": "n/a", 43 | "skin_color": "gold", 44 | "eye_color": "yellow", 45 | "birth_year": "112BBY", 46 | "gender": "n/a", 47 | "homeworld": "https://swapi.co/api/planets/1/", 48 | "films": [ 49 | "https://swapi.co/api/films/2/", 50 | "https://swapi.co/api/films/5/", 51 | "https://swapi.co/api/films/4/", 52 | "https://swapi.co/api/films/6/", 53 | "https://swapi.co/api/films/3/", 54 | "https://swapi.co/api/films/1/" 55 | ], 56 | "species": [ 57 | "https://swapi.co/api/species/2/" 58 | ], 59 | "vehicles": [], 60 | "starships": [], 61 | "created": "2014-12-10T15:10:51.357000Z", 62 | "edited": "2014-12-20T21:17:50.309000Z", 63 | "url": "https://swapi.co/api/people/2/" 64 | }, 65 | { 66 | "name": "R2-D2", 67 | "height": "96", 68 | "mass": "32", 69 | "hair_color": "n/a", 70 | "skin_color": "white, blue", 71 | "eye_color": "red", 72 | "birth_year": "33BBY", 73 | "gender": "n/a", 74 | "homeworld": "https://swapi.co/api/planets/8/", 75 | "films": [ 76 | "https://swapi.co/api/films/2/", 77 | "https://swapi.co/api/films/5/", 78 | "https://swapi.co/api/films/4/", 79 | "https://swapi.co/api/films/6/", 80 | "https://swapi.co/api/films/3/", 81 | "https://swapi.co/api/films/1/", 82 | "https://swapi.co/api/films/7/" 83 | ], 84 | "species": [ 85 | "https://swapi.co/api/species/2/" 86 | ], 87 | "vehicles": [], 88 | "starships": [], 89 | "created": "2014-12-10T15:11:50.376000Z", 90 | "edited": "2014-12-20T21:17:50.311000Z", 91 | "url": "https://swapi.co/api/people/3/" 92 | }, 93 | { 94 | "name": "Darth Vader", 95 | "height": "202", 96 | "mass": "136", 97 | "hair_color": "none", 98 | "skin_color": "white", 99 | "eye_color": "yellow", 100 | "birth_year": "41.9BBY", 101 | "gender": "male", 102 | "homeworld": "https://swapi.co/api/planets/1/", 103 | "films": [ 104 | "https://swapi.co/api/films/2/", 105 | "https://swapi.co/api/films/6/", 106 | "https://swapi.co/api/films/3/", 107 | "https://swapi.co/api/films/1/" 108 | ], 109 | "species": [ 110 | "https://swapi.co/api/species/1/" 111 | ], 112 | "vehicles": [], 113 | "starships": [ 114 | "https://swapi.co/api/starships/13/" 115 | ], 116 | "created": "2014-12-10T15:18:20.704000Z", 117 | "edited": "2014-12-20T21:17:50.313000Z", 118 | "url": "https://swapi.co/api/people/4/" 119 | }, 120 | { 121 | "name": "Leia Organa", 122 | "height": "150", 123 | "mass": "49", 124 | "hair_color": "brown", 125 | "skin_color": "light", 126 | "eye_color": "brown", 127 | "birth_year": "19BBY", 128 | "gender": "female", 129 | "homeworld": "https://swapi.co/api/planets/2/", 130 | "films": [ 131 | "https://swapi.co/api/films/2/", 132 | "https://swapi.co/api/films/6/", 133 | "https://swapi.co/api/films/3/", 134 | "https://swapi.co/api/films/1/", 135 | "https://swapi.co/api/films/7/" 136 | ], 137 | "species": [ 138 | "https://swapi.co/api/species/1/" 139 | ], 140 | "vehicles": [ 141 | "https://swapi.co/api/vehicles/30/" 142 | ], 143 | "starships": [], 144 | "created": "2014-12-10T15:20:09.791000Z", 145 | "edited": "2014-12-20T21:17:50.315000Z", 146 | "url": "https://swapi.co/api/people/5/" 147 | }, 148 | { 149 | "name": "Owen Lars", 150 | "height": "178", 151 | "mass": "120", 152 | "hair_color": "brown, grey", 153 | "skin_color": "light", 154 | "eye_color": "blue", 155 | "birth_year": "52BBY", 156 | "gender": "male", 157 | "homeworld": "https://swapi.co/api/planets/1/", 158 | "films": [ 159 | "https://swapi.co/api/films/5/", 160 | "https://swapi.co/api/films/6/", 161 | "https://swapi.co/api/films/1/" 162 | ], 163 | "species": [ 164 | "https://swapi.co/api/species/1/" 165 | ], 166 | "vehicles": [], 167 | "starships": [], 168 | "created": "2014-12-10T15:52:14.024000Z", 169 | "edited": "2014-12-20T21:17:50.317000Z", 170 | "url": "https://swapi.co/api/people/6/" 171 | }, 172 | { 173 | "name": "Beru Whitesun lars", 174 | "height": "165", 175 | "mass": "75", 176 | "hair_color": "brown", 177 | "skin_color": "light", 178 | "eye_color": "blue", 179 | "birth_year": "47BBY", 180 | "gender": "female", 181 | "homeworld": "https://swapi.co/api/planets/1/", 182 | "films": [ 183 | "https://swapi.co/api/films/5/", 184 | "https://swapi.co/api/films/6/", 185 | "https://swapi.co/api/films/1/" 186 | ], 187 | "species": [ 188 | "https://swapi.co/api/species/1/" 189 | ], 190 | "vehicles": [], 191 | "starships": [], 192 | "created": "2014-12-10T15:53:41.121000Z", 193 | "edited": "2014-12-20T21:17:50.319000Z", 194 | "url": "https://swapi.co/api/people/7/" 195 | }, 196 | { 197 | "name": "R5-D4", 198 | "height": "97", 199 | "mass": "32", 200 | "hair_color": "n/a", 201 | "skin_color": "white, red", 202 | "eye_color": "red", 203 | "birth_year": "unknown", 204 | "gender": "n/a", 205 | "homeworld": "https://swapi.co/api/planets/1/", 206 | "films": [ 207 | "https://swapi.co/api/films/1/" 208 | ], 209 | "species": [ 210 | "https://swapi.co/api/species/2/" 211 | ], 212 | "vehicles": [], 213 | "starships": [], 214 | "created": "2014-12-10T15:57:50.959000Z", 215 | "edited": "2014-12-20T21:17:50.321000Z", 216 | "url": "https://swapi.co/api/people/8/" 217 | }, 218 | { 219 | "name": "Biggs Darklighter", 220 | "height": "183", 221 | "mass": "84", 222 | "hair_color": "black", 223 | "skin_color": "light", 224 | "eye_color": "brown", 225 | "birth_year": "24BBY", 226 | "gender": "male", 227 | "homeworld": "https://swapi.co/api/planets/1/", 228 | "films": [ 229 | "https://swapi.co/api/films/1/" 230 | ], 231 | "species": [ 232 | "https://swapi.co/api/species/1/" 233 | ], 234 | "vehicles": [], 235 | "starships": [ 236 | "https://swapi.co/api/starships/12/" 237 | ], 238 | "created": "2014-12-10T15:59:50.509000Z", 239 | "edited": "2014-12-20T21:17:50.323000Z", 240 | "url": "https://swapi.co/api/people/9/" 241 | }, 242 | { 243 | "name": "Obi-Wan Kenobi", 244 | "height": "182", 245 | "mass": "77", 246 | "hair_color": "auburn, white", 247 | "skin_color": "fair", 248 | "eye_color": "blue-gray", 249 | "birth_year": "57BBY", 250 | "gender": "male", 251 | "homeworld": "https://swapi.co/api/planets/20/", 252 | "films": [ 253 | "https://swapi.co/api/films/2/", 254 | "https://swapi.co/api/films/5/", 255 | "https://swapi.co/api/films/4/", 256 | "https://swapi.co/api/films/6/", 257 | "https://swapi.co/api/films/3/", 258 | "https://swapi.co/api/films/1/" 259 | ], 260 | "species": [ 261 | "https://swapi.co/api/species/1/" 262 | ], 263 | "vehicles": [ 264 | "https://swapi.co/api/vehicles/38/" 265 | ], 266 | "starships": [ 267 | "https://swapi.co/api/starships/48/", 268 | "https://swapi.co/api/starships/59/", 269 | "https://swapi.co/api/starships/64/", 270 | "https://swapi.co/api/starships/65/", 271 | "https://swapi.co/api/starships/74/" 272 | ], 273 | "created": "2014-12-10T16:16:29.192000Z", 274 | "edited": "2014-12-20T21:17:50.325000Z", 275 | "url": "https://swapi.co/api/people/10/" 276 | } 277 | ] 278 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/app/CustomInstrumentationRunner.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.app 2 | 3 | import android.app.Application 4 | import android.content.Context 5 | import androidx.test.runner.AndroidJUnitRunner 6 | 7 | /** 8 | * Custom Instrumentation Test runner. 9 | * Helps to configure environment with new App instance. 10 | */ 11 | class CustomInstrumentationRunner : AndroidJUnitRunner() { 12 | override fun newApplication(cl: ClassLoader, 13 | className: String, 14 | context: Context): Application { 15 | return super.newApplication(cl, 16 | TestMainApp::class.java.name, 17 | context) 18 | } 19 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/app/TestMainApp.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.app 2 | 3 | import org.koin.core.module.Module 4 | 5 | /** 6 | * Helps to configure required dependencies for Instru Tests. 7 | * Method provideDependency can be overrided and new dependencies can be supplied. 8 | */ 9 | class TestMainApp : MainApp() { 10 | override fun provideDependency() = listOf() 11 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/base/BaseUITest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.base 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import okhttp3.mockwebserver.MockResponse 5 | import okhttp3.mockwebserver.MockWebServer 6 | import org.junit.After 7 | import org.junit.Before 8 | import org.koin.core.context.stopKoin 9 | import org.koin.test.KoinTest 10 | import java.io.BufferedReader 11 | import java.io.Reader 12 | 13 | abstract class BaseUITest : KoinTest { 14 | 15 | /** 16 | * For MockWebServer instance 17 | */ 18 | private lateinit var mockServer: MockWebServer 19 | 20 | 21 | /** 22 | * Default, let server be shut down 23 | */ 24 | private var mShouldStart = false 25 | 26 | @Before 27 | open fun setUp(){ 28 | startMockServer(true) 29 | } 30 | 31 | /** 32 | * Helps to read input file returns the respective data in mocked call 33 | */ 34 | fun mockNetworkResponseWithFileContent(fileName: String, responseCode: Int) = mockServer.enqueue( 35 | 36 | MockResponse() 37 | .setResponseCode(responseCode) 38 | .setBody(getJson(fileName))) 39 | 40 | /** 41 | * Reads input file and converts to json 42 | */ 43 | fun getJson(path : String) : String { 44 | var content: String = "" 45 | val testContext = InstrumentationRegistry.getInstrumentation().context 46 | val inputStream = testContext.assets.open(path) 47 | val reader = BufferedReader(inputStream.reader() as Reader?) 48 | reader.use { reader -> 49 | content = reader.readText() 50 | } 51 | return content 52 | } 53 | 54 | /** 55 | * Start Mockwebserver 56 | */ 57 | private fun startMockServer(shouldStart:Boolean){ 58 | if (shouldStart){ 59 | mShouldStart = shouldStart 60 | mockServer = MockWebServer() 61 | mockServer.start() 62 | } 63 | } 64 | 65 | /** 66 | * Set Mockwebserver url 67 | */ 68 | fun getMockWebServerUrl() = mockServer.url("/").toString() 69 | 70 | /** 71 | * Stop Mockwebserver 72 | */ 73 | private fun stopMockServer() { 74 | if (mShouldStart){ 75 | mockServer.shutdown() 76 | } 77 | } 78 | 79 | @After 80 | open fun tearDown(){ 81 | //Stop Mock server 82 | stopMockServer() 83 | //Stop Koin as well 84 | stopKoin() 85 | } 86 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/di/MainAppInstrumentationDI.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | /** 4 | * Main Koin DI component for Instrumentation Testing 5 | */ 6 | fun generateTestAppComponent(baseApi: String) 7 | = listOf( 8 | configureNetworkForInstrumentationTest(baseApi), 9 | UseCaseDependency, 10 | MockWebServerInstrumentationTest, 11 | RepoDependency 12 | ) -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/di/MockServerInstrumentationDI.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import okhttp3.mockwebserver.MockWebServer 4 | import org.koin.dsl.module 5 | 6 | /** 7 | * Mock web server Koin DI component for Instrumentation Testing 8 | */ 9 | val MockWebServerInstrumentationTest = module { 10 | 11 | factory { 12 | MockWebServer() 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/di/NetworkInstrumentationDI.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import com.dev.ccodetest.network.login.LoginAPIService 4 | import com.google.gson.GsonBuilder 5 | import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory 6 | import org.koin.dsl.module 7 | import retrofit2.Retrofit 8 | import retrofit2.converter.gson.GsonConverterFactory 9 | 10 | /** 11 | * Retrofit Koin DI component for Instrumentation Testing 12 | */ 13 | fun configureNetworkForInstrumentationTest(baseTestApi: String) = module { 14 | 15 | single { 16 | Retrofit.Builder() 17 | .baseUrl(baseTestApi) 18 | .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) 19 | .addCallAdapterFactory(CoroutineCallAdapterFactory()) 20 | .build() 21 | } 22 | factory{ get().create(LoginAPIService::class.java) } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/helpers/RecyclerMatcher.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.helpers 2 | 3 | import android.view.View 4 | import androidx.annotation.NonNull 5 | import androidx.recyclerview.widget.RecyclerView 6 | import androidx.test.espresso.matcher.BoundedMatcher 7 | import org.hamcrest.Description 8 | import org.hamcrest.Matcher 9 | 10 | /** 11 | * Custom matcher for recyclerview testing. 12 | */ 13 | fun recyclerItemAtPosition(position: Int, @NonNull itemMatcher: Matcher): Matcher { 14 | return object : BoundedMatcher(RecyclerView::class.java) { 15 | override fun describeTo(description: Description) { 16 | description.appendText("has item at position $position: ") 17 | itemMatcher.describeTo(description) 18 | } 19 | override fun matchesSafely(view: RecyclerView): Boolean { 20 | val viewHolder = view.findViewHolderForAdapterPosition(position) 21 | ?: 22 | return false 23 | return itemMatcher.matches(viewHolder.itemView) 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dev/ccodetest/screens/login/LoginActivityTest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | import android.os.SystemClock 4 | import androidx.arch.core.executor.testing.InstantTaskExecutorRule 5 | import androidx.test.espresso.Espresso.onView 6 | import androidx.test.espresso.assertion.ViewAssertions.matches 7 | import androidx.test.espresso.contrib.RecyclerViewActions 8 | import androidx.test.espresso.matcher.ViewMatchers 9 | import androidx.test.espresso.matcher.ViewMatchers.withId 10 | import androidx.test.espresso.matcher.ViewMatchers.withText 11 | import androidx.test.ext.junit.runners.AndroidJUnit4 12 | import androidx.test.rule.ActivityTestRule 13 | import com.dev.ccodetest.R 14 | import com.dev.ccodetest.base.BaseUITest 15 | import com.dev.ccodetest.di.generateTestAppComponent 16 | import com.dev.ccodetest.helpers.recyclerItemAtPosition 17 | import okhttp3.mockwebserver.MockWebServer 18 | import org.junit.Before 19 | import org.junit.Rule 20 | import org.junit.Test 21 | import org.junit.runner.RunWith 22 | import org.koin.core.context.loadKoinModules 23 | import org.koin.test.inject 24 | import java.net.HttpURLConnection 25 | 26 | @RunWith(AndroidJUnit4::class) 27 | class LoginActivityTest : BaseUITest(){ 28 | 29 | @Rule 30 | @JvmField 31 | val instantTaskExecutorRule = InstantTaskExecutorRule() 32 | 33 | @Rule 34 | @JvmField 35 | var mActivityTestRule = ActivityTestRule(LoginActivity::class.java, true, false) 36 | 37 | //Inject login use case created with koin 38 | val mLoginUseCase : LoginUseCase by inject() 39 | //Inject Mockwebserver created with koin 40 | val mMockWebServer : MockWebServer by inject() 41 | 42 | val mNameTestOne = "Luke Skywalker" 43 | val mDOBTestOne = "19BBY" 44 | val mNameTestTwo = "Obi-Wan Kenobi" 45 | val mDOBTestTwo = "57BBY" 46 | 47 | @Before 48 | fun start(){ 49 | super.setUp() 50 | loadKoinModules(generateTestAppComponent(getMockWebServerUrl()).toMutableList()) 51 | } 52 | 53 | @Test 54 | fun test_recyclerview_elements_for_expected_response() { 55 | mActivityTestRule.launchActivity(null) 56 | 57 | mockNetworkResponseWithFileContent("success_resp_list.json", HttpURLConnection.HTTP_OK) 58 | 59 | //Wait for MockWebServer to get back with response 60 | SystemClock.sleep(1000) 61 | 62 | //Check if item at 0th position is having 0th element in json 63 | onView(withId(R.id.landingListRecyclerView)) 64 | .check( 65 | matches( 66 | recyclerItemAtPosition( 67 | 0, 68 | ViewMatchers.hasDescendant(withText(mNameTestOne)) 69 | ))) 70 | 71 | onView(withId(R.id.landingListRecyclerView)) 72 | .check( 73 | matches( 74 | recyclerItemAtPosition( 75 | 0, 76 | ViewMatchers.hasDescendant(withText(mDOBTestOne)) 77 | ))) 78 | 79 | //Scroll to last index in json 80 | onView(withId(R.id.landingListRecyclerView)).perform( 81 | RecyclerViewActions.scrollToPosition(9)) 82 | 83 | //Check if item at 9th position is having 9th element in json 84 | onView(withId(R.id.landingListRecyclerView)) 85 | .check(matches(recyclerItemAtPosition(9, ViewMatchers.hasDescendant(withText(mNameTestTwo))))) 86 | 87 | onView(withId(R.id.landingListRecyclerView)) 88 | .check(matches(recyclerItemAtPosition(9, ViewMatchers.hasDescendant(withText(mDOBTestTwo))))) 89 | 90 | } 91 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/app/MainApp.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.app 2 | 3 | import androidx.multidex.MultiDexApplication 4 | import com.dev.ccodetest.di.appComponent 5 | import org.koin.android.ext.koin.androidContext 6 | import org.koin.core.context.startKoin 7 | 8 | /** 9 | * Main Application class. 10 | * Dependency Injection initiated for all sub modules. 11 | */ 12 | open class MainApp : MultiDexApplication() { 13 | 14 | override fun onCreate() { 15 | super.onCreate() 16 | initiateKoin() 17 | } 18 | 19 | private fun initiateKoin() { 20 | startKoin{ 21 | androidContext(this@MainApp) 22 | modules(provideDependency()) 23 | } 24 | } 25 | 26 | open fun provideDependency() = appComponent 27 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/constants/AppConstants.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.constants 2 | 3 | const val TIME_OUT : Long = 120 4 | const val DS_SHARED_PREF_CCA = "DS_SHARED_PREF_CCA" -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/contracts/AlertCallBack.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.contracts 2 | 3 | /** 4 | * Dialog alert call backs. 5 | */ 6 | interface AlertCallBack { 7 | fun negativeAlertCallBack() 8 | fun positiveAlertCallBack() 9 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/contracts/AppLogger.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.contracts 2 | 3 | /** 4 | * Common util for logging events. 5 | * Can be used to store logs locally as well. 6 | */ 7 | interface AppLogger { 8 | //May extend the functionality for accumulating log files into device memory as well 9 | fun logD(tag: String, message: String) 10 | 11 | fun logE(tag: String, message: String) 12 | 13 | fun logV(tag: String, message: String) 14 | 15 | fun logI(tag: String, message: String) 16 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/di/AppUtilDependency.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import com.dev.ccodetest.utils.AppUtils 4 | import org.koin.android.ext.koin.androidContext 5 | import org.koin.dsl.module 6 | 7 | /** 8 | * DI module for App Util dependency. 9 | */ 10 | val AppUtilDependency = module { 11 | 12 | factory { AppUtils(androidContext()) } 13 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/di/MainDIComponent.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | /** 4 | * Main dependency component. 5 | * This will create and provide required dependencies with sub dependencies. 6 | */ 7 | val appComponent = listOf(UseCaseDependency, AppUtilDependency, NetworkDependency, SharedPrefDependency, RepoDependency) -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/di/NetworkDependency.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import com.dev.ccodetest.BuildConfig 4 | import com.dev.ccodetest.constants.TIME_OUT 5 | import com.dev.ccodetest.network.login.LoginAPIService 6 | import com.google.gson.GsonBuilder 7 | import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory 8 | import okhttp3.OkHttpClient 9 | import okhttp3.logging.HttpLoggingInterceptor 10 | import org.koin.dsl.module 11 | import retrofit2.Retrofit 12 | import retrofit2.converter.gson.GsonConverterFactory 13 | import java.util.concurrent.TimeUnit 14 | 15 | /** 16 | * Network dependency module. 17 | * Provides Retrofit dependency with OkHttp logger. 18 | */ 19 | val NetworkDependency = module { 20 | 21 | single { 22 | val logging = HttpLoggingInterceptor() 23 | logging.level = HttpLoggingInterceptor.Level.BASIC 24 | val okHttpClient = OkHttpClient.Builder() 25 | .connectTimeout(TIME_OUT, TimeUnit.SECONDS) 26 | .readTimeout(TIME_OUT, TimeUnit.SECONDS) 27 | .addInterceptor(logging) 28 | .build() 29 | Retrofit.Builder() 30 | .client(okHttpClient) 31 | .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) 32 | .addCallAdapterFactory(CoroutineCallAdapterFactory()) 33 | .baseUrl(BuildConfig.BASE_URL).build() 34 | } 35 | factory{ get().create(LoginAPIService::class.java) } 36 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/di/RepoDIComponent.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import com.dev.ccodetest.repository.LoginRepository 4 | import org.koin.dsl.module 5 | 6 | /** 7 | * Repository DI module. 8 | * Provides Repo dependency. 9 | */ 10 | val RepoDependency = module { 11 | 12 | factory { 13 | LoginRepository() 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/di/SharedPrefDependency.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import com.dev.ccodetest.platform.SharedPreferenceHelper 4 | import org.koin.android.ext.koin.androidContext 5 | import org.koin.dsl.module 6 | 7 | /** 8 | * Shared Preference DI Module. 9 | * 10 | */ 11 | val SharedPrefDependency = module { 12 | 13 | factory { SharedPreferenceHelper(androidContext()) } 14 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/di/UseCasesDIComponent.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import com.dev.ccodetest.screens.login.LoginUseCase 4 | import org.koin.dsl.module 5 | 6 | /** 7 | * Use case DI module. 8 | * Provide Use case dependency. 9 | */ 10 | val UseCaseDependency = module { 11 | 12 | factory { 13 | LoginUseCase() 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/exceptions/NetworkError.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.exceptions 2 | 3 | /** 4 | * Class for holding network processing error. 5 | */ 6 | class NetworkError(errorDetail: String) : Exception(errorDetail) -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/models/login/AllPeople.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.models.login 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty 4 | 5 | /** 6 | * Parent Data class for sample response. 7 | */ 8 | data class AllPeople( 9 | 10 | @JsonProperty("count") 11 | val count : Int, 12 | 13 | @JsonProperty("next") 14 | val next : String, 15 | 16 | @JsonProperty("previous") 17 | val previous : String, 18 | 19 | @JsonProperty("results") 20 | val results : List 21 | 22 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/models/login/AllPeopleResult.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.models.login 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty 4 | 5 | /** 6 | * Sub data class for sample response. 7 | */ 8 | data class AllPeopleResult ( 9 | @JsonProperty("name") 10 | val name : String, 11 | 12 | @JsonProperty("height") 13 | val height : String, 14 | 15 | @JsonProperty("mass") 16 | val mass : String, 17 | 18 | @JsonProperty("hair_color") 19 | val hair_color : String, 20 | 21 | @JsonProperty("skin_color") 22 | val skin_color : String, 23 | 24 | @JsonProperty("eye_color") 25 | val eye_color : String, 26 | 27 | @JsonProperty("birth_year") 28 | val birth_year : String, 29 | 30 | @JsonProperty("gender") 31 | val gender : String, 32 | 33 | @JsonProperty("homeworld") 34 | val homeworld : String, 35 | 36 | @JsonProperty("films") 37 | val films : List, 38 | 39 | @JsonProperty("species") 40 | val species : List, 41 | 42 | @JsonProperty("vehicles") 43 | val vehicles : List, 44 | 45 | @JsonProperty("starships") 46 | val starships : List, 47 | 48 | @JsonProperty("created") 49 | val created : String, 50 | 51 | @JsonProperty("edited") 52 | val edited : String, 53 | 54 | @JsonProperty("url") 55 | val url : String 56 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/network/login/LoginAPIService.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.network.login 2 | 3 | import com.dev.ccodetest.models.login.AllPeople 4 | import retrofit2.http.GET 5 | import retrofit2.http.Query 6 | 7 | /** 8 | * Login service Retrofit API. 9 | */ 10 | interface LoginAPIService{ 11 | 12 | @GET("people/") 13 | suspend fun getLoginData(@Query("page") page:String): AllPeople 14 | 15 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/platform/AppGlideModule.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.platform 2 | 3 | import android.content.Context 4 | import com.bumptech.glide.GlideBuilder 5 | import com.bumptech.glide.load.engine.DiskCacheStrategy 6 | import com.bumptech.glide.module.AppGlideModule 7 | import com.bumptech.glide.request.RequestOptions 8 | import com.bumptech.glide.signature.ObjectKey 9 | 10 | /** 11 | * Glide module for rendering images. 12 | */ 13 | class AppGlideModule : AppGlideModule(){ 14 | override fun applyOptions(context: Context, builder: GlideBuilder) { 15 | super.applyOptions(context, builder) 16 | builder.apply { RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).signature( 17 | ObjectKey( 18 | System.currentTimeMillis().toShort() 19 | ) 20 | ) } 21 | } 22 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/platform/BaseActivity.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.platform 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | 5 | /** 6 | * Base Activity for all Activities. 7 | */ 8 | abstract class BaseActivity : AppCompatActivity() -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/platform/BaseFragment.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.platform 2 | 3 | import android.content.Context 4 | import android.content.DialogInterface 5 | import android.os.Bundle 6 | import android.util.Log 7 | import android.view.Gravity 8 | import android.view.LayoutInflater 9 | import android.view.View 10 | import android.view.ViewGroup 11 | import android.widget.ImageView 12 | import android.widget.Toast 13 | import androidx.appcompat.app.AlertDialog 14 | import androidx.fragment.app.Fragment 15 | import com.bumptech.glide.Glide 16 | import com.dev.ccodetest.contracts.AlertCallBack 17 | import com.dev.ccodetest.contracts.AppLogger 18 | import com.google.android.material.dialog.MaterialAlertDialogBuilder 19 | 20 | /** 21 | * Base Fragment for all Fragments. 22 | * Handles Alert, Toast, Logger etc 23 | */ 24 | abstract class BaseFragment: Fragment(), AppLogger, AlertCallBack { 25 | 26 | // OVERRIDE --- 27 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 28 | return inflater.inflate(getLayoutId(), container, false) 29 | } 30 | 31 | // ABSTRACT --- 32 | abstract fun getLayoutId(): Int 33 | 34 | private var mCallBackAlertDialog: AlertDialog? = null 35 | 36 | fun showAlert(message :Int, positiveBtnText: Int, negativeBtnText:Int, 37 | positiveListener: DialogInterface.OnClickListener, 38 | negativeListener: DialogInterface.OnClickListener 39 | ) { 40 | activity?.let { 41 | MaterialAlertDialogBuilder(it) 42 | .setMessage(message) 43 | .setPositiveButton(positiveBtnText, positiveListener) 44 | .setNegativeButton(negativeBtnText, negativeListener) 45 | .setCancelable(false) 46 | .show() 47 | } 48 | } 49 | 50 | fun showToast(msg: String) { 51 | activity!!.runOnUiThread { 52 | val toast = Toast.makeText(activity, msg, Toast.LENGTH_SHORT) 53 | toast.setGravity(Gravity.CENTER, 0, 0) 54 | toast.show() 55 | } 56 | } 57 | 58 | fun loadImageURL(context: Context, imageView: ImageView, imageURL: String) { 59 | Glide.with(context).load(imageURL) 60 | .fallback(android.R.drawable.stat_notify_error) 61 | .timeout(4500) 62 | .into(imageView) 63 | } 64 | 65 | override fun negativeAlertCallBack() { 66 | mCallBackAlertDialog!!.dismiss() 67 | } 68 | 69 | override fun positiveAlertCallBack() { 70 | mCallBackAlertDialog!!.dismiss() 71 | } 72 | 73 | override fun logD(tag: String, message: String) { 74 | Log.d(tag,message) 75 | } 76 | 77 | override fun logE(tag: String, message: String) { 78 | Log.e(tag,message) 79 | } 80 | 81 | override fun logV(tag: String, message: String) { 82 | Log.v(tag,message) 83 | } 84 | 85 | override fun logI(tag: String, message: String) { 86 | Log.i(tag,message) 87 | } 88 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/platform/BaseViewModelFactory.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.platform 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.ViewModelProvider 5 | import com.dev.ccodetest.screens.login.LoginUseCase 6 | import com.dev.ccodetest.screens.login.LoginActivityViewModel 7 | import kotlinx.coroutines.CoroutineDispatcher 8 | 9 | /** 10 | * Base VM Factory for creation of different VM's 11 | */ 12 | class BaseViewModelFactory constructor( 13 | private val mainDispather: CoroutineDispatcher 14 | ,private val ioDispatcher: CoroutineDispatcher 15 | ,private val useCase: LoginUseCase 16 | ) : 17 | ViewModelProvider.Factory { 18 | 19 | override fun create(modelClass: Class): T { 20 | return if (modelClass.isAssignableFrom(LoginActivityViewModel::class.java)) { 21 | LoginActivityViewModel(mainDispather, ioDispatcher,useCase) as T 22 | } else { 23 | throw IllegalArgumentException("ViewModel Not configured") as Throwable 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/platform/LiveDataWrapper.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.platform 2 | 3 | /** 4 | * Custom result object. 5 | */ 6 | class LiveDataWrapper( 7 | val responseStatus: RESPONSESTATUS, 8 | val response: T? = null, 9 | val error: Throwable? = null 10 | ) { 11 | 12 | enum class RESPONSESTATUS { 13 | SUCCESS, LOADING, ERROR 14 | } 15 | 16 | companion object { 17 | fun loading() = LiveDataWrapper(RESPONSESTATUS.LOADING) 18 | fun success (data: T) = LiveDataWrapper(RESPONSESTATUS.SUCCESS, data) 19 | fun error(err: Throwable) = LiveDataWrapper(RESPONSESTATUS.ERROR, null, err) 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/platform/SharedPreferenceHelper.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.platform 2 | 3 | import android.content.Context 4 | import android.content.SharedPreferences 5 | import com.dev.ccodetest.constants.DS_SHARED_PREF_CCA 6 | 7 | /** 8 | * Shared Preference helper with common access methods for different data types. 9 | */ 10 | class SharedPreferenceHelper(var context: Context) { 11 | 12 | private val mSharedPrefKey = DS_SHARED_PREF_CCA 13 | private val mSharedPref : SharedPreferences = context.getSharedPreferences(mSharedPrefKey, Context.MODE_PRIVATE) 14 | 15 | private val mSharedPrefEditor : SharedPreferences.Editor = mSharedPref.edit() 16 | 17 | fun setBooleanData(key : String, value: Boolean){ 18 | mSharedPrefEditor.putBoolean(key,value) 19 | mSharedPrefEditor.commit() 20 | } 21 | 22 | fun getBooleanData(key : String): Boolean{ 23 | return if(mSharedPref.contains(key)) 24 | mSharedPref.getBoolean(key,false) 25 | else 26 | false 27 | } 28 | 29 | fun setStringData(key : String, value: String){ 30 | mSharedPrefEditor.putString(key, value) 31 | mSharedPrefEditor.commit() 32 | } 33 | 34 | fun getStringData(key : String): String { 35 | return if(mSharedPref.contains(key)) 36 | mSharedPref.getString(key,"")!! 37 | else 38 | "" 39 | } 40 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/repository/LoginRepository.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.repository 2 | 3 | import com.dev.ccodetest.models.login.AllPeople 4 | import com.dev.ccodetest.network.login.LoginAPIService 5 | import org.koin.core.KoinComponent 6 | import org.koin.core.inject 7 | 8 | /** 9 | * Repository for Login Flow. 10 | * Requests data from either Network or DB. 11 | * 12 | */ 13 | class LoginRepository : KoinComponent { 14 | 15 | val mLoginAPIService: LoginAPIService by inject() 16 | /** 17 | * Request data from backend 18 | */ 19 | suspend fun getLoginData(query: String): AllPeople { 20 | 21 | return processDataFetchLogic(query) 22 | 23 | } 24 | 25 | suspend fun processDataFetchLogic(parameter:String): AllPeople{ 26 | 27 | for (x in 0 until 3){ 28 | println(" Data manipulation prior to REST API request if required $x") 29 | } 30 | 31 | val dataReceived = mLoginAPIService.getLoginData(parameter) 32 | 33 | for (x in 0 until 3){ 34 | println(" Data manipulation post REST API request if required $x") 35 | } 36 | 37 | return dataReceived 38 | } 39 | 40 | 41 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/screens/login/LoginActivity.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | import android.os.Bundle 4 | import com.dev.ccodetest.R 5 | import com.dev.ccodetest.platform.BaseActivity 6 | 7 | /** 8 | * Activity holder for Login Flow. 9 | */ 10 | class LoginActivity : BaseActivity() { 11 | 12 | override fun onCreate(savedInstanceState: Bundle?) { 13 | super.onCreate(savedInstanceState) 14 | setContentView(R.layout.activity_main) 15 | configureAndShowFragment() 16 | } 17 | 18 | private fun configureAndShowFragment() { 19 | var fragment = supportFragmentManager.findFragmentById(R.id.base_frame_layout) as LoginActivityFragment? 20 | if(fragment == null){ 21 | supportFragmentManager.beginTransaction() 22 | .add(R.id.base_frame_layout, LoginActivityFragment.getMainActivityFragment()) 23 | .commit() 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/screens/login/LoginActivityFragment.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | 4 | import android.os.Bundle 5 | import android.os.Handler 6 | import android.os.Looper 7 | import android.view.View 8 | import androidx.lifecycle.Observer 9 | import androidx.lifecycle.ViewModelProviders 10 | import androidx.recyclerview.widget.LinearLayoutManager 11 | import com.dev.ccodetest.R 12 | import com.dev.ccodetest.models.login.AllPeople 13 | import com.dev.ccodetest.models.login.AllPeopleResult 14 | import com.dev.ccodetest.platform.BaseFragment 15 | import com.dev.ccodetest.platform.BaseViewModelFactory 16 | import com.dev.ccodetest.platform.LiveDataWrapper 17 | import kotlinx.android.synthetic.main.fragment_main_activity.* 18 | import kotlinx.coroutines.Dispatchers 19 | import org.koin.android.ext.android.inject 20 | 21 | /** 22 | * Login Fragment. 23 | * Handles UI. 24 | * Fires rest api initiation. 25 | */ 26 | class LoginActivityFragment : BaseFragment() { 27 | 28 | companion object{ 29 | fun getMainActivityFragment() = LoginActivityFragment() 30 | } 31 | 32 | //---------------Class variables---------------// 33 | 34 | val mLoginUseCase : LoginUseCase by inject() 35 | private val mBaseViewModelFactory : BaseViewModelFactory = 36 | BaseViewModelFactory(Dispatchers.Main, Dispatchers.IO,mLoginUseCase) 37 | private val TAG = LoginActivityFragment::class.java.simpleName 38 | val mDemoParam = "1" 39 | lateinit var mRecyclerViewAdapter: LoginRecyclerViewAdapter 40 | 41 | private val mViewModel : LoginActivityViewModel by lazy { 42 | ViewModelProviders.of(this,mBaseViewModelFactory) 43 | .get(LoginActivityViewModel::class.java) } 44 | 45 | //---------------Life Cycle---------------// 46 | 47 | override fun onActivityCreated(savedInstanceState: Bundle?) { 48 | super.onActivityCreated(savedInstanceState) 49 | 50 | //Start observing the targets 51 | this.mViewModel.mAllPeopleResponse.observe(this, this.mDataObserver) 52 | this.mViewModel.mLoadingLiveData.observe(this, this.loadingObserver) 53 | 54 | mRecyclerViewAdapter = LoginRecyclerViewAdapter(activity!!, arrayListOf()) 55 | landingListRecyclerView.adapter = mRecyclerViewAdapter 56 | landingListRecyclerView.layoutManager = LinearLayoutManager(activity!!) 57 | 58 | this.mViewModel.requestLoginActivityData(mDemoParam) 59 | 60 | } 61 | 62 | //---------------Observers---------------// 63 | private val mDataObserver = Observer> { result -> 64 | when (result?.responseStatus) { 65 | LiveDataWrapper.RESPONSESTATUS.LOADING -> { 66 | // Loading data 67 | } 68 | LiveDataWrapper.RESPONSESTATUS.ERROR -> { 69 | // Error for http request 70 | logD(TAG,"LiveDataResult.Status.ERROR = ${result.response}") 71 | error_holder.visibility = View.VISIBLE 72 | showToast("Error in getting data") 73 | 74 | } 75 | LiveDataWrapper.RESPONSESTATUS.SUCCESS -> { 76 | // Data from API 77 | logD(TAG,"LiveDataResult.Status.SUCCESS = ${result.response}") 78 | val mainItemReceived = result.response as AllPeople 79 | val listItems = mainItemReceived.results as ArrayList 80 | processData(listItems) 81 | } 82 | } 83 | } 84 | 85 | override fun getLayoutId(): Int = R.layout.fragment_main_activity 86 | 87 | /** 88 | * Handle success data 89 | */ 90 | private fun processData(listItems: ArrayList) { 91 | logD(TAG,"processData called with data ${listItems.size}") 92 | logD(TAG,"processData listItems = ${listItems}") 93 | 94 | val refresh = Handler(Looper.getMainLooper()) 95 | refresh.post { 96 | mRecyclerViewAdapter.updateListItems(listItems) 97 | } 98 | } 99 | 100 | /** 101 | * Hide / show loader 102 | */ 103 | private val loadingObserver = Observer { visible -> 104 | // Show hide progress bar 105 | if(visible){ 106 | progress_circular.visibility = View.VISIBLE 107 | }else{ 108 | progress_circular.visibility = View.INVISIBLE 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/screens/login/LoginActivityViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | import androidx.lifecycle.MutableLiveData 4 | import androidx.lifecycle.ViewModel 5 | import com.dev.ccodetest.models.login.AllPeople 6 | import com.dev.ccodetest.platform.LiveDataWrapper 7 | import kotlinx.coroutines.* 8 | import org.koin.core.KoinComponent 9 | 10 | /** 11 | * Login View Model. 12 | * Handles connecting with corresponding Use Case. 13 | * Getting back data to View. 14 | */ 15 | 16 | class LoginActivityViewModel( 17 | mainDispatcher: CoroutineDispatcher, 18 | ioDispatcher: CoroutineDispatcher, 19 | private val useCase: LoginUseCase 20 | ) : ViewModel(), KoinComponent 21 | { 22 | 23 | var mAllPeopleResponse = MutableLiveData>() 24 | val mLoadingLiveData = MutableLiveData() 25 | private val job = SupervisorJob() 26 | val mUiScope = CoroutineScope(mainDispatcher + job) 27 | val mIoScope = CoroutineScope(ioDispatcher + job) 28 | 29 | init { 30 | //call reset to reset all VM data as required 31 | resetValues() 32 | } 33 | 34 | //Reset any member as per flow 35 | fun resetValues() { 36 | 37 | } 38 | 39 | //can be called from View explicitly if required 40 | //Keep default scope 41 | fun requestLoginActivityData(param:String) { 42 | if(mAllPeopleResponse.value == null){ 43 | mUiScope.launch { 44 | mAllPeopleResponse.value = LiveDataWrapper.loading() 45 | setLoadingVisibility(true) 46 | try { 47 | val data = mIoScope.async { 48 | return@async useCase.processLoginUseCase(param) 49 | }.await() 50 | try { 51 | mAllPeopleResponse.value = LiveDataWrapper.success(data) 52 | } catch (e: Exception) { 53 | } 54 | setLoadingVisibility(false) 55 | } catch (e: Exception) { 56 | setLoadingVisibility(false) 57 | mAllPeopleResponse.value = LiveDataWrapper.error(e) 58 | } 59 | } 60 | } 61 | } 62 | 63 | private fun setLoadingVisibility(visible: Boolean) { 64 | mLoadingLiveData.postValue(visible) 65 | } 66 | 67 | override fun onCleared() { 68 | super.onCleared() 69 | this.job.cancel() 70 | } 71 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/screens/login/LoginRecyclerViewAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | import android.content.Context 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.TextView 8 | import androidx.recyclerview.widget.RecyclerView 9 | import com.dev.ccodetest.R 10 | import com.dev.ccodetest.models.login.AllPeopleResult 11 | 12 | /** 13 | * Adapter for Login screen recyclerview. 14 | */ 15 | class LoginRecyclerViewAdapter(val context: Context, list: ArrayList): RecyclerView.Adapter() { 16 | 17 | var mItemList = list 18 | 19 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LoginFragViewHolder { 20 | return LoginFragViewHolder(LayoutInflater.from(context).inflate(R.layout.landing_list_view_item,parent,false)) 21 | } 22 | 23 | fun updateListItems(updatedList: ArrayList){ 24 | mItemList.clear() 25 | mItemList = updatedList 26 | notifyDataSetChanged() 27 | } 28 | override fun getItemCount(): Int { 29 | return mItemList.size 30 | } 31 | 32 | override fun onBindViewHolder(holder: LoginFragViewHolder, position: Int) { 33 | val model : AllPeopleResult = mItemList[position] 34 | holder.personName.text = model.name 35 | holder.dob.text = model.birth_year 36 | } 37 | 38 | class LoginFragViewHolder(item: View): RecyclerView.ViewHolder(item){ 39 | val personName : TextView = item.findViewById(R.id.personName) 40 | val dob : TextView = item.findViewById(R.id.dateOfBirth) 41 | } 42 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/screens/login/LoginUseCase.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | import com.dev.ccodetest.models.login.AllPeople 4 | import com.dev.ccodetest.repository.LoginRepository 5 | import org.koin.core.KoinComponent 6 | import org.koin.core.inject 7 | 8 | /** 9 | * Use Case class for handling Login flow. 10 | * Requests data from Repo. 11 | * Process received data into required model and reverts back to ViewModel. 12 | */ 13 | class LoginUseCase : KoinComponent { 14 | 15 | val mLoginRepo : LoginRepository by inject() 16 | 17 | suspend fun processLoginUseCase(query: String) : AllPeople { 18 | for (x in 0 until 3){ 19 | println(" Pre Data manipulation $x") 20 | } 21 | val response = mLoginRepo.getLoginData(query) 22 | 23 | for (x in 0 until 3){ 24 | println(" Post Data manipulation $x") 25 | } 26 | 27 | return response 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/uigeneric/GenericAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.uigeneric 2 | 3 | import android.view.LayoutInflater 4 | import android.view.View 5 | import android.view.ViewGroup 6 | import androidx.recyclerview.widget.RecyclerView 7 | 8 | 9 | /** 10 | * Common adapter for RecyclerViews. 11 | * 12 | */ 13 | abstract class GenericAdapter: RecyclerView.Adapter { 14 | 15 | var mListItems: List 16 | //Can be used for filtering 17 | var mListItemsFiltered: List 18 | var mListener: View.OnClickListener? = null 19 | 20 | init { 21 | mListItems = emptyList() 22 | mListItemsFiltered = emptyList() 23 | } 24 | 25 | constructor(){ 26 | mListItems = emptyList() 27 | mListItemsFiltered = emptyList() 28 | 29 | } 30 | 31 | constructor(listener: View.OnClickListener){ 32 | mListItems = emptyList() 33 | mListItemsFiltered = emptyList() 34 | mListener = listener 35 | } 36 | 37 | fun setItems(listItems: List) { 38 | this.mListItems = listItems 39 | this.mListItemsFiltered = listItems 40 | notifyDataSetChanged() 41 | } 42 | 43 | fun refreshRecyclerView(){ 44 | notifyDataSetChanged() 45 | } 46 | 47 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { 48 | return getViewHolder( 49 | LayoutInflater.from(parent.context) 50 | .inflate(viewType, parent, false) 51 | , viewType) 52 | } 53 | 54 | override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { 55 | 56 | (holder as Binder).bind(mListItemsFiltered[position]) 57 | holder.itemView.tag = mListItemsFiltered[position] 58 | 59 | if(mListener != null) 60 | holder.itemView.setOnClickListener(mListener) 61 | } 62 | 63 | override fun getItemCount(): Int { 64 | 65 | return mListItemsFiltered.size 66 | } 67 | 68 | override fun getItemViewType(position: Int): Int { 69 | 70 | return getLayoutId(position, mListItemsFiltered[position]) 71 | } 72 | 73 | protected abstract fun getLayoutId(position: Int, obj: T): Int 74 | 75 | abstract fun getViewHolder(view: View, viewType: Int):RecyclerView.ViewHolder 76 | 77 | internal interface Binder { 78 | fun bind(data: T) 79 | } 80 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/uigeneric/RecyclerOnScrollListener.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.uigeneric 2 | 3 | 4 | import androidx.recyclerview.widget.RecyclerView 5 | import androidx.recyclerview.widget.LinearLayoutManager 6 | 7 | /** 8 | * Common class for recyclerview listener. 9 | */ 10 | abstract class RecyclerOnScrollListener : RecyclerView.OnScrollListener() { 11 | 12 | var mPreviousTotal = 0 13 | var mLoading = true 14 | 15 | override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { 16 | super.onScrolled(recyclerView, dx, dy) 17 | 18 | val visibleItemCount = recyclerView.childCount 19 | val totalItemCount = recyclerView.layoutManager!!.itemCount 20 | val firstVisibleItem = 21 | (recyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() 22 | 23 | if (mLoading) { 24 | if (totalItemCount > mPreviousTotal) { 25 | mLoading = false 26 | mPreviousTotal = totalItemCount 27 | } 28 | } 29 | val visibleThreshold = 10 30 | if (!mLoading && totalItemCount - visibleItemCount <= firstVisibleItem + visibleThreshold) { 31 | // End has been reached 32 | onLoadMore() 33 | mLoading = true 34 | } 35 | } 36 | abstract fun onLoadMore() 37 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dev/ccodetest/utils/AppUtils.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.utils 2 | 3 | import android.content.Context 4 | import android.net.ConnectivityManager 5 | import android.net.NetworkInfo 6 | 7 | /** 8 | * Common Utilities for App 9 | */ 10 | class AppUtils(val context: Context) { 11 | 12 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_baseline_warning_24.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_main_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 22 | 23 | 32 | 38 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /app/src/main/res/layout/landing_list_view_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | 13 | 17 | 18 | 27 | 28 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5dp 4 | 1dp 5 | 5dp 6 | 10dp 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Testing Clean Code 3 | Error in retrieving data 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/dev/ccodetest/base/BaseUTTest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.base 2 | 3 | import okhttp3.mockwebserver.MockResponse 4 | import okhttp3.mockwebserver.MockWebServer 5 | import org.junit.After 6 | import org.junit.Before 7 | import org.koin.core.context.stopKoin 8 | import org.koin.test.KoinTest 9 | import java.io.File 10 | 11 | /** 12 | * Creates base 13 | */ 14 | abstract class BaseUTTest : KoinTest { 15 | 16 | /** 17 | * For MockWebServer instance 18 | */ 19 | private lateinit var mMockServerInstance: MockWebServer 20 | 21 | /** 22 | * Default, let server be shut down 23 | */ 24 | private var mShouldStart = false 25 | 26 | @Before 27 | open fun setUp(){ 28 | startMockServer(true) 29 | } 30 | 31 | /** 32 | * Helps to read input file returns the respective data in mocked call 33 | */ 34 | fun mockNetworkResponseWithFileContent(fileName: String, responseCode: Int) = mMockServerInstance.enqueue( 35 | MockResponse() 36 | .setResponseCode(responseCode) 37 | .setBody(getJson(fileName))) 38 | 39 | /** 40 | * Reads input file and converts to json 41 | */ 42 | fun getJson(path : String) : String { 43 | val uri = javaClass.classLoader!!.getResource(path) 44 | val file = File(uri.path) 45 | return String(file.readBytes()) 46 | } 47 | 48 | /** 49 | * Start Mockwebserver 50 | */ 51 | private fun startMockServer(shouldStart:Boolean){ 52 | if (shouldStart){ 53 | mShouldStart = shouldStart 54 | mMockServerInstance = MockWebServer() 55 | mMockServerInstance.start() 56 | } 57 | } 58 | 59 | /** 60 | * Set Mockwebserver url 61 | */ 62 | fun getMockWebServerUrl() = mMockServerInstance.url("/").toString() 63 | 64 | /** 65 | * Stop Mockwebserver 66 | */ 67 | private fun stopMockServer() { 68 | if (mShouldStart){ 69 | mMockServerInstance.shutdown() 70 | } 71 | } 72 | 73 | @After 74 | open fun tearDown(){ 75 | //Stop Mock server 76 | stopMockServer() 77 | //Stop Koin as well 78 | stopKoin() 79 | } 80 | } -------------------------------------------------------------------------------- /app/src/test/java/com/dev/ccodetest/di/MainDIComponentTest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | /** 4 | * Main Koin DI component. 5 | * Helps to configure 6 | * 1) Mockwebserver 7 | * 2) Usecase 8 | * 3) Repository 9 | */ 10 | fun configureTestAppComponent(baseApi: String) 11 | = listOf( 12 | MockWebServerDIPTest, 13 | configureNetworkModuleForTest(baseApi), 14 | UseCaseDependency, 15 | RepoDependency 16 | ) 17 | 18 | -------------------------------------------------------------------------------- /app/src/test/java/com/dev/ccodetest/di/MockWebServerDITest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import okhttp3.mockwebserver.MockWebServer 4 | import org.koin.dsl.module 5 | 6 | /** 7 | * Creates Mockwebserver instance for testing 8 | */ 9 | val MockWebServerDIPTest = module { 10 | 11 | factory { 12 | MockWebServer() 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /app/src/test/java/com/dev/ccodetest/di/NetworkDITest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.di 2 | 3 | import com.dev.ccodetest.network.login.LoginAPIService 4 | import com.google.gson.GsonBuilder 5 | import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory 6 | import org.koin.dsl.module 7 | import retrofit2.Retrofit 8 | import retrofit2.converter.gson.GsonConverterFactory 9 | 10 | /** 11 | * Network module test configuration with mockserver url. 12 | */ 13 | fun configureNetworkModuleForTest(baseApi: String) 14 | = module{ 15 | single { 16 | Retrofit.Builder() 17 | .baseUrl(baseApi) 18 | .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) 19 | .addCallAdapterFactory(CoroutineCallAdapterFactory()) 20 | .build() 21 | } 22 | factory{ get().create(LoginAPIService::class.java) } 23 | } -------------------------------------------------------------------------------- /app/src/test/java/com/dev/ccodetest/repository/LoginRepositoryTest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.repository 2 | 3 | import androidx.arch.core.executor.testing.InstantTaskExecutorRule 4 | import com.dev.ccodetest.base.BaseUTTest 5 | import com.dev.ccodetest.di.configureTestAppComponent 6 | import com.dev.ccodetest.models.login.AllPeople 7 | import com.dev.ccodetest.network.login.LoginAPIService 8 | import kotlinx.coroutines.runBlocking 9 | import okhttp3.mockwebserver.MockWebServer 10 | import org.junit.Assert.* 11 | import org.junit.Before 12 | import org.junit.Rule 13 | import org.junit.Test 14 | import org.junit.runner.RunWith 15 | import org.junit.runners.JUnit4 16 | import org.koin.core.context.startKoin 17 | import org.koin.test.inject 18 | import java.net.HttpURLConnection 19 | 20 | @RunWith(JUnit4::class) 21 | class LoginRepositoryTest : BaseUTTest(){ 22 | 23 | //Target 24 | private lateinit var mRepo: LoginRepository 25 | //Inject api service created with koin 26 | val mAPIService : LoginAPIService by inject() 27 | //Inject Mockwebserver created with koin 28 | val mockWebServer : MockWebServer by inject() 29 | 30 | @get:Rule 31 | var instantExecutorRule = InstantTaskExecutorRule() 32 | 33 | val mNextValue = "https://swapi.co/api/people/?page=2" 34 | val mParam = "1" 35 | val mCount = 87 36 | 37 | @Before 38 | fun start(){ 39 | super.setUp() 40 | 41 | startKoin{ modules(configureTestAppComponent(getMockWebServerUrl()))} 42 | } 43 | 44 | @Test 45 | fun test_login_repo_retrieves_expected_data() = runBlocking{ 46 | 47 | mockNetworkResponseWithFileContent("success_resp_list.json", HttpURLConnection.HTTP_OK) 48 | mRepo = LoginRepository() 49 | 50 | val dataReceived = mRepo.getLoginData(mParam) 51 | 52 | assertNotNull(dataReceived) 53 | assertEquals(dataReceived.count, mCount) 54 | assertEquals(dataReceived.next, mNextValue) 55 | } 56 | } -------------------------------------------------------------------------------- /app/src/test/java/com/dev/ccodetest/screens/login/LoginActivityViewModelTest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | import androidx.arch.core.executor.testing.InstantTaskExecutorRule 4 | import com.dev.ccodetest.base.BaseUTTest 5 | import com.dev.ccodetest.di.configureTestAppComponent 6 | import com.dev.ccodetest.models.login.AllPeople 7 | import com.dev.ccodetest.platform.LiveDataWrapper 8 | import com.google.gson.Gson 9 | import io.mockk.MockKAnnotations 10 | import io.mockk.coEvery 11 | import io.mockk.impl.annotations.MockK 12 | import kotlinx.coroutines.Dispatchers 13 | import org.junit.Assert.assertEquals 14 | import org.junit.Before 15 | import org.junit.Rule 16 | import org.junit.Test 17 | import org.junit.runner.RunWith 18 | import org.junit.runners.JUnit4 19 | import org.koin.core.context.startKoin 20 | 21 | @RunWith(JUnit4::class) 22 | class LoginActivityViewModelTest: BaseUTTest(){ 23 | 24 | @get:Rule 25 | var instantExecutorRule = InstantTaskExecutorRule() 26 | 27 | lateinit var mLoginActivityViewModel: LoginActivityViewModel 28 | 29 | val mDispatcher = Dispatchers.Unconfined 30 | 31 | @MockK 32 | lateinit var mLoginUseCase: LoginUseCase 33 | 34 | val mParam = "1" 35 | val mNextValue = "https://swapi.co/api/people/?page=2" 36 | 37 | @Before 38 | fun start(){ 39 | super.setUp() 40 | //Used for initiation of Mockk 41 | MockKAnnotations.init(this) 42 | //Start Koin with required dependencies 43 | startKoin{ modules(configureTestAppComponent(getMockWebServerUrl()))} 44 | } 45 | 46 | @Test 47 | fun test_login_view_model_data_populates_expected_value(){ 48 | 49 | mLoginActivityViewModel = LoginActivityViewModel(mDispatcher,mDispatcher,mLoginUseCase) 50 | val sampleResponse = getJson("success_resp_list.json") 51 | var jsonObj = Gson().fromJson(sampleResponse, AllPeople::class.java) 52 | //Make sure login use case returns expected response when called 53 | coEvery { mLoginUseCase.processLoginUseCase(any()) } returns jsonObj 54 | mLoginActivityViewModel.mAllPeopleResponse.observeForever { } 55 | 56 | mLoginActivityViewModel.requestLoginActivityData(mParam) 57 | 58 | assert(mLoginActivityViewModel.mAllPeopleResponse.value != null) 59 | assert( 60 | mLoginActivityViewModel.mAllPeopleResponse.value!!.responseStatus 61 | == LiveDataWrapper.RESPONSESTATUS.SUCCESS) 62 | val testResult = mLoginActivityViewModel.mAllPeopleResponse.value as LiveDataWrapper 63 | assertEquals(testResult.response!!.next,mNextValue) 64 | } 65 | } -------------------------------------------------------------------------------- /app/src/test/java/com/dev/ccodetest/screens/login/LoginUseCaseTest.kt: -------------------------------------------------------------------------------- 1 | package com.dev.ccodetest.screens.login 2 | 3 | import androidx.arch.core.executor.testing.InstantTaskExecutorRule 4 | import com.dev.ccodetest.base.BaseUTTest 5 | import com.dev.ccodetest.di.configureTestAppComponent 6 | import com.dev.ccodetest.models.login.AllPeople 7 | import com.dev.ccodetest.network.login.LoginAPIService 8 | import com.dev.ccodetest.repository.LoginRepository 9 | import kotlinx.coroutines.runBlocking 10 | import okhttp3.mockwebserver.MockWebServer 11 | import org.junit.Assert.assertEquals 12 | import org.junit.Assert.assertNotNull 13 | import org.junit.Before 14 | import org.junit.Rule 15 | import org.junit.Test 16 | import org.junit.runner.RunWith 17 | import org.junit.runners.JUnit4 18 | import org.koin.core.context.startKoin 19 | import org.koin.test.inject 20 | import java.net.HttpURLConnection 21 | 22 | @RunWith(JUnit4::class) 23 | class LoginUseCaseTest : BaseUTTest(){ 24 | 25 | //Target 26 | private lateinit var mLoginUseCase: LoginUseCase 27 | //Inject login repo created with koin 28 | val mLoginRepo : LoginRepository by inject() 29 | //Inject api service created with koin 30 | val mAPIService : LoginAPIService by inject() 31 | //Inject Mockwebserver created with koin 32 | val mockWebServer : MockWebServer by inject() 33 | 34 | @get:Rule 35 | var instantExecutorRule = InstantTaskExecutorRule() 36 | 37 | val mNextValue = "https://swapi.co/api/people/?page=2" 38 | val mParam = "1" 39 | val mCount = 87 40 | 41 | @Before 42 | fun start(){ 43 | super.setUp() 44 | //Start Koin with required dependencies 45 | startKoin{ modules(configureTestAppComponent(getMockWebServerUrl()))} 46 | } 47 | 48 | @Test 49 | fun test_login_use_case_returns_expected_value()= runBlocking{ 50 | 51 | mockNetworkResponseWithFileContent("success_resp_list.json", HttpURLConnection.HTTP_OK) 52 | mLoginUseCase = LoginUseCase() 53 | 54 | val dataReceived = mLoginUseCase.processLoginUseCase(mParam) 55 | 56 | assertNotNull(dataReceived) 57 | assertEquals(dataReceived.count, mCount) 58 | assertEquals(dataReceived.next, mNextValue) 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /app/src/test/resources/success_resp_list.json: -------------------------------------------------------------------------------- 1 | { 2 | "count": 87, 3 | "next": "https://swapi.co/api/people/?page=2", 4 | "previous": null, 5 | "results": [ 6 | { 7 | "name": "Luke Skywalker", 8 | "height": "172", 9 | "mass": "77", 10 | "hair_color": "blond", 11 | "skin_color": "fair", 12 | "eye_color": "blue", 13 | "birth_year": "19BBY", 14 | "gender": "male", 15 | "homeworld": "https://swapi.co/api/planets/1/", 16 | "films": [ 17 | "https://swapi.co/api/films/2/", 18 | "https://swapi.co/api/films/6/", 19 | "https://swapi.co/api/films/3/", 20 | "https://swapi.co/api/films/1/", 21 | "https://swapi.co/api/films/7/" 22 | ], 23 | "species": [ 24 | "https://swapi.co/api/species/1/" 25 | ], 26 | "vehicles": [ 27 | "https://swapi.co/api/vehicles/14/", 28 | "https://swapi.co/api/vehicles/30/" 29 | ], 30 | "starships": [ 31 | "https://swapi.co/api/starships/12/", 32 | "https://swapi.co/api/starships/22/" 33 | ], 34 | "created": "2014-12-09T13:50:51.644000Z", 35 | "edited": "2014-12-20T21:17:56.891000Z", 36 | "url": "https://swapi.co/api/people/1/" 37 | }, 38 | { 39 | "name": "C-3PO", 40 | "height": "167", 41 | "mass": "75", 42 | "hair_color": "n/a", 43 | "skin_color": "gold", 44 | "eye_color": "yellow", 45 | "birth_year": "112BBY", 46 | "gender": "n/a", 47 | "homeworld": "https://swapi.co/api/planets/1/", 48 | "films": [ 49 | "https://swapi.co/api/films/2/", 50 | "https://swapi.co/api/films/5/", 51 | "https://swapi.co/api/films/4/", 52 | "https://swapi.co/api/films/6/", 53 | "https://swapi.co/api/films/3/", 54 | "https://swapi.co/api/films/1/" 55 | ], 56 | "species": [ 57 | "https://swapi.co/api/species/2/" 58 | ], 59 | "vehicles": [], 60 | "starships": [], 61 | "created": "2014-12-10T15:10:51.357000Z", 62 | "edited": "2014-12-20T21:17:50.309000Z", 63 | "url": "https://swapi.co/api/people/2/" 64 | }, 65 | { 66 | "name": "R2-D2", 67 | "height": "96", 68 | "mass": "32", 69 | "hair_color": "n/a", 70 | "skin_color": "white, blue", 71 | "eye_color": "red", 72 | "birth_year": "33BBY", 73 | "gender": "n/a", 74 | "homeworld": "https://swapi.co/api/planets/8/", 75 | "films": [ 76 | "https://swapi.co/api/films/2/", 77 | "https://swapi.co/api/films/5/", 78 | "https://swapi.co/api/films/4/", 79 | "https://swapi.co/api/films/6/", 80 | "https://swapi.co/api/films/3/", 81 | "https://swapi.co/api/films/1/", 82 | "https://swapi.co/api/films/7/" 83 | ], 84 | "species": [ 85 | "https://swapi.co/api/species/2/" 86 | ], 87 | "vehicles": [], 88 | "starships": [], 89 | "created": "2014-12-10T15:11:50.376000Z", 90 | "edited": "2014-12-20T21:17:50.311000Z", 91 | "url": "https://swapi.co/api/people/3/" 92 | }, 93 | { 94 | "name": "Darth Vader", 95 | "height": "202", 96 | "mass": "136", 97 | "hair_color": "none", 98 | "skin_color": "white", 99 | "eye_color": "yellow", 100 | "birth_year": "41.9BBY", 101 | "gender": "male", 102 | "homeworld": "https://swapi.co/api/planets/1/", 103 | "films": [ 104 | "https://swapi.co/api/films/2/", 105 | "https://swapi.co/api/films/6/", 106 | "https://swapi.co/api/films/3/", 107 | "https://swapi.co/api/films/1/" 108 | ], 109 | "species": [ 110 | "https://swapi.co/api/species/1/" 111 | ], 112 | "vehicles": [], 113 | "starships": [ 114 | "https://swapi.co/api/starships/13/" 115 | ], 116 | "created": "2014-12-10T15:18:20.704000Z", 117 | "edited": "2014-12-20T21:17:50.313000Z", 118 | "url": "https://swapi.co/api/people/4/" 119 | }, 120 | { 121 | "name": "Leia Organa", 122 | "height": "150", 123 | "mass": "49", 124 | "hair_color": "brown", 125 | "skin_color": "light", 126 | "eye_color": "brown", 127 | "birth_year": "19BBY", 128 | "gender": "female", 129 | "homeworld": "https://swapi.co/api/planets/2/", 130 | "films": [ 131 | "https://swapi.co/api/films/2/", 132 | "https://swapi.co/api/films/6/", 133 | "https://swapi.co/api/films/3/", 134 | "https://swapi.co/api/films/1/", 135 | "https://swapi.co/api/films/7/" 136 | ], 137 | "species": [ 138 | "https://swapi.co/api/species/1/" 139 | ], 140 | "vehicles": [ 141 | "https://swapi.co/api/vehicles/30/" 142 | ], 143 | "starships": [], 144 | "created": "2014-12-10T15:20:09.791000Z", 145 | "edited": "2014-12-20T21:17:50.315000Z", 146 | "url": "https://swapi.co/api/people/5/" 147 | }, 148 | { 149 | "name": "Owen Lars", 150 | "height": "178", 151 | "mass": "120", 152 | "hair_color": "brown, grey", 153 | "skin_color": "light", 154 | "eye_color": "blue", 155 | "birth_year": "52BBY", 156 | "gender": "male", 157 | "homeworld": "https://swapi.co/api/planets/1/", 158 | "films": [ 159 | "https://swapi.co/api/films/5/", 160 | "https://swapi.co/api/films/6/", 161 | "https://swapi.co/api/films/1/" 162 | ], 163 | "species": [ 164 | "https://swapi.co/api/species/1/" 165 | ], 166 | "vehicles": [], 167 | "starships": [], 168 | "created": "2014-12-10T15:52:14.024000Z", 169 | "edited": "2014-12-20T21:17:50.317000Z", 170 | "url": "https://swapi.co/api/people/6/" 171 | }, 172 | { 173 | "name": "Beru Whitesun lars", 174 | "height": "165", 175 | "mass": "75", 176 | "hair_color": "brown", 177 | "skin_color": "light", 178 | "eye_color": "blue", 179 | "birth_year": "47BBY", 180 | "gender": "female", 181 | "homeworld": "https://swapi.co/api/planets/1/", 182 | "films": [ 183 | "https://swapi.co/api/films/5/", 184 | "https://swapi.co/api/films/6/", 185 | "https://swapi.co/api/films/1/" 186 | ], 187 | "species": [ 188 | "https://swapi.co/api/species/1/" 189 | ], 190 | "vehicles": [], 191 | "starships": [], 192 | "created": "2014-12-10T15:53:41.121000Z", 193 | "edited": "2014-12-20T21:17:50.319000Z", 194 | "url": "https://swapi.co/api/people/7/" 195 | }, 196 | { 197 | "name": "R5-D4", 198 | "height": "97", 199 | "mass": "32", 200 | "hair_color": "n/a", 201 | "skin_color": "white, red", 202 | "eye_color": "red", 203 | "birth_year": "unknown", 204 | "gender": "n/a", 205 | "homeworld": "https://swapi.co/api/planets/1/", 206 | "films": [ 207 | "https://swapi.co/api/films/1/" 208 | ], 209 | "species": [ 210 | "https://swapi.co/api/species/2/" 211 | ], 212 | "vehicles": [], 213 | "starships": [], 214 | "created": "2014-12-10T15:57:50.959000Z", 215 | "edited": "2014-12-20T21:17:50.321000Z", 216 | "url": "https://swapi.co/api/people/8/" 217 | }, 218 | { 219 | "name": "Biggs Darklighter", 220 | "height": "183", 221 | "mass": "84", 222 | "hair_color": "black", 223 | "skin_color": "light", 224 | "eye_color": "brown", 225 | "birth_year": "24BBY", 226 | "gender": "male", 227 | "homeworld": "https://swapi.co/api/planets/1/", 228 | "films": [ 229 | "https://swapi.co/api/films/1/" 230 | ], 231 | "species": [ 232 | "https://swapi.co/api/species/1/" 233 | ], 234 | "vehicles": [], 235 | "starships": [ 236 | "https://swapi.co/api/starships/12/" 237 | ], 238 | "created": "2014-12-10T15:59:50.509000Z", 239 | "edited": "2014-12-20T21:17:50.323000Z", 240 | "url": "https://swapi.co/api/people/9/" 241 | }, 242 | { 243 | "name": "Obi-Wan Kenobi", 244 | "height": "182", 245 | "mass": "77", 246 | "hair_color": "auburn, white", 247 | "skin_color": "fair", 248 | "eye_color": "blue-gray", 249 | "birth_year": "57BBY", 250 | "gender": "male", 251 | "homeworld": "https://swapi.co/api/planets/20/", 252 | "films": [ 253 | "https://swapi.co/api/films/2/", 254 | "https://swapi.co/api/films/5/", 255 | "https://swapi.co/api/films/4/", 256 | "https://swapi.co/api/films/6/", 257 | "https://swapi.co/api/films/3/", 258 | "https://swapi.co/api/films/1/" 259 | ], 260 | "species": [ 261 | "https://swapi.co/api/species/1/" 262 | ], 263 | "vehicles": [ 264 | "https://swapi.co/api/vehicles/38/" 265 | ], 266 | "starships": [ 267 | "https://swapi.co/api/starships/48/", 268 | "https://swapi.co/api/starships/59/", 269 | "https://swapi.co/api/starships/64/", 270 | "https://swapi.co/api/starships/65/", 271 | "https://swapi.co/api/starships/74/" 272 | ], 273 | "created": "2014-12-10T16:16:29.192000Z", 274 | "edited": "2014-12-20T21:17:50.325000Z", 275 | "url": "https://swapi.co/api/people/10/" 276 | } 277 | ] 278 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext { 5 | kotlin_version = '1.4.10' 6 | gradle_version = '4.0.1' 7 | app_compat = '1.2.0' 8 | legacy_support = '1.0.0' 9 | koin_version = '2.0.1' 10 | material_design = '1.3.0-alpha03' 11 | retrofit_version = '2.6.0' 12 | jackson_fasterxml = '2.9.7' 13 | core_ktx = '1.3.2' 14 | constraint_lyt = '2.0.1' 15 | lifecycle_extension = '2.2.0' 16 | couroutines_version = '1.3.0-M2' 17 | http_logging = '4.1.0' 18 | multidex = '2.0.1' 19 | coroutines_adapter = '0.9.2' 20 | glide_version = '4.10.0' 21 | leakCanary = '2.0-beta-3' 22 | mockwebserver_version = '4.1.0' 23 | recycler_view_version = '1.1.0' 24 | 25 | //testing libraries 26 | junit_x_ext = '1.1.2' 27 | testx_rules = '1.3.0' 28 | android_arch_core_testing = '1.1.1' 29 | retrofit_mock = '2.3.0' 30 | mockk_version = '1.9.3' 31 | espresso_contrib = '3.3.0' 32 | } 33 | repositories { 34 | google() 35 | jcenter() 36 | 37 | } 38 | dependencies { 39 | classpath "com.android.tools.build:gradle:$gradle_version" 40 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 41 | classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" 42 | // NOTE: Do not place your application dependencies here; they belong 43 | // in the individual module build.gradle files 44 | } 45 | } 46 | 47 | allprojects { 48 | repositories { 49 | google() 50 | jcenter() 51 | 52 | } 53 | } 54 | 55 | task clean(type: Delete) { 56 | delete rootProject.buildDir 57 | } 58 | -------------------------------------------------------------------------------- /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/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreeharikv112/CleanCodeMVVMTesting/e13337f3a2be7c580879c4eb9f033a1189748294/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Oct 04 10:28:58 IST 2020 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-6.1.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 | -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Mon May 25 20:57:22 IST 2020 8 | sdk.dir=/Users/hari/Library/Android/sdk 9 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | rootProject.name='CleanCodeMVVMTesting' 3 | --------------------------------------------------------------------------------