From 13eb1ee289f5626e6bcb0d5a0ae253f333979575 Mon Sep 17 00:00:00 2001 From: Khalid Hamid Date: Fri, 4 Mar 2022 16:16:23 +0800 Subject: [PATCH 01/24] OCBC code challenge --- .gitignore | 16 + GitHubRepos/.gitignore | 1 + GitHubRepos/.idea/.DS_Store | Bin 0 -> 6148 bytes GitHubRepos/.idea/compiler.xml | 4 +- GitHubRepos/.idea/gradle.xml | 4 +- GitHubRepos/.idea/jarRepositories.xml | 5 + GitHubRepos/.idea/misc.xml | 10 +- GitHubRepos/README.md | 56 +- GitHubRepos/app/.DS_Store | Bin 6148 -> 6148 bytes GitHubRepos/app/build.gradle.kts | 2 + .../githubrepos/ExampleInstrumentedTest.kt | 38 - .../com/khalid/hamid/githubrepos/TestApp.kt | 29 +- .../hamid/githubrepos/ui/RepoFragmentTest.kt | 115 -- .../githubrepos/utilities/AppTestRunner.kt | 29 +- .../utilities/CountingAppExecutorsRule.kt | 29 +- .../utilities/DataBindingIdlingResource.kt | 29 +- .../DataBindingIdlingResourceRule.kt | 29 +- .../githubrepos/utilities/EspressoTestUtil.kt | 29 +- .../hamid/githubrepos/utilities/MockitoExt.kt | 29 +- .../TaskExecutorWithIdlingResourceRule.kt | 31 +- .../githubrepos/utilities/ViewModelUtil.kt | 29 +- .../src/debug/java/com/khalid/hamid/.DS_Store | Bin .../com/khalid/hamid/githubrepos}/.DS_Store | Bin 6148 -> 6148 bytes .../hamid/githubrepos/di/DebugModule.kt | 26 + .../testing/CountingAppExecutors.kt | 29 +- .../githubrepos/testing/LiveDataTestUtil.kt | 104 ++ .../githubrepos/testing/OpenForTesting.kt | 29 +- .../testing/SingleFragmentActivity.kt | 29 +- .../com/khalid/hamid/githubrepos/GithubApp.kt | 29 +- .../hamid/githubrepos/core/BaseFragment.kt | 10 +- .../khalid/hamid/githubrepos/db/GithubDb.kt | 36 - .../githubrepos/db/GithubTypeConverters.kt | 43 - .../khalid/hamid/githubrepos/db/RepoDao.kt | 36 - .../khalid/hamid/githubrepos/db/WordDao.kt | 28 - .../khalid/hamid/githubrepos/di/AppModule.kt | 52 +- .../githubrepos/di/AppViewModelModule.kt | 18 +- .../hamid/githubrepos/di/CoroutinesModule.kt | 29 +- .../githubrepos/network/BaseDataSource.kt | 59 +- .../githubrepos/network/BaseRepository.kt | 77 +- .../githubrepos/network/GitHubService.kt | 64 +- .../githubrepos/network/RepositoryImple.kt | 99 +- .../hamid/githubrepos/network/Resource.kt | 31 +- .../hamid/githubrepos/network/Result.kt | 111 +- .../hamid/githubrepos/network/Status.kt | 29 +- .../network/local/LocalDataSource.kt | 75 - .../network/remote/AuthInterceptor.kt | 53 + .../network/remote/RemoteDataSource.kt | 96 +- .../githubrepos/ui/DataBoundListAdapter.kt | 53 - .../githubrepos/ui/DataBoundViewHolder.kt | 27 - .../hamid/githubrepos/ui/MainActivity.kt | 118 +- .../hamid/githubrepos/ui/RepoAdapter.kt | 67 - .../hamid/githubrepos/ui/RepoFragment.kt | 110 -- .../hamid/githubrepos/ui/RepoViewModel.kt | 100 -- .../ui/SimpleDividerItemDecoration.kt | 49 - .../githubrepos/ui/balance/BalanceFragment.kt | 131 ++ .../ui/balance/BalanceViewModel.kt | 99 ++ .../ui/balance/TransactionResponse.kt | 83 ++ .../githubrepos/ui/login/LoginFragment.kt | 106 ++ .../ui/login/LoginResponseDummy.kt | 39 + .../githubrepos/ui/login/LoginViewModel.kt | 134 ++ .../ui/register/RegisterFragment.kt | 112 ++ .../ui/register/RegisterViewModel.kt | 136 ++ .../ui/transfer/TransferFragment.kt | 155 +++ .../ui/transfer/TransferViewModel.kt | 141 ++ .../hamid/githubrepos/utilities/.DS_Store | Bin 0 -> 6148 bytes .../githubrepos/utilities/AbsentLiveData.kt | 29 +- .../githubrepos/utilities/AppExecutors.kt | 29 +- .../githubrepos/utilities/AutoClearedValue.kt | 39 +- .../githubrepos/utilities/BindingAdapters.kt | 29 +- .../githubrepos/utilities/CommonUtils.kt | 94 ++ .../hamid/githubrepos/utilities/Constants.kt | 43 +- .../utilities/CrashReportingTree.kt | 29 +- .../githubrepos/utilities/ErrorResponse.kt | 41 + .../utilities/EspressoIdlingResource.kt | 29 +- .../hamid/githubrepos/utilities/Event.kt | 43 +- .../githubrepos/utilities/ForceRefresh.kt | 29 +- .../utilities/FragmentBindingAdapters.kt | 38 +- .../utilities/FragmentDataBindingComponent.kt | 29 +- .../hamid/githubrepos/utilities/Prefs.kt | 59 +- .../githubrepos/utilities/RetryListener.kt | 29 +- .../utilities/SimpleCountingIdlingResource.kt | 29 +- .../khalid/hamid/githubrepos/vo/BuiltBy.kt | 28 - .../khalid/hamid/githubrepos/vo/GitRepos.kt | 250 ++++ .../hamid/githubrepos/vo/Repositories.kt | 40 - .../com/khalid/hamid/githubrepos/vo/Word.kt | 32 - GitHubRepos/app/src/main/res/anim/exit.xml | 9 + .../app/src/main/res/anim/slide_in_left.xml | 22 + .../app/src/main/res/anim/slide_in_right.xml | 22 + .../app/src/main/res/anim/slide_out_left.xml | 22 + .../app/src/main/res/anim/slide_out_right.xml | 22 + .../src/main/res/animator/back_flip_in.xml | 22 + .../src/main/res/animator/front_flip_out.xml | 16 + .../src/main/res/animator/in_animation.xml | 24 + .../src/main/res/animator/out_animation.xml | 16 + .../app/src/main/res/drawable/bg_edittext.xml | 9 + .../app/src/main/res/drawable/btn_border.xml | 9 + .../main/res/drawable/btn_rounded_black.xml | 5 + .../main/res/drawable/btn_rounded_solid.xml | 5 + .../src/main/res/drawable/footer_button.xml | 5 + .../src/main/res/drawable/shape_button.xml | 10 + .../app/src/main/res/layout/activity_main.xml | 34 +- .../app/src/main/res/layout/alert_dialog.xml | 55 + .../src/main/res/layout/container_error.xml | 77 -- .../src/main/res/layout/container_loading.xml | 50 - .../src/main/res/layout/container_main.xml | 48 - .../src/main/res/layout/fragment_balance.xml | 160 +++ .../src/main/res/layout/fragment_login.xml | 166 +++ .../src/main/res/layout/fragment_register.xml | 182 +++ .../app/src/main/res/layout/fragment_repo.xml | 190 --- .../src/main/res/layout/fragment_transfer.xml | 175 +++ .../app/src/main/res/layout/item_details.xml | 5 - .../app/src/main/res/layout/item_main.xml | 148 -- .../src/main/res/layout/item_place_holder.xml | 56 - .../app/src/main/res/layout/view_edittext.xml | 37 + .../src/main/res/layout/view_transaction.xml | 50 + .../res/layout/view_transaction_bubble.xml | 46 + .../app/src/main/res/menu/arrange_list.xml | 13 +- .../app/src/main/res/navigation/main.xml | 73 +- .../app/src/main/res/values/colors.xml | 10 +- .../app/src/main/res/values/dimens.xml | 7 + .../app/src/main/res/values/integer.xml | 6 + .../app/src/main/res/values/strings.xml | 45 +- .../app/src/main/res/values/styles.xml | 18 +- .../assets/api-response/repos-khalid.json | 733 ---------- .../hamid/githubrepos/di/RepositoryModule.kt | 35 +- .../hamid/githubrepos/network/MockUtil.kt | 29 +- .../network/remote/FakeDataSource.kt | 86 +- .../hamid/githubrepos/di/RepositoryModule.kt | 34 +- .../hamid/githubrepos/di/ReleaseModule.kt | 4 +- .../ui/balance/BalanceViewModelTest.kt | 74 + .../ui/login/LoginViewModelTest.kt | 87 ++ .../ui/register/RegisterViewModelTest.kt | 147 ++ .../ui/transfer/TransferViewModelTest.kt | 106 ++ .../hamid/githubrepos/utils/BaseUnitTest.kt | 48 + .../githubrepos/utils/MainCoroutineRule.kt | 54 + .../resources/api-response/repos-git.json | 1228 +++++++++++++++++ .../app/src/test/resources/ocbc/balance.json | 5 + .../app/src/test/resources/ocbc/login.json | 6 + .../app/src/test/resources/ocbc/payees.json | 30 + .../app/src/test/resources/ocbc/register.json | 4 + .../src/test/resources/ocbc/transaction.json | 27 + GitHubRepos/build.gradle.kts | 1 + GitHubRepos/buildSrc/.DS_Store | Bin 0 -> 6148 bytes GitHubRepos/buildSrc/build.gradle.kts | 16 +- .../java/com/dependencies/Dependencies.kt | 3 +- .../main/java/com/dependencies/PluginExt.kt | 23 + GitHubRepos/library/.DS_Store | Bin 0 -> 6148 bytes GitHubRepos/release/.DS_Store | Bin 0 -> 6148 bytes .../Screenshot 2022-02-15 at 10.43.19 PM.png | Bin 0 -> 167619 bytes .../Screenshot 2022-02-15 at 10.43.37 PM.png | Bin 0 -> 89349 bytes .../release/Screenshot_20220215_222225.png | Bin 0 -> 52359 bytes GitHubRepos/release/app-prod-debug.apk | Bin 0 -> 7683684 bytes 152 files changed, 6128 insertions(+), 2855 deletions(-) create mode 100644 .gitignore create mode 100644 GitHubRepos/.idea/.DS_Store delete mode 100644 GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ExampleInstrumentedTest.kt delete mode 100644 GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ui/RepoFragmentTest.kt rename .DS_Store => GitHubRepos/app/src/debug/java/com/khalid/hamid/.DS_Store (100%) rename GitHubRepos/{ => app/src/debug/java/com/khalid/hamid/githubrepos}/.DS_Store (97%) create mode 100644 GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/LiveDataTestUtil.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubDb.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubTypeConverters.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/RepoDao.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/WordDao.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/local/LocalDataSource.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/AuthInterceptor.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundListAdapter.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundViewHolder.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/RepoAdapter.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/RepoFragment.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/RepoViewModel.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/SimpleDividerItemDecoration.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/balance/BalanceFragment.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/balance/BalanceViewModel.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/balance/TransactionResponse.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/login/LoginFragment.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/login/LoginResponseDummy.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/login/LoginViewModel.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/register/RegisterFragment.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/register/RegisterViewModel.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/transfer/TransferFragment.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/transfer/TransferViewModel.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/utilities/.DS_Store create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/utilities/CommonUtils.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/utilities/ErrorResponse.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/vo/BuiltBy.kt create mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/vo/GitRepos.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/vo/Repositories.kt delete mode 100644 GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/vo/Word.kt create mode 100644 GitHubRepos/app/src/main/res/anim/exit.xml create mode 100644 GitHubRepos/app/src/main/res/anim/slide_in_left.xml create mode 100644 GitHubRepos/app/src/main/res/anim/slide_in_right.xml create mode 100644 GitHubRepos/app/src/main/res/anim/slide_out_left.xml create mode 100644 GitHubRepos/app/src/main/res/anim/slide_out_right.xml create mode 100644 GitHubRepos/app/src/main/res/animator/back_flip_in.xml create mode 100644 GitHubRepos/app/src/main/res/animator/front_flip_out.xml create mode 100644 GitHubRepos/app/src/main/res/animator/in_animation.xml create mode 100644 GitHubRepos/app/src/main/res/animator/out_animation.xml create mode 100644 GitHubRepos/app/src/main/res/drawable/bg_edittext.xml create mode 100644 GitHubRepos/app/src/main/res/drawable/btn_border.xml create mode 100644 GitHubRepos/app/src/main/res/drawable/btn_rounded_black.xml create mode 100644 GitHubRepos/app/src/main/res/drawable/btn_rounded_solid.xml create mode 100644 GitHubRepos/app/src/main/res/drawable/footer_button.xml create mode 100644 GitHubRepos/app/src/main/res/drawable/shape_button.xml create mode 100644 GitHubRepos/app/src/main/res/layout/alert_dialog.xml delete mode 100644 GitHubRepos/app/src/main/res/layout/container_error.xml delete mode 100644 GitHubRepos/app/src/main/res/layout/container_loading.xml delete mode 100644 GitHubRepos/app/src/main/res/layout/container_main.xml create mode 100644 GitHubRepos/app/src/main/res/layout/fragment_balance.xml create mode 100644 GitHubRepos/app/src/main/res/layout/fragment_login.xml create mode 100644 GitHubRepos/app/src/main/res/layout/fragment_register.xml delete mode 100644 GitHubRepos/app/src/main/res/layout/fragment_repo.xml create mode 100644 GitHubRepos/app/src/main/res/layout/fragment_transfer.xml delete mode 100644 GitHubRepos/app/src/main/res/layout/item_details.xml delete mode 100644 GitHubRepos/app/src/main/res/layout/item_main.xml delete mode 100644 GitHubRepos/app/src/main/res/layout/item_place_holder.xml create mode 100644 GitHubRepos/app/src/main/res/layout/view_edittext.xml create mode 100644 GitHubRepos/app/src/main/res/layout/view_transaction.xml create mode 100644 GitHubRepos/app/src/main/res/layout/view_transaction_bubble.xml create mode 100644 GitHubRepos/app/src/main/res/values/integer.xml delete mode 100644 GitHubRepos/app/src/mock/assets/api-response/repos-khalid.json create mode 100644 GitHubRepos/app/src/test/kotlin/com/khalid/hamid/githubrepos/ui/balance/BalanceViewModelTest.kt create mode 100644 GitHubRepos/app/src/test/kotlin/com/khalid/hamid/githubrepos/ui/login/LoginViewModelTest.kt create mode 100644 GitHubRepos/app/src/test/kotlin/com/khalid/hamid/githubrepos/ui/register/RegisterViewModelTest.kt create mode 100644 GitHubRepos/app/src/test/kotlin/com/khalid/hamid/githubrepos/ui/transfer/TransferViewModelTest.kt create mode 100644 GitHubRepos/app/src/test/kotlin/com/khalid/hamid/githubrepos/utils/BaseUnitTest.kt create mode 100644 GitHubRepos/app/src/test/kotlin/com/khalid/hamid/githubrepos/utils/MainCoroutineRule.kt create mode 100644 GitHubRepos/app/src/test/resources/api-response/repos-git.json create mode 100644 GitHubRepos/app/src/test/resources/ocbc/balance.json create mode 100644 GitHubRepos/app/src/test/resources/ocbc/login.json create mode 100644 GitHubRepos/app/src/test/resources/ocbc/payees.json create mode 100644 GitHubRepos/app/src/test/resources/ocbc/register.json create mode 100644 GitHubRepos/app/src/test/resources/ocbc/transaction.json create mode 100644 GitHubRepos/buildSrc/.DS_Store create mode 100644 GitHubRepos/library/.DS_Store create mode 100644 GitHubRepos/release/.DS_Store create mode 100644 GitHubRepos/release/Screenshot 2022-02-15 at 10.43.19 PM.png create mode 100644 GitHubRepos/release/Screenshot 2022-02-15 at 10.43.37 PM.png create mode 100644 GitHubRepos/release/Screenshot_20220215_222225.png create mode 100644 GitHubRepos/release/app-prod-debug.apk diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..df4004c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +/build +/captures +.externalNativeBuild +.cxx +.DG_Store +/.DS_Store +/buildSrc/build/ diff --git a/GitHubRepos/.gitignore b/GitHubRepos/.gitignore index d5c3b1c4..5a4f7d3f 100644 --- a/GitHubRepos/.gitignore +++ b/GitHubRepos/.gitignore @@ -13,5 +13,6 @@ .externalNativeBuild .cxx .DG_Store +/.DS_Store /buildSrc/build/ /.idea/shelf/ diff --git a/GitHubRepos/.idea/.DS_Store b/GitHubRepos/.idea/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e79dd34d074f6ee1b9f5bed6eba273f12da0f162 GIT binary patch literal 6148 zcmeHK!AiqG5S@)hTNUY15OnWFu?O)QYy5)VJZW0dLPCn#g9jo1ApU_z|3Yv6ir`Ui z`etXTiD|@>h|Iw3+w9J4Uf!nJ4iTB&WY{HY6Hyb+7_`vLFrH_xSl~yNfyS-Tr%Sq` zgih$960HM&Q32k&MH)~>L+n~%|B};ge~_hPA-!^47nf#H6en4X`R2`Y=l=ceVXr=R z_~Y2|S@HfBOHvDO)`(&{r(^ht-?9?R`OfC-s3OOm+u*6a@yHx!te0k=48M;o@mt$w zehZX39VhY&C~*r8@2U|+UR(RSuP^J4*OzsQoa&=Wd;-61%t}oaPz6+hsscE(*_um& z+NuJofGRLmfWHqG&KP>k1>L6ujVl3w4GcR&pKA%uu^vN@xgcg>MpA*2YJ7=dBpu_{ z$AunqK}jd$ix1=PYU<}L2Xq5RiLcEoLsi~{6DC#|I0~wr3$D5|4ISV zi26|vkL1tR%ER$lYvD(5Hjc{$rzz;TqZqM#6tBXaVcc>D7<$YFF#^*+0!{{PRDoYr F-~)7hci;d3 literal 0 HcmV?d00001 diff --git a/GitHubRepos/.idea/compiler.xml b/GitHubRepos/.idea/compiler.xml index fb7f4a8a..0e2950ee 100644 --- a/GitHubRepos/.idea/compiler.xml +++ b/GitHubRepos/.idea/compiler.xml @@ -1,6 +1,8 @@ - + + + \ No newline at end of file diff --git a/GitHubRepos/.idea/gradle.xml b/GitHubRepos/.idea/gradle.xml index 45f2e8bb..8fe7a181 100644 --- a/GitHubRepos/.idea/gradle.xml +++ b/GitHubRepos/.idea/gradle.xml @@ -4,7 +4,7 @@ diff --git a/GitHubRepos/.idea/jarRepositories.xml b/GitHubRepos/.idea/jarRepositories.xml index d564599f..54e8a244 100644 --- a/GitHubRepos/.idea/jarRepositories.xml +++ b/GitHubRepos/.idea/jarRepositories.xml @@ -36,5 +36,10 @@ - + diff --git a/GitHubRepos/README.md b/GitHubRepos/README.md index 0297a75f..7bdc6397 100644 --- a/GitHubRepos/README.md +++ b/GitHubRepos/README.md @@ -11,50 +11,46 @@ ### Functionality +App shows has coding assignment for OCBC +Has below features +1. Login +2. Register user +3. Show balance +4. Show transactions +5. Make transfer +6. Logout -App shows list of github repos with Local DB as single source of truth. - -Swipe to refresh will update the list - -#### RepoFragment -This fragment displays the list of a repository +This has Clean code architecture and has below layers +1. Repository layer: provides mock and remote interfaces +2. ViewModel Layer: business layer +3. View Layer: Android View layer #### Building ##[The project uses Gradle KTS script] ## The project is in Kotlin -## APK can be found in [release/apks] -## Screenshots can be found in [release/apks] - +Run below command to build and install +`./gradlew app:installProdDebug` +## Quality +1. This project is formatted with Spotless can be added in commit/push hooks + to maintain clean code review process +2. Has Lint baseline static check which can be added in CICD +3. Has Jacoco for code coverage config which can be added in CICD +4. Has checkstyle config static check which can be added in CICD -### Testing -The project uses both instrumentation tests that run on the device -and local unit tests that run on your computer. -To run both of them and generate a coverage report, you can run: +## APK can be found in [release] +## Screenshots can be found in [release] +# Demo Finish +![alt text](release/Screenshot_20220215_222225.png) -#### Device Tests -##### UI Tests -The projects uses Espresso for UI testing. Since each fragment -is limited to a ViewModel, each test mocks related ViewModel to -run the tests. -##### Database Tests -The project creates an in memory database for each database test but still -runs them on the device. +![alt text](release/Screenshot 2022-02-15 at 10.43.19 PM.png) -#### Local Unit Tests -##### ViewModel Tests -Each ViewModel is tested using local unit tests with mock Repository -implementations. -##### Repository Tests -Each Repository is tested using local unit tests with mock web service and -mock database. -##### Webservice Tests -The project uses [MockWebServer][mockwebserver] and Fake implementations +![alt text](release/Screenshot 2022-02-15 at 10.43.37 PM.png) ## Jacoco run "jacocoPRODDEBUGReport" diff --git a/GitHubRepos/app/.DS_Store b/GitHubRepos/app/.DS_Store index 39e69f3b0d58ed476bfa5c0cb54c13bcb1b8bfc7..c2716decd3fa6095cc15a6ef324128e1167daf02 100644 GIT binary patch delta 509 zcmZoMXfc=|#>B!ku~2NHo}wrp0|Nsi1A_nqLk>eKLkdG8LkUCT=8ufand?DPTnxoP zpU bff4tec{0CB`mu~2NHo}wrd0|Nsi1A_oVaY0f}eiD$kBdK6w;qu7_A}o{LS+8#H wVXJ1`tiZv}GO=OdW_AvK4xrx6f*jwOC-aL~3INrEw1Z3m(JY%IMAk3^0N8gHRsaA1 diff --git a/GitHubRepos/app/build.gradle.kts b/GitHubRepos/app/build.gradle.kts index bc632db7..3d8efc67 100644 --- a/GitHubRepos/app/build.gradle.kts +++ b/GitHubRepos/app/build.gradle.kts @@ -63,6 +63,7 @@ dependencies { implementation(Dependencies.CRASH) implementation(Dependencies.CR_CORE) implementation(Dependencies.CR_ANDROID) + implementation("com.dynatrace.agent:agent-android:8.231.2.1007") implementation(Dependencies.DAGGER_RUNTIME) api(Dependencies.DAGGER_ANDROID) @@ -113,6 +114,7 @@ dependencies { kapt(Dependencies.GLIDE_COMPILER) // Nav implementation(Dependencies.NAV_RUNTIME_FRAGMENT_KTX) + debugImplementation(Dependencies.CHUCKER) } pln("script end") diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ExampleInstrumentedTest.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ExampleInstrumentedTest.kt deleted file mode 100644 index 9161d3bc..00000000 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos - -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.platform.app.InstrumentationRegistry -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.khalid.hamid.gojekgithubrepos", appContext.packageName) - } -} diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/TestApp.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/TestApp.kt index 3900123d..4cd4744d 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/TestApp.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/TestApp.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ui/RepoFragmentTest.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ui/RepoFragmentTest.kt deleted file mode 100644 index c1094d8b..00000000 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/ui/RepoFragmentTest.kt +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.ui - -import androidx.databinding.DataBindingComponent -import androidx.lifecycle.MutableLiveData -import androidx.navigation.NavController -import androidx.test.espresso.Espresso.onView -import androidx.test.espresso.assertion.ViewAssertions.matches -import androidx.test.espresso.matcher.ViewMatchers.isDisplayed -import androidx.test.espresso.matcher.ViewMatchers.withId -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.filters.LargeTest -import androidx.test.rule.ActivityTestRule -import com.khalid.hamid.githubrepos.R -import com.khalid.hamid.githubrepos.network.Resource -import com.khalid.hamid.githubrepos.testing.SingleFragmentActivity -import com.khalid.hamid.githubrepos.utilities.* -import com.khalid.hamid.githubrepos.vo.Repositories -import org.junit.Before -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.Mockito -import org.mockito.Mockito.mock - -/** - * Large End-to-End test for the RepoFragment module. - */ -@RunWith(AndroidJUnit4::class) -@LargeTest -class RepoFragmentTest { - - @Rule - @JvmField - val activityRule = ActivityTestRule(SingleFragmentActivity::class.java, true, true) - - private lateinit var repository: Repositories - - @Rule - @JvmField - val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(activityRule) - - @Rule - @JvmField - val countingAppExecutors = CountingAppExecutorsRule() - - private lateinit var viewModel: RepoViewModel - private lateinit var mockBindingAdapter: FragmentBindingAdapters - private val repoListData = MutableLiveData>>() - - private val testFragment = TestUserFragment().apply { - } - - @Before - fun init() { - viewModel = mock(RepoViewModel::class.java) - Mockito.`when`(viewModel._items).thenReturn(repoListData) - mockBindingAdapter = mock(FragmentBindingAdapters::class.java) - - testFragment.executors = countingAppExecutors.appExecutors - testFragment.viewModelFactory = ViewModelUtil.createFor(viewModel) - testFragment.dataBindingComponent = object : DataBindingComponent { - override fun getFragmentBindingAdapters(): FragmentBindingAdapters { - return mockBindingAdapter - } - } - activityRule.activity.setFragment(testFragment) - activityRule.runOnUiThread { - testFragment.binding.repoList.itemAnimator = null - } - EspressoTestUtil.disableProgressBarAnimations(activityRule) - } - - @Test - fun loading() { - repoListData.postValue(Resource.loading(null)) - onView(withId(R.id.shimmer_view_container)).check(matches(isDisplayed())) - } - - @Test - fun test() { - } - - @Test - fun error() { - repoListData.postValue(Resource.error("", null)) - onView(withId(R.id.error_content)).check(matches(isDisplayed())) - } - - @Test - fun success() { - repoListData.postValue(Resource.success(emptyList())) - onView(withId(R.id.content_success)).check(matches(isDisplayed())) - } - - class TestUserFragment : RepoFragment() { - val navController = Mockito.mock(NavController::class.java) - override fun navController() = navController - } -} diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/AppTestRunner.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/AppTestRunner.kt index 15a071b2..7e7352d0 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/AppTestRunner.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/AppTestRunner.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.utilities diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/CountingAppExecutorsRule.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/CountingAppExecutorsRule.kt index bc4eaf21..dfb9b01c 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/CountingAppExecutorsRule.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/CountingAppExecutorsRule.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.utilities diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResource.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResource.kt index 83d45ac0..34bfc15a 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResource.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResource.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.utilities diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResourceRule.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResourceRule.kt index d8d90cd8..b3e15670 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResourceRule.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/DataBindingIdlingResourceRule.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.utilities diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/EspressoTestUtil.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/EspressoTestUtil.kt index af4c4a85..c59eb9b8 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/EspressoTestUtil.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/EspressoTestUtil.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.utilities diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/MockitoExt.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/MockitoExt.kt index a5f0c64e..cac6c097 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/MockitoExt.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/MockitoExt.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.utilities diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/TaskExecutorWithIdlingResourceRule.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/TaskExecutorWithIdlingResourceRule.kt index 3611feaa..e2473285 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/TaskExecutorWithIdlingResourceRule.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/TaskExecutorWithIdlingResourceRule.kt @@ -1,20 +1,29 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ -package com.khalid.hamid.gojekgithubrepos.utilities +package com.khalid.hamid.githubrepos.utilities import androidx.arch.core.executor.testing.CountingTaskExecutorRule import androidx.test.espresso.IdlingRegistry diff --git a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/ViewModelUtil.kt b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/ViewModelUtil.kt index eadd4bc1..2d94b056 100644 --- a/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/ViewModelUtil.kt +++ b/GitHubRepos/app/src/androidTest/java/com/khalid/hamid/githubrepos/utilities/ViewModelUtil.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.utilities diff --git a/.DS_Store b/GitHubRepos/app/src/debug/java/com/khalid/hamid/.DS_Store similarity index 100% rename from .DS_Store rename to GitHubRepos/app/src/debug/java/com/khalid/hamid/.DS_Store diff --git a/GitHubRepos/.DS_Store b/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/.DS_Store similarity index 97% rename from GitHubRepos/.DS_Store rename to GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/.DS_Store index 6783c048150425a88a692ca0f92097e03ba5a1c9..15c6cf4f56ac605d3af47fdbc2be9380e0a46fac 100644 GIT binary patch delta 60 zcmZoMXfc=&&6&rLo>H8glaxPkqq+CLnZ@UGA|t{!? LiveData.getOrAwaitValue( + time: Long = 2, + timeUnit: TimeUnit = TimeUnit.SECONDS, + afterObserve: () -> Unit = {} +): T { + var data: T? = null + val latch = CountDownLatch(1) + val observer = object : Observer { + override fun onChanged(o: T?) { + data = o + latch.countDown() + this@getOrAwaitValue.removeObserver(this) + } + } + this.observeForever(observer) + + try { + afterObserve.invoke() + + // Don't wait indefinitely if the LiveData is not set. + if (!latch.await(time, timeUnit)) { + throw TimeoutException("LiveData value was never set.") + } + } finally { + this.removeObserver(observer) + } + + @Suppress("UNCHECKED_CAST") + return data as T +} + +/** + * Observes a [LiveData] until the `block` is done executing. + */ +fun LiveData.observeForTesting(block: () -> Unit) { + val observer = Observer { } + try { + observeForever(observer) + block() + } finally { + removeObserver(observer) + } +} + +/** + * Gets the value of a LiveData safely. + */ +@Throws(InterruptedException::class) +fun getValue(liveData: LiveData): T? { + var data: T? = null + val latch = CountDownLatch(1) + val observer = object : Observer { + override fun onChanged(o: T?) { + data = o + latch.countDown() + liveData.removeObserver(this) + } + } + liveData.observeForever(observer) + latch.await(2, TimeUnit.SECONDS) + + return data +} diff --git a/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/OpenForTesting.kt b/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/OpenForTesting.kt index e8e277f0..9e99162f 100644 --- a/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/OpenForTesting.kt +++ b/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/OpenForTesting.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.testing diff --git a/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/SingleFragmentActivity.kt b/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/SingleFragmentActivity.kt index 8f872151..715394f6 100644 --- a/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/SingleFragmentActivity.kt +++ b/GitHubRepos/app/src/debug/java/com/khalid/hamid/githubrepos/testing/SingleFragmentActivity.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.testing diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/GithubApp.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/GithubApp.kt index 87e1377e..3a49ffe8 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/GithubApp.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/GithubApp.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/core/BaseFragment.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/core/BaseFragment.kt index 6c3e123b..11c46f7f 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/core/BaseFragment.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/core/BaseFragment.kt @@ -54,7 +54,7 @@ abstract class BaseFragment : @Inject lateinit var pickerViewModelProvider: AppViewModelProvider - abstract fun getPageName(): String? + open fun getPageName(): String? = this::class.java.simpleName lateinit var mContext: Context var appCompatActivity: AppCompatActivity? = null @@ -197,11 +197,6 @@ abstract class BaseFragment : ) } - override fun onActivityCreated(savedInstanceState: Bundle?) { - super.onActivityCreated(savedInstanceState) - logBreadcrumb("onActivityCreated" + getPageName()) - } - protected fun logBreadcrumb(crumb: String) { // TODO: log } @@ -281,9 +276,6 @@ abstract class BaseFragment : when (asyncState.status) { State.SUCCESS -> { hideLoader() -// if (asyncState.msg.equalsIgnoreCase("pageload")) { -// onPageLoaded() -// } } State.FAILED -> { hideLoader() diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubDb.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubDb.kt deleted file mode 100644 index 5f1840fd..00000000 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubDb.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.db - -import androidx.room.Database -import androidx.room.RoomDatabase -import com.khalid.hamid.githubrepos.vo.Repositories - -/** - * Main database description. - */ -@Database( - entities = [ - Repositories::class - ], - version = 1, - exportSchema = true -) -abstract class GithubDb : RoomDatabase() { - - abstract fun getRepoDao(): RepoDao -} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubTypeConverters.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubTypeConverters.kt deleted file mode 100644 index ef95e846..00000000 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/GithubTypeConverters.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.db - -import androidx.room.TypeConverter -import timber.log.Timber - -object GithubTypeConverters { - @TypeConverter - @JvmStatic - fun stringToIntList(data: String?): List? { - return data?.let { - it.split(",").map { - try { - it.toInt() - } catch (ex: NumberFormatException) { - Timber.e(ex, "Cannot convert $it to number") - null - } - } - }?.filterNotNull() - } - - @TypeConverter - @JvmStatic - fun intListToString(ints: List?): String? { - return ints?.joinToString(",") - } -} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/RepoDao.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/RepoDao.kt deleted file mode 100644 index 6cb72a47..00000000 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/RepoDao.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.db - -import androidx.room.Dao -import androidx.room.Insert -import androidx.room.OnConflictStrategy -import androidx.room.Query -import com.khalid.hamid.githubrepos.vo.Repositories - -/** - * Interface for database access on Repositories related operations. - */ -@Dao -abstract class RepoDao { - - @Insert(onConflict = OnConflictStrategy.REPLACE) - abstract suspend fun insertRepos(repositories: List) - - @Query("SELECT * FROM repo_table") - abstract suspend fun getRepoList(): List -} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/WordDao.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/WordDao.kt deleted file mode 100644 index e135fb04..00000000 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/db/WordDao.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.db - -import androidx.room.Dao -import androidx.room.Query -import com.khalid.hamid.githubrepos.vo.Word - -@Dao -abstract class WordDao { - - @Query("SELECT * from word_table") - abstract fun getWords(): List -} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppModule.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppModule.kt index 0dd9bc5d..ff3ec325 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppModule.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppModule.kt @@ -1,26 +1,33 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.di import android.app.Application -import androidx.room.Room -import com.khalid.hamid.githubrepos.db.GithubDb -import com.khalid.hamid.githubrepos.db.RepoDao import com.khalid.hamid.githubrepos.network.GitHubService +import com.khalid.hamid.githubrepos.network.remote.AuthInterceptor import com.khalid.hamid.githubrepos.utilities.AppExecutors import com.khalid.hamid.githubrepos.utilities.Constants import com.khalid.hamid.githubrepos.utilities.Prefs @@ -29,9 +36,11 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import okhttp3.OkHttpClient +import java.util.concurrent.TimeUnit +import javax.inject.Singleton +import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory -import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) @@ -43,25 +52,16 @@ object AppModule { fun provideGithubService(okHttpClient: OkHttpClient): GitHubService { return Retrofit.Builder() .baseUrl(Constants.BASE_URL) - .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient) + .addConverterFactory(GsonConverterFactory.create()) .build() .create(GitHubService::class.java) } @Singleton @Provides - fun provideDb(app: Application): GithubDb { - return Room - .databaseBuilder(app, GithubDb::class.java, Constants.DB_NAME) - .fallbackToDestructiveMigration() - .build() - } - - @Singleton - @Provides - fun provideRepoDao(db: GithubDb): RepoDao { - return db.getRepoDao() + fun provideAuthInterceptor(context: Application, pref: Prefs): AuthInterceptor { + return AuthInterceptor(context, pref) } @Singleton diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppViewModelModule.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppViewModelModule.kt index 8a9369c9..1ab34815 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppViewModelModule.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/AppViewModelModule.kt @@ -1,19 +1,3 @@ -/* - * Copyright 2022 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package com.khalid.hamid.githubrepos.di import com.khalid.hamid.githubrepos.utilities.AppViewModelProvider @@ -30,4 +14,4 @@ class AppViewModelModule { @Singleton @Provides fun provideViewModelFactory(): AppViewModelProvider = AppViewModelProvider() -} +} \ No newline at end of file diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/CoroutinesModule.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/CoroutinesModule.kt index 38f87bfe..69394415 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/CoroutinesModule.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/di/CoroutinesModule.kt @@ -1,17 +1,26 @@ /* - * Copyright 2022 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.di diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseDataSource.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseDataSource.kt index 11f2637a..1c7742a6 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseDataSource.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseDataSource.kt @@ -1,29 +1,62 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network import androidx.annotation.WorkerThread -import com.khalid.hamid.githubrepos.vo.Repositories +import com.khalid.hamid.githubrepos.ui.balance.BalanceResponse +import com.khalid.hamid.githubrepos.ui.balance.TransactionResponse +import com.khalid.hamid.githubrepos.ui.login.LoginRequest +import com.khalid.hamid.githubrepos.ui.login.LoginResponse +import com.khalid.hamid.githubrepos.ui.register.RegisterRequest +import com.khalid.hamid.githubrepos.ui.register.RegisterResponse +import com.khalid.hamid.githubrepos.ui.transfer.PayeeResponse +import com.khalid.hamid.githubrepos.ui.transfer.TransferRequest +import com.khalid.hamid.githubrepos.ui.transfer.TransferResponse +import com.khalid.hamid.githubrepos.vo.GitRepos interface BaseDataSource { @WorkerThread - suspend fun getRepositories(): Result> + suspend fun fetchRepos(): Result + + @WorkerThread + suspend fun register(registerRequest: RegisterRequest): Result + + @WorkerThread + suspend fun login(loginRequest: LoginRequest): Result + + @WorkerThread + suspend fun balance(): Result + + @WorkerThread + suspend fun transactions(): Result + + @WorkerThread + suspend fun payees(): Result @WorkerThread - suspend fun fetchRepos(): Result> + suspend fun transfer(transferRequest: TransferRequest): Result } diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseRepository.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseRepository.kt index 78f8bfa8..f4ae290b 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseRepository.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/BaseRepository.kt @@ -1,35 +1,76 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network -import com.khalid.hamid.githubrepos.vo.Repositories -import timber.log.Timber +import com.khalid.hamid.githubrepos.testing.OpenForTesting +import com.khalid.hamid.githubrepos.ui.balance.BalanceResponse +import com.khalid.hamid.githubrepos.ui.balance.TransactionResponse +import com.khalid.hamid.githubrepos.ui.login.LoginRequest +import com.khalid.hamid.githubrepos.ui.login.LoginResponse +import com.khalid.hamid.githubrepos.ui.register.RegisterRequest +import com.khalid.hamid.githubrepos.ui.register.RegisterResponse +import com.khalid.hamid.githubrepos.ui.transfer.PayeeResponse +import com.khalid.hamid.githubrepos.ui.transfer.TransferRequest +import com.khalid.hamid.githubrepos.ui.transfer.TransferResponse +import com.khalid.hamid.githubrepos.vo.GitRepos import javax.inject.Singleton +import timber.log.Timber +@OpenForTesting @Singleton -class BaseRepository(private val baseDataSource: BaseDataSource) : BaseDataSource { +class BaseRepository( + private val baseDataSource: BaseDataSource +) : BaseDataSource { - override suspend fun getRepositories(): Result> { + override suspend fun fetchRepos(): Result { Timber.d("getRepositories") - return baseDataSource.getRepositories() + return baseDataSource.fetchRepos() } - override suspend fun fetchRepos(): Result> { - Timber.d("getRepositories") - return baseDataSource.getRepositories() + override suspend fun register(registerRequest: RegisterRequest): Result { + return baseDataSource.register(registerRequest) + } + + override suspend fun login(loginRequest: LoginRequest): Result { + return baseDataSource.login(loginRequest) + } + + override suspend fun balance(): Result { + return baseDataSource.balance() + } + + override suspend fun transactions(): Result { + return baseDataSource.transactions() + } + + override suspend fun payees(): Result { + return baseDataSource.payees() + } + + override suspend fun transfer(transferRequest: TransferRequest): Result { + return baseDataSource.transfer(transferRequest) } } diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/GitHubService.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/GitHubService.kt index 5b89d022..dca166b3 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/GitHubService.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/GitHubService.kt @@ -1,27 +1,65 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network -import com.khalid.hamid.githubrepos.vo.Repositories +import com.khalid.hamid.githubrepos.ui.balance.BalanceResponse +import com.khalid.hamid.githubrepos.ui.balance.TransactionResponse +import com.khalid.hamid.githubrepos.ui.login.LoginRequest +import com.khalid.hamid.githubrepos.ui.login.LoginResponse +import com.khalid.hamid.githubrepos.ui.register.RegisterRequest +import com.khalid.hamid.githubrepos.ui.register.RegisterResponse +import com.khalid.hamid.githubrepos.ui.transfer.PayeeResponse +import com.khalid.hamid.githubrepos.ui.transfer.TransferRequest +import com.khalid.hamid.githubrepos.ui.transfer.TransferResponse +import com.khalid.hamid.githubrepos.vo.GitRepos import retrofit2.Response +import retrofit2.http.Body import retrofit2.http.GET +import retrofit2.http.POST interface GitHubService { - @GET("/repositories") - suspend fun getRepositories(): Response> + @GET("repositories") + suspend fun fetchRepos(): Response + + @POST("register") + suspend fun register(@Body registerRequest: RegisterRequest): Response + + @POST("login") + suspend fun login(@Body loginRequest: LoginRequest): Response + + @GET("balance") + suspend fun balance(): Response + + @GET("payees") + suspend fun payees(): Response + + @GET("transactions") + suspend fun transactions(): Response + + @POST("transfer") + suspend fun transfer(@Body transferRequest: TransferRequest): Response } diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/RepositoryImple.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/RepositoryImple.kt index 9d6de477..82993a2a 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/RepositoryImple.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/RepositoryImple.kt @@ -1,69 +1,60 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network import com.khalid.hamid.githubrepos.network.Result.Success -import com.khalid.hamid.githubrepos.network.local.LocalDataSource import com.khalid.hamid.githubrepos.network.remote.RemoteDataSource +import com.khalid.hamid.githubrepos.ui.balance.BalanceResponse +import com.khalid.hamid.githubrepos.ui.balance.TransactionResponse +import com.khalid.hamid.githubrepos.ui.login.LoginRequest +import com.khalid.hamid.githubrepos.ui.login.LoginResponse +import com.khalid.hamid.githubrepos.ui.register.RegisterRequest +import com.khalid.hamid.githubrepos.ui.register.RegisterResponse +import com.khalid.hamid.githubrepos.ui.transfer.PayeeResponse +import com.khalid.hamid.githubrepos.ui.transfer.TransferRequest +import com.khalid.hamid.githubrepos.ui.transfer.TransferResponse import com.khalid.hamid.githubrepos.utilities.EspressoIdlingResource -import com.khalid.hamid.githubrepos.vo.Repositories +import com.khalid.hamid.githubrepos.vo.GitRepos +import javax.inject.Inject import timber.log.Timber /** * This will return data from either DB or get from network */ -class RepositoryImple constructor( - private val localDataSource: LocalDataSource, +class RepositoryImple @Inject constructor( private val remoteDataSource: RemoteDataSource ) : BaseDataSource { - override suspend fun getRepositories(): Result> { - Timber.d("getRepositories ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - return fetchTasksFromRemoteOrLocal() - } - - private suspend fun fetchTasksFromRemoteOrLocal(): Result> { - Timber.d("fetchTasksFromRemoteOrLocal") - - // check - val hasExpired = localDataSource.hasCacheExpired() - - if (!hasExpired) { - Timber.d("cache is valid ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - Timber.d("hasExpired is false retirnung DB data ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - return localDataSource.getRepositories() - } - - // session has expired - Timber.d("cache is invalid ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - - return fetchRepos() - } - - override suspend fun fetchRepos(): Result> { + override suspend fun fetchRepos(): Result { val fetchedData = remoteDataSource.fetchRepos() Timber.d("fetched data ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") when (fetchedData) { is Success -> { Timber.d("Success ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - localDataSource.saveData(fetchedData.data) - val savedData = localDataSource.getRepos() - Timber.d(" saved data in DB $savedData") - return localDataSource.getRepositories() + return fetchedData } else -> { @@ -72,4 +63,28 @@ class RepositoryImple constructor( } } } + + override suspend fun register(registerRequest: RegisterRequest): Result { + return remoteDataSource.register(registerRequest) + } + + override suspend fun login(loginRequest: LoginRequest): Result { + return remoteDataSource.login(loginRequest) + } + + override suspend fun balance(): Result { + return remoteDataSource.balance() + } + + override suspend fun transactions(): Result { + return remoteDataSource.transactions() + } + + override suspend fun payees(): Result { + return remoteDataSource.payees() + } + + override suspend fun transfer(transferRequest: TransferRequest): Result { + return remoteDataSource.transfer(transferRequest) + } } diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Resource.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Resource.kt index 47898cc8..f899cb36 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Resource.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Resource.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network @@ -23,7 +32,7 @@ package com.khalid.hamid.githubrepos.network data class Resource(val status: Status, val data: T?, val message: String?) { companion object { fun success(data: T?): Resource { - return Resource(Status.SUCCESS, data, null) + return Resource(Status.SUCCESS, data, "success yay !") } fun error(msg: String, data: T?): Resource { diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Result.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Result.kt index da08cfde..0b402d55 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Result.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Result.kt @@ -1,22 +1,39 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network +import com.google.gson.Gson +import com.google.gson.JsonSyntaxException import com.khalid.hamid.githubrepos.network.Result.Success +import com.khalid.hamid.githubrepos.utilities.ErrorResponse +import java.net.UnknownHostException +import retrofit2.Call +import retrofit2.Response +import timber.log.Timber + /** * A generic class that holds a value with its loading status. * @param @@ -24,16 +41,22 @@ import com.khalid.hamid.githubrepos.network.Result.Success sealed class Result { data class Success(val data: T) : Result() - data class Error_(val exception: Exception) : Result() + data class Failure(val exception: Exception) : Result() object Loading : Result() override fun toString(): String { return when (this) { is Success<*> -> "Success[data=$data]" - is Error_ -> "Error[exception=$exception]" + is Failure -> "Error[exception=$exception]" Loading -> "Loading" } } + + companion object { + val MSG_GENERIC_ERROR = "Something went wrong, please try again." + val MSG_UNABLE_TO_CONNECT = + "Unable to connect, please check your internet connection and try again." + } } /** @@ -41,3 +64,67 @@ sealed class Result { */ val Result<*>.succeeded get() = this is Success && data != null + +fun getErrorMsgFor(e: Throwable?): String? { + return when (e) { + is UnknownHostException -> Result.MSG_UNABLE_TO_CONNECT + else -> Result.MSG_GENERIC_ERROR + } +} +fun Call.getRetrofitResult(converter: (T) -> E): Result { + val response: Response + try { + response = execute() + } catch (e: Throwable) { + Timber.e("exception ", e) + return Result.Failure(java.lang.Exception(getErrorMsgFor(e))) + } + + return if (response.isSuccessful) { + try { + val data = converter(response.body() as T) + Success(data) + } catch (e: java.lang.Exception) { + Result.Failure(java.lang.Exception(getErrorMsgFor(e))) + } + } else { + Result.Failure(java.lang.Exception(Result.MSG_GENERIC_ERROR)) + } +} + +fun Response.getRetrofitResult(converter: (T) -> E): Result { + return if (isSuccessful) { + try { + val data = converter(body() as T) + Success(data) + } catch (e: java.lang.Exception) { + Result.Failure(java.lang.Exception(getErrorMsgFor(e))) + } + } else { + var data = errorBody()?.string() + if (data != null) { + var errorResponse: ErrorResponse? = null + try { + errorResponse = Gson().fromJson(data, ErrorResponse::class.java) + Result.Failure(java.lang.Exception(errorResponse.errorMessage)) + } catch (e: JsonSyntaxException) { + e.printStackTrace() + Result.Failure(java.lang.Exception(Result.MSG_GENERIC_ERROR)) + } + } else { + Result.Failure(java.lang.Exception(Result.MSG_GENERIC_ERROR)) + } + } +} + +inline fun Result.onSuccess(action: (T) -> Unit): Result { + if (this is Success) + action(data) + return this +} + +inline fun Result.onError(action: (Throwable) -> Unit): Result { + if (this is Result.Failure && exception != null) + action(exception) + return this +} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Status.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Status.kt index 888de11a..73aa4e3f 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Status.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/Status.kt @@ -1,17 +1,26 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/local/LocalDataSource.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/local/LocalDataSource.kt deleted file mode 100644 index 3cf37228..00000000 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/local/LocalDataSource.kt +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.network.local - -import com.khalid.hamid.githubrepos.db.RepoDao -import com.khalid.hamid.githubrepos.network.BaseDataSource -import com.khalid.hamid.githubrepos.network.Result -import com.khalid.hamid.githubrepos.network.Result.Success -import com.khalid.hamid.githubrepos.utilities.EspressoIdlingResource -import com.khalid.hamid.githubrepos.utilities.Prefs -import com.khalid.hamid.githubrepos.vo.Repositories -import timber.log.Timber -import javax.inject.Inject - -/** - * This will always fetch data from database - * - */ -open class LocalDataSource @Inject constructor( - private val repoDao: RepoDao, - private val pref: Prefs -) : BaseDataSource { - - val TWOHOURS: Long = 2 * 60 * 60 * 1000 - - override suspend fun getRepositories(): Result> { - return Success(getRepos() ?: emptyList()) - } - - fun hasCacheExpired(): Boolean { - Timber.d("hasCacheExpired") - if (pref.cachedTime.isEmpty()) { - Timber.d("no last saved time ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - return true - } - - val currentTime = System.currentTimeMillis() - val prevTime = pref.cachedTime.toLong() - val elapsedTime = currentTime - prevTime - if (elapsedTime >= TWOHOURS) { - Timber.d("cache expired ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - return true - } - return false - } - - suspend fun saveData(list: List) { - Timber.d(" saveData ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - pref.cachedTime = System.currentTimeMillis().toString() - repoDao.insertRepos(list) - } - - suspend fun getRepos(): List { - Timber.d("getRepos ${EspressoIdlingResource.countingIdlingResource.getCounterVal()}") - return repoDao.getRepoList() - } - - override suspend fun fetchRepos(): Result> { - return Result.Error_(Exception("Illegal call")) - } -} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/AuthInterceptor.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/AuthInterceptor.kt new file mode 100644 index 00000000..77bb5c46 --- /dev/null +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/AuthInterceptor.kt @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright 2022 Mohammed Khalid Hamid. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.khalid.hamid.githubrepos.network.remote + +import android.content.Context +import com.khalid.hamid.githubrepos.utilities.Constants +import com.khalid.hamid.githubrepos.utilities.Prefs +import okhttp3.Interceptor +import okhttp3.Request +import okhttp3.Response + +class AuthInterceptor( + private val context: Context, + private val pref: Prefs +) : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + return chain.proceed(chain.request().withAdditionalHeader()) + } + + private fun Request.withAdditionalHeader(): Request { + return newBuilder().apply { + if (pref.accessToken.isBlank()) { + removeHeader(Constants.Header.AUTHORIZATION) + } else { + addHeader(Constants.Header.AUTHORIZATION, pref.accessToken) + } + }.build() + } +} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/RemoteDataSource.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/RemoteDataSource.kt index bba10e7a..85cfc6dd 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/RemoteDataSource.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/network/remote/RemoteDataSource.kt @@ -1,56 +1,76 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.network.remote import com.khalid.hamid.githubrepos.network.GitHubService import com.khalid.hamid.githubrepos.network.Result -import com.khalid.hamid.githubrepos.network.Result.Error_ -import com.khalid.hamid.githubrepos.network.Result.Success -import com.khalid.hamid.githubrepos.vo.Repositories -import retrofit2.HttpException -import timber.log.Timber +import com.khalid.hamid.githubrepos.network.getRetrofitResult +import com.khalid.hamid.githubrepos.ui.balance.BalanceResponse +import com.khalid.hamid.githubrepos.ui.balance.TransactionResponse +import com.khalid.hamid.githubrepos.ui.login.LoginRequest +import com.khalid.hamid.githubrepos.ui.login.LoginResponse +import com.khalid.hamid.githubrepos.ui.register.RegisterRequest +import com.khalid.hamid.githubrepos.ui.register.RegisterResponse +import com.khalid.hamid.githubrepos.ui.transfer.PayeeResponse +import com.khalid.hamid.githubrepos.ui.transfer.TransferRequest +import com.khalid.hamid.githubrepos.ui.transfer.TransferResponse +import com.khalid.hamid.githubrepos.vo.GitRepos import javax.inject.Inject +import timber.log.Timber class RemoteDataSource @Inject constructor( private val gitHubService: GitHubService ) { - suspend fun fetchRepos(): Result> { - Timber.d("fetchRepos") - try { - val dataResponse = gitHubService.getRepositories() - Timber.d("data response is ${dataResponse.body()}") - - if (dataResponse.isSuccessful) { - Timber.d("dataResponse.isSuccessful") - - return Success(dataResponse?.body() ?: emptyList()) - } else { - Timber.d("dataResponse.Error_") - return Error_(Exception(dataResponse?.message() ?: "Unknown error")) - } - } catch (httpException: HttpException) { - Timber.d("dataResponse.Error_") - httpException.printStackTrace() - return Error_(httpException) - } catch (unknown: Exception) { - Timber.d("dataResponse.Error_") - unknown.printStackTrace() - return Error_(unknown) - } + suspend fun fetchRepos(): Result { + Timber.d("fetchRespos") + return Result.Failure(Exception("")) + } + + suspend fun register(registerRequest: RegisterRequest): Result { + return gitHubService.register(registerRequest).getRetrofitResult { it } + } + + suspend fun login(loginRequest: LoginRequest): Result { + return gitHubService.login(loginRequest).getRetrofitResult { it } + } + + suspend fun balance(): Result { + return gitHubService.balance().getRetrofitResult { it } + } + + suspend fun transactions(): Result { + return gitHubService.transactions().getRetrofitResult { it } + } + + suspend fun payees(): Result { + return gitHubService.payees().getRetrofitResult { it } + } + + suspend fun transfer(transferRequest: TransferRequest): Result { + return gitHubService.transfer(transferRequest).getRetrofitResult { it } } } diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundListAdapter.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundListAdapter.kt deleted file mode 100644 index 5afd83b7..00000000 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundListAdapter.kt +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.ui - -import android.view.ViewGroup -import androidx.databinding.ViewDataBinding -import androidx.recyclerview.widget.AsyncDifferConfig -import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.ListAdapter -import com.khalid.hamid.githubrepos.utilities.AppExecutors - -/** - * A generic RecyclerView adapter that uses Data Binding & DiffUtil. - * - * @param Type of the items in the list - * @param The type of the ViewDataBinding - */ -abstract class DataBoundListAdapter( - appExecutors: AppExecutors, - diffCallback: DiffUtil.ItemCallback -) : ListAdapter>( - AsyncDifferConfig.Builder(diffCallback) - .setBackgroundThreadExecutor(appExecutors.diskIO()) - .build() -) { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder { - val binding = createBinding(parent) - return DataBoundViewHolder(binding) - } - - protected abstract fun createBinding(parent: ViewGroup): V - - override fun onBindViewHolder(holder: DataBoundViewHolder, position: Int) { - bind(holder.binding, getItem(position)) - holder.binding.executePendingBindings() - } - - protected abstract fun bind(binding: V, item: T) -} diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundViewHolder.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundViewHolder.kt deleted file mode 100644 index 88bc13a0..00000000 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/DataBoundViewHolder.kt +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2020 Mohammed Khalid Hamid. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.khalid.hamid.githubrepos.ui - -import androidx.databinding.ViewDataBinding -import androidx.recyclerview.widget.RecyclerView - -/** - * A generic ViewHolder that works with a [ViewDataBinding]. - * @param The type of the ViewDataBinding. - */ -class DataBoundViewHolder constructor(val binding: T) : - RecyclerView.ViewHolder(binding.root) diff --git a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/MainActivity.kt b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/MainActivity.kt index 0e829017..23a9c3c7 100644 --- a/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/MainActivity.kt +++ b/GitHubRepos/app/src/main/java/com/khalid/hamid/githubrepos/ui/MainActivity.kt @@ -1,33 +1,127 @@ /* - * Copyright 2020 Mohammed Khalid Hamid. + * MIT License * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Copyright 2021 Mohammed Khalid Hamid. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package com.khalid.hamid.githubrepos.ui +import android.app.Dialog +import android.content.Context import android.os.Bundle +import android.view.View +import android.view.ViewGroup +import android.view.Window +import android.widget.Button +import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.Toolbar +import androidx.databinding.DataBindingUtil import com.khalid.hamid.githubrepos.R +import com.khalid.hamid.githubrepos.core.BaseActivity +import com.khalid.hamid.githubrepos.databinding.ActivityMainBinding import dagger.hilt.android.AndroidEntryPoint import timber.log.Timber @AndroidEntryPoint -class MainActivity : AppCompatActivity() { +class MainActivity : BaseActivity() { + + // Common loader dialog for all activities + private var loaderDialog: Dialog? = null + + override val layout = R.layout.activity_main + + private lateinit var binding: ActivityMainBinding + + lateinit var mToolbar: Toolbar override fun onCreate(savedInstanceState: Bundle?) { Timber.d("onCreate") super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + binding = DataBindingUtil.setContentView(this, layout) + binding.lifecycleOwner = this + setupToolbar() + } + + override fun setupToolbar() { + mToolbar = binding.singlePageToolbar + setSupportActionBar(mToolbar) + mToolbar.visibility = View.VISIBLE + } + + /** + * Common methods to show and hide dialogs from child activities. + */ + private fun createLoadingDialog(): Dialog { + val dialog = Dialog(this) + + dialog.apply { + requestWindowFeature(Window.FEATURE_NO_TITLE) + setCancelable(false) + setContentView(R.layout.layout_loader) + window?.let { + it.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) + it.setDimAmount(0.5F) + } + } + return dialog + } + + fun showAlert(context: Context, alertTitle: String, alertMessage: String, cancelable: Boolean) { + showAlert( + context, + alertTitle, + alertMessage, + {}, + context?.resources?.getString(R.string.ok), + cancelable + ) + } + open fun showAlert( + context: Context, + alertTitle: String, + alertMessage: String?, + positiveClickAction: () -> Unit?, + OkBtnText: String?, + cancelable: Boolean + ) { + val dialog = Dialog(context) + dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) + dialog.setContentView(R.layout.alert_dialog) + dialog.window?.setLayout( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ) + val tvAlertTitle = dialog.findViewById(R.id.tv_alert_title) + tvAlertTitle.text = alertTitle + val tvAlertMessage = dialog.findViewById(R.id.tv_message) + tvAlertMessage.text = alertMessage + val btnOK = dialog.findViewById