package com.convenient.android.lib.util

import androidx.lifecycle.SavedStateHandle
import androidx.navigation.NavHostController

/**
 * @author: hubowen
 * @date: 2022/9/22
 * @description:
 */
/**
 * 返回指定的route并回调参数
 */
fun NavHostController.goRouteWithParams(
    route: String,
    autoPop: Boolean = true,
    callback: (SavedStateHandle.() -> Unit)? = null,
) {
    val entry = getBackStackEntry(route)
    entry.savedStateHandle.let {
        callback?.invoke(it)
    }
    if (autoPop) {
        popBackStack()
    }
}

/**
 * 回到上级页面，并回调参数
 */
fun NavHostController.goBackWithParams(
    autoPop: Boolean = true,
    callback: (SavedStateHandle.() -> Unit)? = null,
) {
    previousBackStackEntry?.savedStateHandle?.let {
        callback?.invoke(it)
    }
    if (autoPop) {
        popBackStack()
    }
}
