GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - vicky7230/Headlines: Simple Headlines app to showcase MVVM(Model-View-ViewModel) architecture with OFFLINE functionality in android.

BranchesTags
Open more actions menu

Latest commit

 

History

22 Commits
22 Commits

Folders and files

NameName
Last commit message
Last commit date
.idea
.idea
 
 
app
app
 
 
gradle/wrapper
gradle/wrapper
 
 
graphics
graphics
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

alt text

Headlines

License

Simple Headlines app to showcase MVVM(Model-View-ViewModel) architecture in android.

Get it on google play

The Model-View-ViewModel Pattern

The main components in the MVVM pattern are:

  • The View — that informs the ViewModel about the user’s actions
  • The ViewModel — exposes streams of data relevant to the View
  • The DataModel — abstracts the data source. The ViewModel works with the DataModel to get and save the data.

MVVM pattern was created to simplify the event driven programming of user interfaces.

In MVVM, ViewModel exposes streams of events to which the Views can bind to. Because of this, the ViewModel does not need to hold a reference to the View anymore. This also means that all the interfaces that the MVP pattern requires, are now dropped. View has a reference to ViewModel but ViewModel has no information about the View. The consumer of the data should know about the producer, but the producer — the ViewModel — doesn’t know, and doesn’t care, who consumes the data.

Offline functionality implementation

The app fetches the aticles from the server and saves them in Database locally when it is opened for the first time. When the user opens it next time, the app loads articles from the Database first and then makes a network request in the background, once the network request is complete, the articles on the screen are swapped with the fresh articles from the server. Doing this gives the app a snappy user experience.

In the app there are two sources of data :

  1. a Local Database(implemented through Room)
  2. a Server(implemented through Retrofit)

So the results(data) that come out of the Database are very quick because the Database is locally available on your device. But the problem with having the results purely in the local Database is that they tend to be stale. The newest form of data is available from the Server. So typically what we also want to do is execute a network request and that network request, typically network requests take a longer time to complete, but once it completes we want to take those results and swap it out with the Local Database results. Doing this gives our app a snapy experience. So the user opens the app and immediately sees the results from the local database and the network request happens in the background and as the network request completes we swap the results. So if we want to do it in rxjava, how do we do it.

Concat comes to rescue

The Concat operator concatenates the output of multiple Observables so that they act like a single Observable, with all of the items emitted by the first Observable being emitted before any of the items emitted by the second Observable (and so forth, if there are more than two).

Concat waits to subscribe to each additional Observable that you pass to it until the previous Observable completes.

So we can create two observables observables to load data from :

  1. one will load data from the Database
  2. the other will fetch the data from the server
    fun getArticlesFromDatabase(): Observable<Headlines?> {
           return Observable.defer { Observable.just(dataManager.selectArticles()) }
                   .subscribeOn(Schedulers.computation())
       }

   fun getArticlesFromNetwork(): Observable<Headlines?> {
           return dataManager.getArticles()
                   .subscribeOn(Schedulers.io())
       }

And them concat these observables and observe them :

    Observable.concat(
                   newsViewModel.getArticlesFromDatabase(),
                   newsViewModel.getArticlesFromNetwork()
           )
                   .observeOn(AndroidSchedulers.mainThread(), true)
                   .subscribe(object : Observer<Headlines?> {
                       override fun onComplete() {
                           refresh_layout.isRefreshing = false
                       }

                       override fun onSubscribe(d: Disposable) {
                           refresh_layout.isRefreshing = true
                           compositeDisposable.add(d)
                       }

                       override fun onNext(t: Headlines) {
                           articlesAdapter.addItems(t.articles as MutableList<Article>?)
                       }

                       override fun onError(e: Throwable) {
                           refresh_layout.isRefreshing = false
                           Timber.e(e.message)
                       }
                   })

Study the repository to understand the implementation completely.

License

   Copyright (C) 2018 VIPIN KUMAR

   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.

About

Simple Headlines app to showcase MVVM(Model-View-ViewModel) architecture with OFFLINE functionality in android.

Topics

dagger2mvvm-architectureoffline-firstroom-persistence-libraryrxjava2-retrofit2viewmodel

Resources

Readme
Apache-2.0 license
Activity

Stars

46 stars

Watchers

3 watching

Forks

11 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.