android-mvvm-architecture
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Top Related Projects
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
An android boilerplate project using clean architecture
Quick Overview
The android-mvvm-architecture repository is a comprehensive example of implementing the MVVM (Model-View-ViewModel) architecture pattern in Android applications. It demonstrates best practices for building scalable, maintainable, and testable Android apps using modern development tools and libraries.
Pros
- Provides a clear and well-structured implementation of MVVM architecture
- Incorporates popular libraries like Dagger 2, RxJava, and Retrofit for efficient development
- Includes examples of unit testing and UI testing
- Offers a good starting point for developers looking to adopt MVVM in their Android projects
Cons
- May be overwhelming for beginners due to its comprehensive nature
- Some of the libraries used might be considered outdated compared to more recent alternatives (e.g., Dagger 2 vs. Hilt)
- Lacks examples of newer Android development features like Jetpack Compose
- Requires a solid understanding of various Android concepts and libraries to fully grasp the implementation
Code Examples
- ViewModel implementation:
class MainViewModel @Inject constructor(
private val schedulerProvider: SchedulerProvider,
private val compositeDisposable: CompositeDisposable,
private val networkHelper: NetworkHelper,
private val userRepository: UserRepository
) : BaseViewModel(schedulerProvider, compositeDisposable, networkHelper) {
private val _users = MutableLiveData<List<User>>()
val users: LiveData<List<User>> = _users
fun fetchUsers() {
compositeDisposable.add(
userRepository.getUsers()
.subscribeOn(schedulerProvider.io())
.subscribe(
{ userList ->
_users.postValue(userList)
},
{
handleNetworkError(it)
}
)
)
}
}
This example shows a ViewModel implementation using dependency injection, RxJava, and LiveData to manage and expose data to the UI.
- Dagger module for providing dependencies:
@Module
class ApplicationModule(private val application: Application) {
@Provides
@Singleton
fun provideApplication(): Application = application
@Provides
@Singleton
fun provideContext(): Context = application
@Provides
@Singleton
fun provideNetworkHelper(): NetworkHelper = NetworkHelper(application)
}
This code demonstrates how to set up a Dagger module to provide application-wide dependencies.
- Repository implementation:
class UserRepository @Inject constructor(
private val networkService: NetworkService,
private val userDao: UserDao
) {
fun getUsers(): Single<List<User>> =
networkService.getUsers()
.map { it.data }
.flatMap { users ->
userDao.insertUsers(users)
.andThen(Single.just(users))
}
}
This example shows a repository class that combines data from a network service and local database using RxJava operators.
Getting Started
-
Clone the repository:
git clone https://github.com/janishar/android-mvvm-architecture.git
-
Open the project in Android Studio.
-
Build and run the app on an emulator or physical device.
-
Explore the codebase to understand the MVVM implementation and various components used in the project.
Competitor Comparisons
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Pros of android-mvvm-architecture
- Implements a clean MVVM architecture pattern
- Includes comprehensive unit and integration tests
- Provides a solid foundation for building scalable Android apps
Cons of android-mvvm-architecture
- May have a steeper learning curve for beginners
- Could be considered over-engineered for simple projects
- Requires more initial setup time compared to simpler architectures
Code Comparison
android-mvvm-architecture:
class MainViewModel @Inject constructor(
private val userRepository: UserRepository,
private val networkHelper: NetworkHelper
) : ViewModel() {
private val _users = MutableLiveData<Resource<List<User>>>()
val users: LiveData<Resource<List<User>>> = _users
}
Both repositories appear to be the same project, so there isn't a meaningful code comparison to be made between them. The repository showcases a well-structured MVVM architecture implementation for Android, including dependency injection, repository pattern, and the use of LiveData for reactive programming.
The project demonstrates best practices for building maintainable and testable Android applications, making it a valuable resource for developers looking to implement MVVM architecture in their projects.
This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
Pros of Android-CleanArchitecture
- Implements a more comprehensive Clean Architecture approach, promoting better separation of concerns
- Provides a more scalable and maintainable codebase structure for large projects
- Includes detailed unit tests and integration tests, demonstrating better test coverage
Cons of Android-CleanArchitecture
- Steeper learning curve due to its more complex architecture
- May be overkill for smaller projects or simpler applications
- Less focus on specific Android components and libraries compared to android-mvvm-architecture
Code Comparison
Android-CleanArchitecture:
class GetUserList @Inject constructor(
private val userRepository: UserRepository
) : UseCase<List<User>, Void?>() {
override suspend fun run(params: Void?) = userRepository.users()
}
android-mvvm-architecture:
class UserRepository @Inject constructor(
private val apiHelper: ApiHelper,
private val dbHelper: DatabaseHelper
) {
suspend fun getUsers(): Resource<List<User>> {
return ResponseHandler.handleSuccess(apiHelper.getUsers())
}
}
The Android-CleanArchitecture example showcases a use case implementation, emphasizing the separation of business logic from data sources. The android-mvvm-architecture example demonstrates a more direct approach to data retrieval within the repository layer.
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
Pros of architecture-samples
- Official Google sample, ensuring best practices and up-to-date implementations
- Covers multiple architectural approaches (MVC, MVP, MVVM)
- Extensive documentation and explanations for each architectural pattern
Cons of architecture-samples
- May be overwhelming for beginners due to multiple architectural examples
- Less focused on a single, opinionated implementation
- Lacks some real-world scenarios and complex use cases
Code Comparison
architecture-samples (MVVM):
class TasksViewModel(
private val tasksRepository: TasksRepository
) : ViewModel() {
private val _items = MutableLiveData<List<Task>>().apply { value = emptyList() }
val items: LiveData<List<Task>> = _items
}
android-mvvm-architecture:
class MainViewModel(
private val appDataManager: AppDataManager,
private val schedulerProvider: SchedulerProvider
) : BaseViewModel<MainNavigator>(appDataManager, schedulerProvider) {
private val blogListLiveData: MutableLiveData<List<BlogResponse.Blog>> = MutableLiveData()
fun getBlogListLiveData(): LiveData<List<BlogResponse.Blog>> = blogListLiveData
}
Both repositories demonstrate MVVM architecture, but android-mvvm-architecture includes additional components like AppDataManager and SchedulerProvider, offering a more opinionated and structured approach. architecture-samples focuses on simplicity and clarity in its examples.
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
Pros of architecture-samples
- Official Google sample, ensuring best practices and up-to-date implementations
- Covers multiple architectural approaches (MVC, MVP, MVVM)
- Extensive documentation and explanations for each architectural pattern
Cons of architecture-samples
- May be overwhelming for beginners due to multiple architectural examples
- Less focused on a single, opinionated implementation
- Lacks some real-world scenarios and complex use cases
Code Comparison
architecture-samples (MVVM):
class TasksViewModel(
private val tasksRepository: TasksRepository
) : ViewModel() {
private val _items = MutableLiveData<List<Task>>().apply { value = emptyList() }
val items: LiveData<List<Task>> = _items
}
android-mvvm-architecture:
class MainViewModel(
private val appDataManager: AppDataManager,
private val schedulerProvider: SchedulerProvider
) : BaseViewModel<MainNavigator>(appDataManager, schedulerProvider) {
private val blogListLiveData: MutableLiveData<List<BlogResponse.Blog>> = MutableLiveData()
fun getBlogListLiveData(): LiveData<List<BlogResponse.Blog>> = blogListLiveData
}
Both repositories demonstrate MVVM architecture, but android-mvvm-architecture includes additional components like AppDataManager and SchedulerProvider, offering a more opinionated and structured approach. architecture-samples focuses on simplicity and clarity in its examples.
An android boilerplate project using clean architecture
Pros of android-clean-architecture-boilerplate
- Implements a more comprehensive Clean Architecture approach
- Utilizes Kotlin coroutines for asynchronous programming
- Includes a robust testing setup with example unit and integration tests
Cons of android-clean-architecture-boilerplate
- More complex project structure, which may be overwhelming for beginners
- Requires a deeper understanding of Clean Architecture principles
- Less focus on MVVM specifically, as it encompasses a broader architectural approach
Code Comparison
android-clean-architecture-boilerplate:
class GetBufferoos @Inject constructor(
private val bufferooRepository: BufferooRepository,
private val postExecutionThread: PostExecutionThread
) : UseCase<List<Bufferoo>, Void?>(postExecutionThread) {
public override fun buildUseCaseObservable(params: Void?): Observable<List<Bufferoo>> {
return bufferooRepository.getBufferoos()
}
}
android-mvvm-architecture:
public class LoginViewModel extends ViewModel {
private final LoginUseCase loginUseCase;
private final MutableLiveData<LoginResponse> loginLiveData = new MutableLiveData<>();
@Inject
public LoginViewModel(LoginUseCase loginUseCase) {
this.loginUseCase = loginUseCase;
}
}
The android-clean-architecture-boilerplate example showcases a use case implementation using Kotlin and RxJava, while the android-mvvm-architecture example demonstrates a ViewModel class in Java with LiveData. The former focuses on Clean Architecture principles, while the latter emphasizes MVVM patterns.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
This Project is [Deprecated] goto latest project: Modern Android Development - WhereIsMyMotivation
Deprecated
Android MVVM Architecture: Sample App
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava, FastAndroidNetworking, PlaceHolderView and AndroidDebugDatabase
About The Author
You can connect with me here:
The app has following packages:
- data: It contains all the data accessing and manipulating components.
- di: Dependency providing classes using Dagger2.
- ui: View classes along with their corresponding ViewModel.
- utils: Utility classes.
Classes have been designed in such a way that it could be inherited and maximize the code reuse.
Library reference resources:
- Dagger2: https://github.com/janishar/android-dagger2-example
- PlaceHolderView: https://github.com/janishar/PlaceHolderView
- Calligraphy: https://github.com/chrisjenx/Calligraphy
- Room: https://developer.android.com/topic/libraries/architecture/room.html
Concept reference resources:
- Introduction to Dagger 2: Part 1
- Introduction to Dagger 2: Part 2
- Android Dagger2: Critical things to know before you implement
- Android Tinder Swipe View Example
- RxJava Anatomy: What is RxJava, how RxJava is designed, and how RxJava works.
Looking for Kotlin MVP Architecture - Check here
Looking for MVP Architecture - Check here
License
Copyright (C) 2023 JANISHAR ALI ANWAR
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.
Contributing to Android MVVM Architecture
Just make pull request. You are in!
Top Related Projects
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
An android boilerplate project using clean architecture
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot