View Activity Extension
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
inline fun <reified T:AppCompatActivity> AppCompatActivity.startActivity() = | |
Intent (this, T::class.java) |
View Activity Extension with intent
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
inline fun <reified T : AppCompatActivity> Activity.startActivityEtx() = | |
Intent(this, T::class.java) | |
.apply { | |
startActivity(this) | |
} |
View Context - hasNetwork
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
fun Context.hasNetwork(): Boolean { | |
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
val activeNetwork = connectivityManager.activeNetwork ?: return false | |
val capabilities = connectivityManager.getNetworkCapabilities(activeNetwork) ?: return false | |
return when { | |
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true | |
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true | |
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true | |
else -> false |
View try and catch extension
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
inline fun <reified E : Throwable> tryAndCatch(blockTry: () -> Unit, blockCatch: (E) -> Unit) { | |
try { | |
blockTry() | |
} catch (e: Exception) { | |
blockCatch(e as E) | |
} | |
} |
View Edittext AddTextChangeListener
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
fun EditText.onTextChanged(listener: (String) -> Unit) { | |
this.addTextChangedListener(object : TextWatcher { | |
override fun afterTextChanged(s: Editable?) { listener(s.toString()) } | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} | |
}) | |
} |
View EditText onDone
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
fun EditText.onDone(callback: (String) -> Unit) { | |
setOnEditorActionListener { _, actionId, _ -> | |
if (actionId == EditorInfo.IME_ACTION_DONE) { | |
callback.invoke(text.toString()) | |
true | |
} | |
false | |
} | |
} |
View when exhaustive
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
val <T> T.makeExhaustive: T | |
get() = this |
View hasPermissions
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
fun AppCompatActivity.hasPermissions(vararg permissions: String): Boolean { | |
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) | |
permissions.all { singlePermission -> | |
applicationContext.checkSelfPermission(singlePermission) == PackageManager.PERMISSION_GRANTED | |
} | |
else true |
View requestPermissions
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
fun Activity.requestPermissions(vararg permissions: String, @IntRange(from = 0) requestCode: Int) = | |
ActivityCompat.requestPermissions(this, permissions, requestCode) |
View ofType extension
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
inline fun <reified T> Any.ofType(block: (T) -> Unit) { | |
if (this is T) { | |
block(this as T) | |
} | |
} |
OlderNewer