Skip to content

Instantly share code, notes, and snippets.

View edwardaux's full-sized avatar

Craig Edwards edwardaux

View GitHub Profile
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
// This function can be passed to the [CupertinoSliverRefreshControl] builder function
// in order to achieve a much more natural pull-to-refresh spinner. The out-of-the-box
// implementation provided by Flutter uses a down-arrow which is very unfamiliar to iOS
// users. This implementation is an attempt to make a more faithful implementation.
//
@edwardaux
edwardaux / LoadingState.dart
Created October 3, 2018 04:37
A hacky way to create Dart enum's with associated values
// Models an enum with three mutually exclusive states:
// - loading
// - success (with an object of some type)
// - error (with an Error object)
abstract class LoadingStatus<T> {
static const loading = const LoadingStatusLoading();
factory LoadingStatus.success(T object) = LoadingStatusSuccess;
factory LoadingStatus.error(Error error) = LoadingStatusError;
}
@edwardaux
edwardaux / gist:01d7705622e897ef422dbf703a17b586
Created November 29, 2017 00:03
Decoding Reference Types in Swift 4

Decoding Reference Types in Swift 4

Apple's Swift 4 recently introduced some amazing new features to support archiving/unarchiving as part of Foundation. This post explores some techniques for unarchiving/decoding while ensuring that instances of reference types can be shared within the object graph based on an arbitrary identifier.

Problem Description

Stepping back a little, imagine you had a simple construct representing a Car and its owner/driver. Using value types, this might look like:

struct Person : Codable {
    let identifier: Int
}
//: Playground - noun: a place where people can play
import Foundation
class DecodableCache<Key,Value> where Key : Hashable {
private var values: [Key:Value] = [:]
subscript(index: Key) -> Value? {
get {
return values[index]
}
@edwardaux
edwardaux / gist:60c106ab6876c3c3c16216ae8d714108
Created February 24, 2017 07:18
A simple expression watcher for Swift apps
// Sometimes you want to watch a particular expression over time. Continually dumping
// the value in a loop can be painful because of the sheer volume of values. This snippet
// loops on a background thread checking the result of the passed expression - if the
// value changes (and only then) does it output the new value. Note that this is NOT meant
// as a replacement for a KVO-observable property... it is better suited to expressions
// that do not post KVO events.
//
// Usage: watchExpression(every: 0.1) { someCodeThatReturnsAValue() }
//
// You can control how often the value will be checked using the every parameter (the example
@edwardaux
edwardaux / patch0001.diff
Created September 30, 2015 22:49
Patches for Argo #237
From bf726b714c977045974a92d91d561a72c3045924 Mon Sep 17 00:00:00 2001
From: Tony DiPasquale <[email protected]>
Date: Fri, 13 Mar 2015 13:08:22 -0400
Subject: [PATCH] Update the documentation for Swift 2
This also breaks up the main README.md into a Documentations directory
with separate documents for different topics. This will be expanded upon
with new PRs going forward.
---
ArgoTests/JSON/JSONFileReader.swift | 3 +-
@edwardaux
edwardaux / gist:8a2319844ba00582c0b7
Created April 16, 2015 23:23
Output of `carthage update` when adding Box to a brand new project
Build settings from command line:
SDKROOT = iphonesimulator8.3
=== BUILD TARGET Box-iOS OF PROJECT Box WITH CONFIGURATION Release ===
Check dependencies
ProcessInfoPlistFile /Users/edwardsc/Library/Developer/Xcode/DerivedData/Box-davkzunrjrmqakbqofsdvgpbxmfk/Build/Products/Release-iphoneos/Box.framework/Info.plist Box/Info.plist
cd /Users/edwardsc/Desktop/tmp/Blah/Carthage/Checkouts/Box
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Users/edwardsc/bin:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Server.app/Contents/ServerRoot/usr/bin:/Applications/Server.app/Contents/ServerRoot/usr/sbin:/usr/local/mysql/bin:/Users/edwardsc/.ec2/bin"