Skip to content

Instantly share code, notes, and snippets.

View LouisCAD's full-sized avatar

Louis CAD LouisCAD

View GitHub Profile
import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import timber.log.Timber;
@LouisCAD
LouisCAD / DebugUtils.java
Created July 5, 2016 08:43
Utilities to detect problems during debug without exposing potential crashes in production
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.example.app.BuildConfig;
import timber.log.Timber;
/**
* Utils for debugging.
*/
@LouisCAD
LouisCAD / DebugConnectionCallbacks.java
Last active September 10, 2016 00:15
Allows one-liner GoogleApiClient Callbacks logging
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import timber.log.Timber;
import xyz.louiscad.common.Constants;
@LouisCAD
LouisCAD / View#onMeasure(…)
Created October 11, 2016 07:32
Makes any view square
/**
* Ensures the view is square by making it the size of the smallest side.
*/
@Override
protected final void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(size, size);
}
@LouisCAD
LouisCAD / LoadListener.kt
Last active November 10, 2016 09:00
A simple LoaderCallbacks implementation for kotlin and it's lambdas
import android.os.Bundle
import android.support.v4.app.LoaderManager
import android.support.v4.content.Loader
class LoadListener<D, out L : Loader<D>>(private val createLoader: () -> L,
private val onLoadDone: (loader: L, data: D) -> Unit)
: LoaderManager.LoaderCallbacks<D> {
override fun onCreateLoader(id: Int, args: Bundle?) = createLoader()
@Suppress("UNCHECKED_CAST")
@LouisCAD
LouisCAD / GoogleApiClientLoader.kt
Created November 10, 2016 15:43
Can be used to load data from a Google Play Services API such as FusedLocationProvider or Wearable DataApi
import android.content.Context
import android.os.Bundle
import android.support.v4.content.Loader
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.api.GoogleApiClient
import timber.log.Timber
/**
@LouisCAD
LouisCAD / ConnectivityListener.kt
Created November 10, 2016 18:08
An Agera BaseObservable to monitor connectivity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager.CONNECTIVITY_ACTION
import com.google.android.agera.BaseObservable
import org.jetbrains.anko.connectivityManager
class ConnectivityListener(private val context: Context) : BaseObservable() {
import android.content.Context
import android.preference.PreferenceManager
inline fun Context.runOnce(uniqueKey: String, f: () -> Unit) {
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val isFirstRun = prefs.getBoolean(uniqueKey, true)
if (isFirstRun) {
prefs.edit().putBoolean(uniqueKey, false).commit()
f()
}
@LouisCAD
LouisCAD / generate-pushid.js
Created November 24, 2016 13:55 — forked from mikelehen/generate-pushid.js
JavaScript code for generating Firebase Push IDs
/**
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the
* latter ones will sort after the former ones. We do this by using the previous random bits
* but "incrementing" them by 1 (only in the case of a timestamp collision).
*/