This repository contains the source code for a news app that demonstrates some concepts in Android development, including the use of MVVM architecture, navigation components, Retrofit, Room, and Coroutines. The app fetches current news from the News API and provides features such as breaking news, saved news, search functionality, and more.
-
MVVM Architecture: The app follows the MVVM (Model-View-ViewModel) architecture, ensuring a clean and organized codebase structure.
-
Navigation Components: Android's Navigation Components are employed for seamless and intuitive navigation throughout the app.
-
Coroutines: Kotlin Coroutines are utilized to handle asynchronous tasks, enhancing the app's responsiveness and overall performance.
-
Retrofit: API calls to the News API are made using Retrofit, fetching the latest news data for display in the app.
-
Room Database: Room is integrated into the app for storing and retrieving saved articles locally, providing offline access to users.
-
Pagination: The breaking news section features a paginated list, dynamically loading more news items as the user scrolls down.
-
Search Functionality: Users can search for news articles based on keywords, and the app dynamically loads results from the News API.
-
Save and Delete Articles: Articles can be saved to the local Room database, and users have the option to delete them, complete with an undo functionality.
-
Floating Action Button: A floating action button is provided for users to easily save articles to their local database.
-
Internet Connectivity Handling: The app includes robust handling for internet connectivity exceptions. If there is no internet connection, a message is displayed to inform the user about the lack of internet access.
Before running the app:
- Obtain an API key from News API.
- Insert the obtained API key into the appropriate configuration file.
This screenshot represents the main tab: the breaking news available from the API, represented by the BreakingNewsFragment
This screenshot represents the article view, ArticleFragment, which is displayed on clicking on the article
This screenshot represents the saved news tab: which contains the articles liked by the user. It is represented by the SavedNewsFragment. The data is stored via the room database. Liked items can be deleted by swiping.
This screenshot represents the search tab, represented by the SearchFragment. We can search for news in the entire API, exploring all available news. Upon entering the search query, the app retrieves and displays news articles relevant to the specified term. The implemented pagination feature ensures that as the user scrolls down, additional news items are automatically loaded. The search functionality incorporates a deliberate delay before initiating the search, preventing an immediate flood of requests upon each keystroke.
-
View Model (UI Package): The NewsViewModel is responsible for managing and coordinating data related to news articles. It exposes LiveData objects for Breaking News and Search News: Two MutableLiveData objects are declared: breakingNews and searchNews, representing the breaking news and search news data, respectively. It implements functions for getting breaking news and searching news, handling responses, and managing pagination. It also handles internet connectivity checks.
-
Repository (Repository Package): The NewsRepository class is responsible for fetching data from both the local database and the remote data source (API). It acts as an intermediary between the data sources (API and local database) and the rest of the application, particularly the NewsViewModel.
-
View Model Provider Factory (UI Package): NewsViewModelProviderFactory inherits from ViewModelProvider.Factory. It is a Factory class used to create instances of the NewsViewModel.
-
Activity Setup: In the News Activity, the NewsRepository and NewsViewModelProviderFactory are instantiated. The NewsViewModel is then instantiated using the ViewModelProvider with the provided factory.
-
Fragment Setup(ui.fragments): In each fragment (BreakingNewsFragment, SavedNewsFragment, SearchNewsFragment, and ArticleFragment), the NewsViewModel is instantiated in the onViewCreated method. The fragments use the ViewModelProvider and cast the activity to access the shared NewsViewModel.
-
Resource Class (util Package): This is a sealed class that we used to wrap network responses. It has three subclasses: Success (with data), Error (with an error message), and Loading (indicating a loading state). This class helps us differentiate between successful and error responses and handle loading states.
- MVVM Architecture: MVVM allows for the separation of concerns, making the codebase more modular and maintainable. LiveData and ViewModel: LiveData ensures that UI components are always up-to-date, and ViewModel survives configuration changes. Resource Class: The Resource class simplifies handling different states of a network request, making UI logic cleaner. Repository Pattern: Centralized data access through the repository, promoting a clean architecture.
- Android Architecture Components (ViewModel, LiveData)
- Room Database
- Coroutine Extensions for Room
- Kotlin Coroutines
- Retrofit (to make the API calls)
- OkHttp Logging Interceptor (to debug our API requests )
- Navigation Component
- Glide (to load the images from the API)