MVVM News Application with clean code architecture & android jetpack components.

Overview

Android - Clean Architecture - Kotlin

The purpose of this repo is to follow up Clean Architecture principles by bringing them to Android. The repo contains a Sample News Application which shows current news from https://newsapi.org/ API.

Clean Architecture

Clean architecture promotes separation of concerns, making the code loosely coupled. This results in a more testable and flexible code. This approach divides the project into 3 modules: presentation, data, and domain.

  • Presentation: Layer with the Android Framework, the MVVM pattern and the DI module. Depends on the domain to access the use cases and on di, to inject dependencies.
  • Domain: Layer with the business logic. Contains the use cases, in charge of calling the correct repository or data member.
  • Data: Layer with the responsibility of selecting the proper data source for the domain layer. It contains the implementations of the repositories declared in the domain layer. It may, for example, check if the data in a database is up to date, and retrieve it from service if it’s not.

Functionality

The app's functionality includes:

  1. Fetch Current News data from https://newsapi.org/ & show them in RecylerView with smooth pagination.
  2. When an item is selected from RecyclerView it will load the news article in a Webview.
  3. From Details view , a news article can be added to Favorite news - which will store the News article in the Room database.
  4. From Today's news section users can search for specific news topic & return the search results with pagination.
  5. From favorite news section users can view all their saved news articles, they can also swipe left/right to delete the article from local database.

Architecture

The app uses clean architecture with MVVM(Model View View Model) design pattern. MVVM provides better separation of concern, easier testing, Live data & lifecycle awareness, etc.

UI

The UI consists of two parts

  1. View - Activity screen, Host the navigation component fragments.

  2. Fragment - Contains two fragments:

    a) FeedFragement - Show & search for today's news. Listen for onScrollListener using EndlessRecyclerOnScrollListener for Recylcerview.

    b) FavoriteFragment - Show saved news articles.

    c) DetailsFragment - Show webview with floating action button for loading & saving news article.

Model

Model is generated from JSON data into a Kotlin data class. In addition entity class has been added for room database along with Type converter for saving/retrieving custom object data.

ViewModel

MainViewModel.kt

Used for fetching today's news, searching news & update livedata. Also send out the status of the network call like Loading, Success, Error using sealed class.

The ViewModel also responsible for pagination of data using page count.

Dependency Injection

The app uses Dagger-hilt as a dependency injection library.

The ApplicationModule.kt class provides Singleton reference for Retrofit, OkHttpClient, Repository etc.

Network

The network layer is composed of Repository, ApiService. NewsApi - Is an interface containing the suspend functions for retrofit API call.

NewsRepository - Holds the definition of the remote/local repository call.

Building

In-order to successfully run & test the application you will need an api key.

Go to - https://newsapi.org/ and click Get Api Key

Now Go to - app/src/main/java/utils/Constants.kt

And replace

const val API_KEY = "YOUR_API_KEY"

You can open the project in Android studio and press run. Android Studio version used to build the project: Arctic fox 2020.3.1

Gradle plugin used in the project will require Java 11.0 to run.

you can set the gradle jdk in Preferences->Build Tools->Gradle->Gradle JDK

The project uses Kotin-Dsl build scripts to maintain dependencies & plugins.

Libraries used

  1. Android appcompat, core, constraint layout, Material Support.
  2. Android View Binding
  3. Hilt for dependency injection
  4. Retrofit for REST API communication
  5. Coroutine for Network call
  6. Lifecycle, ViewModel
  7. LiveData
  8. Room for local database.
  9. Navigation Component for supporting navigation through the app.
  10. GLide for image loading.
  11. Swipe Refresh Layout for pull-to-refresh RecyclerView.
  12. EndlessRecyclerOnScrollListener for Recylerview Infinite Scroll.
  13. Mockito & Junit for Unit testing.
  14. Robolectric for Instrumentation testing
  15. Truth for Assertion in testing.
  16. Espresso for UI testing.

Testing

Unit testing has been added for MainViewModel & NewsRepository.

MainViewModelTest.kt

Test the viewmodel of the app using CoroutineRule & LiveData Observer.

The test cases comprise of testing different states like Loading, Success, Error with fake data for testing News Response & Search Response.

NewsRepositoryTest.kt

Test the Repository of the app using Robolectric.

The test comprises of testing the functionality of Favorite News Room Database like Insertion, Remove, Get saved news etc.

Mock Webserver is used to test the Network api response in case of successful data, empty, failed case.

Sample app

License

Copyright (c) 2020 Rafsan Ahmad

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.
You might also like...
This is a News Application for android mobiles, fetching news through Guardian API.
This is a News Application for android mobiles, fetching news through Guardian API.

News-In-Hand This is a NEWS FEED APP which gives a user regularly-updated news from the Internet. To achieve this, I use the Guardian API. This is a w

Food Recipes - Android application built with Jetpack compose, MVVM and clean architecture approach.
Food Recipes - Android application built with Jetpack compose, MVVM and clean architecture approach.

