primers

Gradually, programmers become lazy, and then one framework, one plug-in came into being, and let programmers in the road of laziness will never stop, the beautiful name is: reduce our unnecessary attention to details, and focus on the greater experience at the business level, improve the speed of development. Most people agree with this statement, and the result is: the sharing I use here in the development framework. Regardless of whether it’s good or bad, it’s a great tool in terms of development efficiency and collaborative development. It saves the company a lot of time by focusing less on structure and collaboration, and it’s also a great way to travel at home. Without further ado, this article focuses on mvM-Livedata-Kotlin in Google’s Android Architecture Component. If say wrong place, still hope everybody sees an officer to point out correction.

introduce

MVVM has been around for a long time, and this pattern is a combination of MVVM and Kotlin, which seems to fit perfectly. Kotlin’s own short, lambda writing and LiveData observer pattern make the structure and hierarchy of the code more distinct. The mVVM-LiveDatA_kotlin structure consists of several parts. Here is a brief analysis of each part of the structure and pairing Retrofit with LiveData to implement network requests (instead of RxJava+Retrofit).

  • Composition structure of MVVM
  • Use of MVVM DataBinding
  • Use of LiveData for MVVM
  • MVVM Retrofit integration with LiveData
  • Simple encapsulation of MVVM ViewModel

This article falls into the usual background introduction to MVVM.

The MVVM structure

First, let’s talk about what MVVM is. The binding of interface and data in Android development has always been the focus of research. As APP interfaces become more and more diverse and businesses become more and more complex, more and more data needs to be processed on an interface. At this time, the traditional findView method obviously causes dissatisfaction among developers. Therefore, annotations bound controls such as Butterknife appear, and the processing of business and interface state changes gradually becomes lengthy. This led to data bindings like Databinding, on which MVVM is based an APP development framework. Its structure is as follows:

Architecture Component

The use of MVVM is implemented by Google’s Android Architecture Component, which contains the following components:

  • ROOM ROOM is a encapsulation of Google for local database, through annotations to achieve a local database creation management component, as follows is a ROOM use case:
@Database(entities = arrayOf(Task::class), version = 1)
abstract class ToDoDatabase : RoomDatabase() {

    abstract fun taskDao(a): TasksDao

    companion object {

        private var INSTANCE: ToDoDatabase? = null

        private val lock = Any()

        fun getInstance(context: Context): ToDoDatabase {
            synchronized(lock) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.applicationContext,
                            ToDoDatabase::class.java."Tasks.db")
                            .build()
                }
                returnINSTANCE!! }}}}Copy the code
  • Lifecycle- Aware Components Lifecycle- Aware is a component that Google uses to manage the Lifecycle. Used for processing data in MVVM binding state changes, let we don’t need to care about the View state changes, the View layer in MVVM FragmentActivity, androidx fragments. The app. The fragments component implements the component.

  • The ViewModels VM layer of the MVVM framework is used to achieve bidirectional binding with view and control a component of Model. The ViewModel is mainly used to decouple the INTERFACE and logic of MVVM and facilitate the later maintenance of code.


class BaseViewModel : ViewModel() {

 // ViewModel implementation layer

}

Copy the code
  • An Observer mode component developed by LiveData Google, which can replace some of the functionality of RxJava, is a rxJava-like component.

  • Databinding is used to bind interfaces to data. Databinding is built into Andorid Studio and can be enabled directly by setting the following code in build.gradle:


android {

  ...
  dataBinding {
    enabled = true}}Copy the code

With the above components, you can start the component MVVM one by one.

The original link

Welcome to Enjoytoday, a blog about python, Kotlin, Java and Gradle development!