Skip to content

Instantly share code, notes, and snippets.

@bulwinkel
bulwinkel / SequenceDiff.swift
Last active June 23, 2016 01:43
Run diff on 2 sequences
extension SequenceType where Self.Generator.Element : Equatable {
typealias Diff = (added: [Self.Generator.Element], removed: [Self.Generator.Element])
func diff(changed:[Self.Generator.Element]) -> Diff {
let added:[Self.Generator.Element] = changed.reduce([]) {
!self.contains($1) ? $0 + [$1] : $0
}
let removed:[Self.Generator.Element] = self.reduce([]) {
@bulwinkel
bulwinkel / PicassoImageObservable.kt
Created July 13, 2016 04:43
Generates Bitmap Observables with Picasso instance
class PicassoImageObservable(
private val picasso: Picasso
) : Func1<String, Observable<Bitmap>> {
override fun call(url: String): Observable<Bitmap> {
return Observable.fromAsync({ emitter ->
val target = object : Target {
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
//do nothing
}
@bulwinkel
bulwinkel / SerializedProperty.java
Created July 31, 2016 23:44
Serialize Properties into Bundles and back (Experimental)
import android.os.Bundle;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import java.io.IOException;
/**
* Removes some of the boilerplate associated with Properties in android fragments by handling
* the serialization and deserialization of wrapped objects into and out of argument
* {@link Bundle}s.
*
@bulwinkel
bulwinkel / Windows.java
Created August 18, 2016 13:08
Android Window Utilities
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.Window;
import android.view.WindowManager;
public class Windows {
private Windows() {
//no instances
@bulwinkel
bulwinkel / TypedValues.java
Created August 18, 2016 13:11
Android TypedValue Utilities (px -> dp)
import android.content.Context;
import android.util.TypedValue;
public class TypedValues {
private TypedValues() {
//no instances
}
public static int dip(Context context, float dp) {
@bulwinkel
bulwinkel / mapToBundle.kt
Created January 7, 2017 13:35
Partial implementation of converting a `Map<String, V>` to a Bundle.
package com.bulwinkel.android
import android.os.Bundle
import android.os.IBinder
import android.os.Parcelable
import java.io.Serializable
fun <V> Map<String, V>.toBundle(bundle: Bundle = Bundle()): Bundle = bundle.apply {
forEach {
val k = it.key
@bulwinkel
bulwinkel / CharSequences.java
Created January 12, 2017 01:40
Android helper functions for highlighting a search term in a given CharSequence.
import android.support.annotation.ColorInt;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import io.realm.RealmList;
import io.realm.RealmModel;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@bulwinkel
bulwinkel / HttpExceptions.java
Last active July 4, 2017 07:51
Utility class for checking if a Throwableis a HttpException of a specific codes.
package com.nm.support.okhttp;
import retrofit2.HttpException;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
public final class HttpExceptions {
private HttpExceptions() {
@bulwinkel
bulwinkel / org-res.sh
Created January 15, 2018 03:01
script that moved imageRes-mdpi.png into res/mdpi/imageRes.png
RESOLUTIONS=('-mdpi' '-hdpi' '-xhdpi' '-xxhdpi' '-xxxhdpi')
for res in "${RESOLUTIONS[@]}"; do
echo $res
DIR="res/drawable${res}"
mkdir -p ${DIR}
for file in $(find . -type f -iname "*${res}*"); do
echo "${file}"
mv "$file" "${DIR}/${file/${res}/}"
done