抽模塊的回調(diào)處理

抽模塊的回調(diào)處理

1. 創(chuàng)建回調(diào)接口 (core-ui 模塊)

文件位置: core-ui/../NavigationCallback.kt

// 頁(yè)面跳轉(zhuǎn)回調(diào)
interface NavigationCallback {
    fun navigateToSuccess()
    fun navigateToSuccessWithOrder(orderData: Any?)
}

// 應(yīng)用狀態(tài)管理
interface AppStateManager {
    fun getTimeoutSecond(): Int
    fun isFormSubmitted(): Boolean
    fun setFormSubmitted(submitted: Boolean)
    fun getShrimpChannelConcurrency(): List<Int>?
}

// 回調(diào)管理器
object CallbackManager {
    private var navigationCallback: NavigationCallback? = null
    private var appStateManager: AppStateManager? = null
    
    fun setNavigationCallback(callback: NavigationCallback)
    fun getNavigationCallback(): NavigationCallback?
    fun setAppStateManager(manager: AppStateManager)
    fun getAppStateManager(): AppStateManager?
}

2. 創(chuàng)建接口實(shí)現(xiàn) (app 模塊)

文件位置: app/../AppCallbackImpl.kt

// 導(dǎo)航回調(diào)實(shí)現(xiàn)
class AppNavigationCallback(private val activity: Activity) : NavigationCallback {
    override fun navigateToSuccess() {
        MyApplication.isForm = true
        activity.startActivity(Intent(activity, WmSuccessActivity::class.java))
        activity.setResult(AppCompatActivity.RESULT_OK)
        activity.finish()
    }
    
    override fun navigateToSuccessWithOrder(orderData: Any?) {
        MyApplication.isForm = true
        val intent = Intent(activity, WmSuccessActivity::class.java)
        intent.putExtra("orderData", orderData)
        activity.startActivity(intent)
        activity.setResult(AppCompatActivity.RESULT_OK)
        activity.finish()
    }
}

// 應(yīng)用狀態(tài)管理實(shí)現(xiàn)
class AppStateManagerImpl : AppStateManager {
    override fun getTimeoutSecond(): Int = MyApplication.timeout_second
    override fun isFormSubmitted(): Boolean = MyApplication.isForm
    override fun setFormSubmitted(submitted: Boolean) {
        MyApplication.isForm = submitted
    }
    override fun getShrimpChannelConcurrency(): List<Int>? {
        return MyApplication.shrimp_channel_concurrency
    }
}

3. 修改 (core-ui 模塊)

修改前:

var OVERTIME: Long = (MyApplication.timeout_second * 1000).toLong()

fun goSuccessActivity(context: Activity) {
    MyApplication.isForm = true
    context.startActivity(Intent(context, WmSuccessActivity::class.java))
    context.setResult(AppCompatActivity.RESULT_OK)
    context.finish()
}

val codeList = MyApplication.shrimp_channel_concurrency

修改后:

var OVERTIME: Long = ((CallbackManager.getAppStateManager()?.getTimeoutSecond() ?: 10) * 1000).toLong()

fun goSuccessActivity(context: Activity) {
    CallbackManager.getAppStateManager()?.setFormSubmitted(true)
    CallbackManager.getNavigationCallback()?.navigateToSuccess()
}

val codeList = CallbackManager.getAppStateManager()?.getShrimpChannelConcurrency()

?? 使用方法

Activity 中初始化回調(diào)

class QuanLiuChengFormLoanActivity : CommonActivity<UserViewModel, ZxdFormOneLayoutBinding>() {
    
    override fun init() {
        // 1. 設(shè)置回調(diào)(必須在使用 ViewModel 之前)
        CallbackManager.setNavigationCallback(AppNavigationCallback(this))
        CallbackManager.setAppStateManager(AppStateManagerImpl())
        
        // 2. 其他初始化代碼
       
    }
    
    override fun onDestroy() {
        super.onDestroy()
        // 清理回調(diào)(可選)
        CallbackManager.setNavigationCallback(null)
        CallbackManager.setAppStateManager(null)
    }
}

在其他使用 QuanLiuChengFormViewModel 的地方

任何使用 QuanLiuChengFormViewModel 的 Activity 都需要設(shè)置回調(diào):

class SomeActivity : CommonActivity<UserViewModel, SomeBinding>() {
    
    private val formViewModel by lazy { QuanLiuChengFormViewModel() }
    
    override fun init() {
        // 設(shè)置回調(diào)
        CallbackManager.setNavigationCallback(AppNavigationCallback(this))
        CallbackManager.setAppStateManager(AppStateManagerImpl())
        
        // 使用 ViewModel
        formViewModel.someMethod(...)
    }
}

?? 優(yōu)勢(shì)

1. 解耦

  • ? core-ui 模塊不再依賴 app 模塊
  • ? 可以在不同的 app 中復(fù)用 core-ui

2. 靈活性

  • ? 可以在不同的 app 中提供不同的實(shí)現(xiàn)
  • ? 易于測(cè)試(可以提供 Mock 實(shí)現(xiàn))

3. 可維護(hù)性

  • ? 職責(zé)清晰,接口定義明確
  • ? 修改跳轉(zhuǎn)邏輯只需修改實(shí)現(xiàn)類
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容