Created
January 19, 2018 10:43
-
-
Save jeffersontpadua/d0ae18323d823833bf1e35fbc25ee896 to your computer and use it in GitHub Desktop.
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
//View Group Extensions | |
fun ViewGroup.inflate(@LayoutRes layout: Int, attachToRoot: Boolean = false) = | |
LayoutInflater.from(context).inflate(layout, this, attachToRoot) | |
//Context Extensions | |
fun Context.showSnackbar( | |
anchor: View, | |
@StringRes text: Int, | |
lenght: Int = Snackbar.LENGTH_LONG, | |
@StringRes actionName: Int? = null, action: () -> Unit = {} | |
) { | |
val snackbar = Snackbar.make(anchor, text, lenght) | |
if (actionName != null) { | |
snackbar.setAction(actionName) { | |
action() | |
} | |
} | |
snackbar.show() | |
} | |
fun Context.toast(@StringRes msg: Int, length: Int = Toast.LENGTH_LONG) { | |
Toast.makeText(this, getString(msg), length).show() | |
} | |
fun Context.showAlertDialog( | |
@StringRes title: Int, | |
@StringRes message: Int, | |
@StringRes positiveButtonText: Int = R.string.all_confirmar, | |
positiveButtonAction: (DialogInterface) -> Unit, | |
@StringRes negativeButtonText: Int = R.string.all_cancelar, | |
negativeButtonAction: (DialogInterface) -> Unit = { it.dismiss() } | |
) { | |
AlertDialog.Builder(this) | |
.setTitle(title) | |
.setMessage(message) | |
.setPositiveButton(positiveButtonText) { dialog, _ -> positiveButtonAction(dialog) } | |
.setNegativeButton(negativeButtonText, { dialog, _ -> negativeButtonAction(dialog) }) | |
.create() | |
.show() | |
} | |
//AppCompatActivity Extensions | |
fun <T> AppCompatActivity.show(dialogFragment: T) where T : DialogFragment, T : TaggedFragment { | |
dialogFragment.show(supportFragmentManager, dialogFragment.TAG) | |
} | |
fun AppCompatActivity.requestPermissionsCompat(permissions: Array<out String>, requestCode: Int) { | |
ActivityCompat.requestPermissions(this, permissions, requestCode) | |
} | |
fun AppCompatActivity.shouldExplainRequest(permissao: String) = | |
ActivityCompat.shouldShowRequestPermissionRationale(this, permissao) | |
fun AppCompatActivity.hasPermission(permissao: String) = | |
ContextCompat.checkSelfPermission(this, permissao) == PackageManager.PERMISSION_GRANTED | |
fun AppCompatActivity.withToolbar( | |
@IdRes id: Int = R.id.app_toolbar, | |
showHome: Boolean = true, | |
@DrawableRes homeIcon: Int = R.drawable.ic_menu_white_24dp | |
) { | |
setSupportActionBar(findViewById(id)) | |
supportActionBar?.apply { | |
setDisplayHomeAsUpEnabled(showHome) | |
setHomeAsUpIndicator(homeIcon) | |
} | |
} | |
fun AppCompatActivity.replaceFragment(@IdRes frameLayout: Int, fragment: Fragment) { | |
supportFragmentManager.beginTransaction() | |
.replace(frameLayout, fragment) | |
.commit() | |
} | |
//EditText Extensions | |
fun EditText.addSimpleTextWatcher(function: (String) -> Unit) { | |
this.addTextChangedListener(object : TextWatcher { | |
override fun afterTextChanged(p0: Editable?) { | |
} | |
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
} | |
override fun onTextChanged(seq: CharSequence, p1: Int, p2: Int, p3: Int) { | |
function(seq.toString()) | |
} | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment