Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View V8tr's full-sized avatar

Vadim Bulavin V8tr

View GitHub Profile
@V8tr
V8tr / AutoLayoutDSL.swift
Last active October 31, 2023 17:42
Auto Layout DSL
import UIKit
/// Represents a single `NSLayoutConstraint`
enum LayoutAnchor {
case constant(attribute: NSLayoutConstraint.Attribute,
relation: NSLayoutConstraint.Relation,
constant: CGFloat)
case relative(attribute: NSLayoutConstraint.Attribute,
relation: NSLayoutConstraint.Relation,
@V8tr
V8tr / disable_ats_debug.sh
Created October 21, 2022 16:43
Disables App Transport Security for debug builds in Xcode
#!/bin/sh
# disable_ats_debug.sh
INFO_PLIST="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
PLISTBUDDY="/usr/libexec/PlistBuddy"
if [ "${CONFIGURATION}" == "Debug" ]; then
$PLISTBUDDY -c "Add :NSAppTransportSecurity dict" "${INFO_PLIST}" || true
$PLISTBUDDY -c "Add :NSAppTransportSecurity:NSAllowsArbitraryLoads bool true" "${INFO_PLIST}" || true
$PLISTBUDDY -c "Set :NSAppTransportSecurity:NSAllowsArbitraryLoads true" "${INFO_PLIST}"
@V8tr
V8tr / SheetModalPresentationController.swift
Created August 17, 2022 17:44 — forked from vinczebalazs/SheetModalPresentationController.swift
A presentation controller to use for presenting a view controller modally, which can be dismissed by a pull down gesture. The presented view controller's height is also adjustable.
import UIKit
extension UIView {
var allSubviews: [UIView] {
subviews + subviews.flatMap { $0.allSubviews }
}
func firstSubview<T: UIView>(of type: T.Type) -> T? {
allSubviews.first { $0 is T } as? T
@V8tr
V8tr / GHRunLoopWatchdog.h
Created August 16, 2022 11:13 — forked from jspahrsummers/GHRunLoopWatchdog.h
A class for logging excessive blocking on the main thread
/// Observes a run loop to detect any stalling or blocking that occurs.
///
/// This class is thread-safe.
@interface GHRunLoopWatchdog : NSObject
/// Initializes the receiver to watch the specified run loop, using a default
/// stalling threshold.
- (id)initWithRunLoop:(CFRunLoopRef)runLoop;
/// Initializes the receiver to detect when the specified run loop blocks for
@V8tr
V8tr / Buildable.swift
Created July 13, 2022 18:23
KeyPath-based builder in Swift
protocol Buildable {}
extension Buildable {
func set<T>(_ keyPath: WritableKeyPath<Self, T>, to newValue: T) -> Self {
var copy = self
copy[keyPath: keyPath] = newValue
return copy
}
}
@V8tr
V8tr / BellmanFord.swift
Last active February 16, 2022 16:49
Bellman Ford in an acyclic weighed undirected graph
struct Edge {
var u: Int
var v: Int
var weight: Int
func other(_ vertex: Int) -> Int {
return vertex == u ? v : u
}
}
@V8tr
V8tr / UnionFind.swift
Created February 16, 2022 16:43
Union find with path compression and weighted union
class UnionFind {
private var id: [Int]
private var sizes: [Int]
private(set) var components: Int
init(_ N: Int) {
id = (0..<N).map { $0 }
sizes = Array(repeating: 1, count: N)
components = N
}
@V8tr
V8tr / RefactoringAppDelegate-Composite.swift
Last active June 13, 2021 18:26
Refactoring Massive App Delegate using Composite pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
class PushNotificationsAppDelegate: AppDelegateType {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Registered successfully
}
}
class StartupConfiguratorAppDelegate: AppDelegateType {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Perform startup configurations, e.g. build UI stack, setup UIApperance
return true
[
{
"currencyCode": "AUD",
"country": "Australia",
"currencyName": "Australian Dollar",
"countryCode": "AU"
},
{
"currencyCode": "BGN",
"country": "Bulgaria",
import Foundation
import UIKit
struct ViewStyle<T> {
let style: (T) -> Void
}
let filled = ViewStyle<UIButton> {
$0.setTitleColor(.white, for: .normal)
$0.backgroundColor = .red