Home

Awesome

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

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:

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:

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.