Skip to content

Instantly share code, notes, and snippets.

import SwiftUI
extension CGPoint {
static func *(lhs: Self, rhs: CGFloat) -> Self {
.init(x: lhs.x * rhs, y: lhs.y * rhs)
}
}
// Idea: https://www.framer.com/showcase/project/lo2Qka8jtPXrjzZaPZdB/
@steipete
steipete / URLCacheTest.swift
Last active April 4, 2024 18:16
Using URLCache with download tasks (NSURLCache & NSURLSessionDownloadTask)
import Foundation
import os.log
class URLCacheTest {
let logger = Logger(subsystem: "URLCacheTest", category: "main")
// HTTP HEADERS:
// Date: Wed, 04 Nov 2020 11:13:24 GMT
// Server: Apache
// Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
diff --git a/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift b/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift
index db4ab12..ab96c5f 100644
--- a/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift
+++ b/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift
@@ -128,3 +128,24 @@ public func logging<Value, Action>(
}] + effects
}
}
+
+
@mattt
mattt / UIViewControllerPreview.swift
Last active January 8, 2024 23:09
Generic structures to host previews of UIView and UIViewController subclasses.
import UIKit
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct UIViewControllerPreview<ViewController: UIViewController>: UIViewControllerRepresentable {
let viewController: ViewController
init(_ builder: @escaping () -> ViewController) {
viewController = builder()
}
extension Store {
public func send<LocalValue>(
_ action: @escaping (LocalValue) -> Action,
binding keyPath: KeyPath<Value, LocalValue>
) -> Binding<LocalValue> {
Binding(
get: { self.value[keyPath: keyPath] },
set: { self.send(action($0)) }
)
}
extension View {
func presentation(_ alert: Alert?) -> some View {
guard let alert = alert else { return AnyView(self) }
let binding = Binding<Bool>.constant(true)
return AnyView(self.presentation(binding) { alert })
}
}
@IanKeen
IanKeen / Debounce.swift
Created January 16, 2019 19:13
Simple debouncer
func debounce<T>(delay: TimeInterval, function: @escaping (T) -> Void, complete: @escaping () -> Void = { }) -> (T) -> Void {
let queue = DispatchQueue(label: "Debouncer")
var current: DispatchWorkItem?
return { input in
current?.cancel()
let new = DispatchWorkItem {
function(input)
complete()
}
@IanKeen
IanKeen / BatchedSequence.swift
Created December 5, 2018 23:51
BatchedSequence
import Foundation
/// A sequence whose elements are emitted in batches of the specified size
struct BatchedSequence<Base: Sequence>: Sequence {
private let size: Int
private let base: Base
init(base: Base, size: Int) {
self.size = size
self.base = base
@IanKeen
IanKeen / NotificationCenter+TypeSafe.swift
Last active December 7, 2022 21:31
Type safe NotificationCenter
struct Notification<T> {
let name: NSNotification.Name
}
private let notificationData = "_notificationData"
extension NotificationCenter {
func post<T>(_ notification: Notification<T>, object: Any? = nil, data: T) {
post(name: notification.name, object: object, userInfo: [notificationData: data])
}
@IanKeen
IanKeen / Example+Object.swift
Last active August 2, 2023 16:39
Simple, highly composable Validator
extension Validator where Input == User, Output == User {
static var validUser: Validator<User, User> {
return .keyPath(\.name, .isNotNil && .isNotEmpty)
}
}
struct User {
let name: String?
}