Last active
April 28, 2021 08:41
-
-
Save addeeandra/70429fa345062680fcf7055072c18fe0 to your computer and use it in GitHub Desktop.
Kotlin Extensions of BottomNavigation, Calendar, EditText, View, InputStream, String, Context, Activity
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
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) |
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
import java.text.SimpleDateFormat | |
import java.util.* | |
fun Calendar.format(outFormat: String): String { | |
return SimpleDateFormat(outFormat, Locale.getDefault()).format(time) | |
} |
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
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 | |
) | |
} |
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
import android.widget.EditText | |
fun EditText.enable() { | |
isEnabled = true | |
} | |
fun EditText.disable() { | |
isEnabled = false | |
} | |
val EditText.value | |
get() = text.toString() |
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
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) | |
} |
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
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 | |
} | |
} |
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
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