Commit 680622c7 authored by wanglei's avatar wanglei

...

parent 51ff6dd2
......@@ -21,7 +21,6 @@ abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
setContentView(binding.root)
eventActivity()
// EventUtils.event("page_${javaClass.simpleName}")
initView()
initListener()
}
......
......@@ -27,6 +27,11 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
buildConfig = true
}
}
dependencies {
......
package com.koko.drouter.ad
import android.app.Activity
import com.didi.drouter.api.DRouter
const val INTER_BATTERY_INFO = "INTER_BATTERY_INFO"
val interAdService by lazy {
DRouter.build(InterAdService::class.java).getService()
}
interface InterAdService {
fun showInterAd(activity: Activity, interWhere: String = "", callBack: () -> Unit) = Unit
......
......@@ -2,9 +2,14 @@ package com.koko.drouter.ad
import android.app.Activity
import android.widget.FrameLayout
import com.didi.drouter.api.DRouter
const val NativeBatteryType: String = "NativeBatteryType"
val nativeAdService by lazy {
DRouter.build(NativeAdService::class.java).getService()
}
interface NativeAdService {
fun showNative(activity: Activity, flAd: FrameLayout, nativeType: String)
......
package com.koko.drouter.aes
import com.didi.drouter.api.DRouter
val aesService by lazy {
DRouter.build(AesService::class.java).getService()
}
interface AesService {
fun decrypt(aes: String): String
}
\ No newline at end of file
package com.koko.drouter.app
import android.app.Activity
import com.didi.drouter.api.DRouter
import com.koko.drouter.ad.NativeAdService
val appService by lazy {
DRouter.build(AppService::class.java).getService()
}
interface AppService {
......
package com.koko.drouter.base
import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultCaller
import androidx.activity.result.contract.ActivityResultContracts
class ActivityLauncher(activityResultCaller: ActivityResultCaller) {
//region 权限
private var permissionCallback: ActivityResultCallback<Map<String, Boolean>>? = null
private val permissionLauncher =
activityResultCaller.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result: Map<String, Boolean> ->
permissionCallback?.onActivityResult(result)
}
fun launch(
permissionArray: Array<String>,
permissionCallback: ActivityResultCallback<Map<String, Boolean>>?
) {
this.permissionCallback = permissionCallback
permissionLauncher.launch(permissionArray)
}
//endregion
//region intent跳转
private var activityResultCallback: ActivityResultCallback<ActivityResult>? = null
private val intentLauncher =
activityResultCaller.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult: ActivityResult ->
activityResultCallback?.onActivityResult(activityResult)
}
/**
* it.resultCode == Activity.RESULT_OK
*/
fun launch(
intent: Intent,
activityResultCallback: ActivityResultCallback<ActivityResult>? = null
) {
this.activityResultCallback = activityResultCallback
intentLauncher.launch(intent)
}
//endregion
//region saf
// private var safResultCallback: ActivityResultCallback<Uri?>? = null
// private val safLauncher =
// activityResultCaller.registerForActivityResult(
// ActivityResultContracts.OpenDocument(),
// ) { uri ->
// safResultCallback?.onActivityResult(uri)
// }
//
// fun launch(array: Array<String>, safResultCallback: ActivityResultCallback<Uri?>?) {
// this.safResultCallback = safResultCallback
// safLauncher.launch(array)
// }
//end region
}
\ No newline at end of file
package com.koko.drouter.base
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewbinding.ViewBinding
import com.didi.drouter.api.DRouter
import com.koko.drouter.app.AppService
import com.koko.drouter.event.EventService
import java.util.Locale
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.random.Random
abstract class BaseActivity<VB : ViewBinding>(
bindingInflater: (LayoutInflater) -> VB
) : AppCompatActivity() {
open val TAG = javaClass.simpleName
private val tagNo = Random.nextInt(500)
val binding by lazy(LazyThreadSafetyMode.NONE) {
bindingInflater(layoutInflater)
}
lateinit var launcher: ActivityLauncher
private var currentLocale: Locale? = null
private val eventService by lazy {
DRouter.build(EventService::class.java).getService()
}
private var onCreateI = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
useDefaultImmersive()
eventService.eventActivity(this)
onCreateI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onCreate ${javaClass.simpleName} $onCreateI")
launcher = ActivityLauncher(this)
setContentView(binding.root)
eventActivity()
// currentLocale = Locale(appLanguageSp, appLanguageCountrySp)
initView()
initListener()
initData()
}
open fun initData() {}
open fun useDefaultImmersive() {
enableEdgeToEdge(SystemBarStyle.light(Color.TRANSPARENT, Color.TRANSPARENT))
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
}
private fun eventActivity() {
val eventService = DRouter.build(EventService::class.java).getService()
eventService.eventActivity(this)
}
protected open fun initView() {}
protected open fun initListener() {}
fun finishToMainTop() {
val appService = DRouter.build(AppService::class.java).getService()
appService.finishToMainTop(this)
}
override fun onDestroy() {
super.onDestroy()
}
// override fun attachBaseContext(newBase: Context?) {
// super.attachBaseContext(MultiLanguages.attach(newBase))
// }
// fun changeLanguage(currentActivity: Activity, bundle: Bundle = Bundle()): Boolean {
// val spLanguage = Locale(appLanguageSp, appLanguageCountrySp)
// val flag = currentLocale != spLanguage
// val restart = MultiLanguages.setAppLanguage(this, spLanguage)
// LogEx.logDebug(
// TAG, "changeLanguage " +
// "flag=$flag restart=$restart currentLocale=$currentLocale spLanguage=$spLanguage"
// )
// if (restart) {
// currentLocale = Locale(appLanguageSp, appLanguageCountrySp)
// }
//
// if (flag || restart) {
// // 1.使用 recreate 来重启 Activity,效果差,有闪屏的缺陷
//// recreate();
//
// // 2.使用常规 startActivity 来重启 Activity,有从左向右的切换动画
// // 稍微比 recreate 的效果好一点,但是这种并不是最佳的效果
// // startActivity(new Intent(this, LanguageActivity.class));
// // finish();
//
// // 3.我们可以充分运用 Activity 跳转动画,在跳转的时候设置一个渐变的效果,相比前面的两种带来的体验是最佳的
// //需要设置启动模式,避免AndroidManifest.xml设置singleTop启动模式导致重复启动不生效
// //这种方式如果设置了singleTop或者singleTask的,再次回到该activity无法改变于语言,因为还在堆栈里没有重新创建
// needRecreate()
// if (needRecreate()) {
// LogEx.logDebug(TAG, "changeLanguage recreate")
// recreate()
// } else {
// startActivity(Intent(this, currentActivity::class.java).apply {
// putExtras(bundle)
// })
// overridePendingTransition(R.anim.activity_alpha_in, R.anim.activity_alpha_out)
// finish()
// }
// return true
// }
// return false
// }
/**
* 判断是否需要重新创建对SingleTop启动模式的有效
*/
fun needRecreate(): Boolean {
return TAG.contains("MainActivity")
}
/**
* 避免在onResume--onPause周期内,多次调用
* 在onStop中重置标志
*/
private val onResumeCalled = AtomicBoolean(false)
private var onResumeOneShootI = 0
open fun onResumeOneShoot() {
onResumeOneShootI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onResumeOneShoot ${javaClass.simpleName} $onResumeOneShootI")
}
private var onResumeI = 0
override fun onResume() {
super.onResume()
onResumeI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onResume ${javaClass.simpleName} $onResumeI")
if (!onResumeCalled.get()) {
onResumeCalled.set(true)
onResumeOneShoot()
}
}
private var onStartI = 0
override fun onStart() {
super.onStart()
onStartI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onStart ${javaClass.simpleName} $onStartI")
}
private var onStopI = 0
override fun onStop() {
super.onStop()
onStopI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onStop ${javaClass.simpleName} $onStopI")
onResumeCalled.set(false)
}
private var onPauseI = 0
override fun onPause() {
super.onPause()
onPauseI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onPause ${javaClass.simpleName} $onPauseI")
}
}
\ No newline at end of file
@file:Suppress("unused")
package com.koko.drouter.base
import android.app.Activity
import android.content.Intent
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 kotlin.random.Random
/**
*
* @param VB : ViewBinding
* 基本的fragment绑定数据
*/
abstract class BaseFragment<VB : ViewBinding>(
private val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : Fragment() {
private var _binding: VB? = null
private var fragmentInit = false
open val TAG = javaClass.simpleName
private val tagNo = Random.nextInt(500)
//重写binding get()方法不直接暴露真实的可空_binding数据
@Suppress("MemberVisibilityCanBePrivate")
val binding: VB
get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = bindingInflater.invoke(inflater, container, false)
return binding.root
}
private var onViewCreatedI = 0
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView()
initListener()
onViewCreatedI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onViewCreated ${javaClass.simpleName} $onViewCreatedI")
}
protected open fun initView() {}
protected open fun initListener() {}
open fun onResumeOneShoot() = Unit
private var onResumeOneShootI = 0
private var onResumeI = 0
override fun onResume() {
onResumeI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onResume ${javaClass.simpleName} $onResumeI")
if (!fragmentInit) {
onResumeOneShoot()
onResumeOneShootI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onResumeOneShoot ${javaClass.simpleName} $onResumeOneShootI")
fragmentInit = true
}
super.onResume()
}
private var onDestroyViewI = 0
override fun onDestroyView() {
super.onDestroyView()
//要手动置null防止内存泄漏
_binding = null
onDestroyViewI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onDestroyView ${javaClass.simpleName} $onDestroyViewI")
}
private var onPauseI = 0
override fun onPause() {
super.onPause()
onPauseI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onPause ${javaClass.simpleName} $onPauseI")
}
private var onStopI = 0
override fun onStop() {
super.onStop()
onStopI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onStop ${javaClass.simpleName} $onStopI")
fragmentInit = false
}
private var onDestroyI = 0
override fun onDestroy() {
super.onDestroy()
onDestroyI++
// LogEx.logDebug(TAG, "lifecycle $tagNo onDestroy ${javaClass.simpleName} $onDestroyI")
}
}
inline fun <VB : ViewBinding> BaseFragment<VB>.viewBind(block: VB.() -> Unit) {
binding.apply(block)
}
inline fun Fragment.goToAc(clazz: Class<out Activity>, block: (Intent.() -> Unit) = {}) {
context?.startActivity(Intent(context, clazz).apply(block))
}
package com.koko.drouter.event
import android.app.Activity
import com.didi.drouter.api.DRouter
val eventService by lazy {
DRouter.build(EventService::class.java).getService()
}
interface EventService {
......
package com.koko.drouter.gobalConfig
import com.didi.drouter.api.DRouter
val globalConfigService by lazy {
DRouter.build(GlobalConfigService::class.java).getService()
}
interface GlobalConfigService {
fun getPackageName(): String
fun getApiUri(): String
fun getAppName(): String
}
\ No newline at end of file
package com.koko.drouter.sp
import com.didi.drouter.api.DRouter
val spService by lazy {
DRouter.build(SpService::class.java).getService()
}
interface SpService {
fun getBoolean(key: String): Boolean
fun putBoolean(key: String, value: Boolean)
fun getLong(key: String): Long
fun putLong(key: String, value: Long)
fun getInt(key: String): Int
fun putInt(key: String, value: Int)
fun getString(key: String): String
fun putString(key: String, value: String)
}
\ No newline at end of file
......@@ -81,11 +81,11 @@ dependencies {
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.navigation.fragment.ktx)
implementation(libs.androidx.navigation.ui.ktx)
implementation(project(":BatteryInfo"))
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation(project(":Drouter"))
implementation(project(":BatteryInfo"))
//网络请求
implementation("com.google.code.gson:gson:2.11.0")
......
package com.base.appzxhy.bean.config
class AdConfigBean(
var isAdShow: Boolean = false,//广告开关
var adSwitch: Boolean = true,//true 走admob,false走max
var numDisplayLimit: Int = -1,
var numRequestLimit: Int = -1,
......@@ -9,9 +10,9 @@ class AdConfigBean(
var openAdLoading: Int = 15,
var functionBackShowAd: Boolean = true,
var functionInShowAd: Boolean = true,
){
) {
companion object{
companion object {
/**
* 广告配置项目
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment