Skip to content

Instantly share code, notes, and snippets.

View alexito4's full-sized avatar

Alejandro Martínez alexito4

View GitHub Profile
@nicklockwood
nicklockwood / OSKit.swift
Created January 28, 2023 11:32
A lightweight approach to writing cross-platform code in SwiftUI without a lot of conditional compilation blocks
import SwiftUI
enum OSDocumentError: Error {
case unknownFileFormat
}
#if canImport(UIKit)
import UIKit
@defagos
defagos / BodyCounter.swift
Last active December 13, 2022 16:07
Decorate the view with a bordered debugging frame whose attached label displays how many times the view body has been evaluated.
import SwiftUI
import UIKit
private struct BodyCounterView: UIViewRepresentable {
let color: UIColor
private let id = UUID() // Force view updates
func makeUIView(context: Context) -> _BodyCounterView {
let view = _BodyCounterView()
//
// PagingView.swift
// Wallaroo - https://wallaroo.app
//
// Created by Sean Heber (@BigZaphod) on 8/9/22.
//
import SwiftUI
// This exists because SwiftUI's paging setup is kind of broken and/or wrong out of the box right now.
@ole
ole / _StackLayoutCache.swift
Last active September 6, 2022 15:33
Structure of _StackLayoutCache and related types, used by SwiftUI as the Cache type for VStackLayout and HStackLayout
// Structure of _StackLayoutCache and related types.
// Used by SwiftUI as the Cache type for VStackLayout and HStackLayout.
//
// As of: iOS 16.0 simulator in Xcode 14.0b6
import SwiftUI
struct _StackLayoutCache {
var stack: StackLayout
}
@chriseidhof
chriseidhof / ContentView.swift
Last active March 27, 2024 19:14
Variadic Views
import SwiftUI
struct MyValue: _ViewTraitKey {
static var defaultValue: Int = 0
}
extension View {
func myValue(_ value: Int) -> some View {
_trait(MyValue.self, value)
}
@ole
ole / HeterogeneousDictionary.swift
Last active May 17, 2023 19:23
Code for my article "A heterogeneous dictionary with strong types in Swift" https://oleb.net/2022/heterogeneous-dictionary/
// A heterogeneous dictionary with strong types in Swift, https://oleb.net/2022/heterogeneous-dictionary/
// Ole Begemann, April 2022
/// A key in a `HeterogeneousDictionary`.
public protocol HeterogeneousDictionaryKey {
/// The "namespace" the key belongs to. Every `HeterogeneousDictionary` has its associated domain,
/// and only keys belonging to that domain can be stored in the dictionary.
associatedtype Domain
/// The type of the values that can be stored under this key in the dictionary.
associatedtype Value
import SwiftUI
extension View {
func cascadingAction<ActionInput>(path: CascadingActionNodeModifier<ActionInput>.Path, handler: @escaping (ActionInput) -> Void) -> some View {
self.modifier(CascadingActionNodeModifier(path: path, handler: handler))
}
func cascadingAction(path: CascadingActionNodeModifier<Void>.Path, handler: @escaping () -> Void) -> some View {
self.modifier(CascadingActionNodeModifier(path: path, handler: handler))
}
@kylehughes
kylehughes / TornRectangle.swift
Last active August 12, 2023 01:20
A rectangle shape for SwiftUI that can render any edge like a torn piece of paper.
// Copyright 2021 Kyle Hughes
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
@christianselig
christianselig / Locale+SFSymbol.swift
Created September 3, 2021 21:54
Returns an SF Symbol currency image that match's the device's current locale, for instance dollar in North America, Indian rupee in India, etc.
extension Locale {
/// Returns an SF Symbol currency image that match's the device's current locale, for instance dollar in North America, Indian rupee in India, etc.
func currencySFSymbol(filled: Bool, withConfiguration configuration: UIImage.Configuration? = nil) -> UIImage {
// Default currency symbol will be the Animal Crossing Leaf coin 􁂬 to remain impartial to any specific country
let defaultSymbol = UIImage(systemName: "leaf.circle\(filled ? ".fill" : "")")!
guard let currencySymbolName = currencySymbolNameForSFSymbols() else { return defaultSymbol }
let systemName = "\(currencySymbolName).circle\(filled ? ".fill" : "")"
return UIImage(systemName: systemName, withConfiguration: configuration) ?? defaultSymbol
// Simplified version of https://gist.github.com/anandabits/d9494d14fef221983ff4f1cafa318d47#file-areequatablyequal-swift
func isEqual(x: Any, y: Any) -> Bool {
func f<LHS>(_ lhs: LHS) -> Bool {
let p = Wrapper<LHS>.self as? AnyEquatable.Type
return p?.isEqual(x, y) ?? false
}
return _openExistential(x, do: f)
}