refactor(frame): 优化框架
- 添加和优化扩展方法 - 优化屏幕适配兼容性问题
This commit is contained in:
@ -43,7 +43,6 @@ dependencies {
|
||||
api DependencyConfig.GitHub.AutoSize
|
||||
api DependencyConfig.GitHub.ARoute
|
||||
api DependencyConfig.GitHub.RecyclerViewAdapter
|
||||
api DependencyConfig.GitHub.StatusBar
|
||||
api DependencyConfig.GitHub.EventBus
|
||||
api DependencyConfig.GitHub.PermissionX
|
||||
api DependencyConfig.GitHub.AutoService
|
||||
|
||||
@ -4,16 +4,18 @@ import android.text.InputFilter
|
||||
import android.widget.EditText
|
||||
|
||||
/**
|
||||
* @Author: QuYunShuo
|
||||
* @Time: 2020/9/17
|
||||
* @Class: EditTextKtx
|
||||
* @Remark: EditText相关扩展方法
|
||||
* EditText相关扩展方法
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2020/9/17
|
||||
*/
|
||||
|
||||
/**
|
||||
* 过滤掉空格和回车
|
||||
*/
|
||||
fun EditText.filterBlankAndCarriageReturn() {
|
||||
this.filters =
|
||||
arrayOf(InputFilter { source, _, _, _, _, _ -> if (source == " " || source == "\n") "" else null })
|
||||
val filterList = mutableListOf<InputFilter>()
|
||||
filterList.addAll(filters)
|
||||
filterList.add(InputFilter { source, _, _, _, _, _ -> if (source == " " || source == "\n") "" else null })
|
||||
filters = filterList.toTypedArray()
|
||||
}
|
||||
@ -9,10 +9,10 @@ import androidx.lifecycle.LiveData
|
||||
* 使用示例
|
||||
* ```
|
||||
* override fun initObserve() {
|
||||
* observe(mViewModel.stateViewLD, ::stateViewLivaDataHandler)
|
||||
* observeLiveData(mViewModel.stateViewLD, ::processStateViewLivaData)
|
||||
* }
|
||||
*
|
||||
* private fun stateViewLivaDataHandler(data: StateLayoutEnum) {
|
||||
* private fun processStateViewLivaData(data: StateLayoutEnum) {
|
||||
* ...
|
||||
* }
|
||||
* ```
|
||||
@ -22,6 +22,9 @@ import androidx.lifecycle.LiveData
|
||||
* @param action action: (t: T) -> Unit 处理订阅内容的方法
|
||||
* @return Unit
|
||||
*/
|
||||
fun <T> LifecycleOwner.observe(liveData: LiveData<T>, action: (t: T) -> Unit) {
|
||||
inline fun <T> LifecycleOwner.observeLiveData(
|
||||
liveData: LiveData<T>,
|
||||
crossinline action: (t: T) -> Unit
|
||||
) {
|
||||
liveData.observe(this, { it?.let { t -> action(t) } })
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.ktx
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
/**
|
||||
* 开启一个线程调度模式为[Dispatchers.IO]的协程 有默认的异常处理器
|
||||
*
|
||||
* @receiver ViewModel
|
||||
*
|
||||
* @param exceptionHandler CoroutineExceptionHandler 异常处理器
|
||||
* @param block suspend CoroutineScope.() -> Unit 协程体
|
||||
* @return Job
|
||||
*/
|
||||
fun ViewModel.launchIO(
|
||||
exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
|
||||
throwable.printStackTrace()
|
||||
},
|
||||
block: suspend CoroutineScope.() -> Unit
|
||||
): Job = viewModelScope.launch(Dispatchers.IO + exceptionHandler, block = block)
|
||||
@ -1,18 +1,17 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.mvvm.v
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.os.Bundle
|
||||
import android.os.Looper
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.BindingReflex
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.*
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.network.AutoRegisterNetListener
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.network.NetworkStateChangeListener
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.network.NetworkTypeEnum
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.status.ViewStatusHelper
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.status.imp.BaseFrameViewStatusHelperImp
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.toast
|
||||
import me.jessyan.autosize.AutoSizeCompat
|
||||
|
||||
/**
|
||||
* Activity基类
|
||||
@ -20,7 +19,7 @@ import com.quyunshuo.androidbaseframemvvm.base.utils.toast
|
||||
* @author Qu Yunshuo
|
||||
* @since 8/27/20
|
||||
*/
|
||||
abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : BaseFrameStatusActivity(),
|
||||
abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : AppCompatActivity(),
|
||||
FrameView<VB>, NetworkStateChangeListener {
|
||||
|
||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||
@ -29,11 +28,6 @@ abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : BaseFra
|
||||
|
||||
protected abstract val mViewModel: VM
|
||||
|
||||
/**
|
||||
* 基础UI状态管理工具 保存了是否重建的状态信息
|
||||
*/
|
||||
private lateinit var mStatusHelper: BaseFrameViewStatusHelperImp
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(mBinding.root)
|
||||
@ -80,17 +74,20 @@ abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : BaseFra
|
||||
toast(if (isConnected) "网络已连接" else "网络已断开")
|
||||
}
|
||||
|
||||
override fun isRecreate(): Boolean = mStatusHelper.isRecreate
|
||||
|
||||
override fun onRegisterStatusHelper(): ViewStatusHelper? {
|
||||
mStatusHelper = BaseFrameViewStatusHelperImp(super.onRegisterStatusHelper())
|
||||
return mStatusHelper
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
|
||||
this
|
||||
)
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun getResources(): Resources {
|
||||
// 主要是为了解决 AndroidAutoSize 在横屏切换时导致适配失效的问题
|
||||
// 但是 AutoSizeCompat.autoConvertDensity() 对线程做了判断 导致Coil等图片加载框架在子线程访问的时候会异常
|
||||
// 所以在这里加了线程的判断 如果是非主线程 就取消单独的适配
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()))
|
||||
}
|
||||
return super.getResources()
|
||||
}
|
||||
}
|
||||
@ -4,14 +4,13 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.BindingReflex
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.status.ViewStatusHelper
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.status.imp.BaseFrameViewStatusHelperImp
|
||||
|
||||
/**
|
||||
* Fragment基类
|
||||
@ -19,7 +18,7 @@ import com.quyunshuo.androidbaseframemvvm.base.utils.status.imp.BaseFrameViewSta
|
||||
* @author Qu Yunshuo
|
||||
* @since 8/27/20
|
||||
*/
|
||||
abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFrameStatusFragment(),
|
||||
abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : Fragment(),
|
||||
FrameView<VB> {
|
||||
|
||||
/**
|
||||
@ -31,11 +30,6 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFra
|
||||
|
||||
protected abstract val mViewModel: VM
|
||||
|
||||
/**
|
||||
* 基础UI状态管理工具
|
||||
*/
|
||||
private lateinit var mBaseStatusHelper: BaseFrameViewStatusHelperImp
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
@ -57,13 +51,6 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : BaseFra
|
||||
initRequestData()
|
||||
}
|
||||
|
||||
override fun isRecreate(): Boolean = mBaseStatusHelper.isRecreate
|
||||
|
||||
override fun onRegisterStatusHelper(): ViewStatusHelper? {
|
||||
mBaseStatusHelper = BaseFrameViewStatusHelperImp(super.onRegisterStatusHelper())
|
||||
return mBaseStatusHelper
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
|
||||
@ -24,9 +24,4 @@ interface FrameView<VB : ViewBinding> {
|
||||
* 用于在页面创建时进行请求接口
|
||||
*/
|
||||
fun initRequestData()
|
||||
|
||||
/**
|
||||
* 页面是否重建,fragment被回收重新展示的时候为true,系统环境发生变化activity重新创建时为true
|
||||
*/
|
||||
fun isRecreate(): Boolean
|
||||
}
|
||||
Reference in New Issue
Block a user