package com.lukouguoji.module_base.ktx

import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import java.lang.Exception

/**
 * @author：孟凡华
 * @date：2022/11/11
 * @desc：Context拓展
 */

/**
 * 根据Context来关闭Activity
 */
fun Context.finish() {
    if (this is Activity) {
        this.finish()
    }
}

/**
 * 获取context对应的Activity
 */
fun Context.getActivity(): Activity {
    if (this is Activity) {
        return this
    } else if (this is Fragment) {
        return requireActivity()
    }
    throw Exception("Context is not LifecycleOwner")
}

/**
 * 获取生命周期持有者
 */
fun Context.getLifecycleOwner(): LifecycleOwner {
    if (this is LifecycleOwner) {
        return this
    } else {
        throw Exception("Context is not LifecycleOwner")
    }
}