Skip to content

Instantly share code, notes, and snippets.

@alwarren
alwarren / PerPageTrait.php
Last active April 22, 2016 08:56
A Laravel 5.2 trait that stores a value in the session that can be used with a form select to set the per page value for a length aware paginator.
<?php
namespace App\Traits;
use Illuminate\Http\Request;
/**
* A Laravel 5.2 trait that stores a value in the session that can be used with
* a form select to set the per page value for a length aware paginator.
*
@alwarren
alwarren / CursorRecyclerAdapter.java
Created August 8, 2017 14:24 — forked from nesquena/CursorRecyclerAdapter.java
Cursor Adapters for RecyclerView
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 ARNAUD FRUGIER
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
/*
* Copyright (C) 2014 skyfish.jy@gmail.com
*
* 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
@alwarren
alwarren / RecyclerViewAdapter.java
Created August 10, 2017 01:16 — forked from slidenerd/RecyclerViewAdapter.java
A single adapter that supports Cursor + an optional header + optional footer
import android.database.Cursor;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
public abstract class RecyclerCursorAdapter<U, V extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnSwipeListener {
//The number of headers to be displayed by default if child classes want a header
public static final int HEADER_COUNT = 1;
//The number of footers to be displated by default if child classes want a footer
PORTRAIT MODE
MDPI is 320x480 dp = 320x480px (1x)
LDPI is 0.75 x MDPI = 240x360px
HDPI is 1.5 x MDPI = 480x720px
XHDPI is 2 x MDPI = 640x960px
XXHDPI is 3 x MDPI = 960x1440px
XXXHDPI is 4 x MDPI = 1280x1920px
LANDSCAPE MODE
MDPI is 480x320 dp = 480x320px (1x)
@alwarren
alwarren / AndroidAPI26Gradle.txt
Last active August 23, 2017 21:50
Android API26 (O) Gradle Setup
Android API26 (O) Gradle Setup
--------------------------------------------------------------------------------
#gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
@alwarren
alwarren / HelloViewModel.kt
Last active February 28, 2018 06:49
Android LiveData ViewModel (Kotlin/Dagger)
internal class HelloViewModel @Inject constructor(private val dataSource: Hello) : ViewModel() {
// observed by ViewModel as a trigger to retrieve data from the source
private var liveEndpoint: MutableLiveData<String> = MutableLiveData()
// observed by Activity/Fragment or some other class as a trigger to do something with the data
private var liveData: LiveData<String>
init {
// default observable endpoint
@alwarren
alwarren / ExtensionsSnackbar.kt
Last active March 8, 2018 05:22 — forked from antoniolg/HomeActivity.kt
Snackbar extensions on Kotlin, to create a useful small DSL.
fun Context.colorInt(color: Int) = ContextCompat.getColor(this, color)
inline fun View.snack(@IntegerRes messageRes: Int, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) {
snack(resources.getString(messageRes), length, f)
}
inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) {
val snack = Snackbar.make(this, message, length)
snack.f()
snack.show()
@alwarren
alwarren / Connectivity.java
Created March 8, 2018 15:45 — forked from emil2k/Connectivity.java
Android utility class for checking device's network connectivity and speed.
/*
* Copyright (c) 2017 Emil Davtyan
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
@alwarren
alwarren / States-v3.md
Created March 10, 2018 09:34 — forked from andymatuschak/States-v3.md
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,