All Projects → karntrehan → Posts

karntrehan / Posts

A sample Android app using Kotlin, Dagger 2, RxJava, RxAndroid, Retrofit and Android Architecture Components with a modular setup & effective networking

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Posts

Starwars
A sample modular Android app written in Kotlin using Rx, Koin, Coroutines, Dagger 2 and Architecture components
Stars: ✭ 41 (-93.16%)
Mutual labels:  modular, architecture-components
Androidmodulararchiteture
✔️ Android组件化架构,支持组件代码完全隔离/组件循环依赖/便捷集成调试/快速接入,组件内基于 mvvm结构,组件提供高度服用的模块可直接使用,采用 wanAndroid api进行迭代开发。Android componentized architecture, support component code complete isolation / component circular dependency / convenient integrated debugging / fast access, component based mvvm structure, iterative development using wanAndroid api
Stars: ✭ 144 (-75.96%)
Mutual labels:  modular, architecture-components
Burnside
Fast and Reliable E2E Web Testing with only Javascript
Stars: ✭ 389 (-35.06%)
Mutual labels:  modular
Archapp
Simple Android app to show how to design a multi-modules MVVM Android app (fully tested)
Stars: ✭ 551 (-8.01%)
Mutual labels:  architecture-components
Gank
干货集中营 app 安卓实现,基于 RxFlux 架构使用了 RxJava、Retrofit、Glide、Koin等
Stars: ✭ 444 (-25.88%)
Mutual labels:  architecture-components
Module Shop
一个基于 .NET Core构建的简单、跨平台、模块化的商城系统
Stars: ✭ 398 (-33.56%)
Mutual labels:  modular
Modular
A modular front end development framework
Stars: ✭ 475 (-20.7%)
Mutual labels:  modular
Pext
Python-based extendable tool
Stars: ✭ 380 (-36.56%)
Mutual labels:  modular
Changedetection
Automatically track websites changes on Android in background.
Stars: ✭ 563 (-6.01%)
Mutual labels:  architecture-components
Beehive
🐝 BeeHive is a solution for iOS Application module programs, it absorbed the Spring Framework API service concept to avoid coupling between modules.
Stars: ✭ 4,117 (+587.31%)
Mutual labels:  modular
Tabler
Tabler is free and open-source HTML Dashboard UI Kit built on Bootstrap
Stars: ✭ 24,611 (+4008.68%)
Mutual labels:  modular
Mastering Modular Javascript
📦 Module thinking, principles, design patterns and best practices.
Stars: ✭ 3,972 (+563.11%)
Mutual labels:  modular
Tentcss
🌿 A CSS survival kit. Includes only the essentials to make camp.
Stars: ✭ 400 (-33.22%)
Mutual labels:  modular
Lives
Lives - Android LiveData Extensions for Kotlin and Java
Stars: ✭ 509 (-15.03%)
Mutual labels:  architecture-components
Para
Open source back-end server for web, mobile and IoT. The backend for busy developers. (self-hosted or hosted)
Stars: ✭ 389 (-35.06%)
Mutual labels:  modular
Orchardcore
Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
Stars: ✭ 5,591 (+833.39%)
Mutual labels:  modular
Onelog
Dead simple, super fast, zero allocation and modular logger for Golang
Stars: ✭ 389 (-35.06%)
Mutual labels:  modular
Wanandroid
🏄 基于Architecture Components dependencies (Lifecycles,LiveData,ViewModel,Room)构建的WanAndroid开源项目。 你值得拥有的MVVM快速开发框架:https://github.com/jenly1314/MVVMFrame
Stars: ✭ 410 (-31.55%)
Mutual labels:  architecture-components
Livedata Ktx
Kotlin extension for LiveData, chaining like RxJava
Stars: ✭ 466 (-22.2%)
Mutual labels:  architecture-components
Rasa
Extremely modular text editor built in Haskell
Stars: ✭ 597 (-0.33%)
Mutual labels:  modular

Posts

A sample app to demonstrate the building of a good, modular and scalable Android app using Kotlin, Android Architecture Components (LiveData, ViewModel & Room), Dagger, RxJava and RxAndroid among others.

Features

