Skip to content

Instantly share code, notes, and snippets.

{
"uid": "1309SS",
"userType": "single"
}
@EmmanuelGuther
EmmanuelGuther / RecomendedImageSizesForMobile.md
Last active July 10, 2020 20:10
How should an image url be in the api? Well it should be an object of urls images not just a url recommended image dimensions for mobile app

How should an image url be in the api? Well it should be an object of urls images not just a url, so we can have different sizes.

Recommended image dimensions for mobile app

Thumb (~32x48) /images/xs/1-a-bomb.jpg

Small (~160x240) /images/sm/1-a-bomb.jpg

Medium (~320x480) /images/md/1-a-bomb.jpg

@EmmanuelGuther
EmmanuelGuther / FunctionAsParameterThatInTurnReceivesParameter.kt
Last active February 21, 2020 21:05
Kotlin Higher-Order Function -- pass a function as a parameter that in turn receives a parameter
fun launchEmail(activity: FragmentActivity, subject: String, text: String) {
val i = Intent(Intent.ACTION_SEND)
i.type = "message/rfc822"
i.putExtra(Intent.EXTRA_SUBJECT, subject)
i.putExtra(Intent.EXTRA_TEXT, text)
try {
activity.startActivity(Intent.createChooser(i, "Send mail..."))
} catch (ex: android.content.ActivityNotFoundException) {
Toast.makeText(activity, "There are no email clients installed.", Toast.LENGTH_SHORT).show()
}
@EmmanuelGuther
EmmanuelGuther / ConstraintLayoutGroupClickListener.kt
Created February 19, 2020 07:50
Add listener to all views in constraintlayout group
fun Group.addOnClickListener(listener: (view: View) -> Unit) {
referencedIds.forEach { id ->
rootView.findViewById<View>(id).setOnClickListener(listener)
}
}
companion object {
var callbackLoadMore: ((Int) -> Unit)? = null
var callbackLikeClick: ((Promotion) -> Unit)? = null
fun forPage(callbackLoadMore: (Int) -> Unit, callbackLikeClick: (Promotion) -> Unit): ShowcasePageOneFragment {
this.callbackLoadMore = callbackLoadMore
this.callbackLikeClick = callbackLikeClick
return ShowcasePageOneFragment()
}
}
@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().
*
*
@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 / 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 / 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 {