Skip to content

Instantly share code, notes, and snippets.

View rlac's full-sized avatar

Robert Carr rlac

  • Brisbane, Australia
View GitHub Profile
sudo apt install libc6:i386 libncurses5:i386 libstdc++6:i386 libbz2-1.0:i386 zlib1g:i386 openjdk-8-jdk
@rlac
rlac / DividerItemDecoration.java
Created August 22, 2015 23:22
RecyclerView ItemDecorator to add dividers to vertical LinearLayoutManager items
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class ItemDividerDecoration extends RecyclerView.ItemDecoration {
private final Drawable divider;
@rlac
rlac / StreamSwitcher.java
Created March 4, 2015 04:34
Observable stream switcher
/**
* Abstraction over {@link Observable#switchOnNext(Observable)}, allowing an Observable to publish
* results from multiple streams that may be switched over time, for example from an event source
* that may change.
*
* https://stackoverflow.com/questions/21836818/subscribing-to-a-future-observable/21839376#21839376
*/
public final class StreamSwitcher<T> {
Subject<Observable<T>, Observable<T>> publisher;
Observable<T> stream;
@rlac
rlac / Parcelable.kt
Created January 15, 2015 23:08
Parcelable creator convenience function
import android.os.Parcelable
import android.os.Parcel
import kotlin.InlineOption.ONLY_LOCAL_RETURN
public inline fun creator<reified T : Parcelable>(
inlineOptions(ONLY_LOCAL_RETURN) createFromParcel: (Parcel) -> T?): Parcelable.Creator<T> =
object : Parcelable.Creator<T> {
override fun createFromParcel(source: Parcel): T? = createFromParcel(source)
override fun newArray(size: Int): Array<out T?> = arrayOfNulls(size)
}
import kotlin.properties.ReadWriteProperty
import android.content.SharedPreferences
import android.app.Activity
import android.content.Context
import android.app.Fragment
import android.support.v4.app.Fragment as SupportFragment
import kotlin.properties.Delegates
public fun Activity.intPreference(prefs: String, key: String, default: Int): ReadWriteProperty<Any, Int> = intPref(prefs, key, default)
public fun Activity.stringPreference(prefs: String, key: String, default: String): ReadWriteProperty<Any, String> = stringPref(prefs, key, default)
import android.view.View
import kotlin.properties.ReadOnlyProperty
import android.app.Activity
import kotlin.properties.Delegates
public fun Activity.bindView<T: View>(id: Int): ReadOnlyProperty<Any, T> = ViewVal(id)
public fun ViewHolder.bindView<T: View>(id: Int): ReadOnlyProperty<Any, T> = ViewVal(id)
public trait ViewHolder {
public val view: View;
import android.os.Bundle
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import android.app.Fragment
import android.support.v4.app.Fragment as SupportFragment
import kotlin.properties.Delegates
public fun bundledBoolean(bundle: Bundle, key: String, default: Boolean = false): ReadWriteProperty<Any, Boolean> = bundleVar({ bundle }, key, default)
public fun bundledString(bundle: Bundle, key: String, default: String = ""): ReadWriteProperty<Any, String> = bundleVar({ bundle }, key, default)
public fun bundledInt(bundle: Bundle, key: String, default: Int = 0): ReadWriteProperty<Any, Int> = bundleVar({ bundle }, key, default)
@rlac
rlac / Spannable.kt
Created October 10, 2014 09:58
A simple Kotlin builder for creating SpannableStrings. Original idea from https://gist.github.com/JakeWharton/11274467.
// Copyright 2014 Robert Carr
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// Apache 2.0 licensed.
// Based on Jake Wharton's 'BindableAdapter.java' - https://gist.github.com/JakeWharton/5423616
import android.widget.BaseAdapter
import android.content.Context
import android.view.LayoutInflater
import kotlin.properties.Delegates
import android.view.View
import android.view.ViewGroup