Skip to content

Instantly share code, notes, and snippets.

View JonathanStorey's full-sized avatar

Jonathan Storey JonathanStorey

View GitHub Profile
@JonathanStorey
JonathanStorey / PageView.swift
Created September 8, 2024 14:32
A PageView for SwiftUI that supports collection-based creation of pages.
public struct PageView<Data, ID, Content>: View where Data: RandomAccessCollection, ID: Hashable, Content: View {
let data: Data
private let id: KeyPath<Data.Element, ID>
@Binding private var selection: ID?
private let content: (Data.Element) -> Content
@JonathanStorey
JonathanStorey / NavigationStack.swift
Last active September 27, 2024 21:28
An extension to NavigationStack for SwiftUI that supports page navigation by index
// Created by Jonathan Storey
// An extension for NavigationStack that mimics a page viewer. Perhaps most helpful for
// static type navigation where the series of pages has already been determined. In some
// circumstances this can replace the TabView (with PageTabViewStyle modifier). Supports
// the type-erased NavigationPath in addition to other Collection types. Added some
// safeguards to prevent crashes if the selection index is out of range.
// NavigationStack(pages: strings, selection: $index) { // strings are [String]
// ...
@JonathanStorey
JonathanStorey / PermutableCollection.swift
Last active June 2, 2023 23:12
A collection that supports moving and sorting of elements.
/// A collection that supports moving and sorting of elements.
public protocol PermutableCollection: Collection {
mutating func swapAt(_ i: Self.Index, _ j: Self.Index)
// mutating func move(fromOffsets source: IndexSet, toOffset destination: Int)
// mutating func partition(by belongsInSecondPartition: (Element) throws -> Bool) rethrows -> Index
}
struct KeyedList<Key, Element> : KeyableCollection { \\ ID or KEY... do I need ID???
private var _elements: [Element]
// init?()... if attempting to add elements, make init failable
init(id keyPath: KeyPath<Element, Key>) where Key: Hashable {
}
init() where Element: Hashable, Key == Never { \\ ID == Self??
@propertyWrapper
struct NavigationState<Value>: DynamicProperty {
@Shared private var path: Value
var wrappedValue: Value {
get { path }
nonmutating set { path = newValue }
}
struct ChildView: View {
let text : String
var body: some View {
// THERE IS NO ACCESS TO THE PATH DATA!
...
}
struct ContentView: View {
@NavigationState var path
var body: some View {
NavigationStack($path) {
...
}
struct ChildView: View {
typealias NavigationState = Shared<NavigationPath>
extension NavigationState where Value == NavigationPath {
init(_ key: String? = nil) {
self.init(wrappedValue: NavigationPath(), key)
}
}
@propertyWrapper
class Observed<Value>: ObservableObject {
@Published public var wrappedValue: Value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}
}
/// Dictionary-backed storage allowing for for single instantiation and retrieval of an object.
struct Singletons {
private static var dictionary : [String : AnyObject] = [:]
private static func key<T: AnyObject>(for type: T.Type, key: String? = nil) -> String {
let type = String(reflecting: T.self) // describing: String; reflecting: Swift.String
guard let key else { return type } // returns Swift.String
return type + "." + key // returns Swift.String.key
}