Skip to content

Instantly share code, notes, and snippets.

View ZakTaccardi's full-sized avatar

Zak Taccardi ZakTaccardi

View GitHub Profile
//build.gradle for "server" module
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
@ZakTaccardi
ZakTaccardi / RxMap.kt
Created October 14, 2016 20:59
A basic RxMap
/**
* A reactive map implementation that allows you to observe a key and its value.
*/
class RxMap<K, V>(
private val map: AbstractMap<K, V> = HashMap(),
private val rxMap: AbstractMap<K, Relay<V, V>> = HashMap()
) : Map<K, V> {
/**
* @return a hot observable that emits the value for the specified key. When you subscribe,
@ZakTaccardi
ZakTaccardi / ControllerChangeEvent.java
Created December 9, 2016 16:36
For observing controller change events. Borrow's heavily from Jake Wharton's RxBinding
public final class ControllerChangeEvent {
@Nullable final Controller to;
@Nullable final Controller from;
final boolean isPush;
@Nullable final ViewGroup container;
@Nullable final ControllerChangeHandler handler;
final boolean isComplete;
@Override
@ZakTaccardi
ZakTaccardi / RxActivity.kt
Created December 13, 2016 15:47
RxActivity
/**
* Observe the activity events!
*/
class RxActivityDelegate : RxActivity {
private val relay = BehaviorRelay.create<ActivityLifecycleEvent>().toSerialized()
fun onCreate() {
relay.call(CREATE)
log("onCreate")
@ZakTaccardi
ZakTaccardi / StaleDataObservatory.kt
Last active January 5, 2017 16:55
Refresh data on an interval, as long as your data set has a subscriber.
/**
* Handles checking when data should be considered stale, and invokes a function to refresh data.
*
*/
@DataScope class StaleDataObservatory(
/**
* Duration the data is considered "fresh"
*/
syncInterval: Long,
@ZakTaccardi
ZakTaccardi / ScanMap.kt
Last active January 20, 2017 21:25
ScanMap - an operator that allows you to run a function over an emission and its previous emission. This function can map to a different type, unlike `.scan()`
fun <T, R> Observable<T>.scanMap(func2: (T?, T) -> R): Observable<R> {
return this.startWith(null as T?) //emit a null value first, otherwise the .buffer() below won't emit at first (needs 2 emissions to emit)
.buffer(2, 1) //buffer the previous and current emission
.filter { it.size >= 2 }
.map { func2.invoke(it[0], it[1]) }
}
fun <T, R> Observable<T>.scanMap(initialValue: T, func2: (T, T) -> R): Observable<R> {
return this.startWith(initialValue) //use initially provided value instead of null
.buffer(2, 1)
@ZakTaccardi
ZakTaccardi / Crossfader.kt
Last active April 11, 2017 19:35
Animate between multiple views, allowing only one view to be visible at a time.
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.view.View
import android.view.ViewGroup
/**
* Animates a view between its states.
*
* Currently only supports crossfading.
@ZakTaccardi
ZakTaccardi / KotlinExtensionTest.kt
Created February 20, 2017 16:45
Making tests more readable with Kotlin’s extension functions
@Test
fun testClear() {
clickNumber(1)
clickNumber(2)
clickNumber(3)
state.assertValue(123)
longPressClear()
state.assertValue(0)
}
@ZakTaccardi
ZakTaccardi / DealCardsUi.kt
Last active March 29, 2017 22:18
DealCardsUi
/**
* The user interface for dealing cards.
*
* @see DealCardsActivity
*/
interface DealCardsUi : StateRenderer<DealCardsUi.State> {
val state: State
override fun render(state: State)
class TemporalCoupling {
fun incorrectUsageOfBadApi() {
val badApi: BadApi = BadApi()
badApi.username = "Zak"
badApi.password = "temporalCouplingIsBad_123"
badApi.login() //throws an NullPointerException
}