How to use Kotlin to realize the Android rapid development framework of MVP architecture?

Preface

Since Google officially set Kotlin as the official language, more and more people have begun to learn Kotlin. Not long ago, a friend asked me how to learn Kotlin.

These days, I have been thinking about this issue. Here is a project to share with you, and teach you how to use Kotlin to implement the Android rapid development framework of MVP architecture?

Introduction

  • The KCommon rapid development framework is based on third-party libraries such as rxjava, rxcache, okhttp, retrofit, and rxlifecycle. It adopts the mvp architecture and a third-party library developed in kotlin language. It can be used with one-click Android Studio template files that generate mvp-related code files. Users develop quickly and focus on code logic instead of cumbersome configuration related to mvp. It also integrates the loading, success, failure and empty page switching, and also provides a template for pull-down refresh and pull-up to load more lists (BaseRefreshAndLoadMoreActivity||BaseRefreshAndLoadMoreFragment), eliminating the need for Android developers to repeat the same type of code write. Github address

  • Some pre-knowledge required to use this development framework

    • Familiar with the mvp architecture in android
    • Familiar with responsive programming frameworks such as rxjava, rxcache, rxlifecycle, etc.
    • Familiar with okhttp, retrofit
    • Will copy the rapid development template to the specified location and use
  • KCommon provides two templates for easy development

    • BaseActivity and BaseFragment starting with Base. This is suitable for general development. For specific usage, please refer to the relevant demo.
    • BaseRefreshAndLoadMoreActivity and BaseRefreshAndLoadMoreFragment beginning with BaseRefreshAndLoadMore. This kind of page development suitable for list display has the functions of pull-down refresh and pull-up to load more data. For specific usage, please refer to the relevant demo.
    • Kotlin version of demo  java version of demo

image display

  • Total presentation
  • Pull down to refresh and pull up to load more
  • Empty page

  • Load cache in case of disconnection
  • Network request error and reload

Instructions based on Kotlin (For the use of Java, please refer to the java version of the demo)

  • Import dependencies

    • Add in the build.gradle file in the root directory
    maven { url "https://jitpack.io" }
    
    • Add in the build.gradle file under the application
  • Initialize CommonLibrary in the onCreate method of the custom Application class

//Initialize KCommon
CommonLibrary.instance.initLibrary(this,

            BuildConfig.APP_URL,
            ApiService::class.java,
            CacheService::class.java)
* 建议配合一键生成相关MVP类的Android Studio模板进行开发,可极大提高开发效率。模板位置 [Android Studio 快速开发模板](https://github.com/BlackFlagBin/KCommonTemplate)

### 详细说明
* CommonLibrary.instance.initLibrary()
/**
 * 初始化
 *
 * @param context Application
 * @param baseUrl retrofit所需的baseUrl
 * @param apiClass retrofit使用的ApisService::Class.java
 * @param cacheClass rxcache使用的CacheService::Class.java
 * @param spName Sharedpreference文件名称
 * @param isDebug 是debug环境还是release环境。debug环境有网络请求的日志,release反之
 * @param startPage 分页列表的起始页,有可能是0,或者是2,这个看后台
 * @param pageSize 分页大小
 * @param headerMap 网络请求头的map集合,便于在网络请求添加统一的请求头,比如token之类
 * @param errorHandleMap 错误处理的map集合,便于针对相关网络请求返回的错误码来做相应的处理,比如错误码401,token失效需要重新登录
 * @param onPageCreateListener 对应页面activity或fragment相关生命周期的回调,便于在页面相关时机做一些统一处理,比如加入友盟统计需要在所有页面的相关生命周期加入一些处理
 * @param onPageDestroyListener 对应页面activity或fragment相关生命周期的回调,便于在页面相关时机做一些统一处理,比如加入友盟统计需要在所有页面的相关生命周期加入一些处理
 * @param onPageResumeListener 对应页面activity或fragment相关生命周期的回调,便于在页面相关时机做一些统一处理,比如加入友盟统计需要在所有页面的相关生命周期加入一些处理
 * @param onPagePauseListener 对应页面activity或fragment相关生命周期的回调,便于在页面相关时机做一些统一处理,比如加入友盟统计需要在所有页面的相关生命周期加入一些处理
 *
 */
fun initLibrary(
        context: Application,
        baseUrl: String,
        apiClass: Class<*>,
        cacheClass: Class<*>,
        spName: String = "kcommon",
        isDebug: Boolean = true,

Author: BlackFlagBin
original address: https://segmentfault.com/a/1190000014387119?utm_source=sf-related

The author of the project has recently maintained it. Although there are still some problems, there are still many things worth learning for many people. Interested friends can delve into it.

The above is the actual sharing of the Kotlin project. Below I want to bring you Kotlin学习手册"Kotlin From Entry to Proficiency" compiled by a foreign boss.

This manual contains profound theories in a simple way, splits and explains the knowledge points in detail, and is very suitable as the first material for beginners to get started. Files are shared for free, friends who need it, remember to **like + leave a message, and then click the blue font here to enter my GitHub . **There is a collection channel in it.

table of Contents

  • Ready to start
  • basis
  • Classes and objects
  • Functions and lambda expressions
  • Interoperability
  • tool
  • FAQ
  • ……

Ready to start

  • Basic grammar
  • Idioms
  • Coding style
  • ……

basis

  • basic type
  • package
  • Control flow
  • Return and jump
  • ……

Classes and objects

  • Classes and inheritance
  • Attributes and fields
  • interface
  • Visibility modifier
  • Expand
  • Data object
  • Generic
  • Nested class
  • Enumeration class
  • Object expressions and declarations
  • Agency model
  • Agent attributes
  • ……

Functions and lambda expressions

  • function
  • Advanced functions and lambda expressions
  • Inline function
  • ……

Interoperability

  • Dynamic typing
  • ……

tool

  • Kotlin code documentation
  • Use Maven
  • Use Ant
  • Use Griffon
  • Use Gradle
  • ……

FAQ

  • Contrast with java
  • Contrast with Scala
  • ……

This manual contains profound theories in a simple way, splits and explains the knowledge points in detail, and is very suitable as the first material for beginners to get started. Files are shared for free, friends who need it, remember to **like + leave a message, and then click the blue font here to enter my GitHub . **There is a collection channel in it.

Guess you like

Origin blog.csdn.net/weixin_49559515/article/details/112389929
Recommended