This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Event<T> { | |
private val handlers = arrayListOf<(T) -> Unit>() | |
operator fun plusAssign(handler: (T) -> Unit) { handlers.add(handler) } | |
operator fun invoke(value: T) { for (handler in handlers) handler(value) } | |
} | |
val e = Event<String>() // define event | |
fun main(args : Array<String>) { | |
e += { println(it) } // subscribe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.properties.ReadWriteProperty | |
import kotlin.reflect.KProperty | |
/** | |
* Property delegate to allow lazy initialization of a mutable variable. | |
*/ | |
class MutableLazy<T>(val init: () -> T) : ReadWriteProperty<Any?, T> { | |
private var value: Optional<T> = Optional.None() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Repeater(val interval: Long, | |
val context: CoroutineContext = UI, | |
val block: () -> Unit | |
) { | |
private var updatingJob: Job? = null | |
private val updatingJobLock = Any() | |
fun start() = synchronized(updatingJobLock) { | |
if (updatingJob != null && updatingJob!!.isActive) { | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Rect; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public class GridSpacingDecoration extends RecyclerView.ItemDecoration { | |
private int cols; | |
private int spacing; | |
public GridSpacingDecoration(int cols, int spacing) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional | |
// Actual implementation | |
fun <T> deferringScope(block: DeferringScope<T>.() -> T): Optional<T> { | |
val scope = DeferringScope<T>() | |
try { | |
scope.result = Optional.of(scope.block()) | |
} catch (e: Throwable) { | |
scope.error = e.message ?: e.toString() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.v7.util.DiffUtil | |
open class DiffUtilCallback<T>(val oldList: List<T>, val newList: List<T>) : DiffUtil.Callback() { | |
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) | |
= oldList[oldItemPosition] == newList[newItemPosition] | |
override fun getOldListSize() | |
= oldList.size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseActivity : Activity { | |
val permissionHandler: PermissionRequestHandler | |
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
val handled = permissionHandler.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
if(!handled) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T, U> memoized(function: (T) -> U): (T) -> U { | |
val cache = mutableMapOf<T, U>() | |
return { t -> cache.getOrPut(t) { function(t) } } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a new List with the element at [fromIndex] moved to [toIndex] | |
*/ | |
private fun <T> List<T>.move(fromIndex: Int, toIndex: Int) = this.mapIndexed { index, element -> when { | |
index == toIndex -> this[fromIndex] | |
fromIndex < toIndex && index in fromIndex..toIndex -> this[index + 1] | |
fromIndex > toIndex && index in toIndex..fromIndex -> this[index - 1] | |
else -> element | |
} } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.UUID | |
/** | |
* Superclass for classes that wrap a single value for type safety purposes. | |
*/ | |
abstract class WrapperType<T : Any>(val value: T) { | |
override fun toString() = value.toString() | |
override fun equals(other: Any?) = other is WrapperType<*> && value == other.value | |
override fun hashCode() = value.hashCode() | |
} |
OlderNewer