Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Extension for Network connectivity check #160

Closed
jitinsharma opened this issue Feb 5, 2018 · 3 comments
Closed

Extension for Network connectivity check #160

jitinsharma opened this issue Feb 5, 2018 · 3 comments

Comments

@jitinsharma
Copy link
Contributor

This may seem specific but almost every project which does network call would be using this.

@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
fun Context.isConnected(): Boolean {
    val connectivityManager = this
            .getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
    connectivityManager?.let {
        val netInfo = it.activeNetworkInfo
        netInfo?.let {
            if (it.isConnected) return true
        }
    }
    return false
}
@JakeWharton
Copy link
Contributor

I do find this very specific, yes, and I'm struggling to justify such an extension on such a visible type. This would set a precedent for adding hundreds of extensions as convenience accessors onto context which would be difficult to maintain and would make it hard to say "no" in the future. Beyond that, the primary goal of this library is to add extensions which enable Kotlin-specific language features and helpers and not just moving access to existing things.

@jitinsharma
Copy link
Contributor Author

Ok.

@tobiasschuerg
Copy link

tobiasschuerg commented Feb 15, 2018

Would also prefer an isOnline()/isConnected function or similar to be in as this is something I'm having to add to any project anyways.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants