Skip to content

Instantly share code, notes, and snippets.

Benjamin Encz Ben-G

Block or report user

Report or block Ben-G

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Ben-G
Ben-G / ReactiveListExample.swift
Last active Feb 6, 2018
Reactive List Example
View ReactiveListExample.swift
/// 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 Oct 13, 2017
Keypath Based Joins in Swift
View KeyPathJoins.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 Jun 16, 2017
Very simple automated test to ensure localizations exist and are well-formed on iOS.
View L10NTests.swift
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 Apr 12, 2017
User Defaults Wrapper Swift
View UserDefaultsWrapper.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 Feb 3, 2018
Date Equality Swift
View DateEquality.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 Mar 23, 2017
Date Equality Swift
View DateEquality.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 Jan 24, 2017
Function that lifts a function with non-optional input to one with optional input.
View LiftToOptional.swift
// 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 Jun 21, 2016
(Don't actually do this) Lift Tuples to IOUs as Workaround for Swift Bug SR-42
View LiftTupleToIOU.swift
// 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 Jun 11, 2016
ReSwift Allow For Effects - Experiment
View ReSwiftEffectsExperiment.swift
//: 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
View ZipWithKey.swift
//: Playground - noun: a place where people can play
import UIKit
struct Person {
let name: String
let age: Int
}
struct Car {
You can’t perform that action at this time.