Skip to content

Instantly share code, notes, and snippets.

View kiwiandroiddev's full-sized avatar

Matt Clarke kiwiandroiddev

  • Auckland, New Zealand
View GitHub Profile
@kiwiandroiddev
kiwiandroiddev / KotlinUtils.kt
Last active June 1, 2016 06:04
Small general-purpose extension functions and utilities for Kotlin
/**
* If [assumed] is non-null, converts it to a non-null type and executes [block] on the non-null
* type as an extension function (like with() in the Standard kotlin library)
*
* Similar to Swift's "if let = ..." construct.
*/
fun <T> if_let(assumed : T?, block : T.() -> Unit) {
if (assumed == null)
return
@kiwiandroiddev
kiwiandroiddev / git.gradle
Created April 22, 2016 03:37
Git interface functions for gradle build scripts (e.g. for Android). To use add this to your build script: apply from: 'git.gradle'
def getGitCommit() {
return 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
}
def getGitTag() {
def p = Runtime.getRuntime().exec("git describe HEAD --abbrev=0")
if (p.waitFor() != 0) {
return "none" // no tag
}
@kiwiandroiddev
kiwiandroiddev / rx-retry-backoff.js
Created February 7, 2016 15:33
Exponential backoff with RxJs (modified sample code from docs)
var Rx = require('rx')
var MAX_RETRIES = 4
Rx.Observable.throw(new Error("always fails"))
.retryWhen(function (errors) {
return Rx.Observable.zip(
Rx.Observable.range(1, MAX_RETRIES), errors, function (i, e) { return i })
.flatMap(function (i) {
console.log("delay retry by " + i + " second(s)");
return Rx.Observable.timer(i * 1000);
@kiwiandroiddev
kiwiandroiddev / ThemeUtils.java
Created October 8, 2015 21:48
Resolves a custom Android attribute to a resource ID for an Activity's theme.
public class ThemeUtils {
public static int getResourceIdForAttribute(@AttrRes int attrId, Activity activity) {
TypedValue outValue = new TypedValue();
activity.getTheme().resolveAttribute(attrId, outValue, true);
return outValue.resourceId;
}
}