Skip to content

Instantly share code, notes, and snippets.

@kean
kean / EnvironmentPicker.swift
Created November 21, 2021 03:06
Code from "Designing an API Client in Swift"
enum APIEnvironment: String, CaseIterable {
case dev
case test
case stage
case prod
}
struct ContentView: View {
@State private var selection: APIEnvironment? = env
@kean
kean / cURLDescription.swift
Last active April 28, 2022 09:55
test-octokit 2
extension URLRequest {
public func cURLDescription() -> String {
guard let url = url, let method = httpMethod else {
return "$ curl command generation failed"
}
var components = ["curl -v"]
components.append("-X \(method)")
for header in allHTTPHeaderFields ?? [:] {
let escapedValue = header.value.replacingOccurrences(of: "\"", with: "\\\"")
components.append("-H \"\(header.key): \(escapedValue)\"")
import UIKit
// Almost pseudocode, but should be possible to write in proper Swift
// MARK: High-Level APIs
// Decodes the document. By default, ignores errors and warnings.
func decode(data: Data, isStrict: Bool = false) -> throws OpenAPI.Document {
let parser = JSONDecoder().decoder(DocumentParser.self, from: data)
// Or maybe be even more granular: "strict", "ignoreWarnings", "ignoreAll"?
public protocol LayoutItem { // `UIView`, `UILayoutGuide`
var superview: UIView? { get }
}
extension UIView: LayoutItem {}
extension UILayoutGuide: LayoutItem {
public var superview: UIView? { owningView }
}
public struct Alignment {
import SwiftUI
@main
struct FocusTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
MessageCommands()
// Version 1: Using initializer directly
struct ContentView: View {
@State var count = 0
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text("container: \(count)")
.padding()
@kean
kean / share.swift
Last active December 9, 2020 22:43
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
extension NSAttributedString {
static func make(html: String, size: CGFloat, color: UIColor) -> NSAttributedString? {
// TODO: Size is configured programatically which means we can
// change it dynamically if needed (aka dynamic type)
@kean
kean / Example.swift
Last active November 13, 2020 20:48
FetchedImage
public struct ImageView: View {
@ObservedObject var image: FetchImage
public var body: some View {
ZStack {
Rectangle().fill(Color.gray)
image.view?
.resizable()
.aspectRatio(contentMode: .fill)
}
struct ContentView: View {
@State private var isShowingDetailView = false
var body: some View {
NavigationView {
NavigationLink(destination: Text("Second View"), isActive: $isShowingDetailView) {
Text("Show Detail")
}
.navigationBarTitle("Navigation")
}
@kean
kean / tbd
Created April 13, 2020 19:07
asd