Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
EmmanuelGuther / build.gradle
Created July 16, 2019 07:55
Function to increase the versionCode automatically
private Object increaseVersionCode() {
Properties versionProps = new Properties()
def versionPropsFile = file('version.properties')
if (versionPropsFile.exists())
versionProps.load(new FileInputStream(versionPropsFile))
def code = (versionProps['VERSION_CODE'] ?: "410").toInteger() + 1
versionProps['VERSION_CODE'] = code.toString()
versionProps.store(versionPropsFile.newWriter(), null)
return code
}
@EmmanuelGuther
EmmanuelGuther / RandomString.kt
Created July 18, 2019 09:48
Kotlin function random string
/** USAGE -> ('a'..'z').randomString(6) */
fun ClosedRange<Char>.randomString(lenght: Int) =
(1..lenght)
.map { (Random().nextInt(endInclusive.toInt() - start.toInt()) + start.toInt()).toChar() }
.joinToString("")
@EmmanuelGuther
EmmanuelGuther / GenerateRandomLocationInsideRadius.kt
Created July 18, 2019 09:53
Kotlin generate random location inside radius
fun generateRandomLocationInsideRadius(x0: Double, y0: Double, radius: Int): LatLng {
val random = Random()
// Convert radius from meters to degrees
val radiusInDegrees = (radius / 111000f).toDouble()
val u = random.nextDouble()
val v = random.nextDouble()
val w = radiusInDegrees * sqrt(u)
val t = 2.0 * Math.PI * v
val x = w * cos(t)
val y = w * sin(t)
fun TextView.afterTextChangedDelayed(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
var timer: CountDownTimer? = null
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
@EmmanuelGuther
EmmanuelGuther / TextView.showCountIncrementAnimation.kt
Created August 1, 2019 10:29
A kotlin extension to set numbers in textview one by one
fun TextView.showCountIncrementAnimation(from: Int? = 0, to: Int?) {
var duration: Long? = null
val animator = ValueAnimator()
animator.setObjectValues(from, to)// here you set the range, from 0 to "count" value
animator.addUpdateListener { animation -> this.text = animation.animatedValue.toString() }
duration = when (to) {
in 0..4 -> 200
in 5..10 -> 1000
in 11..50 -> 3000
else -> 2000
@EmmanuelGuther
EmmanuelGuther / ElvisOperatorExample.kt
Created August 22, 2019 11:41
Kotlin elvis operator example
Elvis Operator
When we have a nullable reference r, we can say "if r is not null, use it, otherwise use some non-null value x":
val l: Int = if (b != null) b.length else -1
Along with the complete if-expression, this can be expressed with the Elvis operator, written ?::
val l = b?.length ?: -1
If the expression to the left of ?: is not null, the elvis operator returns it, otherwise it returns the expression to the right. Note that the right-hand side expression is evaluated only if the left-hand side is null.
Note that, since throw and return are expressions in Kotlin, they can also be used on the right hand side of the elvis operator. This can be very handy, for example, for checking function arguments:
@EmmanuelGuther
EmmanuelGuther / LockeableSwipeViewPager.kt
Created October 18, 2019 10:54
A viewpager with possibilities of being blocked the swipe
/** A viewpager with possibilities of being blocked the swipe */
class BViewPager(context: Context, attrs: AttributeSet) : ViewPager(context, attrs) {
private var enabledSwipe: Boolean = false
init {
this.enabledSwipe = true
}
override fun onTouchEvent(event: MotionEvent): Boolean = when {
@EmmanuelGuther
EmmanuelGuther / LuhnValidator.kt
Created October 25, 2019 11:53
KOTLIN function to validate credit card numbers
class LuhnValidator {
fun isValid(cardNumber: String): Boolean {
var s1 = 0
var s2 = 0
val reverse = StringBuffer(cardNumber).reverse().toString()
for (i in reverse.indices) {
val digit = Character.digit(reverse[i], 10)
when {
i % 2 == 0 -> s1 += digit
else -> {
@EmmanuelGuther
EmmanuelGuther / CreditCardUtils.kt
Created October 25, 2019 12:01
A collection to kotlin extension functions to manage different credit card
const val CARD_NUMBER_SEPARATOR = " "
const val CARD_DATE_SEPARATOR = "/"
fun EditText.creditCardNumberFormatter(afterTextChanged: (String) -> Unit) {
var count = 0
this.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
@EmmanuelGuther
EmmanuelGuther / SingleLiveEvent.kt
Created October 25, 2019 18:20
A lifecycle-aware observable that sends only new updates after subscription, used for events like navigation and Snackbar messages. This avoids a common problem with events: on configuration change (like rotation) an update can be emitted if the observer is active. This LiveData only calls the observable if there's an explicit call to setValue()…
/**
* A lifecycle-aware observable that sends only new updates after subscription, used for events like
* navigation and Snackbar messages.
*
*
* This avoids a common problem with events: on configuration change (like rotation) an update
* can be emitted if the observer is active. This LiveData only calls the observable if there's an
* explicit call to setValue() or call().
*
*