Skip to content

Commit

Permalink
commit 1.2.7 添加ViewBinding,后续无需重写layoutId()方法
Browse files Browse the repository at this point in the history
  • Loading branch information
hegaojian committed Jan 13, 2022
1 parent 5fbc627 commit 977b57d
Show file tree
Hide file tree
Showing 59 changed files with 51,935 additions and 9,492 deletions.
4 changes: 2 additions & 2 deletions JetpackMvvm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 26
versionName "1.2.6"
versionCode 27
versionName "1.2.7"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
Expand Down
7 changes: 7 additions & 0 deletions JetpackMvvm/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,12 @@
-keep class com.tbruyelle.rxpermissions2.** { *; }
-keep interface com.tbruyelle.rxpermissions2.** { *; }

################ ViewBinding & DataBinding ###############
-keepclassmembers class * implements androidx.viewbinding.ViewBinding {
public static * inflate(android.view.LayoutInflater);
public static * inflate(android.view.LayoutInflater, android.view.ViewGroup, boolean);
public static * bind(android.view.View);
}



Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.hgj.jetpackmvvm.base.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.getVmClazz
import me.hgj.jetpackmvvm.ext.util.notNull
import me.hgj.jetpackmvvm.network.manager.NetState
import me.hgj.jetpackmvvm.network.manager.NetworkStateManager

Expand All @@ -16,11 +18,6 @@ import me.hgj.jetpackmvvm.network.manager.NetworkStateManager
*/
abstract class BaseVmActivity<VM : BaseViewModel> : AppCompatActivity() {

/**
* 是否需要使用DataBinding 供子类BaseVmDbActivity修改,用户请慎动
*/
private var isUserDb = false

lateinit var mViewModel: VM

abstract fun layoutId(): Int
Expand All @@ -33,11 +30,11 @@ abstract class BaseVmActivity<VM : BaseViewModel> : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!isUserDb) {
initDataBind().notNull({
setContentView(it)
}, {
setContentView(layoutId())
} else {
initDataBind()
}
})
init(savedInstanceState)
}

