Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexander Grebenyuk kean

View GitHub Profile
View parser-validator.swift
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"?
View cURLDescription.swift
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)\"")
@kean
kean / EnvironmentPicker.swift
Created Nov 21, 2021
Code from "Designing an API Client in Swift"
View EnvironmentPicker.swift
enum APIEnvironment: String, CaseIterable {
case dev
case test
case stage
case prod
}
struct ContentView: View {
@State private var selection: APIEnvironment? = env
View FocusList.swift
import SwiftUI
@main
struct FocusTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
MessageCommands()
View PinEdges.swift
public protocol LayoutItem { // `UIView`, `UILayoutGuide`
var superview: UIView? { get }
}
extension UIView: LayoutItem {}
extension UILayoutGuide: LayoutItem {
public var superview: UIView? { owningView }
}
public struct Alignment {
View StateObject.swift
// 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()
View share.swift
//: 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)
View navigation-view.swift
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 / Example.swift
Last active Nov 13, 2020
FetchedImage
View Example.swift
public struct ImageView: View {
@ObservedObject var image: FetchImage
public var body: some View {
ZStack {
Rectangle().fill(Color.gray)
image.view?
.resizable()
.aspectRatio(contentMode: .fill)
}