package cn.muchinfo.rma.lifecycle



import android.os.Build
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import org.jetbrains.anko.AnkoContextImpl
import kotlin.reflect.KClass


inline fun <reified T : ViewModel> Fragment.getViewModel(): T {
    return ViewModelProviders.of(this).get(T::class.java)
}

inline fun <reified T : ViewModel> FragmentActivity.getViewModel(): T {
    return ViewModelProviders.of(this).get(T::class.java)
}

fun <T : ViewModel> FragmentActivity.getViewModel(clz: KClass<T>): T {
    return ViewModelProviders.of(this).get(clz.java)
}

fun Fragment.ui(): AnkoContextImpl<Fragment> {
    return AnkoContextImpl(activity!!, this, false)
}

fun Fragment.fitsSystemWindows(fitsSystemWindows: Boolean) {
    view?.let {
        it.fitsSystemWindows = fitsSystemWindows
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            it.requestApplyInsets()
        }
    }
}
