Skip to content

Instantly share code, notes, and snippets.

View StanislavK's full-sized avatar
🚜
Working

StanislavK StanislavK

🚜
Working
View GitHub Profile
@juliensagot
juliensagot / ConditionalEnvironmentOverlay.swift
Created November 8, 2024 10:40
ConditionalEnvironmentOverlay
private struct ConditionalEnvironmentOverlay<Value: Equatable, OverlayContent: View>: ViewModifier {
@Environment(\.self) private var environmentValues
private let keyPath: KeyPath<EnvironmentValues, Value>
private let value: Value
private let overlayContent: OverlayContent
init(
condition: KeyPath<EnvironmentValues, Value>,
equals value: Value,
@finestructure
finestructure / Charting.swift
Last active October 23, 2023 20:22
Swift playground example to create charts with a PDF export button
import SwiftUI
import Charts
import PlaygroundSupport
let spiData: [(package: String, fileCount: Int, mbSize: Int)] = [
(package: "swift-markdown-ui", fileCount: 3796, mbSize: 44),
(package: "Microya", fileCount: 414, mbSize: 4),
(package: "swift-url-routing", fileCount: 9430, mbSize: 101),
@phranck
phranck / PlatformVisibility.swift
Last active August 30, 2024 00:05
A Swift view modifier to handle visibility of views for specific platforms
import SwiftUI
public struct Platform: OptionSet {
public var rawValue: UInt8
public static let iOS = Platform(rawValue: 1 << 0)
public static let macOS = Platform(rawValue: 1 << 1)
public static let tvOS = Platform(rawValue: 1 << 2)
public static let watchOS = Platform(rawValue: 1 << 3)
public static let all: Platform = [.iOS, .macOS, .tvOS, .watchOS]
@christianselig
christianselig / UITableViewHeaderFooterView+Swizzle.swift
Created October 1, 2021 20:11
Swizzle UITableViewHeaderFooterView to make header color customizable. Done in willMoveToWindow (once) because it should occur right before visible.
extension UITableViewHeaderFooterView {
private static var hasFiredOnceKey = "apollo_hasFiredOnceKey"
static func apollo_swizzle() {
// To be called in AppDelegate.didFinishLaunching
let originalSelector = #selector(willMove(toWindow:))
let swizzledSelector = #selector(apollo_willMove(toWindow:))
guard let originalMethod = class_getInstanceMethod(self, originalSelector), let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) else {
assertionFailure("Original method or swizzled method is unavailable")
@mattt
mattt / UIViewControllerPreview.swift
Last active October 7, 2024 06:51
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()
}
@discardableResult
public func with<T>(_ value: T, _ builder: (T) -> Void) -> T {
builder(value)
return value
}
@discardableResult
public func with<T>(_ value: T, _ builder: (T) throws -> Void ) rethrows -> T {
try builder(value)
return value
@raven
raven / ViewController.swift
Last active May 11, 2017 20:12
rdar://30739102
//
// ViewController.swift
// AttributedString
//
// Created by Peter Goldsmith on 27/02/2017.
//
import UIKit
class ViewController: UIViewController {
@JohnSundell
JohnSundell / Perform.swift
Last active December 21, 2019 14:43
A function that enables you to easily wrap throwing APIs, to provide a custom error
/**
* Perform a throwing expression, and throw a custom error in case the expression threw
*
* - parameter expression: The expression to execute
* - parameter error: The custom error to throw instead of the expression's error
* - throws: The given error
* - returns: The return value of the given expression
*/
func perform<T>(_ expression: @autoclosure () throws -> T, orThrow errorExpression: @autoclosure () -> Error) throws -> T {
do {
@simme
simme / debounce-throttle.swift
Created December 20, 2016 10:04
Swift 3 debounce & throttle
//
// debounce-throttle.swift
//
// Created by Simon Ljungberg on 19/12/16.
// License: MIT
//
import Foundation
extension TimeInterval {
@YusukeHosonuma
YusukeHosonuma / UIViewController.swift
Last active December 9, 2021 12:30
Capture UITableView full content.
//
// ViewController.swift
// UIViewCapture
//
// Created by Yusuke on 11/7/16.
// Copyright © 2016 Yusuke. All rights reserved.
//
import UIKit