Some of the features of the app include

  • Effective Networking - Using a combination of Retrofit, Rx, Room and LiveData, we are able to handle networking in the most effective way.

  • 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.

  • MVVM architecture - Using the lifecycle aware viewmodels, the view observes changes in the model / repository.

  • Kotlin - This app is completely written in Kotlin.

  • Android Architecture Components - Lifecycle awareness has been achieved using a combination of LiveData, ViewModels and Room.

  • 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.

  • 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.

  • Dependency Injection - Common elements like context, networking interface are injected using Dagger 2.

  • Feature based packaging - This screen-wise / feature-wise packaging makes code really easy to read and debug.

Working

Working

Networking

Data flow Diagram

Activity

viewModel.getPosts()

ViewModel

fun getPosts() {
    if (postsOutcome.value == null)
        repo.fetchPosts()
}

Repository

val postFetchOutcome: PublishSubject<Outcome<List<PostWithUser>>> = PublishSubject.create<Outcome<List<PostWithUser>>>()

override fun fetchPosts() {
    postFetchOutcome.loading(true)
    //Observe changes to the db
    local.getPostsWithUsers()
            .performOnBackOutOnMain(scheduler)
            .doAfterNext {
                if (Synk.shouldSync(SynkKeys.POSTS_HOME, 2, TimeUnit.HOURS))
                    refreshPosts()
            }
            .subscribe({ retailers ->
                postFetchOutcome.success(retailers)
                }, { error -> handleError(error) })
            .addTo(compositeDisposable)
}

override fun refreshPosts() {
    postFetchOutcome.loading(true)
    Flowable.zip(
            remote.getUsers(),
            remote.getPosts(),
             zipUsersAndPosts()
    )
            .performOnBackOutOnMain(scheduler)
            .updateSynkStatus(key = SynkKeys.POSTS_HOME)
            .subscribe({}, { error -> handleError(error) })
            .addTo(compositeDisposable)
}

private fun zipUsersAndPosts() =
        BiFunction<List<User>, List<Post>, Unit> { users, posts ->
            saveUsersAndPosts(users, posts)
        }

override fun saveUsersAndPosts(users: List<User>, posts: List<Post>) {
    local.saveUsersAndPosts(users, posts)
}

override fun handleError(error: Throwable) {
    postFetchOutcome.failed(error)
}

ViewModel

val postsOutcome: LiveData<Outcome<List<Post>>> by lazy {
    //Convert publish subject to livedata
    repo.postFetchOutcome.toLiveData(compositeDisposable)
}

Activity

viewModel.postsOutcome.observe(this, Observer<Outcome<List<Post>>> { outcome ->
    when (outcome) {

        is Outcome.Progress -> srlPosts.isRefreshing = outcome.loading

        is Outcome.Success -> {
            Log.d(TAG, "initiateDataListener: Successfully loaded data")
            adapter.setData(outcome.data)
        }

        is Outcome.Failure -> {
            if (outcome.e is IOException)
                Toast.makeText(context, R.string.need_internet_posts, Toast.LENGTH_LONG).show()
            else
                Toast.makeText(context, R.string.failed_post_try_again, Toast.LENGTH_LONG).show()
        }

    }
})

Testing:

To run all the unit tests, run ./gradlew test. This would test the repositories and the viewmodels.

To run all the instrumented tests, run ./gradlew connectedAndroidTest. This would test the LocalDataSources (Room)

Build info:

  • Android Studio - 3.1 Canary 8
  • Compile SDK - 28
  • MinSDK - 16, Target - 28

Articles

To read more about the architecture choices and the decisions behind this project, kindly refer to the following articles:

Talk to the developer about this project: @karntrehan

Other samples

Below are some of the other samples I have opensourced:

  • Starwars : 2019 - A sample modular Android app written in Kotlin using Rx, Koin, Coroutines, Dagger 2 and Architecture components
  • Agni : 2019 - Android app template for modular apps with Dagger 2, Coroutines, LiveData, ViewModel and RxJava 2.
  • Talko : 2019 - A sample messaging UI app for Android writen in Kotlin with a working local persistence layer.

Libraries used

License

Copyright 2018 Karan Trehan

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].