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
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
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 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
/** 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
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
val fiveMinutes = (1000 * 60 * 5).toLong() //5m in milliseconds |
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 Long.checkTimeIsOldThan(days: Int): Boolean { | |
val daysInMillis = TimeUnit.DAYS.toMillis(days.toLong()) | |
val currentTime = Date().time | |
val previousTime = this | |
val differ = currentTime - previousTime | |
return !(differ < daysInMillis && differ > -daysInMillis) | |
} |
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
--------------------------- ----- ------------ --------------- ------- ----------- ---------------- --- ---------- | |
Device Inches ResolutionPX Density DPI ResolutionDP AspectRatios SysNavYorN ContentResolutionDP | |
--------------------------- ----- ------------ --------------- ------- ----------- ---------------- --- ---------- | |
Galaxy Y 320 x 240 ldpi 0.75 120 427 x 320 4:3 1.3333 427 x 320 | |
? 400 x 240 ldpi 0.75 120 533 x 320 5:3 1.6667 533 x 320 | |
? 432 x 240 ldpi 0.75 120 576 x 320 9:5 1.8000 576 x 320 | |
Galaxy Ace 480 x 320 mdpi 1 160 480 x 320 3:2 1.5000 |
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
internal fun handleChatMessages(messages: List<MessageModel>?) { | |
hideProgress() | |
adapter.collection = messages.orEmpty() | |
animate { runLayoutAnimation() } | |
} | |
private fun animate(function: () -> Unit) { | |
when { | |
firstTimeCreated -> function.invoke() | |
} |