This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
/** USAGE -> ('a'..'z').randomString(6) */ | |
fun ClosedRange<Char>.randomString(lenght: Int) = | |
(1..lenght) | |
.map { (Random().nextInt(endInclusive.toInt() - start.toInt()) + start.toInt()).toChar() } | |
.joinToString("") |
This file contains hidden or 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
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) |
This file contains hidden or 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
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) { | |
} |
This file contains hidden or 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
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 |
This file contains hidden or 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
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: |
This file contains hidden or 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
/** 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 { |
This file contains hidden or 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
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 -> { |
This file contains hidden or 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
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) { | |
} |
This file contains hidden or 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
/** | |
* 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(). | |
* | |
* |