{"id":13428782,"url":"https://github.com/karntrehan/Posts","last_synced_at":"2025-03-16T01:33:38.239Z","repository":{"id":74705266,"uuid":"93308442","full_name":"karntrehan/Posts","owner":"karntrehan","description":"A sample Android app using Kotlin, Dagger 2, RxJava, RxAndroid, Retrofit and Android Architecture Components with a modular setup \u0026 effective networking","archived":false,"fork":false,"pushed_at":"2019-10-06T10:53:17.000Z","size":5016,"stargazers_count":609,"open_issues_count":0,"forks_count":92,"subscribers_count":35,"default_branch":"master","last_synced_at":"2024-10-27T06:39:06.631Z","etag":null,"topics":["architecture-components","kotlin","modular","rxjava2-dagger2-retrofit2"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karntrehan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-06-04T11:17:25.000Z","updated_at":"2024-10-11T16:27:53.000Z","dependencies_parsed_at":"2024-01-14T02:37:40.712Z","dependency_job_id":"c0f4e513-b8b8-4901-a8d5-ce67764a3839","html_url":"https://github.com/karntrehan/Posts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karntrehan%2FPosts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karntrehan%2FPosts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karntrehan%2FPosts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karntrehan%2FPosts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karntrehan","download_url":"https://codeload.github.com/karntrehan/Posts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243814905,"owners_count":20352037,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["architecture-components","kotlin","modular","rxjava2-dagger2-retrofit2"],"created_at":"2024-07-31T01:01:05.006Z","updated_at":"2025-03-16T01:33:37.694Z","avatar_url":"https://github.com/karntrehan.png","language":"Kotlin","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"# Posts\nA sample app to demonstrate the building of a good, modular and scalable Android app using Kotlin, Android Architecture Components (LiveData, ViewModel \u0026 Room), Dagger, RxJava and RxAndroid among others.\n\n# Features\nSome of the features of the app include\n\n- **Effective Networking** - Using a combination of Retrofit, Rx, Room and LiveData, we are able to handle networking in the most effective way.\n\n- **Modular** - The app is broken into modules of features and libraries which can be combined to build instant-apps, complete apps or lite version of apps.\n\n- **MVVM architecture** - Using the lifecycle aware viewmodels, the view observes changes in the model / repository.\n\n- **Kotlin** - This app is completely written in Kotlin.\n\n- **Android Architecture Components** - Lifecycle awareness has been achieved using a combination of LiveData, ViewModels and Room.\n\n - **Offline first architecture** - All the data is first tried to be loaded from the db and then updated from the server. This ensures that the app is usable even in an offline mode.\n\n - **Intelligent sync** -Intelligent hybrid syncing logic makes sure your Android app does not make repeated calls to the same back-end API for the same data in a particular time period.\n\n - **Dependency Injection** - Common elements like `context`, `networking` interface are injected using Dagger 2.\n\n - **Feature based packaging** - This screen-wise / feature-wise packaging makes code really easy to read and debug.\n\n# Working\n![Working](posts.gif)\n\n# Networking\n![Data flow Diagram](DataFlow.png)\n\n### [Activity](posts/src/main/java/com/karntrehan/posts/list/ListActivity.kt)\n```java\nviewModel.getPosts()\n```\n\n### [ViewModel](posts/src/main/java/com/karntrehan/posts/list/viewmodel/ListViewModel.kt)\n```java\nfun getPosts() {\n    if (postsOutcome.value == null)\n        repo.fetchPosts()\n}\n```\n\n###  [Repository](posts/src/main/java/com/karntrehan/posts/list/model/ListRepository.kt)\n```java\nval postFetchOutcome: PublishSubject\u003cOutcome\u003cList\u003cPostWithUser\u003e\u003e\u003e = PublishSubject.create\u003cOutcome\u003cList\u003cPostWithUser\u003e\u003e\u003e()\n\noverride fun fetchPosts() {\n    postFetchOutcome.loading(true)\n    //Observe changes to the db\n    local.getPostsWithUsers()\n            .performOnBackOutOnMain(scheduler)\n            .doAfterNext {\n                if (Synk.shouldSync(SynkKeys.POSTS_HOME, 2, TimeUnit.HOURS))\n                    refreshPosts()\n            }\n            .subscribe({ retailers -\u003e\n                postFetchOutcome.success(retailers)\n                }, { error -\u003e handleError(error) })\n            .addTo(compositeDisposable)\n}\n\noverride fun refreshPosts() {\n    postFetchOutcome.loading(true)\n    Flowable.zip(\n            remote.getUsers(),\n            remote.getPosts(),\n             zipUsersAndPosts()\n    )\n            .performOnBackOutOnMain(scheduler)\n            .updateSynkStatus(key = SynkKeys.POSTS_HOME)\n            .subscribe({}, { error -\u003e handleError(error) })\n            .addTo(compositeDisposable)\n}\n\nprivate fun zipUsersAndPosts() =\n        BiFunction\u003cList\u003cUser\u003e, List\u003cPost\u003e, Unit\u003e { users, posts -\u003e\n            saveUsersAndPosts(users, posts)\n        }\n\noverride fun saveUsersAndPosts(users: List\u003cUser\u003e, posts: List\u003cPost\u003e) {\n    local.saveUsersAndPosts(users, posts)\n}\n\noverride fun handleError(error: Throwable) {\n    postFetchOutcome.failed(error)\n}\n```\n\n### [ViewModel](posts/src/main/java/com/karntrehan/posts/list/viewmodel/ListViewModel.kt) ###\n```java\nval postsOutcome: LiveData\u003cOutcome\u003cList\u003cPost\u003e\u003e\u003e by lazy {\n    //Convert publish subject to livedata\n    repo.postFetchOutcome.toLiveData(compositeDisposable)\n}\n```\n\n###  [Activity](posts/src/main/java/com/karntrehan/posts/list/ListActivity.kt)\n```java\nviewModel.postsOutcome.observe(this, Observer\u003cOutcome\u003cList\u003cPost\u003e\u003e\u003e { outcome -\u003e\n    when (outcome) {\n\n        is Outcome.Progress -\u003e srlPosts.isRefreshing = outcome.loading\n\n        is Outcome.Success -\u003e {\n            Log.d(TAG, \"initiateDataListener: Successfully loaded data\")\n            adapter.setData(outcome.data)\n        }\n\n        is Outcome.Failure -\u003e {\n            if (outcome.e is IOException)\n                Toast.makeText(context, R.string.need_internet_posts, Toast.LENGTH_LONG).show()\n            else\n                Toast.makeText(context, R.string.failed_post_try_again, Toast.LENGTH_LONG).show()\n        }\n\n    }\n})\n```\n\n\n# Testing:\nTo run all the unit tests, run `./gradlew test`. This would test the repositories and the viewmodels.\n\nTo run all the instrumented tests, run  `./gradlew connectedAndroidTest`. This would test the LocalDataSources (Room)\n\n# Build info:\n  - Android Studio - 3.1 Canary 8\n  - Compile SDK - 28\n  - MinSDK - 16, Target - 28\n\n# Articles\nTo read more about the architecture choices and the decisions behind this project, kindly refer to the following articles:\n* [Effective Networking On Android using Retrofit, Rx and Architecture Components](https://medium.com/mindorks/effective-networking-on-android-using-retrofit-rx-and-architecture-components-4554ca5b167d)\n* [Writing a modular project on Android](https://medium.com/mindorks/writing-a-modular-project-on-android-304f3b09cb37)\n* [To Synk or not to Synk](https://medium.com/mindorks/to-synk-or-not-to-synk-fcc6e4c56e14)\n\n**Talk to the developer about this project**: [@karntrehan](https://twitter.com/karntrehan)\n\n# Other samples\nBelow are some of the other samples I have opensourced: \n* [Starwars](https://github.com/karntrehan/Starwars) : 2019 - A sample modular Android app written in Kotlin using Rx, Koin, Coroutines, Dagger 2 and Architecture components\n* [Agni](https://github.com/karntrehan/Agni) : 2019 - Android app template for modular apps with Dagger 2, Coroutines, LiveData, ViewModel and RxJava 2.\n* [Talko](https://github.com/karntrehan/Talko) : 2019 - A sample messaging UI app for Android writen in Kotlin with a working local persistence layer.\n\n# Libraries used\n* [Android Support Libraries](https://developer.android.com/topic/libraries/support-library/index.html)\n* [Dagger 2](https://google.github.io/dagger/)\n* [Retrofit](http://square.github.io/retrofit/)\n* [OkHttp](http://square.github.io/okhttp/)\n* [Picasso](http://square.github.io/picasso/)\n* [Stetho](http://facebook.github.io/stetho/)\n* [Room](https://developer.android.com/topic/libraries/architecture/room.html)\n* [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel.html)\n* [LiveData](https://developer.android.com/topic/libraries/architecture/livedata.html)\n* [RxJava](https://github.com/ReactiveX/RxJava)\n* [RxAndroid](https://github.com/ReactiveX/RxAndroid)\n\n# License\n\n    Copyright 2018 Karan Trehan\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarntrehan%2FPosts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarntrehan%2FPosts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarntrehan%2FPosts/lists"}