项目作者: umangburman

项目描述 :
Android's MVVM Architecture in Kotlin ft. Retrofit
高级语言: Kotlin
项目地址: git://github.com/umangburman/MVVM-Retrofit-Kotlin-Example.git


Android’s MVVM Architecture in Kotlin ft. Retrofit

This is an example to demonstrate MVVM Architecture in Kotlin with Retrofit in Android.

This example will demonstrate the working of MVVM using Live data and Retrofit in Kotlin. Just follow the steps and you will be able to try out the same in your Android Studio as well.

So Let’s Get Started:

  1. What is MVVM, LiveData, ViewModel, Model, Repository?
  2. Implementation Step-by-Step
  3. Conclusion

1. What is MVVM, LiveData, ViewModel, Model, Repository?

Answer: Let’s see what are the important concepts in MVVM.

MVVM: Model-View-ViewModel (i.e MVVM) is a template of a client application architecture, proposed by John Gossman as an alternative to MVC and MVP patterns when using Data Binding technology. Its concept is to separate data presentation logic
from business logic by moving it into particular class for a clear distinction.

LiveData: LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.

Advantages of Using LiveData:

Ensures your UI matches your data state: LiveData follows the observer pattern. LiveData notifies Observer objects when the lifecycle state changes. You can consolidate your code to update the UI in these Observer objects. Instead of updating the UI every time the app data changes, your observer can update the UI every time there’s a change.

No memory leaks: Observers are bound to Lifecycle objects and clean up after themselves when their associated lifecycle is destroyed.

No crashes due to stopped activities: If the observer’s lifecycle is inactive, such as in the case of an activity in the back stack, then it doesn’t receive any LiveData events.

No more manual lifecycle handling: UI components just observe relevant data and don’t stop or resume observation. LiveData automatically manages all of this since it’s aware of the relevant lifecycle status changes while observing.

Always up to date data: If a lifecycle becomes inactive, it receives the latest data upon becoming active again. For example, an activity that was in the background receives the latest data right after it returns to the foreground.

Proper configuration changes: If an activity or fragment is recreated due to a configuration change, like device rotation, it immediately receives the latest available data.

Sharing resources: You can extend a LiveData object using the singleton pattern to wrap system services so that they can be shared in your app. The LiveData object connects to the system service once, and then any observer that needs the resource can just watch the LiveData object. For more information, see Extend LiveData.

ViewModel: The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

Model: Model can be applied to a class which represents your application’s data model, and will cause instances of the class to become observable, such that a read of a property of an instance of this class during the invocation of a composable function will cause that component to be “subscribed” to mutations of that instance. Composable functions which directly or indirectly read properties of the model class, the composables will be recomposed whenever any properties of the the model are written to.

Repository: Repository modules handle data operations. They provide a clean API so that the rest of the app can retrieve this data easily. They know where to get the data from and what API calls to make when data is updated. You can consider repositories to be mediators between different data sources, such as persistent models, web services, and caches.

2. Implementation Step-by-Step?

As said before, this example uses MVVM with Retrofit using Kotlin. Let’s dive into the steps of doing it.

Step1: Add dependencies to your project:

  1. dependencies {
  2. ...
  3. ...
  4. def lifecycle_version = "2.2.0"
  5. // - - ViewModel
  6. implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
  7. // - - LiveData
  8. implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
  9. // - - Retrofit2
  10. def retrofit_version = "2.9.0"
  11. def logging_version = "4.3.1"
  12. implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
  13. implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
  14. implementation "com.squareup.okhttp3:okhttp:$logging_version"
  15. implementation "com.squareup.okhttp3:logging-interceptor:$logging_version"
  16. // - - WP7 Progress Bar
  17. implementation 'com.github.shadowalker77:wp7progressbar:1.0.5'
  18. ...
  19. ...
  20. }

Step2: Create different folders that relate to MVVM:

Step3: Design your MainActivity which should look like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".view.MainActivity">
  8. <androidx.appcompat.widget.AppCompatButton
  9. android:id="@+id/btnClick"
  10. android:layout_width="0dp"
  11. android:layout_height="wrap_content"
  12. android:text="Click to Start"
  13. android:textColor="#000000"
  14. android:textSize="15sp"
  15. android:layout_margin="15dp"
  16. app:layout_constraintEnd_toEndOf="parent"
  17. app:layout_constraintStart_toStartOf="parent"
  18. app:layout_constraintTop_toTopOf="parent" ></androidx.appcompat.widget.AppCompatButton>
  19. <ir.alirezabdn.wp7progress.WP7ProgressBar
  20. android:id="@+id/wp7progressBar"
  21. android:layout_width="0dp"
  22. android:layout_height="10dp"
  23. android:layout_centerVertical="true"
  24. app:animationDuration="2300"
  25. app:indicatorColor="@color/colorPrimary"
  26. app:indicatorHeight="7"
  27. app:indicatorRadius="5"
  28. app:interval="100"
  29. android:layout_marginStart="10dp"
  30. android:layout_marginEnd="10dp"
  31. app:layout_constraintBottom_toTopOf="@+id/lblYourResponseHere"
  32. app:layout_constraintEnd_toEndOf="parent"
  33. app:layout_constraintStart_toStartOf="parent"
  34. app:layout_constraintTop_toBottomOf="@+id/btnClick" ></ir.alirezabdn.wp7progress.WP7ProgressBar>
  35. <androidx.appcompat.widget.AppCompatTextView
  36. android:id="@+id/lblYourResponseHere"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="Your Response Here"
  40. android:layout_marginBottom="30dp"
  41. android:textSize="20sp"
  42. android:textColor="#000000"
  43. app:layout_constraintBottom_toTopOf="@+id/lblResponse"
  44. app:layout_constraintLeft_toLeftOf="parent"
  45. app:layout_constraintRight_toRightOf="parent" ></androidx.appcompat.widget.AppCompatTextView>
  46. <androidx.appcompat.widget.AppCompatTextView
  47. android:id="@+id/lblResponse"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="- - -"
  51. app:layout_constraintBottom_toBottomOf="parent"
  52. app:layout_constraintLeft_toLeftOf="parent"
  53. app:layout_constraintRight_toRightOf="parent"
  54. app:layout_constraintTop_toTopOf="parent" ></androidx.appcompat.widget.AppCompatTextView>
  55. </androidx.constraintlayout.widget.ConstraintLayout>

Step4: Now let’s create few singleton classes:

In Kotlin, Singletons are very easy to create they just use a keyword called object before the class name. Check the code below

a. Retrofit Singleton

  1. package com.example.mvvmkotlinexample.retrofit
  2. import com.example.mvvmkotlinexample.BuildConfig
  3. import okhttp3.OkHttpClient
  4. import okhttp3.logging.HttpLoggingInterceptor
  5. import okhttp3.logging.HttpLoggingInterceptor.Level
  6. import retrofit2.Retrofit
  7. import retrofit2.converter.gson.GsonConverterFactory
  8. object RetrofitClient {
  9. const val MainServer = "http://api.drfriday.in/api/user/"
  10. val retrofitClient: Retrofit.Builder by lazy {
  11. val levelType: Level
  12. if (BuildConfig.BUILD_TYPE.contentEquals("debug"))
  13. levelType = Level.BODY else levelType = Level.NONE
  14. val logging = HttpLoggingInterceptor()
  15. logging.setLevel(levelType)
  16. val okhttpClient = OkHttpClient.Builder()
  17. okhttpClient.addInterceptor(logging)
  18. Retrofit.Builder()
  19. .baseUrl(MainServer)
  20. .client(okhttpClient.build())
  21. .addConverterFactory(GsonConverterFactory.create())
  22. }
  23. val apiInterface: ApiInterface by lazy {
  24. retrofitClient
  25. .build()
  26. .create(ApiInterface::class.java)
  27. }
  28. }

b. Repository Singleton

  1. package com.example.mvvmkotlinexample.repository
  2. import android.util.Log
  3. import androidx.lifecycle.MutableLiveData
  4. import com.example.mvvmkotlinexample.model.ServicesSetterGetter
  5. import com.example.mvvmkotlinexample.retrofit.RetrofitClient
  6. import retrofit2.Call
  7. import retrofit2.Callback
  8. import retrofit2.Response
  9. object MainActivityRepository {
  10. val serviceSetterGetter = MutableLiveData<ServicesSetterGetter>()
  11. fun getServicesApiCall(): MutableLiveData<ServicesSetterGetter> {
  12. val call = RetrofitClient.apiInterface.getServices()
  13. call.enqueue(object: Callback<ServicesSetterGetter> {
  14. override fun onFailure(call: Call<ServicesSetterGetter>, t: Throwable) {
  15. // TODO("Not yet implemented")
  16. Log.v("DEBUG : ", t.message.toString())
  17. }
  18. override fun onResponse(
  19. call: Call<ServicesSetterGetter>,
  20. response: Response<ServicesSetterGetter>
  21. ) {
  22. // TODO("Not yet implemented")
  23. Log.v("DEBUG : ", response.body().toString())
  24. val data = response.body()
  25. val msg = data!!.message
  26. serviceSetterGetter.value = ServicesSetterGetter(msg)
  27. }
  28. })
  29. return serviceSetterGetter
  30. }
  31. }

Step5: Next step is to create the Model class:

  1. package com.example.mvvmkotlinexample.model
  2. data class ServicesSetterGetter (
  3. val message: String? = null
  4. )

Step6: Next we create ApiInterface for the APIs:

  1. package com.example.mvvmkotlinexample.retrofit
  2. import com.example.mvvmkotlinexample.model.ServicesSetterGetter
  3. import retrofit2.Call
  4. import retrofit2.http.GET
  5. interface ApiInterface {
  6. @GET("services")
  7. fun getServices() : Call<ServicesSetterGetter>
  8. }

Step7: Next and very important step is to have a ViewModel in the project:

  1. package com.example.mvvmkotlinexample.viewmodel
  2. import androidx.lifecycle.LiveData
  3. import androidx.lifecycle.MutableLiveData
  4. import androidx.lifecycle.ViewModel
  5. import com.example.mvvmkotlinexample.model.ServicesSetterGetter
  6. import com.example.mvvmkotlinexample.repository.MainActivityRepository
  7. class MainActivityViewModel : ViewModel() {
  8. var servicesLiveData: MutableLiveData<ServicesSetterGetter>? = null
  9. fun getUser() : LiveData<ServicesSetterGetter>? {
  10. servicesLiveData = MainActivityRepository.getServicesApiCall()
  11. return servicesLiveData
  12. }
  13. }

Step8: Finally, we code the MainActivity kotlin file:

  1. package com.example.mvvmkotlinexample.view
  2. import android.content.Context
  3. import androidx.appcompat.app.AppCompatActivity
  4. import android.os.Bundle
  5. import androidx.lifecycle.Observer
  6. import androidx.lifecycle.ViewModelProvider
  7. import com.example.mvvmkotlinexample.R
  8. import com.example.mvvmkotlinexample.viewmodel.MainActivityViewModel
  9. import kotlinx.android.synthetic.main.activity_main.*
  10. class MainActivity : AppCompatActivity() {
  11. lateinit var context: Context
  12. lateinit var mainActivityViewModel: MainActivityViewModel
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15. setContentView(R.layout.activity_main)
  16. context = this@MainActivity
  17. mainActivityViewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
  18. btnClick.setOnClickListener {
  19. wp7progressBar.showProgressBar()
  20. mainActivityViewModel.getUser()!!.observe(this, Observer { serviceSetterGetter ->
  21. wp7progressBar.hideProgressBar()
  22. val msg = serviceSetterGetter.message
  23. lblResponse.text = msg
  24. })
  25. }
  26. }
  27. }

For any clarifications please refer to the repository.

Conclusion

The goal of the MVVM using Kotlin and Retrofit is to acheive the best possible solution and save development time by using the best architectural pattern suggested by Google.

I hope it will help you too.