Last active
August 3, 2021 11:40
-
-
Save Turskyi/2148d562a92a19abac2fecb1661fdbce to your computer and use it in GitHub Desktop.
Convenient context extensions for kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tailrec fun Context.getActivity(): Activity? = when (this) { | |
is Activity -> this | |
else -> (this as? ContextWrapper)?.baseContext?.getActivity() | |
} | |
/** | |
* Starts the Activity [A], in a more concise way, while still allowing to configure the [Intent] in | |
* the optional [configIntent] lambda. | |
*/ | |
inline fun <reified A : Activity> Context.start(configIntent: Intent.() -> Unit = {}) { | |
startActivity(Intent(this, A::class.java).apply(configIntent)) | |
} | |
/** | |
* @Description | |
* Checks if device is online or not | |
*/ | |
fun Context.isOnline(): Boolean { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
val connectivityManager = | |
this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val network = connectivityManager.activeNetwork | |
return if (network != null) { | |
val networkCapabilities = connectivityManager.getNetworkCapabilities(network)!! | |
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) | |
|| networkCapabilities.hasTransport( | |
NetworkCapabilities.TRANSPORT_WIFI | |
) | |
} else false | |
} else { | |
/* Initial Value */ | |
var isConnected: Boolean? = false | |
val connectivityManager = | |
this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
@Suppress("DEPRECATION") val activeNetwork = connectivityManager.activeNetworkInfo | |
@Suppress("DEPRECATION") | |
if (activeNetwork != null && activeNetwork.isConnected) { | |
isConnected = true | |
} | |
return isConnected ?: false | |
} | |
} | |
/** show "toast" message */ | |
fun Context.toastLong(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_LONG).show() | |
fun Context.toast(@StringRes stringRes: Int) = Toast.makeText(this, stringRes, Toast.LENGTH_SHORT).show() | |
/** | |
* converts drawable vector resource to bitmap; | |
* returns nullable Bitmap (very important) | |
*/ | |
private fun Context.vectorToBitmap(vector: Int): Bitmap? { | |
val res = resources | |
return BitmapFactory.decodeResource(res, vector) | |
} | |
/** converts url to SpannableString */ | |
fun Context.convertPictureToSpannableString(imgRes: String): SpannableString { | |
val spannableString = SpannableString(" ") | |
val url = URL(imgRes) | |
Thread { | |
val image: Bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()) | |
val imageSpan = ImageSpan(this, image) | |
spannableString.setSpan(imageSpan, " ".length - 1, " ".length, 0) | |
}.start() | |
return spannableString | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment