Skip to content

Instantly share code, notes, and snippets.

Avatar
🦅
Hacking in Swift

Adrian M. DevAndArtist

🦅
Hacking in Swift
View GitHub Profile
View UnconstrainedEnvironmentObject.swift
import SwiftUI
extension EnvironmentValues {
struct UnconstrainedObjectKey<Object>:
EnvironmentKey
where
Object: AnyObject
{
static var defaultValue: Object? {
nil
View swiftui_helper_property_wrapper.swift
import Combine
import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper
public struct Model<Value>: DynamicProperty {
final class _Box: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var value: Value {
@DevAndArtist
DevAndArtist / reimplemented_parts_of_swiftui.swift
Last active Jul 17, 2022
This is a custom implementation of some parts of SwiftUI which I could observe.
View reimplemented_parts_of_swiftui.swift
import CoreGraphics
final class TransactionStack {
static let shared = TransactionStack()
private var _transactions: [Transaction]
private init() {
dispatchPrecondition(condition: .onQueue(.main))
_transactions = []
@DevAndArtist
DevAndArtist / CustomStateObject.swift
Last active Dec 15, 2021
Custom `StateObject` which should be backwards compatible with iOS 13.
View CustomStateObject.swift
import SwiftUI
import Combine
/// The idea is to use State to create a storage object which will be
/// cached and restored by the framework. Then during the update phase we
/// re-inject the object into a nested ObservableObject and subscribe to the
/// ObjectType’s objectWillChange to forward to the _MessageForwarder’s
/// objectWillChange which is already subscribed by the framework as it’s an
/// inner dynamic property of our custom PW which is also a dynamic property.
@propertyWrapper
@DevAndArtist
DevAndArtist / endian-conversion-cheat-sheet.swift
Last active Nov 13, 2021
A cheat sheet for byte conversion of number types in Swift 3.0
View endian-conversion-cheat-sheet.swift
func _convertToBytes<T>(_ value: T, withCapacity capacity: Int) -> [UInt8] {
var mutableValue = value
return withUnsafePointer(to: &mutableValue) {
return $0.withMemoryRebound(to: UInt8.self, capacity: capacity) {
return Array(UnsafeBufferPointer(start: $0, count: capacity))
}
}
View small_but_shiny.swift
public protocol MapKey {
associatedtype Value
static var defaultValue: Value { get }
}
public struct Map<Base> {
typealias _Key = ObjectIdentifier
var _values: [_Key: Any]
View interchangeable_matched_geometry_effect.swift
import SwiftUI
struct HeroAnimationTest: View {
let indices = 0 ..< 3
@State
var showsOtherView = false
@Namespace
var namespaceID: Namespace.ID
View ReferenceFileDocument_Example.swift
import SwiftUI
import UniformTypeIdentifiers
// DOCUMENT EXAMPLE
extension UTType {
static var exampleText: UTType {
UTType(importedAs: "com.example.plain-text")
}
}
View swiftui_picker_attempt.swift
import Combine
import SwiftUI
// To re-align the cells to the center we use a workaround through debouncing
// the nearest cell ID which is computed every time the scroll view moves.
//
// HOWEVER there is a bug that still needs to be solved:
// If you drag the scroll view and hold, the debounce event will still happen
// and reposition the cell.
//
@DevAndArtist
DevAndArtist / fake_animation_completion.swift
Created Sep 18, 2019
SwiftUI Fake Animation completion for animations that do not overshoot the final value
View fake_animation_completion.swift
fileprivate struct _CompletionPreferenceKey: PreferenceKey {
typealias Value = CGFloat
static let defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}