feat:新增core模块(核心组件模块,慢慢整理重构)

This commit is contained in:
Max
2023-10-25 16:48:32 +08:00
parent 6ec57834fd
commit d93b6fbf60
9 changed files with 193 additions and 10 deletions

View File

@@ -36,6 +36,7 @@ android {
'src/module_easyphoto/java',
'src/module_common/java',
'src/module_utils/java',
'src/module_core/java',
]
@@ -45,6 +46,7 @@ android {
'src/module_easyphoto/res',
'src/module_common/res',
'src/module_utils/res',
'src/module_core/res',
]

View File

@@ -0,0 +1,62 @@
import androidx.lifecycle.*
/**
* Created by Max on 2023/10/24 15:11
* Desc:跟随目标生命周期销毁
**/
interface LifecycleCleared : LifecycleEventObserver {
/**
* 是否启用
*/
fun isEnabledLifecycleClear(): Boolean {
return true
}
/**
* 获取监听的目标生命周期
*/
abstract fun getTargetLifecycle(): Lifecycle?
/**
* 目标生命周期已销毁:执行清除资源操作
*/
abstract fun onTargetCleared()
/**
* 获取要执行清理的事件
*/
fun getClearEvent(): Lifecycle.Event? {
return Lifecycle.Event.ON_DESTROY
}
/**
* 绑定生命周期
*/
fun bindLifecycleClear() {
if (!isEnabledLifecycleClear()) {
return
}
getTargetLifecycle()?.addObserver(this)
}
/**
* 取消绑定生命周期(如果实现类是自己主动销毁的,需要主动调下本方法)
*/
fun unBindLifecycleClear() {
if (!isEnabledLifecycleClear()) {
return
}
getTargetLifecycle()?.removeObserver(this)
}
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (!isEnabledLifecycleClear()) {
return
}
if (getClearEvent() == event) {
unBindLifecycleClear()
onTargetCleared()
}
}
}

View File

@@ -0,0 +1,54 @@
package com.chuhai.core.component
import LifecycleCleared
import android.content.Context
import android.content.DialogInterface
import androidx.lifecycle.Lifecycle
import com.chuhai.utils.ktx.asLifecycle
import com.google.android.material.bottomsheet.BottomSheetDialog
/**
* Created by Max on 2023/10/24 15:11
* Desc:BottomSheetDialog
*/
open class SuperBottomSheetDialog : BottomSheetDialog, LifecycleCleared {
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, theme: Int) : super(context, theme) {
init()
}
constructor(
context: Context,
cancelable: Boolean,
cancelListener: DialogInterface.OnCancelListener?
) : super(context, cancelable, cancelListener) {
init()
}
protected open fun init() {
}
override fun getTargetLifecycle(): Lifecycle? {
return context.asLifecycle()
}
override fun onTargetCleared() {
dismiss()
}
override fun show() {
super.show()
bindLifecycleClear()
}
override fun dismiss() {
super.dismiss()
unBindLifecycleClear()
}
}

View File

@@ -3,7 +3,7 @@ package com.chuhai.utils
import android.os.SystemClock
/**
* Created by Max on 3/4/21 5:00 PM
* Created by Max on 2023/10/24 15:11
* Desc:服务器时间
*/
object ServiceTime {

View File

@@ -6,7 +6,7 @@ import android.view.ViewOutlineProvider
import kotlin.math.min
/**
* Created by Max on 2/25/21 1:50 PM
* Created by Max on 2023/10/24 15:11
* Desc:
*/
class ShapeViewOutlineProvider {

View File

@@ -12,9 +12,7 @@ import androidx.core.view.ViewCompat
/**
*
* @author Max
* @date 2019-12-10.
* Created by Max on 2023/10/24 15:11
*/

View File

@@ -0,0 +1,70 @@
package com.chuhai.utils.ktx
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
/**
* Created by Max on 2023/10/25 15:57
* Desc:Context相关工具
**/
/**
* Context转换为Activity
*/
fun Context?.asActivity(): Activity? {
return when {
this is Activity -> {
this
}
(this as? ContextWrapper)?.baseContext?.applicationContext != null -> {
baseContext.asActivity()
}
else -> {
null
}
}
}
/**
* Context转换为Lifecycle
*/
fun Context?.asLifecycle(): Lifecycle? {
if (this == null) return null
return when (this) {
is Lifecycle -> {
this
}
is LifecycleOwner -> {
this.lifecycle
}
is ContextWrapper -> {
this.baseContext.asLifecycle()
}
else -> {
null
}
}
}
/**
* Context转换为LifecycleOwner
*/
fun Context?.asLifecycleOwner(): LifecycleOwner? {
if (this == null) return null
return when (this) {
is LifecycleOwner -> {
this
}
is ContextWrapper -> {
this.baseContext.asLifecycleOwner()
}
else -> {
null
}
}
}

View File

@@ -12,9 +12,8 @@ import androidx.fragment.app.Fragment
import com.chuhai.utils.AppUtils
/**
* Created by Max on 2023/10/24 15:11
* 资源工具类
* @author Max
* @date 2019-11-26.
*/

View File

@@ -4,9 +4,7 @@ import com.chuhai.utils.UiUtils
import kotlin.math.roundToInt
/**
* UI
* @author Max
* @date 2020-01-10.
* Created by Max on 2023/10/24 15:11
*/