code
### check permission ``` fun Activity.hasPermission(permission: String): Boolean { return ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED } ```code
```kotlin fun View.visible() { changeViewVisibility(View.VISIBLE) } ```fun View.gone() {
changeViewVisibility(View.GONE) }
fun View.invisible() {
changeViewVisibility(View.INVISIBLE) }
private fun View.changeViewVisibility(newState: Int) {
visibility = newState }
code
fun View.showSmallSnackbar(text: String) { showSnackbar(text, Snackbar.LENGTH_SHORT) }
fun View.showLongSnackbar(text: String) { showSnackbar(text, Snackbar.LENGTH_LONG) }
fun View.showIndefiniteSnackbar(text: String) { showSnackbar(text, Snackbar.LENGTH_INDEFINITE) }
private fun View.showSnackbar(text: String, duration: Int) { Snackbar.make(this, text, duration).show() }
code
- To add a fragment using
commitNow()
.
inline fun < reified T: Fragment> FragmentActivity.addFragmentNow(id: Int) {
val fragment = T::class.java
supportFragmentManager.beginTransaction().add(id, fragment.newInstance()).commitNow()
}
- To replace a fragment using
commitNow()
.
inline fun < reified T: Fragment> FragmentActivity.replaceFragmentNow(id: Int) {
val fragment = T::class.java
supportFragmentManager.beginTransaction().replace(id, fragment.newInstance()).commitNow()
}
- To add a fragment using
commit()
.
inline fun < reified T: Fragment> FragmentActivity.addFragment(id: Int) {
val fragment = T::class.java
supportFragmentManager.beginTransaction().add(id, fragment.newInstance()).commit()
}
- To replace a fragment using
commit()
.
inline fun < reified T: Fragment> FragmentActivity.replaceFragment(id: Int) {
val fragment = T::class.java
supportFragmentManager.beginTransaction().replace(id, fragment.newInstance()).commit()
}
code
fun Context.showSmallLengthToast(text: String) { showToast(text, Toast.LENGTH_SHORT) }
fun Context.showLongLengthToast(text: String) { showToast(text, Toast.LENGTH_LONG) }
private fun Context.showToast(text:String, duration: Int) { Toast.makeText(this, text, duration).show() }
</details>
code
fun ViewModel.loadRequest(context:Context, block: suspend () -> Unit): Job {
return viewModelScope.launch {
try {
block()
}catch (ex: Exception) {
Toast.makeText(context, ex.message, Toast.LENGTH_LONG).show()
}
}
}
suspend fun <T:Any> ControlledRunner<T>.cancelLastThenRun(block: suspend () -> T): T {
return cancelPreviousThenRun(block)
}
suspend fun <T:Any> ControlledRunner<T>.addLastThenRun(block: suspend () -> T): T {
return joinPreviousOrRun(block)
}
</details>
code
package com.himanshu.customktx.extensions.activity
import android.app.Activity import android.content.Intent import android.os.Bundle import android.widget.EditText import android.widget.TextView import androidx.annotation.IdRes import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity
/**
- Extensions used on views using ResId. */ fun Activity.findAndSetTextInTextView(@IdRes id: Int, text: String) { findViewById(id).text = text }
fun Activity.findAndSetTextInEditText(@IdRes id: Int, text: String) { findViewById(id).setText(text) }
/**
- Explicit Intent Extensions. */
/**
shouldGoTo
is used to- start an intent without any
- extra Bundle(). */ inline fun Activity.shouldGoTo() { makeAnIntent() }
/**
shouldFinishAndGoTo()
finishes the existingActivity
after the intent is fired. */ inline fun Activity.shouldFinishAndGoTo() { makeAnIntent() finishItOff() }
/**
shouldGoWithDataTo
is used to- fire an intent with a
- Bundle(). */ inline fun Activity.shouldGoWithDataTo(key:String, bundle: Bundle) { makeAnIntent(key, bundle) }
/**
shouldFinishAndGoWithDataTo
is used to- finish the existing
Activity
after an - intent is fired with
Bundle
. */ inline fun Activity.shouldFinishAndGoWithDataTo(key:String, bundle: Bundle) { makeAnIntent(key, bundle) finishItOff() }
/**
- Creates an intent with an
Activity
. */ inline fun Activity.makeAnIntent(key:String? = null, bundle: Bundle? = null) { val intent = Intent(this, T::class.java) intent.putExtra(key, bundle) startActivity(intent) }
/**
-
Finishes an
Activity
. */ fun Activity.finishItOff() { finish() }