Skip to content

Instantly share code, notes, and snippets.

package com.nhaarman.test;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;

Keybase proof

I hereby claim:

  • I am nhaarman on github.
  • I am nhaarman (https://keybase.io/nhaarman) on keybase.
  • I have a public key whose fingerprint is B50E 6209 DFFC 8D41 017D 7148 9AB1 2E03 91A2 FE24

To claim this, I am signing this object:

sealed class Distance : Comparable<Distance> {
data class DistanceMeters(val value: Float) : Distance()
data class DistanceFeet(val value: Float) : Distance()
fun toMeters() = when (this) {
is DistanceMeters -> this
is DistanceFeet -> DistanceMeters(value * .3048f)
}
fun toFeet() = when (this) {
@nhaarman
nhaarman / UndoRedo.kt
Created March 7, 2018 13:54
Simple Rx undo/redo stream
fun <T, R> Observable<Command<T>>.undoRedo(initialValue: R, accumulator: (R, T) -> R): Observable<R> {
return this
.scan(UndoRedoStack.create<T>()) { stack, command ->
when (command) {
is Command.Do -> stack.push(command.action)
is Command.Undo -> stack.undo()
is Command.Redo -> stack.redo()
}
}
.map { stack ->
data class Device(
val name: String
)
interface DeviceScanner {
/** Emits a Device opon each new discovery event */
fun devices() : Observable<Device>
}
class BluetoothDeviceScanner(
@nhaarman
nhaarman / Screen.kt
Created September 3, 2018 21:08
A very simple contract for a screen in a mobile application.
interface Screen<V : Container> {
fun onStart() {}
fun attach(v: V) {}
fun detach(v: V) {}
fun onStop() {}
fun onDestroy() {}
interface SimpleContainer {
var text: String
}
class SimpleScreen(
val name: String
) : Screen<SimpleContainer> {
override fun attach(v: SimpleContainer) {
v.text = "Hello, $name!"
class SelectPictureScreen(
private val listener: Events
) : Screen<SelectPictureContainer> {
override fun attach(c: SelectPictureContainer) {
c.setOnPictureClickListener { listener.onPictureSelected(it.id) }
}
interface Events {
fun onPictureSelected(id: Long)
interface Navigator {
fun addListener(listener: Events)
fun saveInstanceState() : SavedState
interface Events {
fun screen(screen: Screen<out Container)
class PaymentFlowNavigator private constructor(
private var shippingInfo: ShippingInfo? = null,
savedState: SavedState?
): StackNavigator(savedState),
ShippingInfoScreen.Events,
PaymentInfoScreen.Events,
ConfirmationScreen.Events {
override fun initialStack() = listOf(ShoppingCartScreen())