<commit_before>package com.mcxiaoke.koi.async

import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.os.Looper
import java.lang.ref.WeakReference
import android.support.v4.app.Fragment as SupportFragment

/**
 * User: mcxiaoke
 * Date: 16/1/31
 * Time: 14:19
 */


interface Detachable {
    fun isDetached(): Boolean
}

class WeakContext<T>(val weakRef: WeakReference<T>) {

    fun sleep(time: Long) = Thread.sleep(time)

    fun getContext(): T = weakRef.get()

    fun isContextAlive(): Boolean {
        val context = weakRef.get() ?: return false
        return isContextAlive(context)
    }
}

//*******************************************************************//
//    Execute Task on Main Thread
//*******************************************************************//

fun isMainThread(): Boolean = Looper.myLooper() == Looper.getMainLooper()

fun <T> isContextAlive(context: T?): Boolean {
    return when (context) {
        null -> false
        is Activity -> !context.isFinishing<commit_msg>add need check to WeakContext<commit_after>package com.mcxiaoke.koi.async

import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.os.Looper
import java.lang.ref.WeakReference
import android.support.v4.app.Fragment as SupportFragment

/**
 * User: mcxiaoke
 * Date: 16/1/31
 * Time: 14:19
 */


interface Detachable {
    fun isDetached(): Boolean
}

class WeakContext<T>(val weakRef: WeakReference<T>) {
    protected val needCheck: Boolean

    init {
        val ctx = weakRef.get()
        needCheck = when (ctx) {
            is Activity -> true
            is Fragment -> true
            is SupportFragment -> true
            is Detachable -> true
            else -> false
        }
    }

    fun sleep(time: Long) = Thread.sleep(time)

    fun getContext(): T = weakRef.get()

    fun isContextAlive(): Boolean {
        val context = weakRef.get() ?: return false
        return if (needCheck) isContextAlive(context) else true
    }
}

//*******************************************************************//
//    Execute Task on Main Thread
//*******************************************************************//

fun isMainThread(): Boolean = Looper.myLooper() == Looper.getMainLooper()

fun <T> isContextAlive(context: T?): Boolean {
    return when (context) {
        null -> false
        is Activity -> !context.isFinishing
{'fraction_non_alphanumeric': 0.21358428805237317, 'fraction_numerical': 0.008183306055646482, 'mean_word_length': 3.3197879858657244, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2365117', 'n_tokens_mistral': 712, 'n_tokens_neox': 666, 'n_words': 176}