Retrofit Fetch API with clean architecture MVVM

shivam kewat
3 min readMay 10, 2021

--

Hello Guys! I know you are very familiar with retrofit and all about the API fetching calls

Today we will discuss one of the practices of doing the same things But in a more elegant and clean way So that our project could be made more scalable and cleanable projects. Without further due,

Let's Get Started~~~

Lets first and foremost we will import the required dependencies for the project

Dependency for the initialize ViewModel in one line

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.activity:activity-ktx:1.1.0"

Retrofit

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp:4.9.0"

Kotlin Coroutines

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'

Let's make our interface for our Register User api retrofit

SignUpAPI

Above you can see our return type is Response<T> from retrofit, let's move forward and create another interface in which we will declare contracting or required methods to be implemented in the inherited class

SignUp

Above you can see our contracting interface, here our method return type Resource<T> which is our helper class, which helps to put result in the correct bottle such as for success response we will get the data in the Resource.Success(), and for the error responses, we will get in the Resource.Failure()

Resource Class

Here are the implemented activity in which we actually run the methods

Our SignUpImplementation class requires our API interface and extends the Signup interface

Above you can see we take the logs for both cases success and error, Here logs are very helpful because whenever you call the api, We always receive logs in the console, This very useful in the debugging purpose

Let take a look at our ViewModel Class

Above you can the new signUpEvent_ variable of type SignUpEvent which we will implement below, Here you can see we took the MutableLiveData of type our Event Class which we will use to observe the values of the event (Data or Error)

private val signUpEvent_ = MutableLiveData<SignUpEvent>()
val signUpEvent:LiveData<SignUpEvent>
get() = signUpEvent_

In the init method, we simple initialize both the interfaces

init {
signUpAPI = RetrofitInstance.getRetrofitInstance().create(SignUpAPI::class.java)
signUp = SignUpImplementation(signUpAPI)
}

Here are our event class for the success or failure of every possible event or empty cases

Here are register method inside ViewModel class which actually triggers the call

Above you can see we use three cases from our event class — LOADING, SUCCESS, ERROR you can use empty for your suitable use case, At the start of the method, we put the event value to SignUpEvent.Loading (for showing progress bar)

signUpEvent_.value = SignUpEvent.Loading

and then we use viewModelScope.launch method to run the suspend function calls, Be sure to always run the suspend functions in the coroutine scope , Like here

Now when the Resource. Success happens we take the data in SignUpEvent.Success(response.data!!)

signUpEvent_.value = SignUpEvent.Success(response.data!!)

and when error we take in

signUpEvent_.value = SignUpEvent.Failure("Sign up Error "!!)

Above you can also take (response.message)in the Failure constructor for better debugging

Now our Activity class, Below our initialization of ViewModel class in one liner

private val signUpViewModel: SignUpViewModel by viewModels()

Let’s trigger the method

signUpViewModel.register(binding.userNameEdit.text.toString(), binding.mobileEdit.text.toString(),binding.passwordEdit.text.toString())

We will use a coroutine lifecycle scope.lauchWhenStarted method for observing the event values

Above we will receive the success response in res.signUpResponse which is the name of our variable in Event class Success EVENT and receive an error state in Failure event, For showing loading at the correct state we use Loading state.

Thanks for reading

Be ready to check out my upcoming articles in the android world.

--

--

shivam kewat

Hello there Devs! I like building apps for the worlds ...