Skip to content

Instantly share code, notes, and snippets.

View amlcurran's full-sized avatar

Alex Curran amlcurran

View GitHub Profile
{
"fruits" : [
{
"name" : "Apple",
"image" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg",
"price" : 35
},
{
"name" : "Banana",
"image" : "https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Bananas_white_background_DS.jpg/320px-Bananas_white_background_DS.jpg",
action_sheet_warning:
name: "Action sheets must define a source view and rect, or barButtonItem"
message: "Action sheets on iPad will crash unless they have a source view and rect, or a source barButtonItem on their popoverPresentationController. If you have done this, you can suppress this warning but not before (swiftlint:disable:this action_sheet_warning)."
regex: "UIAlertController\\(.*, preferredStyle: .actionSheet\\)"
severity: warning
This is some text downloaded from an API!
@amlcurran
amlcurran / ColorAnimator.java
Last active July 18, 2018 11:24
Class which allows animation between two colors. Compatible with NineOldAndroids or the standard Android Animator APIs.
/**
* Copyright 2013 Alex Curran
*
* 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
import UIKit
extension UIView {
func constrain(toSuperview edges: NSLayoutAttribute..., insetBy inset: CGFloat = 0) {
for edge in edges {
constrain(toSuperview: edge, withOffset: offset(for: edge, ofInset: inset))
}
}
func doStuff() {
keyInput.constrain(toSuperviewSafeArea: .top, .leading, insetBy: 16)
blurView.contentView.addSubview(valueInput)
valueInput.constrain(toSuperviewSafeArea: .trailing, .bottom, insetBy: 16)
verticalLayoutConstraint = keyInput.constrain(.bottom, to: valueInput, .top, withOffset: -8)
verticalLayoutConstraint.priority = .defaultHigh
verticalLayoutConstraint.isActive = false
sameWidthConstraint = keyInput.constrain(.width, to: valueInput, .width)
sameTopConstraint = keyInput.constrain(.top, to: valueInput, .top)
@amlcurran
amlcurran / Completion.swift
Last active May 21, 2018 08:56
Better completion blocks by using higher order functions
func completion<Result>(onResult: @escaping (Result) -> Void, onError: @escaping (Error) -> Void) -> ((Result?, Error?) -> Void) {
return { (maybeResult, maybeError) in
if let result = maybeResult {
onResult(result)
} else if let error = maybeError {
onError(error)
} else {
onError(SplitError.NoResultFound)
}
}
func displayErrorMessage(_ optionalMessage: String?) {
if let message = optionalMessage {
print(message)
}
}
displayErrorMessage("Something went wrong") // Prints out "Something went wrong"
displayErrorMessage(nil) // doesn't do anything
#!/bin/bash
if hash lines 2>/dev/null ; then
if [ -f imports.txt -o -f lines.txt ]; then
echo "Previous import.txt and/or lines.txt exist! Delete them first."
exit
fi
imports > imports.txt && lines > lines.txt
open imports.txt lines.txt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if recipes[indexPath.row].isPromoted {
let promotedCell = tableView.dequeueReusableCell(withIdentifier: "promoted", for: indexPath) as! PromotedRecipeCell
promotedCell.recipe = recipe
promotedCell.promotion = recipe.promotion // we've guarded here using the isPromoted method, so the value is unwrapped
return promotedCell
} else {
let normalCell = tableView.dequeueReusableCell(withIdentifier: "normal", for: indexPath) as! RecipeCell
normalCell.recipe = recipe
return normalCell