Skip to content

Instantly share code, notes, and snippets.

View smic's full-sized avatar

Stephan Michels smic

View GitHub Profile
@BigZaphod
BigZaphod / ContentView.swift
Last active July 1, 2024 04:31
Moving views between stacks with SwiftUI while preserving view identity
// This is an experiment for moving a view between different stacks and having SwiftUI animate it properly.
// By "drawing" all of the cards in one place and moving their geometry, it preserves the card view's
// identifity from SwiftUI's POV. This means when things change, SwiftUI can understand how they changed
// and animate it properly. Is there a better way to do this?
class Card : Identifiable, ObservableObject, Equatable {
@Published var name: String
@Published var tagged = false
init(_ name: String) {
@nicklockwood
nicklockwood / CodableVersioning.swift
Last active January 29, 2024 11:31
Example demonstrating how to use versioning for Codable structs
// This gist demonstrates how you can implement versioning for a Codable struct to support loading
// old serialized data after changing the structure. Notable features of this solution:
//
// * No need to make new properties optional, or to perform post-processing on the struct after
// loading in ordeer to populate missing values
// * No need to change the call site - from the outside this struct behaves just the same
// as if we had implemented codable directly in the normal way.
// * Versioning can be applied individually to parents or leaves in a larger tree of
// structs without affecting the other elements
// * This approach will work even if the original struct was not designed with versioning in mind
@johnmay
johnmay / Human_Body.cdxml
Created April 16, 2021 20:30
Human_Body.cdxml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE CDXML SYSTEM "http://www.cambridgesoft.com/xml/cdxml.dtd" >
<CDXML
CreationProgram="ChemDraw 14.0.0.118"
Name="Human_Body.cdx"
BoundingBox="5.57 2.92 538.65 658.35"
WindowPosition="0 0"
WindowSize="0 0"
FractionalWidths="yes"
InterpretChemically="yes"
@brunow
brunow / gist:69763de7793d1d01e2f1f124cec55d4c
Created April 14, 2021 05:41
SwiftUI os specific modifier
/// After reading an article from Antoine v.d. SwiftLee  @twannl about conditional view modifier, I had an idea to improve my code I write for specific OS.
/// https://www.avanderlee.com/swiftui/conditional-view-modifier
extension Bool {
static var iOS: Bool {
#if os(iOS)
return true
#else
return false
#endif
@peterfriese
peterfriese / Color+Codable.swift
Created March 19, 2021 11:00
Making Swift's Color codable
//
// Color+Codable.swift
// FirestoreCodableSamples
//
// Created by Peter Friese on 18.03.21.
//
import SwiftUI
// Inspired by https://cocoacasts.com/from-hex-to-uicolor-and-back-in-swift
@DougGregor
DougGregor / parallel_map.swift
Created December 24, 2020 01:10
Swift async/await implementation of a parallel map
extension Collection {
func parallelMap<T>(
parallelism requestedParallelism: Int? = nil,
_ transform: @escaping (Element) async throws -> T
) async throws -> [T] {
let defaultParallelism = 2
let parallelism = requestedParallelism ?? defaultParallelism
let n = self.count
if n == 0 {
.toolbar {
ToolbarItemGroup {
Button(action: {}, label: {
Image(systemName: "sidebar.left")
})
Spacer()
Button(action: {}, label: {
Image(systemName: "play.fill")
})
Button(action: {}, label: {
@tonyarnold
tonyarnold / NSObject+AnyCancellable.swift
Created September 11, 2020 04:34
A nifty extension that I borrowed from DeclarativeHub's ReactiveKit: https://github.com/declarativehub/reactivekit/
import Combine
import ObjectiveC.runtime
extension NSObject {
private enum AssociatedKeys {
static var CancellablesKey = "CancellablesKey"
}
/// A set that can be used to dispose of Combine cancellables.
public var cancellables: Set<AnyCancellable> {
@IsaacXen
IsaacXen / DispatchQueue+Once.swift
Last active December 5, 2022 19:14
dispatch_once in Swift, and more.
import Foundation
public extension DispatchQueue {
private struct _once_Domain: Hashable {
/// The unique identifier for the class instance as domain.
var identifier: ObjectIdentifier
/// A weak reference to the class instance as domain.
weak var lifetimeObject: NSObject?
@IanKeen
IanKeen / Example_Complex.swift
Last active September 10, 2024 11:53
PropertyWrapper: @transaction binding for SwiftUI to make changes to data supporting commit/rollback
struct User: Equatable {
var firstName: String
var lastName: String
}
@main
struct MyApp: App {
@State var value = User(firstName: "", lastName: "")
@State var showEdit = false