Skip to content

Instantly share code, notes, and snippets.

Avatar
👋
Hi!

Sam Soffes soffes

👋
Hi!
View GitHub Profile
@soffes
soffes / Example.swift
Last active Apr 1, 2021
KeyboardLayoutGuide
View Example.swift
override func viewDidLoad() {
super.viewDidLoad()
let keyboardGuide = KeyboardLayoutGuide()
view.addLayoutGuide(keyboardGuide)
NSLayoutConstraint.activate([
someTextField.bottomAnchor.constraint(lessThanOrEqualTo: view.safeAreaLayoutGuide.bottom, constant: -8),
someTextField.bottomAnchor.constraint(lessThanOrEqualTo: keyboardGuide.topAnchor, constant: -8),
])
@soffes
soffes / Gemfile
Created Mar 11, 2021
Along's Gemfile
View Gemfile
# frozen_string_literal: true
source 'https://rubygems.org'
ruby '3.0.0'
# Instead of depending on `rails`, I just add the gems I need below
rails_version = '>= 6.1.0'
# Active Record from Rails
@soffes
soffes / test_case.rb
Created Mar 11, 2021
Simple ActiveRecord query counter
View test_case.rb
class TestCase < ActiveSupport::TestCase
def setup
super
DatabaseCleaner.start
@queries = []
ActiveSupport::Notifications.subscribe('sql.active_record') do |_, _, _, _, payload|
@queries << payload[:sql] unless payload[:name].in? %w[CACHE SCHEMA]
end
end
@soffes
soffes / fib.rb
Created Mar 3, 2021
Fibonacci without recursion
View fib.rb
# From this delightful video https://www.youtube.com/watch?v=ghxQA3vvhsk
PHI = (1 + Math.sqrt(5)) / 2
def fib(n)
(((PHI**n) - ((-1 / PHI)**n)) / Math.sqrt(5)).to_i
end
(1...10).map { |n| fib(n) }
# => [1, 1, 2, 3, 5, 8, 13, 21, 34]
@soffes
soffes / Decoders+JSON.swift
Last active Jan 8, 2021 — forked from loudmouth/Decode Array<Any> and Dictionary<String, Any> Swift
Decode Array<Any> and Dictionary<String, Any> Swift
View Decoders+JSON.swift
import Foundation
// Inspired by https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24
private struct JSONCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
@soffes
soffes / SafariActivity.swift
Created Oct 26, 2020
Open in Safari UIActivity
View SafariActivity.swift
import UIKit
final class SafariActivity: UIActivity {
// MARK: - Properties
var url: URL?
// MARK: - UIActivity
View ActivityIndicator.swift
import SwiftUI
import UIKit
public struct ActivityIndicator: UIViewRepresentable {
// MARK: - Properties
public let style: UIActivityIndicatorView.Style
@Binding public var isAnimating: Bool
private var color: UIColor?
@soffes
soffes / README.md
Last active May 7, 2020
Open snapshot diffs in Kaleidoscope
View README.md

Apparently, SnapshotTesting has this built-in. 🤦


While working with snapshot tests, it can be hard to debug what the problem is. This script allows you to quickly open the diff in a visual tool, Kaleidoscope, to compare the visual changes.

The output of SnapshotTesting when a snapshot doesn’t match looks like this:

@−
View canRecordScreen.swift
// From https://stackoverflow.com/a/58985069/118631
@available(macOS 10.15, *)
func canRecordScreen() -> Bool {
let runningApplication = NSRunningApplication.current
let processIdentifier = runningApplication.processIdentifier
guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID)
as? [[String: AnyObject]] else
{
@soffes
soffes / test.py
Created Oct 10, 2019
I'm trying to figure out how Xcode formats floats in its XIB/Storyboard XML. So far, I'm stumped.
View test.py
import re
# I'm trying to figure out how Xcode formats floats in its XIB/Storyboard XML. So far, I'm stumped.
# Data from Xcode
data = values = ["0.0", "0.0039215686274509803", "0.0078431372549019607", "0.011764705882352941", "0.0039215686274509803", "0.019607843137254902", "0.023529411764705882", "0.027450980392156862", "0.031372549019607843", "0.035294117647058823", "0.039215686274509803", "0.043137254901960784", "0.047058823529411764", "0.050980392156862744", "0.054901960784313725", "0.058823529411764705", "0.062745098039215685", "0.066666666666666666", "0.070588235294117646", "0.074509803921568626", "0.078431372549019607", "0.082352941176470587", "0.086274509803921567", "0.090196078431372548", "0.094117647058823528", "0.098039215686274508", "0.10196078431372549", "0.10588235294117647", "0.10980392156862745", "0.11372549019607843", "0.11764705882352941", "0.12156862745098039", "0.12549019607843137", "0.12941176470588234", "0.13333333333333333", "0.13725490196078433", "0.14117647058823529", "0.1450980392