package com.convenient.android.common.extension

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import com.convenient.android.common.base.view.BaseActivity


/**
 * 移除所有fragment
 */
fun BaseActivity.removeAllFragments() {
    runCatching {
        supportFragmentManager.beginTransaction().apply {
            for (fragment in supportFragmentManager.fragments) remove(fragment)
            commitNow()
        }
    }
}

/**
 * 跳转界面并关闭当前页面
 * @param clazz 跳转的activity
 * @param extraData 携带的数据
 */
fun Context.readyGo(clazz: Class<*>, extraData : (Intent) -> Unit = {}) {
    Intent(this, clazz).apply {
        extraData.invoke(this)
        startActivity(this)
    }
}


/**
 * 跳转界面并关闭当前页面
 * @param clazz 跳转的activity
 * @param extraData 携带的数据
 */
fun AppCompatActivity.readyGoThenKill(clazz: Class<*>, extraData: (Intent) -> Unit = {}) {
    Intent(this, clazz).apply {
        extraData.invoke(this)
        startActivity(this)
    }
    finish()
}