Skip to content

Instantly share code, notes, and snippets.

val selectedSubcategories = selectedCategories.asSequence().map { it?.subCategories }.fold(mutableSetOf<SubCategoryModel>()) { acc, list ->
list?.asSequence()?.filter { it.checked }?.map { acc.add(it) }?.toList()
acc
}
val subcategoriesJoined = selectedSubcategories.asSequence().map { it.name }.joinToString(separator = " • ")
editTextProfessionalRegisterCategories.setText(subcategoriesJoined)
@EmmanuelGuther
EmmanuelGuther / glidePlaceholderExample.kt
Created October 2, 2018 12:04
Glide placeholder example kotlin
fun example(){
Glide.with(context)
.load(items[position])
.apply(RequestOptions()
.placeholder(R.drawable.animated_loading_icon)
)
@EmmanuelGuther
EmmanuelGuther / DateUtilsFormatDateRange.java
Created October 8, 2018 11:48
Date and time formatting
long lastYear = 1407869895000L; // August 12, 2014, 8:58PM
long before = 1439405895000L; // August 12, 2015, 8:58PM
long now = 1442343495000L; // September 15, 2015, 8:58PM
// August 12 – September 15 (default)
DateUtils.formatDateRange(this, before, now, 0);
// August 12, 8:58PM – September 15, 8:58PM (with time)
DateUtils.formatDateRange(this, before, now, DateUtils.FORMAT_SHOW_TIME);
@EmmanuelGuther
EmmanuelGuther / getFirstElementIfCondition.kt
Created October 16, 2018 12:09
Get first element in collection if the condition is true
listFoo?.singleOrNull { it.id == "oooo" }.let {it...}
/** USAGE -> ('a'..'z').randomString(6) */
fun ClosedRange<Char>.randomString(lenght: Int) =
(1..lenght)
.map { (Random().nextInt(endInclusive.toInt() - start.toInt()) + start.toInt()).toChar() }
.joinToString("")
editTextRegisterStepThreeUserName.setText(('a'..'z').randomString(6))
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 / listHaveElementSpecificId.kt
Last active October 26, 2018 00:57
To check if a list have contains an element that have specific “id”, we can use any functions like this:
fun foo(){
myList.any { it -> it.id == valueToCheck }
}
semicircleHeader?.animate()?.scaleX(1f)?.scaleY(1f)?.setDuration(100)?.start()
@EmmanuelGuther
EmmanuelGuther / AnonymousFunUseExample.kt
Created October 29, 2018 20:28
notifyWhitAction waits as a second argument for a function, and for something as simple as closing an activity we are not going to create a function ... we use an anonymous one.
when (layoutEditableMode) {
true -> notifyWithAction(message, (R.string.exit), { fun() = activity?.finish() }, circleImageViewProfessionalProfileEditable)
false -> notifyWithAction(message, R.string.refresh, ::loadData, circleImageViewProfessionalProfile)
}
@EmmanuelGuther
EmmanuelGuther / BreakALoop.kt
Created November 6, 2018 10:59
how to break a loop
run breaker@{
collectionModified.forEach { itC ->
when {
!itC.checked && selectedCount >= 3 -> {maxSizeListener(); return@breaker}
else -> {
when {
itC.id == category.id -> {
itC.checked = itemView.switchCategorySelector.isChecked
when {
!itemView.switchCategorySelector.isChecked -> itC.subCategories?.map { it.checked = false }