Skip to content

Instantly share code, notes, and snippets.

@addeeandra
Last active April 28, 2021 08:41
Show Gist options
  • Save addeeandra/70429fa345062680fcf7055072c18fe0 to your computer and use it in GitHub Desktop.
Save addeeandra/70429fa345062680fcf7055072c18fe0 to your computer and use it in GitHub Desktop.
Kotlin Extensions of BottomNavigation, Calendar, EditText, View, InputStream, String, Context, Activity
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.ViewModelProvider
import android.app.Activity
import android.content.Intent
inline fun <reified T : Any> Activity.openAndFinish(extras: Intent.() -> Unit = {}) {
open<T>(extras)
finish()
}
inline fun <reified T : Any> Activity.openForResult(requestCode: Int = 20, extras: Intent.() -> Unit = {}) {
startActivityForResult(Intent(this, T::class.java).apply { extras() }, requestCode)
}
/**
* If you use ViewModel
*/
fun <T : ViewModel> AppCompatActivity.obtainViewModel(
model: KClass<T>,
viewModelFactory: ViewModelProvider.Factory? = null
) = ViewModelProviders.of(this, viewModelFactory).get(model.java)
import android.annotation.SuppressLint
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
import android.util.Log
/**
* Disable shifting mode on Bottom Navigation View
*
* Source of the issue,
* https://stackoverflow.com/questions/40176244/how-to-disable-bottomnavigationview-shift-mode
*/
@SuppressLint("RestrictedApi")
fun BottomNavigationView.disableShiftMode() {
val menuView = this.getChildAt(0) as BottomNavigationMenuView
try {
val shiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
shiftingMode.isAccessible = true
shiftingMode.setBoolean(menuView, false)
shiftingMode.isAccessible = false
for (i in 0 until menuView.childCount) {
val item = menuView.getChildAt(i) as BottomNavigationItemView
item.setShiftingMode(false)
item.setChecked(item.itemData.isChecked)
}
} catch (e: NoSuchFieldException) {
Log.e("BNVHelper", "Unable to get shift mode field", e)
} catch (e: IllegalAccessException) {
Log.e("BNVHelper", "Unable to change value of shift mode", e)
}
}
import java.text.SimpleDateFormat
import java.util.*
fun Calendar.format(outFormat: String): String {
return SimpleDateFormat(outFormat, Locale.getDefault()).format(time)
}
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.core.app.ActivityCompat.startActivityForResult
import kotlin.reflect.KClass
fun Context.toast(text: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, text, duration).show()
}
inline fun <reified T : Any> Context.open(extras: Intent.() -> Unit = {}) {
startActivity(Intent(this, T::class.java).apply { extras() })
}
inline fun <reified T : Any> Context.openAsNewTask(extras: Intent.() -> Unit = {}) {
open<T> {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
extras()
}
}
fun <T : Any> Context.openForResult(
activity: Activity,
target: KClass<T>,
requestCode: Int = 20,
bundle: Bundle? = null,
extras: Intent.() -> Unit = {}
) {
startActivityForResult(
activity,
Intent(this, target.java).apply { extras() },
requestCode,
bundle
)
}
import android.widget.EditText
fun EditText.enable() {
isEnabled = true
}
fun EditText.disable() {
isEnabled = false
}
val EditText.value
get() = text.toString()
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.support.media.ExifInterface
import java.io.InputStream
/**
* Fix exif orientation of image taken from Camera or Gallery
* on some devices
*
* Source of the issue,
* https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a
*/
fun InputStream.getExifBitmap(filename: String? = null): Bitmap {
val decodedBitmap = BitmapFactory.decodeStream(this)
val exif = if (filename == null) ExifInterface(this) else ExifInterface(filename)
val exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
val degreeRotation = when (exifRotation) {
ExifInterface.ORIENTATION_ROTATE_90 -> 90
ExifInterface.ORIENTATION_ROTATE_180 -> 180
ExifInterface.ORIENTATION_ROTATE_270 -> 270
else -> 0
}
val matrix = Matrix()
if (exifRotation != 0) matrix.preRotate(degreeRotation.toFloat())
return Bitmap.createBitmap(decodedBitmap, 0, 0, decodedBitmap.width, decodedBitmap.height, matrix, true)
}
import java.text.SimpleDateFormat
import java.util.*
fun String.dateFormat(outFormat: String, inFormat: String = "yyyy-MM-dd HH:mm:ss"): String {
return SimpleDateFormat(inFormat, Locale.getDefault()).run {
val date = parse(this@dateFormat)
applyPattern(outFormat)
date?.let { format(it) }.orEmpty()
}
}
fun String.isValidDateFormat(checkFormat: String): Boolean {
return try {
SimpleDateFormat(checkFormat, Locale.getDefault()).parse(this)
true
} catch (e: ParseException) {
false
}
}
import android.view.View
fun View.visible() {
if (!isVisible) visibility = View.VISIBLE
}
fun View.gone() {
if (!isGone) visibility = View.GONE
}
fun View.invisible() {
if (!isInvisible) visibility = View.INVISIBLE
}
val View.isVisible
get() = visibility == View.VISIBLE
val View.isInvisible
get() = visibility == View.INVISIBLE
val View.isGone
get() = visibility == View.GONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment