kotlin-mvvm

The main technical points

1. RxJava+Retrofit+OkHttp implementation of chained HTTP requests

2. Encapsulate base classes :BaseActivity, BaseVMActivity, BaseFragment, BaseVMFragment, RecycleAdapter, BaseViewModel

3. Encapsulate tool extension classes: CalendarExt, ContextExt, DateExt, EditTextExt, GsonExt, RxJavaExt, StringExt, SnackbarExt

LifeCycle is introduced, tying the ViewModel to the Activity’s LifeCycle

5. Move initialization in Application to ContentProvider instead of wrapping BaseApplication

6. APT(compile-time annotations) encapsulates annotations: OnClickFirstDrawable, OnClickFirstText, OnClickSecondDrawable, OnClickSecondText, Prefs, PrefsField, StatusBar

Minimum compatibility :21

Gradle

1. Add it in Project build.gradle

allprojects {
     repositories {
       maven { url 'https://jitpack.io' }
     }
 }
Copy the code

2. Add build. Gradle in app

Apply plugin: 'kotlin-kapt' // Use kapt annotation processing toolCopy the code

3. Add it under Android of app build.gradle

    buildFeatures {
        viewBinding = true
    }
Copy the code

4. Add dependencies

implementation "com.gitee.catch-pig.kotlin-mvp:mvp:last_version"
kapt "com.gitee.catch-pig.kotlin-mvp:compiler:last_version"
Copy the code

use

1. Configure global parameters in the topic that needs to use the status bar, title bar, and loading animation:

attribute type Must be The default instructions
title_bar_height dimension is There is no Title bar height
title_bar_back_icon DrawableRes is There is no The return icon for the title bar
title_bar_background ColorRes is There is no The background color of the title bar
title_bar_text_color ColorRes is There is no The text color of the title bar
title_bar_show_line boolean no false Whether the line below the title bar is displayed
loading_view_color ColorRes is There is no Loading animation color
loading_view_background ColorRes is There is no Loading Animation background color
recycle_view_empty_layout LayoutRes no emptyLayout List empty pages

Example:

``` <style name="AppThemeBarStyle" parent="Theme.AppCompat.Light.NoActionBar"> <! <item name="title_bar_height">80dp</item> <item name="title_bar_background">@color/colorPrimary</item> <item name="title_bar_back_icon">@drawable/back</item> <item name="title_bar_text_color">@color/white</item> <item name="title_bar_show_line">false</item> <item name="loading_view_color">@color/colorAccent</item> <item name="loading_view_background">@color/white</item> <! </style> ' 'Copy the code

2. Activity

  • Use the MVP inheritance BaseVMActivity
  • Do not use the BaseActivity inheritance of the MVP

3. Fragment

  • Use MVP’s BaseVMFragment inheritance
  • Do not use MVP’s BaseFragment inheritance

4. If you use RecycleView,Adapter can inherit RecycleAdapter to use

Adapter uses ViewBanding and only two methods need to be implemented

override fun itemViewBanding(): Class<ItemUserBinding> {
      return ItemUserBinding::class.java
  }

override fun bindViewHolder(holder: CommonViewHolder<ItemUserBinding>, m: User, position: Int) {
  holder.viewBanding {
      it.name.text = m.name
  }
}
Copy the code

5. Use of annotations

5.1 Title- title

attribute type Must be The default instructions
value StringRes is There is no The title content
backgroundColor ColorRes no Global title background color Title background color
textColor ColorRes no Global title text color Title text color
backIcon DrawableRes no Global title return button icon Title returns button icon

5.2 OnClickFirstDrawable- The first icon button click event on the title

attribute type Must be The default instructions
value StringRes is There is no Button picture content

5.3 OnClickFirstText- Click event for the first text button on the title

attribute type Must be The default instructions
value StringRes is There is no Button text content

5.4 OnClickSecondDrawable- Click event for the second icon button on the title

attribute type Must be The default instructions
value StringRes is There is no Button picture content

5.5 OnClickSecondText- Click event for the second text button on the title

attribute type Must be The default instructions
value StringRes is There is no Button text content

5.6 StatusBar- the status bar

attribute type Must be The default instructions
hide boolean no false Hide status bar
enabled boolean no false The status bar is available
transparent boolean no false Status bar transparency

5.7 Prefs-SharedPreferences Annotation generator

attribute type Must be The default instructions
value String no “” The alias
mode PrefsMode no PrefsMode.MODE_PRIVATE Mode, corresponding to PreferencesMode

5.8 PrefsField-SharedPreferences Field annotation

attribute type Must be The default instructions
value String no “” Field alias or, if empty, the name of the parameter that modifies the field

6. Refresh the page

Use RefreshLayoutWrapper+RecyclerAdapter control to refresh the function

  • RefreshLayoutWrapper inheritance inSmartRefreshLayoutFor details, please refer to the official document of SmartRefreshLayout. By default, the amount of data per page is 16. If you want to change the amount of data per page, you can use the following methods to change it:

    RefreshLayoutWrapper.pageSize = 16
    Copy the code
  • RefreshLayoutWrapper realizedIPageControl, you can get the state of the refresh control and the state of the change by calling the method class within the interface

    / / get refreshed state iPageControl getRefreshStatus ()Copy the code
  • When RecyclerAdapter is instantiated, IPageControl is passed in. After data is successfully obtained, autoUpdateList(list) is called to automatically RefreshLayoutWrapper page numbers and refresh state changes

  • Data update failure can call RecyclerAdapter. UpdateFailed ()

  • To get the amount of data per page and the number of the next page, call the method

    . / / the amount of data per page RecyclerAdapter pageSize = 16 / / next page RecyclerAdapter nextPageIndex = 1Copy the code

7. File Downloaders (DownloadManager))

  • Single file download method download(DownloadInfo.DownloadCallback)
    DownloadManager.download(downloadUrl, {
            
        }, { readLength, countLength ->
            progressLiveData.value = (readLength * 100 / countLength).toInt()
        })
    Copy the code
    • DownloadInfo

      Data class DownloadInfo(/** ** domain name */ val baseUrl:String, /** ** download address */ val url:String, / / Val connectTimeout:Long = 5){override fun toString(): String {return $baseUrl$url"}}Copy the code
    • DownloadCallback

      Interface DownloadCallback {/** * start download */ fun onStart() /** * download success * @param path locally saved address */ fun onSuccess(path:String) /** * Download complete */ fun onComplete() /** * Download progress * @param readLength read progress * @param countLength total progress */ fun OnProgress (readLength: Long, countLength: Long)/download * * * * * @ param t errors information/fun onError (t: Throwable)}Copy the code

Third-party libraries

SmartRefreshLayout- Refresh control

Immersionbar- the status bar

RxJava3

RxAndroid

OkHttp

Retrofit

Gson

AndroidUtilKTX- tools

LoadingView- Loading animation