<repo_name>466262360/MyKotlin<file_sep>/lib/framework/src/main/java/com/test/framework/ui/base/BaseApplication.kt
package com.test.framework.ui.base

import android.app.Application

/**
 * Created by Administrator on 2021/5/27.
 * Des:
 */
open class BaseApplication :Application(){

}<file_sep>/lib/framework/src/main/java/com/test/framework/ui/base/BaseVMActivity.kt
package com.test.framework.ui.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.test.framework.ext.getVMClass

/**
 * Created by Administrator on 2021/5/27.
 * Des:
 */
abstract class BaseVMActivity<VM : BaseViewModel> : AppCompatActivity() {
    lateinit var viewModel: VM
    abstract fun createObserver()

    abstract fun initView()

    abstract fun initDataBinding()

    abstract fun showLoading(it: String = "加载中...")

    abstract fun dismissLoading()

    abstract fun getLayout():Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initDataBinding()
        init()
    }

    private fun init() {
        viewModel = createViewModel()
        registerUIChange()
        initView()
        createObserver()
    }

    private fun registerUIChange() {
        viewModel.uiChange.showDialog.observeInActivity(this, Observer {
            showLoading(it)
        })
        viewModel.uiChange.dismissDialog.observeInActivity(this, Observer {
            dismissLoading()
        })
    }

    private fun createViewModel(): VM {
        return ViewModelProvider(this).get(getVMClass(this))
    }


}
<file_sep>/lib/framework/src/main/java/com/test/framework/ui/base/BaseViewModel.kt
package com.test.framework.ui.base

import androidx.lifecycle.ViewModel
import com.kunminx.architecture.ui.callback.UnPeekLiveData

/**
 * Created by Administrator on 2021/5/27.
 * Des:
 */
class BaseViewModel :ViewModel(){

    val uiChange by lazy{ UiChange()}
    inner class UiChange{
        val showDialog by lazy { UnPeekLiveData<String>() }
        val dismissDialog by lazy { UnPeekLiveData<Boolean>() }
    }
}<file_sep>/lib/framework/src/main/java/com/test/framework/ui/base/BaseVMDBActivity.kt
package com.test.framework.ui.base

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * Created by Administrator on 2021/5/28.
 * Des:
 */
abstract class BaseVMDBActivity<VM:BaseViewModel,DB:ViewDataBinding>:BaseVMActivity<VM>() {
    lateinit var mDataBind:DB
    override fun initDataBinding() {
         mDataBind = DataBindingUtil.setContentView(this, getLayout())
        mDataBind.lifecycleOwner=this
    }
}<file_sep>/app/src/main/java/com/test/myapplication/MainActivity.kt
package com.test.myapplication

import com.test.framework.ui.base.BaseActivity
import com.test.framework.ui.base.BaseViewModel
import com.test.myapplication.databinding.ActivityMainBinding

class MainActivity : BaseActivity<BaseViewModel,ActivityMainBinding>() {
    override fun initView() {
        TODO("Not yet implemented")
    }

    override fun getLayout()=R.layout.activity_main

    override fun createObserver() {

    }

}<file_sep>/lib/framework/src/main/java/com/test/framework/ext/getViewModelExt.kt
package com.test.framework.ext

import java.lang.reflect.ParameterizedType

/**
 * Created by Administrator on 2021/5/28.
 * Des:
 */
@Suppress("UNCHECKED_CAST")
fun <VM> getVMClass(obj:Any):VM{
    return (obj.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as VM
}<file_sep>/settings.gradle
include ':module:main'
include ':module'
include ':lib:framework'
include ':lib'
include ':lib'
include ':app'
rootProject.name = "My Application"<file_sep>/module/main/src/main/java/com/test/main/SplashActivity.kt
package com.test.main

import androidx.appcompat.app.AppCompatActivity

/**
 * Created by Administrator on 2021/5/27.
 * Des:
 */
class SplashActivity : AppCompatActivity(){

}<file_sep>/lib/framework/src/main/java/com/test/framework/ui/base/BaseFragment.kt
package com.test.framework.ui.base

import androidx.fragment.app.Fragment

/**
 * Created by Administrator on 2021/5/21.
 * Des:
 */
abstract class BaseFragment :Fragment(){

}<file_sep>/app/src/main/java/com/test/myapplication/MyApplication.kt
package com.test.myapplication

import android.app.Application
import androidx.viewbinding.BuildConfig
import com.alibaba.android.arouter.launcher.ARouter
import com.test.framework.ui.base.BaseApplication

/**
 * Created by Administrator on 2021/5/21.
 * Des:
 */
class MyApplication :BaseApplication(){
    override fun onCreate() {
        super.onCreate()
        if(BuildConfig.DEBUG){
            ARouter.openDebug()
            ARouter.openLog()
        }
        ARouter.init(this)
    }
}<file_sep>/lib/framework/src/main/java/com/test/framework/ui/base/BaseActivity.kt
package com.test.framework.ui.base

import androidx.databinding.ViewDataBinding

/**
 * Created by Administrator on 2021/5/21.
 * Des:
 */
abstract class BaseActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVMDBActivity<VM, DB>() {

    abstract override fun getLayout(): Int

    abstract override fun initView()

    override fun createObserver() {
        TODO("Not yet implemented")
    }

    override fun showLoading(it: String) {
        TODO("Not yet implemented")
    }
    override fun dismissLoading() {
        TODO("Not yet implemented")
    }


}<file_sep>/config.gradle
ext {
    build = [
            compileSdkVersion: 30,
            buildToolsVersion: "30.0.2",
            minSdkVersion    : 23,
            targetSdkVersion : 30,
            versionCode      : 1,
            versionName      : "1"
    ]

    deps = [
            //kotlin
            core_ktx          : 'androidx.core:core-ktx:1.3.0',
            //androidx
            appcompat         : 'androidx.appcompat:appcompat:1.1.0',
            //ARouter
            arouter_api    : 'com.alibaba:arouter-api:1.5.0',
            //liveData
            livedata       : 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0',
            unpeek_livedata: 'com.kunminx.archi:unpeek-livedata:4.4.1-beta1',
            //ViewModel activity扩展
            activity_ktx   : 'androidx.activity:activity-ktx:1.1.0',
            //ViewModel fragment扩展
            fragment_ktx   : 'androidx.fragment:fragment-ktx:1.2.5',
            //viewModel扩展
            viewModelScope : 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0',
            //material，如BottomNavigationView、TabLayout等
            material       : 'com.google.android.material:material:1.3.0-alpha01',
            //约束布局
            constraintlayout  : 'androidx.constraintlayout:constraintlayout:1.1.3',

    ]
    //单元测试
    apiTestDepds = [
            junit: 'junit:junit:4.12'
    ]
    //android 测试
    apiAndroidTestDepds = [
            junit   : 'androidx.test.ext:junit:1.1.1',
            espresso: 'androidx.test.espresso:espresso-core:3.2.0'
    ]
}