Skip to content

Instantly share code, notes, and snippets.

View Sloy's full-sized avatar

Rafa Vázquez Sloy

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / FirebaseCoroutinesExtensions.kt
Last active January 20, 2021 11:48
Reading Firebase Database as Kotlin coroutines
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
WARNING!!!
NO, SERIOUSLY. REAL WARNING!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class NetworkEndpointPreference extends StringPreference {
@Inject
public NetworkEndpointPreference(SharedPreferences preferences) {
super(preferences, "debug_network_endpoint_url", DebugApiEndpoints.PROD.getUrl());
}
}
@Sloy
Sloy / InjectedInstrumentationTest.java
Last active June 7, 2018 11:15
Injector (Dagger 1)
public class InjectedInstrumentationTestRule implements MethodRule {
private final Object testModule;
public InjectedInstrumentationTestRule(Object testModule) {
this.testModule = testModule;
}
@Override
public Statement apply(final Statement statement, FrameworkMethod frameworkMethod, final Object testClassInstance) {
@Sloy
Sloy / MenuItemValueHolder.java
Last active November 2, 2016 09:57
Wrap your MenuItem with this class and forget about activity/fragment lifecycle with menu items. Initializing the visibility and your menu is not created yet? Don't worry, it will queue actions and trigger them once it's ready.
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.ActionProvider;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import java.util.LinkedList;
import java.util.List;
@Sloy
Sloy / Monitor GcmNetworkManager tasks
Created September 29, 2016 09:18
GcmNetworkManager monitoring command
$ while sleep 1; do adb shell dumpsys activity service GcmService --endpoints my.package.name | grep "Pending:" -A 20; done