Expand Down Expand Up @@ -86,8 +83,8 @@ abstract class BaseVmActivity<VM : BaseViewModel> : AppCompatActivity() {
* 将非该Activity绑定的ViewModel添加 loading回调 防止出现请求时不显示 loading 弹窗bug
* @param viewModels Array<out BaseViewModel>
*/
protected fun addLoadingObserve(vararg viewModels: BaseViewModel){
viewModels.forEach {viewModel ->
protected fun addLoadingObserve(vararg viewModels: BaseViewModel) {
viewModels.forEach { viewModel ->
//显示弹窗
viewModel.loadingChange.showDialog.observeInActivity(this, Observer {
showLoading(it)
Expand All @@ -99,12 +96,10 @@ abstract class BaseVmActivity<VM : BaseViewModel> : AppCompatActivity() {
}
}

fun userDataBinding(isUserDb: Boolean) {
this.isUserDb = isUserDb
}

/**
* 供子类BaseVmDbActivity 初始化Databinding操作
*/
open fun initDataBind() {}
open fun initDataBind(): View? {
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package me.hgj.jetpackmvvm.base.activity

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import android.view.View
import androidx.databinding.ViewDataBinding
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.inflateBindingWithGeneric

/**
* 作者 : hegaojian
Expand All @@ -13,18 +13,15 @@ import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
*/
abstract class BaseVmDbActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmActivity<VM>() {

lateinit var mDatabind: DB
override fun layoutId() = 0

override fun onCreate(savedInstanceState: Bundle?) {
userDataBinding(true)
super.onCreate(savedInstanceState)
}
lateinit var mDatabind: DB

/**
* 创建DataBinding
*/
override fun initDataBind() {
mDatabind = DataBindingUtil.setContentView(this, layoutId())
mDatabind.lifecycleOwner = this
override fun initDataBind(): View? {
mDatabind = inflateBindingWithGeneric(layoutInflater)
return mDatabind.root
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.hgj.jetpackmvvm.base.activity

import android.view.View
import androidx.viewbinding.ViewBinding
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.inflateBindingWithGeneric

/**
* 作者 : hegaojian
* 时间 : 2019/12/12
* 描述 : 包含 ViewModel 和 ViewBinding ViewModelActivity基类,把ViewModel 和 ViewBinding 注入进来了
* 需要使用 ViewBinding 的清继承它
*/
abstract class BaseVmVbActivity<VM : BaseViewModel, VB : ViewBinding> : BaseVmActivity<VM>() {

override fun layoutId(): Int = 0

lateinit var mViewBind: VB

/**
* 创建DataBinding
*/
override fun initDataBind(): View? {
mViewBind = inflateBindingWithGeneric(layoutInflater)
return mViewBind.root

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.getVmClazz
import me.hgj.jetpackmvvm.network.manager.NetState
import me.hgj.jetpackmvvm.network.manager.NetworkStateManager
import me.hgj.jetpackmvvm.ext.inflateBindingWithGeneric

/**
* 作者 : hegaojian
Expand All @@ -23,6 +16,8 @@ import me.hgj.jetpackmvvm.network.manager.NetworkStateManager
*/
abstract class BaseVmDbFragment<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmFragment<VM>() {

override fun layoutId() = 0

//该类绑定的ViewDataBinding
private var _binding: DB? = null
val mDatabind: DB get() = _binding!!
Expand All @@ -32,8 +27,7 @@ abstract class BaseVmDbFragment<VM : BaseViewModel, DB : ViewDataBinding> : Base
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = DataBindingUtil.inflate(inflater, layoutId(), container, false)
mDatabind.lifecycleOwner = this
_binding = inflateBindingWithGeneric(inflater,container,false)
return mDatabind.root
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ abstract class BaseVmFragment<VM : BaseViewModel> : Fragment() {
*/
abstract fun layoutId(): Int


override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.hgj.jetpackmvvm.base.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.inflateBindingWithGeneric

/**
* 作者 : hegaojian
* 时间 : 2019/12/12
* 描述 : ViewModelFragment基类,自动把ViewModel注入Fragment和 ViewBinding 注入进来了
* 需要使用 ViewBinding 的清继承它
*/
abstract class BaseVmVbFragment<VM : BaseViewModel, VB : ViewBinding> : BaseVmFragment<VM>() {

override fun layoutId() = 0

//该类绑定的 ViewBinding
private var _binding: VB? = null
val mViewBind: VB get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = inflateBindingWithGeneric(inflater,container,false)
return mViewBind.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ fun <T> BaseViewModel.request(
resultState.paresResult(it)
}.onFailure {
it.message?.loge()
//打印错误栈信息
it.printStackTrace()
resultState.paresException(it)
}
}
Expand Down Expand Up @@ -133,6 +135,8 @@ fun <T> BaseViewModel.requestNoCheck(
resultState.paresResult(it)
}.onFailure {
it.message?.loge()
//打印错误栈信息
it.printStackTrace()
resultState.paresException(it)
}
}
Expand Down Expand Up @@ -169,6 +173,8 @@ fun <T> BaseViewModel.request(
}.onFailure { e ->
//打印错误消息
e.message?.loge()
//打印错误栈信息
e.printStackTrace()
//失败回调
error(ExceptionHandle.handleException(e))
}
Expand All @@ -177,6 +183,8 @@ fun <T> BaseViewModel.request(
loadingChange.dismissDialog.postValue(false)
//打印错误消息
it.message?.loge()
//打印错误栈信息
it.printStackTrace()
//失败回调
error(ExceptionHandle.handleException(it))
}
Expand Down Expand Up @@ -214,6 +222,8 @@ fun <T> BaseViewModel.requestNoCheck(
loadingChange.dismissDialog.postValue(false)
//打印错误消息
it.message?.loge()
//打印错误栈信息
it.printStackTrace()
//失败回调
error(ExceptionHandle.handleException(it))
}
Expand Down
56 changes: 56 additions & 0 deletions JetpackMvvm/src/main/java/me/hgj/jetpackmvvm/ext/ViewBindUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.hgj.jetpackmvvm.ext

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.ParameterizedType

/**
* 作者 : hegaojian
* 时间 : 2021/12/21
* 描述 :
*/

@JvmName("inflateWithGeneric")
fun <VB : ViewBinding> AppCompatActivity.inflateBindingWithGeneric(layoutInflater: LayoutInflater): VB =
withGenericBindingClass<VB>(this) { clazz ->
clazz.getMethod("inflate", LayoutInflater::class.java).invoke(null, layoutInflater) as VB
}.also { binding ->
if (binding is ViewDataBinding) {
binding.lifecycleOwner = this
}
}

@JvmName("inflateWithGeneric")
fun <VB : ViewBinding> Fragment.inflateBindingWithGeneric(layoutInflater: LayoutInflater, parent: ViewGroup?, attachToParent: Boolean): VB =
withGenericBindingClass<VB>(this) { clazz ->
clazz.getMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.java)
.invoke(null, layoutInflater, parent, attachToParent) as VB
}.also { binding ->
if (binding is ViewDataBinding) {
binding.lifecycleOwner = viewLifecycleOwner
}
}

private fun <VB : ViewBinding> withGenericBindingClass(any: Any, block: (Class<VB>) -> VB): VB {
var genericSuperclass = any.javaClass.genericSuperclass
var superclass = any.javaClass.superclass
while (superclass != null) {
if (genericSuperclass is ParameterizedType) {
try {
return block.invoke(genericSuperclass.actualTypeArguments[1] as Class<VB>)
} catch (e: NoSuchMethodException) {
} catch (e: ClassCastException) {
} catch (e: InvocationTargetException) {
throw e.targetException
}
}
genericSuperclass = superclass.genericSuperclass
superclass = superclass.superclass
}
throw IllegalArgumentException("There is no generic of ViewBinding.")
}
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ android {
applicationId "me.hgj.jetpackmvvm.demo"
minSdkVersion 21
targetSdkVersion 30
versionCode 26
versionName "1.2.6"
versionCode 27
versionName "1.2.7"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
/* ndk {
// 设置支持的SO库架构 一般只设置这个就阔以设配所有的设备了,还可以减少apk的大小
// 设置支持的SO库架构
abiFilters 'armeabi','armeabi-v7a'
}*/
multiDexEnabled true
}
buildFeatures {
//这2个为非必选,想用哪个就保留那个 用的话一定要加上项目中的 ViewBinding & DataBinding 混淆规则
dataBinding = true
viewBinding = true
}

//使用Kotlin实验特性
Expand Down
Loading

0 comments on commit 977b57d

Please sign in to comment.