Skip to content

Instantly share code, notes, and snippets.

View albodelu's full-sized avatar

Al B. albodelu

View GitHub Profile
@albodelu
albodelu / ApiModule.java
Created August 31, 2017 05:28 — forked from msangel/ApiModule.java
Retrofit 1 error handling behaviour in Retrofit 2.3.0
// Dagger 1 example
@Module(
complete = false,
library = true
)
public final class ApiModule {
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, Application app) {
return new Retrofit.Builder()
@albodelu
albodelu / DataCaptionInfo.kt
Created September 21, 2017 04:54 — forked from Atternatt/DataCaptionInfo.kt
Delegating SharedPreferences into parameters
data class DataCaptionInfo(private val context: Context) {
var name: String by Delegate.prefParam(context,"NAME", "")
var lastName: String by Delegate.prefParam(context, "LAST_NAME", "")
var email : String by Delegate.prefParam(context, "EMAIL", "")
var referalCode: String by Delegate.prefParam(context, "REFERAL_ID", "")
@albodelu
albodelu / MonadUsage.kt
Created September 21, 2017 04:55 — forked from Atternatt/MonadUsage.kt
monads
sum4(4) //return 8
div2(4) //return 2
listOf(3,4,6,8,10)
.map(sum4)
.map(div2)
//[3, 4, 5, 6, 7]
fun hello(name: String, textDecorator: () -> String): String {
return "hello $name ${textDecorator()}"
fun <T : Comparable<T>> List<T>.sort() {
Collections.sort(this)
}
list.sort();
val index = list.binarySearch(x);
val
@albodelu
albodelu / OtherExamples.kt
Created September 21, 2017 04:55 — forked from Atternatt/OtherExamples.kt
Kotlin Extension Usages
bundle?.let { intent.putExtras(it) } //si el bundle no es null, lo añadimos como extra al intent
//crear un animationSet al estilo builder
val animationSet = AnimatorSet().apply {
playTogether(animX, animY)
duration = 350
interpolator = FolioBounceInterpolator()
}.start()
@albodelu
albodelu / StringExample.kt
Created September 21, 2017 04:56 — forked from Atternatt/StringExample.kt
Operator Overloading
operator fun String.get(intRange: IntRange): String {
return this.substring(intRange)
}
val firstSentence = "hello Marc!"
val secondSentence = "World Wide"
val mixedSentence = firstSentence[0..4] + secondSentence[0..4]
//Hello World
@albodelu
albodelu / Composed.kt
Created September 21, 2017 04:56 — forked from Atternatt/Composed.kt
Functionalize!
fun twice(int: Int) = int * 2
fun trice(int: Int) = int * 3
fun getString(int: Int) = int.toString()
//el operador rangeTo es el '..' acepta un parametro de entrada y otro de salida
//si usamos mónadas para enlazarlo podemos concatenar la salida de uno con la entrada del otro
operator fun <T,R,V> ((T)->R).rangeTo(other: ((R)->V)): ((T)->V){
return {
other(this(it))
}
@albodelu
albodelu / Examples.kt
Created September 21, 2017 04:56 — forked from Atternatt/Examples.kt
inline, reified, infix
inline fun <reified NewActivity : Activity> Activity.start(bundle: Bundle? = null) {
val intent: Intent = Intent(this, NewActivity::class.java)
bundle?.let { intent.putExtras(it) }
this.startActivity(intent)
}
startActivity<MainActivity>()
infix fun <Value>Boolean.then(block: () -> Value) {
import android.databinding.Observable as DataBindingObservable
private inline fun <reified T : DataBindingObservable, R : Any?> T.observe(
crossinline block: (T) -> R
): Observable<R> = create { subscriber ->
object : android.databinding.Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(observable: DataBindingObservable, id: Int) = try {
subscriber.onNext(block(observable as T))
} catch (e: Exception) {
subscriber.onError(e)
@albodelu
albodelu / ByDouble.kt
Created September 21, 2017 04:56 — forked from Atternatt/ByDouble.kt
Kotlin Delegation Example
class ByDouble(incrementer: Incrementer) : Incrementer by incrementer {
override fun increment(number: Int): Int {
return super.increment(number) * 2
}
}