Food Recipes Android application that consuming (https://spoonacular.com/food-api) "Recipes api". It has been built with clean architecture principles

Movie Android App written in Kotlin, MVVM, RxJava, Coroutine (Upcoming), Android Architecture Components and Jetpack Compose (Upcoming).
Movie Android App written in Kotlin, MVVM, RxJava, Coroutine (Upcoming), Android Architecture Components and Jetpack Compose (Upcoming).

MovieHunt MovieHunt is a sample Android project using The Movie DB API based on MVVM architecture. It showcases the latest Android tech stacks with we

Small training project where dagger, dagger hilt and other components are used for clean architecture
Small training project where dagger, dagger hilt and other components are used for clean architecture

LeagueNow 🏆 LeagueNow is a sample soccer team list Android application 📱 built to demonstrate use of modern Android development tools - (Kotlin, Arc

NewsFeedApp - An android news-feed-app which shows latest news of various sources
NewsFeedApp - An android news-feed-app which shows latest news of various sources

NewsFeedApp An android news-feed-app which shows latest news of various sources

Newesy is news app which uses NewsAPI to fetch news.
Newesy is news app which uses NewsAPI to fetch news.

Newesy is news app which uses NewsAPI to fetch news.The main aim of this app was to learn Modern Android Architecture (MVVM).It uses MVVM,Kotlin-coroutines,Room,ViewModal,Retrofit,GSON and Navigation graph

Readow - Readow RSS Reader application. It’s fast and clean, giving you the freedom to enjoy your favorite news
Readow - Readow RSS Reader application. It’s fast and clean, giving you the freedom to enjoy your favorite news

Readow Readow RSS Reader application. It’s fast and clean, giving you the freedo

Movie application created with TMDB API, Architecture Components, Android Jetpack libraries

MovieApp Movie application created with TMDB API, Architecture Components, Android Jetpack libraries Built With 🛠️ • Kotlin - The language used in th

This is a practical project for Professional Android Developers that covers clean Architecture basics using the following: skills: Real-like coding with Kotlin, MVVM Design pattern, Kotlin Coroutines, Room database, Navigation Controller, Jetpack compose, Use cases, and Dependency injection using Dagger-Hilt.
Comments
  • Duplicate toast message is showing, when screen rotated.

    Duplicate toast message is showing, when screen rotated.

    Duplicate toast is showing in FeedFragmenton Screen Rotation

    mainViewModel.errorToast.observe(viewLifecycleOwner, { value -> if (value.isNotEmpty()) { Toast.makeText(activity, value, Toast.LENGTH_LONG).show() } })

    opened by rafsanahmad 1
Owner
Rafsan Ahmad
Software developer...enthusiastic about latest trend and opportunities in tech world.
Rafsan Ahmad
A news application through which you can learn and browse all the news that interests you by choosing the country and type of news with the ability to browse and add some news to your favorites

MY-NEWS-Android A news application through which you can learn and browse all the news that interests you by choosing the country and type of news wit

Mahmoud ELramady 0 Nov 11, 2021
A News Application Shows Breaking News of the Country with a feature to save News for future Use.

A News Application Shows Breaking News of the Country with a feature to save News for future Use.You can search news on any topic.Used all latest stuffs in android like Navigation Component, MVVM Architecture, Retrofit, Room DataBase, Kotlin Corutines etc

Aman Bhatt 2 Oct 20, 2022
The News App has been carried out within the framework of the MVVM architecture, information about news is obtained by consulting an API, it is built usisng Jetpack Copose, Coroutines, Dependency Injection with Hilt and Retrofit

Journalist The News App consists of an application that displays the latest news from EEUU from an API that provides official and updated information.

null 0 Nov 3, 2021
Shreyas Patil 2.1k Dec 30, 2022
A news app made using android studio in Java with features like favourite news, Location detector for local news, and especially made with HUAWEI APIs

HuaweiGlobalNewsApp A news app made using android studio in Java with features like favourite news, Location detector for local news, and especially m

Christian Imanuel Hadiwidjaja 1 Oct 30, 2021
News-App - A news app Which displays news with the help of JSON data

News-App This is a news app Which displays news with the help of JSON data

Lalith Sharma 0 Feb 16, 2022
Weather application example with Android Architecture components and Clean Architecture

Weather application example with Android Architecture components and Clean Architecture Weather app that shows how to architect an android app in a cl

null 2 Dec 3, 2021
NewsSpac-MVVM-CleanArch-TDD - The App uses MVVM architecture together with Clean architecture

Aplicativo NewsSpace Arquitetura O App utiliza a arquitetura MVVM em conjunto co

null 1 Feb 11, 2022
:movie_camera: Movie discovery app showcasing Android best practices with Google's recommended architecture: MVVM + Repository + Offline support + Android Architecture Components + Paging library & Retrofit2.

Popular Movies Stage 1 + Stage 2 Discover the most popular and top rated movies playing. Movies data fetched using themoviedb.org API. ✨ Screenshots M

Yassin AJDI 189 Nov 26, 2022
🛒A Minimal Expense E-Commerce App built to demonstrate the use of modern android architecture components [Navigation, Room, MotionLayout, etc..] with MVVM Architecture. ✔

E-Store A Simple E-Commerce App ?? built to demonstrate the use of modern android architecture component with MVVM Architecture ?? . Made with love ❤️

Ameen Essa 14 Nov 3, 2022