Skip to content

Instantly share code, notes, and snippets.

View Sloy's full-sized avatar

Rafa Vázquez Sloy

View GitHub Profile
public class NetworkEndpointPreference extends StringPreference {
@Inject
public NetworkEndpointPreference(SharedPreferences preferences) {
super(preferences, "debug_network_endpoint_url", DebugApiEndpoints.PROD.getUrl());
}
}
@Sloy
Sloy / FirebaseCoroutinesExtensions.kt
Last active January 20, 2021 11:48
Reading Firebase Database as Kotlin coroutines
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
WARNING!!!
NO, SERIOUSLY. REAL WARNING!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@Sloy
Sloy / FirebaseLiveData.kt
Created September 12, 2017 18:55
Using LiveData with Firebase Database on Kotlin
class FirebaseLiveData(
private val reference: DatabaseReference
) : LiveData<DataSnapshot>() {
private var eventListener: ValueEventListener? = null
override fun onActive() {
super.onActive()
logd("onActive $reference")
eventListener = reference.addValueEventListener(FValueEventListener(
@Sloy
Sloy / potato.js
Last active October 27, 2017 10:19
Just a JS potato
Object.prototype.potato = function(){
console.log('MMMMMMMMMMMMMMMMMMMMMMMMMMMMMNmdhysssssyhhmNMMMMMMMMMMMMMMMMMMMMMMMMMM','MMMMMMMMMMMMMMMMMMMMMMMmy+:.```.--:::::--.```.:+ydMMMMMMMMMMMMMMMMMMMM','MMMMMMMMMMMMMMMMMMMMh+. .:+shddhhhyyyyyhhhddhs+:. ./hNMMMMMMMMMMMMMMMM','MMMMMMMMMMMMMMMMMMy- .+hdhysoo++++++++++++++ooyhdh+. .sNMMMMMMMMMMMMMM','MMMMMMMMMMMMMMMMd: .sdho+++ssso++++++++++++++++++oydy- -dMMMMMMMMMMMMM','MMMMMMMMMMMMMMMy` +dho+++++ooso++++++++++++++++++oooym+ .dMMMMMMMMMMMM','MMMMMMMMMMMMMMs``sms++++++++++++++++++++++++++++osss+sm+ .NMMMMMMMMMMM','MMMMMMMMMMMMMh``ymo++sso++++++++++++++++++++++++++++++hm- oMMMMMMMMMMM','MMMMMMMMMMMMm` oms+++ooo++++++++++++++++++++++++++++++oms .NMMMMMMMMMM','MMMMMMMMMMMM: /my++++++++++++++++++++++++++++++++++++++hd` dMMMMMMMMMM','MMMMMMMMMMMs .dh+++++++++oyhhyo+++++++++++++++++ohhhyo+ym- sMMMMMMMMMM','MMMMMMMMMMm` smo++++++++ommmmmmo+++++++++++++++ommmmmd+om+ :MMMMMMMMMM','MMMMMMMMMM/ :my++++++++++dmmmmd+++++ssyyyyyso++odmmmmh++dh `dMMMMMMMMM','MMMMMMMMMh``hd++++++
@Sloy
Sloy / GetAndForgetProperty.kt
Created November 22, 2017 11:02
A custom delegate for a property that it's deleted after reading it, so it can only be used once.
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* With this delegate, a property will only return it's value once. Subsequent queries will return the default value.
*/
class GetAndForgetProperty<in R, T>(private val defaultValue: T) : ReadWriteProperty<R, T> {
companion object {
fun <T> getAndForget(defaultValue: T): GetAndForgetProperty<Any, T> {
@Sloy
Sloy / FirebaseLiveData.kt
Last active January 14, 2018 11:20
Kotlin wrapper for observing the Firebase Realtime Database with Live Data
class FirebaseLiveData(
private val reference: DatabaseReference
) : LiveData<DataSnapshot>() {
private var eventListener: ValueEventListener? = null
override fun onActive() {
super.onActive()
logd("onActive $reference")
eventListener = reference.addValueEventListener(FValueEventListener(
@Sloy
Sloy / FirestoreLiveData.kt
Created January 14, 2018 11:28
Kotlin wrappers for observing Firebase Firestore database queries and documents using LiveData
fun Query.observeLiveData(): FirestoreQueryLiveData {
return FirestoreQueryLiveData(this)
}
fun DocumentReference.observeLiveData(): FirestoreDocumentLiveData {
return FirestoreDocumentLiveData(this)
}
class FirestoreQueryLiveData(
@Sloy
Sloy / DebugNotification.kt
Created January 26, 2018 10:15
Utility class to show a debug notification from anywhere in your app
object DebugNotification {
private const val DEBUG_CHANNEL_ID = "debug_channel"
@JvmStatic
@JvmOverloads
fun show(context: Context, title: String, text: String = "Debug notification") {
if (BuildConfig.DEBUG) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel(context)
@Sloy
Sloy / LiveDataExtensions.kt
Created February 6, 2018 11:08
Kotlin extensions for LiveData
package com.sloydev.macookbuk.infrastructure.extensions
import android.arch.lifecycle.*
fun <T, R> LiveData<T>.map(transformation: (T) -> R): LiveData<R> {
return Transformations.map(this, transformation)
}
fun <A, B, C> LiveData<A>.zipWith(other: LiveData<B>, zipFunc: (A, B) -> C): LiveData<C> {
return ZippedLiveData<A, B, C>(this, other, zipFunc)
@Sloy
Sloy / Counter.kt
Last active July 19, 2018 09:08
Reproducing issue with Koin shared state in Android Tests. Inspired by the Dagger 1 version: https://gist.github.com/Sloy/c45dc3291403e603b4b88755e4a67660
open class Counter(context: Context) {
open var count: Int = 0
open fun hit() = count++
}