Skip to content

Instantly share code, notes, and snippets.

@asadfareed777
Created March 31, 2023 06:39
Show Gist options
  • Save asadfareed777/7f67f033c14b006e6b900de650774471 to your computer and use it in GitHub Desktop.
Save asadfareed777/7f67f033c14b006e6b900de650774471 to your computer and use it in GitHub Desktop.
import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.content.res.Resources
import android.util.Log
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.core.content.ContentProviderCompat.requireContext
import com.google.android.material.snackbar.Snackbar
import java.text.SimpleDateFormat
import java.util.*
/**
* Created by Asad Fareed on 3/31/2023
*/
/**
* Print to Logcat
* Usage
* val text = "This is text"
* text.printToLog()
*/
fun Any?.printToLog(tag: String = "DEBUG_LOG") {
if (BuildConfig.DEBUG) {
Log.d(tag, toString())
}
}
/**
* View Visibility
* Usage
* view.gone()
* view.visible()
* view.invisible()
* // dataFound, loading, and condition should be a valid boolean expression
* view goneIf dataFound
* view visibleIf loading
* view invisibleIf condition
*/
fun View.gone() = run { visibility = View.GONE }
fun View.visible() = run { visibility = View.VISIBLE }
fun View.invisible() = run { visibility = View.INVISIBLE }
infix fun View.visibleIf(condition: Boolean) =
run { visibility = if (condition) View.VISIBLE else View.GONE }
infix fun View.goneIf(condition: Boolean) =
run { visibility = if (condition) View.GONE else View.VISIBLE }
infix fun View.invisibleIf(condition: Boolean) =
run { visibility = if (condition) View.INVISIBLE else View.VISIBLE }
/*fun Fragment.toast(message: String) {
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()
}
fun Fragment.toast(@StringRes message: Int) {
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()
}*/
/**
* Fragment Toast Message
* Usage
* requireActivity().toast("Hello")
* requireContext().toast("World")
*/
fun Context.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
fun Context.toast(@StringRes message: Int) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
/**
* Activity Toast Message
* Usage
* toast("This is toast message")
* toast(R.string.toast_message)
*/
fun Activity.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
fun Activity.toast(@StringRes message: Int) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
/**
* SnackBar Message
* Dependency
* implementation 'com.google.android.material:material:1.8.0'
* Usage
* rootView.snackBar("This is snackBar message")
* rootView.snackBar(R.string.snackBar)
*/
fun View.snackBar(message: String, duration: Int = Snackbar.LENGTH_LONG) {
Snackbar.make(this, message, duration).show()
}
fun View.snackBar(@StringRes message: Int, duration: Int = Snackbar.LENGTH_LONG) {
Snackbar.make(this, message, duration).show()
}
/**
* Hide Keyboard
* Usage
* hideKeyboard()
*/
fun Activity.hideKeyboard() {
val imm: InputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: View(this)
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
fun Fragment.hideKeyboard() {
activity?.apply {
val imm: InputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: View(this)
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
/**
* dp and px conversion
* Usage
* params.setMargins(16.px, 16.px, 16.px, 16.px)
*/
// Convert px to dp
val Int.dp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
//Convert dp to px
val Int.px: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
/**
* Digit, Alphabetic, and Alphanumeric Check
* Usage
* val isValidNumber = "1234".isDigitOnly // Return true
* val isValid = "1234abc".isDigitOnly // Return false
* val isOnlyAlphabetic = "abcABC".isAlphabeticOnly // Return true
* val isOnlyAlphabetic2 = "abcABC123".isAlphabeticOnly // Return false
* val isOnlyAlphanumeric = "abcABC123".isAlphanumericOnly // Return true
* val isOnlyAlphanumeric2 = "abcABC@123.".isAlphanumericOnly // Return false
*/
val String.isDigitOnly: Boolean
get() = matches(Regex("^\\d*\$"))
val String.isAlphabeticOnly: Boolean
get() = matches(Regex("^[a-zA-Z]*\$"))
val String.isAlphanumericOnly: Boolean
get() = matches(Regex("^[a-zA-Z\\d]*\$"))
/**
* isNull
* Usage
* if (obj.isNull) {
* // Run if object is null
* } else {
* // Run if object is not null
* }
*/
val Any?.isNull get() = this == null
/**
* ifNull
* Usage
* obj.ifNull {
* // Write code
* }
*/
fun Any?.ifNull(block: () -> Unit) = run {
if (this == null) {
block()
}
}
/**
* Date Formatter
* Usage
* val currentDate = Date().toStringFormat()
* val currentDate2 = Date().toStringFormat(format = "dd-MM-yyyy")
* val date = "2023-01-01".toDate(format = "yyyy-MM-dd")
*/
fun String.toDate(format: String = "yyyy-MM-dd HH:mm:ss"): Date? {
val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
return dateFormatter.parse(this)
}
fun Date.toStringFormat(format: String = "yyyy-MM-dd HH:mm:ss"): String {
val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
return dateFormatter.format(this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment