Skip to content

Instantly share code, notes, and snippets.

View Ben-G's full-sized avatar
💭
💻

Benjamin Encz Ben-G

💭
💻
View GitHub Profile
@Ben-G
Ben-G / ReactiveListExample.swift
Last active February 6, 2018 19:09
Reactive List Example
/// Pure function mapping new state to a new `TableViewModel`. This is invoked each time the state updates
/// in order for ReactiveLists to update the UI.
static func viewModel(forState groups: [ToolGroup]) -> TableViewModel {
// Create a section for every tool group
let sections: [TableSectionViewModel] = groups.map { group in
// Create a single cell for every tool
let cellViewModels = group.tools.map { ToolTableCellModel(tool: $0) }
return TableSectionViewModel(
headerTitle: group.name,
headerHeight: 44,
@Ben-G
Ben-G / KeyPathJoins.swift
Last active October 13, 2017 21:38
Keypath Based Joins in Swift
//: Playground - noun: a place where people can play
import Cocoa
struct IssueAssignee {
let issueUid: String
let assigneeUid: String
}
struct IssueFollower {
@Ben-G
Ben-G / L10NTests.swift
Created June 16, 2017 20:35
Very simple automated test to ensure localizations exist and are well-formed on iOS.
import Foundation
import XCTest
/// Basic sanity check that ensures that we are able to retrieve localized strings for all languages
/// we support.
final class L10NTests: XCTestCase {
func testLocalizations() {
let locales = ["en", "es", "zh-Hans", "zh-Hant", "fi"]
for locale in locales {
@Ben-G
Ben-G / UserDefaultsWrapper.swift
Created April 12, 2017 02:04
User Defaults Wrapper Swift
/// A wrapper around `NSUserDefaults` access for settings that are specific to the PlanGrid app.
/// `NSUserDefaults` should always be accessed through this type, this way we have a good overview
/// of all the settings the app supports and we can document them.
@objc final class PlanGridUserDefaults: NSObject {
/// Keeps track of whether or not a user has already used the annotation filter feature.
static var hasUsedAnnotationFilterFeature = UserDefaultsProperty<Bool>("HasUsedAnnotationFilterFeature")
}
/// A property that wraps around a value that is persisted to NSUserDefaults.
@Ben-G
Ben-G / DateEquality.swift
Last active February 3, 2018 23:09
Date Equality Swift
// Want to check equality of two Date instances *in your tests?* (you should never rely on this in
// production). Add this extension to avoid
// rounding errors when using different initializers.
// See: https://twitter.com/benjaminencz/status/794606769360076800
extension Date {
public static func ==(lhs: Date, rhs: Date) -> Bool {
return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate ||
lhs.timeIntervalSince1970 == rhs.timeIntervalSince1970
}
}
@Ben-G
Ben-G / DateEquality.swift
Created March 23, 2017 01:19
Date Equality Swift
extension Date {
public static func ==(lhs: Date, rhs: Date) -> Bool {
return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate ||
lhs.timeIntervalSince1970 == rhs.timeIntervalSince1970
}
}
@Ben-G
Ben-G / LiftToOptional.swift
Last active January 24, 2017 16:52
Function that lifts a function with non-optional input to one with optional input.
// Thanks to @jckarter for pointing that this is just Optional's map implementation...
{ $0.map(f) }
/// Takes a function with non-optional input and non-optional return and lifts it to a function
/// with optional input and optional return.
/// The lifted function will return `nil` iff the input is `nil`, otherwise the input will be
/// applied to the original function which will return a non-`nil` value.
public func liftToOptional<T,U>(_ f: @escaping (T) -> U) -> (T?) -> U? {
return { arg in
guard let arg = arg else { return nil }
@Ben-G
Ben-G / LiftTupleToIOU.swift
Created June 21, 2016 22:57
(Don't actually do this) Lift Tuples to IOUs as Workaround for Swift Bug SR-42
// Don't actually do this...
func lift<A, B, C>(input: (A, B, C)) -> (A!, B!, C!) {
return (Optional(input.0)!, Optional(input.1)!, Optional(input.2)!)
}
@Ben-G
Ben-G / ReSwiftEffectsExperiment.swift
Created June 11, 2016 23:32
ReSwift Allow For Effects - Experiment
//: Playground - noun: a place where people can play
import Cocoa
protocol Action {}
final class Store<State> {
typealias Reducer = (Action, State) -> (State, [Any])
var state: State
@Ben-G
Ben-G / ZipWithKey.swift
Created June 9, 2016 23:04
ZipWithKeySwift
//: Playground - noun: a place where people can play
import UIKit
struct Person {
let name: String
let age: Int
}
struct Car {