Skip to content

Instantly share code, notes, and snippets.

View shaps80's full-sized avatar
🏠
Working from home

Shaps shaps80

🏠
Working from home
View GitHub Profile
@shaps80
shaps80 / UIMenu+ResultBuilder.swift
Created June 9, 2021 20:52
Adds ResultBuilder support to UIMenu to simplify creation
import SwiftUI
typealias Menu = UIMenu
typealias Action = UIAction
@resultBuilder
struct MenuElementBuilder {
static func buildBlock(_ components: UIMenuElement...) -> [UIMenuElement] { components }
}
@shaps80
shaps80 / Flipped.swift
Created February 19, 2021 14:02
Provides a CGAffineTransform suitable for flipping the Y Axis.
private func flipped(size: CGSize) -> CGAffineTransform {
let mirror = CGAffineTransform(scaleX: 1, y: -1)
let translate = CGAffineTransform(translationX: 0, y: size.height)
return mirror.concatenating(translate)
}
extension Encodable {
var debugJson: String {
(try? JSONEncoder().encode(self).debugJson) ?? ""
}
}
extension Data {
var debugJson: String {
String(decoding: self, as: UTF8.self)
}
@shaps80
shaps80 / strip-intel.sh
Last active December 17, 2020 16:41
Strip Intel architecture's from Xcode frameworks for use on Apple Silicon
#!/bin/sh
# strip-intel.sh
# Usage example: ./strip-intel.sh
# This script is design to remove Intel architecture's from Xcode frameworks
# to enable development and installation of 3rd party pre-compiled frameworks
# that are not currently distributed as either XCFramework or SPM packages.
# The most common use-case would be carthage dependencies that are distributed
extension View {
/// Conditionally apply modifiers to a view.
func `if`<Content: View>(_ condition: () -> Bool, content: (Self) -> Content) -> some View {
condition() ?
ViewBuilder.buildEither(first: content(self)) :
ViewBuilder.buildEither(second: self)
}
func `if`<Content: View>(_ condition: @autoclosure () -> Bool, content: (Self) -> Content) -> some View {
condition() ?
@shaps80
shaps80 / Models.swift
Last active January 9, 2021 16:48
Swift type for representing a UserAgent (includes an implementation similar of Apple’s Version from SPM)
import UIKit
extension UIDevice {
/*
List can be updated here:
https://gist.github.com/adamawolf/3048717
*/
internal static var models: String = """
@shaps80
shaps80 / ActivityView.md
Last active December 3, 2022 15:36
A complete SwiftUI UIActivityViewController implementation.

Moved

This is now available in a dedicated package: ActivityView

Alternatively my SwiftUI Backports now includes a more complete implementation of ShareLink that's also more performant.

@shaps80
shaps80 / SwiftUI-TextView.md
Last active May 6, 2023 22:17
A SwiftUI view that wraps a UITextView but provides almost all functionality though modifiers and attempts to closely match the Text/TextField components.
@shaps80
shaps80 / Font.swift
Last active January 2, 2024 02:01
A set of UIFont/NSFont helpers that matches the equivalent SwiftUI Font API. (Supports iOS 13+ and macOS 10.15+)
import SwiftUI
#if os(macOS)
public typealias Font = NSFont
public typealias FontDescriptor = NSFontDescriptor
#else
public typealias Font = UIFont
public typealias FontDescriptor = UIFontDescriptor
#endif
@shaps80
shaps80 / AuthenticatingSceneDelegate.swift
Last active January 3, 2021 17:49
A UIWindowSceneDelegate that provides lifecycle events and an API for deferring specific tasks automatically for you. Simplifies the implementation of LocalAuthentication or some other authentication implementation.
import UIKit
/// Defines a token for determining the validity of a session
public protocol AuthenticationToken: Codable {
/// The token is currently valid
var isValid: Bool { get }
/// An encoded representation for storage
var encoded: Data? { get }
}